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] = ['/library/images/homeHero/aviva-hero-car.jpg', '/library/images/blank.gif', "2 months' free car insurance when you buy your policy direct from Aviva.", '/car-insurance/filter-page.html?redirect=/cgi-bin/portcullis.cgi?portcullisid=ukdi-motor', 'rel="quoteWindow" id="comparisonTickerHeroCar" name="&lid=centreNav&lpos=car-get-quote-price-check"' ];
hero_banners[2] = ['/library/images/homeHero/aviva-hero-home.jpg', '/library/images/blank.gif', "2 months' free home insurance when you buy your policy direct from Aviva.", '/home-insurance/filter-page.html?redirect=/cgi-bin/portcullis.cgi?portcullisid=ukdi-home-insurance-buildings-contents', ' rel="quoteWindow"  id="comparisonTickerHeroHome" name="&lid=centreNav&lpos=home-get-quote-price-check"' ];
hero_banners[3] = ['/library/images/homeHero/home-hero-life.jpg', '/library/images/blank.gif', "Life insurance with monthly payments guaranteed never to rise", '/our-latest-tv-ads/' ];
hero_banners[4] = ['/library/images/homeHero/aviva-hero-health.jpg', '/library/images/blank.gif', "Individual Private Health Insurance with 3 months free&dagger;, Get a Private Health Insurance Quote", '/health/private-health-insurance/filter-page.html?redirect=/cgi-bin/portcullis.cgi?portcullisid=private-health-insurance&amp;business=healthcare', ' rel="quoteWindow"  id="comparisonTickerHeroHealth" name="&lid=centreNav&lpos=health-get-quote-price-check"' ];
hero_banners[5] = ['/library/images/homeHero/hero-home-business.jpg', '/library/images/blank.gif', "Find out more about insurance for your business", 'http://www.aviva.co.uk/yourbusiness'];

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);
	switch (Math.round(Math.random() * 4))
	{
		case 0:	
			show_the_banner($('#image3, #imageTop3'),true);
			$("#one a").removeClass("selected");
			$("#three a").addClass("selected");
			break;
		case 1:
			show_the_banner($('#image4, #imageTop4'),true);
			$("#one a").removeClass("selected");
			$("#four a").addClass("selected");
			break;
		default:
			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') );
	});

	// for doyle
	var position = $('#comparisonBox').position();
	var width = $('#comparisonBox').width();
	var height = $('#comparisonBox').height();

	var doyleContent = $('#comparisonBox').html();
	$('#nonJsHero').remove();

	//$('.heroImages li:first').append('<div id="comparisonBox">'+doyleContent+'</div>');
	$('.heroImages').after('<div id="comparisonBox">' + doyleContent + '</div>');
	//$('#comparisonBox').animate({ 'opacity': 0.0 }, 0);
	hide_the_div($('#comparisonBox'),true,false);
	tooltip($("#comparisonBox .tooltip"));
});
