Event.observe(window, 'load', function() {
    $$("ul.showing li div.cost-select").invoke('observe', 'mouseover', function(e) {
        e.stop();
        this.down("div").addClassName('active');
    });

    $$("ul.showing li div.cost-select").invoke('observe', 'mouseout', function(e) {
        e.stop();
        this.down("div").removeClassName('active');
    });
});

Event.observe(window, 'load', function() {
    $$("div.view-select").invoke('observe', 'mouseover', function(e) {
        e.stop();
        this.addClassName('hover');
    });

    $$("div.view-select").invoke('observe', 'mouseout', function(e) {
        e.stop();
        this.removeClassName('hover');
    });
});

function set_cookie(name,value,expires, options) {
    if (options===undefined) { options = {}; }
    if ( expires ) {
        var expires_date = new Date();
        expires_date.setDate(expires_date.getDate() + expires)
    }
    document.cookie = name+'='+escape( value ) +
                        ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) +
                        ( ( options.path ) ? ';path=' + options.path : ';path=/' ) +
                        ( ( options.domain ) ? ';domain=' + options.domain : '' ) +
                        ( ( options.secure ) ? ';secure' : '' );
}

function get_cookie( name ) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
        return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ';', len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}

function submit_paypal_form(event) 
{
    Event.element(event).up('form').submit(); 
    event.stop();
}

function close_message_box() {
    Effect.toggle($('message-box'), 'blind', { duration: 0.5 });
}

function show_wait_image(element) 
{
    $(element).update("<img style='float: right;' src='/img/bg-wait.gif' alt='' />");
}

function set_background_error_color(element) 
{
    $(element).style.backgroundColor = "#ff8588";
}

function set_background_default_color(element) 
{
    $(element).style.backgroundColor = "#ffffff";
}

function format_currency(num) {
    num = parseFloat(num);
    return isNaN(num) ? 0.00 : num.toFixed(2);
}

function calculate_talent_total(element) 
{
    var block        = element.up('ul');
    var escrow_free   = $F('escrow-free');    
    var client_total  = element.value;
    var total, escrow = 0;

    total  = parseFloat(client_total)/(1 + escrow_free/100);
    escrow = client_total - total;
            
    block.down('p.surepay-escrow-free').update(format_currency(escrow));
    block.down('input.talent-total').value = format_currency(total);
}                        

function calculate_client_total(element)
{
    var block        = element.up('ul');
    var escrow_free   = $F('escrow-free');
    var talent_total  = element.value;
    var total, escrow = 0;

    escrow = (talent_total/100) * escrow_free;
    total  = parseFloat(talent_total) + escrow;
            
    block.down('p.surepay-escrow-free').update(format_currency(escrow));
    block.down('input.client-total').value = format_currency(total);
}
  
function add_favorite(event, talent_id, source) 
{
    var element = Event.element(event);
    var url = window.location.pathname.substr(0, 8) == '/client/'
                    ? '/client/favorites/add_favorite'
                    : '/favorites/add_favorite';
    
    new Ajax.Request(url, {
        method: 'post',
        parameters: { talent_id: talent_id, source: source, url: document.location },
        onSuccess: function (response, json) {
            if (json && json.success == true) {
                element.update('Saved ( Remove )');
                element.onclick = function () {
                    delete_favorite(event, talent_id, source);
                }
                show_message_box(json.message);
            } else {
                if (json.login_required) {
                    window.location = '/login';
                    return;
                }
                var message = json.message 
                            ? json.message
                            : 'The voice talent you selected has not yet been added to your Favorites. Please try again';
                show_message_box(message, 'Error');
            }
        }
    });
    event.stop();                                      
}   

function delete_favorite(event, talent_id, source)
{
    if (!confirm('Are you sure want to delete this favorite?')) {
        return false;
    }
    
    var element = Event.element(event);
    var url = window.location.pathname.substr(0, 8) == '/client/'
                    ? '/client/favorites/delete_favorite'
                    : '/favorites/delete_favorite';
    
    new Ajax.Request(url, {
        method: 'post',
        parameters: { talent_id: talent_id },
        onSuccess: function (response, json) {  
            if (json && json.success == true) {  
                element.update('Add to Favorites');
                element.onclick = function () {
                    add_favorite(event, talent_id, source);
                } 
                show_message_box(json.message);
            } else {    
                var message = json.message
                            ? json.message
                            : 'The voice talent you selected has not yet been added to your Favorites. Please try again';
                show_message_box(message, 'Error');
            }
        }
    });
    event.stop();
}

function show_message_box(message, title, type) {
    if (!title) title = 'Thank You';
    if (!type) type = 'success'
    if (!$('message-box')) {
        var green_box = new Element('div', {'class': 'message-' + type + '-box', 'id': 'message-box'});
        var box       = new Element('div', {'class': 'box'}).update('<h3>' + title + '</h3>');
        var close     = new Element('a', {'href': '#', 'class': 'close'}).update('CLOSE').observe('click', close_message_box);
        var message   = new Element('div').update(message);

        box.insert(close);
        box.insert(message);
        green_box.insert(box);

        new Insertion.Top($('content').down('div.holder').next(), green_box);
    } else {
        $('message-box').setAttribute('class', 'message-' + type + '-box');
        $('message-box').down('div.box div').update(message);
        $('message-box').down('h3').update(title);
        $('message-box').setStyle({'display': 'block'});
    }
}

/**
 * Functions for the calendar
 */
 
function dateIsPast(date) {
    var today = new Date();
    today.setHours(0,0,0,0);
    return date < today;
}

function disablePastDates(date, year, month, day) {
    if (dateIsPast(date)) {
        return true; // disable this date
    } else {
        return false;
    }
}
