$(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(".sa", "#latin-america", "axn_latinamierica_ro.html", "axn_latinamierica.html" );
    bindRegions(".as", "#asia", "axn_asiapacific_ro.html", "axn_asiapacific.html" );
    bindRegions(".eu", "#europe", "axn_europe_ro.html", "axn_europe.html" );

	$(".close").click( function() {hideRegionOverlay();});
  
});

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/axn_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/axn_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/axn_midamerica-1.gif)');
                $(".ma").css('background-position', 'left top');
                $(".ma").css('background-repeat', 'no-repeat');
            }
        }

    });    
}

function setCookieAndRedirect(url){
	// create expiration date (in days, as per the jquery.cookie.js comments/docs)
	var expires = 730;
	// re-set the cookie
	$.cookie("myAxnCookie", url, { path: '/', expires: expires });
	// redirect
  window.location.replace(url);
}

// 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('myAxnCookie', null, { path: '/'});
}

// if myAxnCookie is set
if( $.cookie("myAxnCookie") ){
	setCookieAndRedirect($.cookie("myAxnCookie"));
}

