var current_tab;
var can_change = true;
var current_quote = 0;
var quote_rotation = false;
var pages = new Array('about', 'services', 'support', 'careers', 'contact', 'login');
var page_data = new Array();

var tab_colors = new Array();
tab_colors['home']     = '#DDDDDD';
tab_colors['about']    = '#DDDDDD';
tab_colors['services'] = '#DDDDDD';
tab_colors['support']  = '#DDDDDD';
tab_colors['careers']  = '#DDDDDD';
tab_colors['contact']  = '#DDDDDD';

$(document).ready( function() {
    rotate_quote();
//    load_via_ajax();
});


function btn_highlight( which, on_or_off ) {

    var target = "#tab_" + which;

    if ( on_or_off == 'on' ) {

        var highlight_color = '#FAFAFA';
        if (which == current_tab) { highlight_color = '#ADF323'; }

        $(target).css( 'background-color', highlight_color );
        
    } else {

        $(target).css( 'background-color', tab_colors[which] );

    }

}


function index_show_tab(which) {

    var target = "#" + which;

    if ( can_change == true && ! $(target).is(':visible') ) { 

        can_change = false;
        setTimeout("can_change = true;", 1000);

        var available_tabs = [ 'overview', 'advantages', 'services' ];
        for (var i = 0; i < available_tabs.length; i++) { 
            var subtarget = "#" + available_tabs[i];
            var photo = "#photo_" + available_tabs[i];

            $(subtarget).fadeOut( 500 );
            if ($(photo).is(':visible')) { $(photo).fadeOut(1000); }            
        }

        var photo = "#photo_" + which;
        setTimeout("$('" + target + "').fadeIn(500);", 510);
        setTimeout("$('" + photo  + "').fadeIn(1000);", 100);

    }

}


function rotate_quote() {

    var available_quotes = [ 'polar', 'scotia', 'pursuit', 'cibc', 'think' ];
    var next_quote = (current_quote * 1) + 1;

    if (next_quote == available_quotes.length) { next_quote = 0; }

    var current_quote_div = "#quote_" + available_quotes[current_quote];
    var new_quote_div     = "#quote_" + available_quotes[next_quote];

    if (quote_rotation == true) {
        $(current_quote_div).fadeOut(800, function() { $(new_quote_div).fadeIn(800); });
        current_quote = next_quote;
    } else {
        $(current_quote_div).fadeIn(800);
    }

    quote_rotation = true;
    setTimeout("rotate_quote();", 9500);

}


function change_consultation_box( show_or_hide ) {

    if ( show_or_hide == 'show' ) {

        if ( $('#consultation_box').is(':visible') ) { return; }

        $('#photo').fadeTo( 800, 0.2 );
        $('#quote_box').fadeTo( 800, 0.2 );
        $('#main_content').fadeTo( 800, 0.2 );
        $('#consultation_box').fadeIn(800);

    } else {

        if ( ! $('#consultation_box').is(':visible') ) { return; }

        $('#consultation_box').fadeOut(800);
        $('#photo').fadeTo( 800, 1.0 );
        $('#quote_box').fadeTo( 800, 1.0 );
        $('#main_content').fadeTo( 800, 1.0 );

    }

}


function load_via_ajax() {

    page_data['index'] = $('#main_content_container').html();

    for (var i = 0; i < pages.length; i++) {

        var current_page = pages[i];

        var data = $.ajax({
            url: current_page + '.php',
            async: false
        }).responseText;

        var content = data.split(/<\!-- HERE -->/);
        var new_content = "<div id=content_" + current_page + " style='display: none'>" + content[1] + "</div>\n";

        $('#extra_content').append(new_content);

    }

}


function swap_page_data( obj ) {

    var this_object = "#" + obj;
    var href = $(this_object).attr("href");
    var page_name = href.replace(/.php$/, '');

    var current_content = '#content_' + current_tab;
    var new_content = '#content_' + page_name;    

    $(current_content).fadeOut(500, function() {
        $(new_content).fadeIn(500);
    } );

    new_tab = obj.replace(/_link/, '');

    var cur_tab_jq = "#tab_" + current_tab;
    var new_tab_jq = "#tab_" + new_tab;

    $(cur_tab_jq).css('background-color', '#DDDDDD');
    $(new_tab_jq).css('background-color', '#c7ed92');
    tab_colors[current_tab] = '#DDDDDD';
    tab_colors['tab_' + new_tab] = '#c7eq92';

    current_tab = new_tab;

}

