
var HIDE_BANNER_LEFT_ATTRIB = 'nu_orig_left';
var HIDE_BANNER_TOP_ATTRIB = 'nu_orig_top';
var HIDE_BANNER_POSITION_ATTRIB = 'nu_orig_position';
var HIDE_BANNER_INDEX = 'nu_orig_index';

var hero_banners = new Array();

// Each Array to be in the format of [largeBannerURL, topOverlapStripURL, bannerAltTag, linkURL, linkParameters]

hero_banners[1] = ['/en/recursos/home-hero/grupo-aviva.jpg', '/en/recursos/home-hero/blank.gif', "Aviva Group - New Corporate video of Aviva Group", '/en/corporativa/quienes-somos/aviva-en-espana/video-corporativo/','' ];
hero_banners[2] = ['/en/recursos/home-hero/Hero-RSC.jpg', '/en/recursos/home-hero/blank.gif', "Aviva Group - Corporate Social Responsibility Section",'/en/corporativa/rsc/','' ];


var live_fade_boxes = new Array();


function fadein_the_banner(div, optionalFunction) {
	div.each( function() {
		var item = $(this);
        if (undefined == item.attr(HIDE_BANNER_TOP_ATTRIB))
            return;
		if (undefined == item.attr(HIDE_BANNER_LEFT_ATTRIB))
            return;
        item.css('top', item.attr(HIDE_BANNER_TOP_ATTRIB) );
		item.css('left', item.attr(HIDE_BANNER_LEFT_ATTRIB) );
        item.css('position', item.attr(HIDE_BANNER_POSITION_ATTRIB) );					
			
		if (optionalFunction) {			   
			item.animate( {'opacity':1.0}, 'slow', optionalFunction);
		} else {
			item.animate( {'opacity':1.0}, 'slow');
		}
	});
}

function fadeout_the_banner(div) {
	div.each( function() {
		$(this).animate( {'opacity':0.0}, 'slow', function() { hide_the_banner($(this),true); } );
	});
}

function hide_the_banner(div,fast,with_fade,customFunction)
{
    div.each( function() {
        var item = $(this);
        if (HIDE_POSITION_TOP == item.css('top'))
            return;
        
        item.attr( HIDE_BANNER_TOP_ATTRIB, item.css('top') );
		item.attr( HIDE_BANNER_LEFT_ATTRIB, item.css('left') );
        item.attr( HIDE_BANNER_POSITION_ATTRIB, item.css('position') );
        
        if (fast) {
            item.css('position','absolute').css('top',HIDE_POSITION_TOP).css('left',0);
        }
        else {
            var settings = null;
            if (with_fade)
				item.animate( {opacity:0.0}, 'normal', function() {
               		$(item).css('position','absolute').css('top',HIDE_POSITION_TOP).css('left',0);
					customFunction;
            	});
			else {
				$(item).css('position','absolute').css('top',HIDE_POSITION_TOP).css('left',0);
				customFunction;
			}
        }
    } );
}

function show_the_banner(div,fast,with_fade,customFunction)
{
    div.each( function() {
        var item = $(this);
        if (undefined == item.attr(HIDE_BANNER_TOP_ATTRIB))
            return;
		if (undefined == item.attr(HIDE_BANNER_LEFT_ATTRIB))
            return;
        if (with_fade)
            item.css('opacity',0.0);
        item.css('top', item.attr(HIDE_BANNER_TOP_ATTRIB) );
		item.css('left', item.attr(HIDE_BANNER_LEFT_ATTRIB) );
        item.css('position', item.attr(HIDE_BANNER_POSITION_ATTRIB) );

        if (with_fade) {
            item.animate( {opacity:1.0}, 'slow', function() { customFunction; }  );
        }
        else {            
            item.css('opacity',1.0);
			customFunction; 
        }
    } );
}

$(document).ready(function(){
						   				   
	var heroImages = $('<ul class="heroImages"></ul>');
	var topHeroImages = $('<ul class="heroTopImages"></ul>');
	for(var i=1; i<hero_banners.length; i++){
		heroImages.append('<li id="image' + i + '"><a href="' + hero_banners[i][3] + '" title="'+ hero_banners[i][2] +'" ' + hero_banners[i][4] + '><img src="' + hero_banners[i][0] + '" alt="' + hero_banners[i][2] + '"  /></a></li>');
		topHeroImages.append('<li id="imageTop' + i + '"><img src="' + hero_banners[i][1] + '" alt="" /></li>');
	}

	$('#homeHero').prepend(topHeroImages);
	$('#homeHero').prepend(heroImages);
	
	hide_the_banner($('.heroImages li, .heroTopImages li'),true);
	$('.heroImages li, .heroTopImages li').css('opacity',0);
	show_the_banner($('#image1, #imageTop1'),true);
	
	$('.heroImages li a').each( function() {
		setupPopup($(this));				  
	});
	
	var count=1;
	$('#homeHero .heroLinks li a').each( function() {
		$(this).attr( HIDE_BANNER_INDEX , count);
		count++;
	});
	
	$('#homeHero .heroLinks li a').mouseover( function() {
		var target_image = '#image' + parseInt($(this).attr(HIDE_BANNER_INDEX));
		var top_target_image = '#imageTop' + parseInt($(this).attr(HIDE_BANNER_INDEX));
		
		$('#homeHero .heroLinks li a').removeClass('selected');
		$(this).addClass('selected');
		
		// If there's already some animating stop them
		if (live_fade_boxes.length>0) {
			$('.heroImages li').stop();
			$('.heroTopImages li').stop();
		}
		
		live_fade_boxes.push( target_image );

		
		fadein_the_banner( $(target_image), function() { live_fade_boxes = new Array(); } );
		fadein_the_banner( $(top_target_image) );
		
		fadeout_the_banner( $(target_image).siblings('li') );
		fadeout_the_banner( $(top_target_image).siblings('li') );
	});
	

});


