var flags = new Array('ca','es','gb','fr','us','cn','de','it','ru','pt','is','jp','ir','in');
var current_flag = 0;
var current_flag_zindex = 30;

var timerIds = new Object();
var menusOpen = new Object();

var current_sponsor_image;
var current_main_image = 1;


jQuery(document).ready( function() {

    setTimeout("rotate_flags();", 4000);
    setTimeout('rotate_main_image();', 10000);



    // Show countdown
    jQuery('.countdown').countdown({
        until:  new Date("April 11, 2010 13:00:00"),
        format: 'dHM',
        timezone: -5,
        layout: '{dn} Days, {hn} Hours, {mn} Minutes'
    });

    translate_content();


    if ( jQuery('#sponsor_logo_1').is(':hidden') ) { rotate_sponsor_image(); }

} );



function rotate_flags() {

    var next_flag = (current_flag * 1) + 1;
    if (next_flag == flags.length) { next_flag = 0; }

    var next_flag_code = '#flag_' + flags[ next_flag ];
    var next_flag_zindex = (current_flag_zindex * 1) + 1;


    jQuery(next_flag_code).css( { 'display': 'none', 'zIndex': next_flag_zindex } );
    jQuery(next_flag_code).fadeIn('normal');

    current_flag = next_flag;
    current_flag_zindex = next_flag_zindex;

    setTimeout("rotate_flags();", 4000);

}


function translate_content() {

    var language = getCookie('language');

    if (language.length > 0) {

        jQuery('.container').translate( language );
        jQuery('ul.top_menu').translate( language );

    }

}


function set_language( which ) {

    if ( which == 'english' ) {
        document.cookie = "language=" + escape(which) + ";expires=Thu, 14 Jan 2010 13:08:44 GMT";
    } else {
        document.cookie = "language=" + escape(which) + ";expires=Fri, 14 Jan 2011 18:08:44 GMT";
    }
    top.location.reload();

}

function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
            c_start=c_start + c_name.length + 1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) { c_end=document.cookie.length; }
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}


function open_submenu( which ) {

    if ( menusOpen[which] == 1 ) { 
        stop_timer(which);
        return;
    }

    for (var i in timerIds) {
        hide_submenu(i);
    }

    jQuery('#' + which + '_submenu').slideDown( 'normal' );
    menusOpen[which] = 1;

}

function start_timer( which ) {
    timerIds[which] = setTimeout("hide_submenu('" + which + "');", 1000);
}

function stop_timer( which ) {
    clearTimeout( timerIds[which] );
}

function hide_submenu( which ) {

    menusOpen[which] = null;
    timerIds[which] = null;

    jQuery('#' + which + "_submenu").slideUp( 'normal' );

}

function rotate_sponsor_image() {

    if ( current_sponsor_image ) {

        var next_sponsor_image = (current_sponsor_image * 1) + 1;

        var jcurrent = '#sponsor_logo_' + current_sponsor_image;
        var jnext = '#sponsor_logo_' + next_sponsor_image;

        if ( ! jQuery(jnext).is(':hidden') ) { 
            jnext = '#sponsor_logo_1';
            next_sponsor_image = 1;
        }

        jQuery(jcurrent).fadeOut('normal', function() {
            jQuery(jnext).fadeIn('normal');
        } );

        current_sponsor_image = next_sponsor_image;

    } else {

        jQuery('#sponsor_logo_1').fadeIn('normal');
        current_sponsor_image = 1;

    }

    setTimeout('rotate_sponsor_image();', 7000);

}


function rotate_main_image() {

    var next_main_image = (current_main_image * 1) + 1;

    var jcurrent = '#promo_' + current_main_image;
    var jnext = '#promo_' + next_main_image;

    if ( ! jQuery(jnext).is(':hidden') ) { 
        jnext = '#promo_1';
        next_main_image = 1;
    }

    jQuery(jnext).fadeIn('slow');
    jQuery(jcurrent).fadeOut('slow');

    current_main_image = next_main_image;

    setTimeout('rotate_main_image();', 10000);

}



