/* 
 * Regional dropdown handling 
 */
function showRegion ( region )
{

  $(".shader").show();
  $(region).show();
}

function hideRegionOverlay()
{

    $(".shader").hide();
    $("#north-america").hide();
    $("#latin-america").hide();
    $("#europe").hide();
    $("#asia").hide();
    $("#africa").hide();
}

function bindRegions ( region, regionTitle, overImage, outImage )
{

    $(region).bind({

        click: function()
        {
            showRegion(regionTitle);
        },
        mouseover: function()
        {
            $(region).css('background-image', 'url(images/'+overImage+')');
            $(region).css('background-position', 'left top');
            $(region).css('background-repeat', 'no-repeat');

            if (region == ".na")
            {
                $(".ma").css('background-image', 'url(images/atvl_midamerica-2.gif)');
                $(".ma").css('background-position', 'left top');
                $(".ma").css('background-repeat', 'no-repeat');
            }
            else
            if (region == ".sa")
            {
                $(".ma").css('background-image', 'url(images/atvl_midamerica-3.gif)');
                $(".ma").css('background-position', 'left top');
                $(".ma").css('background-repeat', 'no-repeat');
            }

        },
        mouseout: function()
        {
            $(region).css('background-image', 'url(images/'+outImage+')');
            $(region).css('background-position', 'left top');
            $(region).css('background-repeat', 'no-repeat');

            if (region == ".na" || region==".sa")
            {
                $(".ma").css('background-image', 'url(images/atvl_midamerica-1.gif)');
                $(".ma").css('background-position', 'left top');
                $(".ma").css('background-repeat', 'no-repeat');
            }
        }

    });    
}

$(document).ready(function()
{
    $(".flash-fade-image").mouseenter(function ()
    {
        $(this).css("background-color", "#fff");
        $(this).find("img").css("opacity", 0);
        $(this).find("img").stop().animate({"opacity": "1"}, "fast",
        function() {
            $(this).closest(".flash-fade-image").css("background-color", "transparent");
        });

    }
    );

    $(".flash-fade-image").each(function()
    {
        $(this).css("background-color", "transparent");
        $(this).css("display", "block");
        $(this).css("height", $(this).find("img").attr("height"));
        $(this).css("width", $(this).find("img").attr("width"));
    }
    );


    hideRegionOverlay();

    bindRegions(".na", "#north-america", "atvl_northamerica_ro.gif", "atvl_northamerica.gif" );
    bindRegions(".sa", "#latin-america", "atvl_latinamierica_ro.gif", "atvl_latinamierica.gif" );
    bindRegions(".as", "#asia", "atvl_asiapacific_ro.gif", "atvl_asiapacific.gif" );
    bindRegions(".af", "#africa", "atvl_africa_ro.gif", "atvl_africa.gif" );
    bindRegions(".eu", "#europe", "atvl_europe_ro.gif", "atvl_europe.gif" );

	$(".close").click( function() {hideRegionOverlay();});
  
});

/* 
 * Redirect handling
 */

// sets a cookie with a URL 
function setAnimaxCookie(url){
	// create expiration date
	var expires = new Date();    
	expires.setTime(expires.getTime() + (1000 * 60 * 60 * 24 * 30)); 
	// set the cookie
	$.cookie("myAnimaxCookie", url, { path: '/', expires: expires });
}

// checks URL for "cookie=reset" (set if we're coming from a country-specific site)
if( window.location.href.match(/cookie=reset/gi) ){
	// reset cookie
	$.cookie('myAnimaxCookie', null, { path: '/'});
}

// redirect if myAnimaxCookie is set
if( $.cookie("myAnimaxCookie") ){
	window.location.replace($.cookie("myAnimaxCookie"));
}
// finds all regional links and calls setAnimaxCookie when they are clicked
$(document).ready(function(){
  $('.region li a').click(function() { 
    setAnimaxCookie($(this).attr("href")); 
    });
});

