// Homepage Takeover Script 
// Use with CARZONE Homepage only  FIX: this may need to work AT eventually so more work required as its a different HTML structure.
// This detects if HPTO is being served automatically
// November 2009
// 
//
//  SETUP INSTRUCTIONS
//  1. In Carzone homepage surround contents with an outer div with an ID of "#homepage-outer-container"
//  2. Include this JS in the head tags of the homepage, preferable at the bottom
//  3. include "cz-takeover.css" in head after "insurance.css", but before javascript include "wz_functions.js".
//  4. Add HPTO banner call at the bottom of the carzone homepage in div with ID "site-take-over" see prototype html page.

function noBannerServed() {
      var noBannerGif1 = "empty.gif";
	  var noBannerGif0 = "AE0.gif";
	  var isBannerServed = false;
	  if (($("#site-take-over img") != null) && ($("#site-take-over img").attr("src") != null))
	  {
		  var bannerImgUrl = $("#site-take-over img").attr("src").split("/");     
		  if (bannerImgUrl[bannerImgUrl.length - 1] == noBannerGif1  || bannerImgUrl[bannerImgUrl.length - 1] == noBannerGif0 ) {
				isBannerServed = true;
		  } else {
				isBannerServed = false;
		  }
	  }
      return isBannerServed;
}


function writeHomePagetakeOver() {		
	/* background image src */	
		var imgSrc = $("#site-take-over img").attr("src");	
	// detects image from accipiter FIX - this is crude so needs refactoring, works for now
		
		$("body").css("background-image", "url("+imgSrc+")"); 
		$("body").css("background-repeat", "no-repeat"); 
		$("body").attr("id", "homepage-take-over");		
		
		var topBannerHeight = 124; /* height manually set as specified by Stakeholder */		
		
		/* Top banner placeholder- kept in to push down page */
		$("#top-banner-placeholder").css({"display" : "block", "height" : topBannerHeight+"px", "min-height" : topBannerHeight+"px"  });			
		 getWindowWidth();	
		
}
function createSiteTakeOverCode() {		
	if (!noBannerServed()) {
		/* creates html containers for HPTO inside of DOM */	
		$("#homepage-outer-container").prepend("<div id='homepage-centre-container'></div>");		
		$("#homepage-outer-container").prepend("<div id='top-site-takeover'></div>");	
		$("#top-site-takeover").prepend("<div id='top-banner-placeholder'></div>");	
		
		/* appends div with class, identifying which are to link */	
		$("#homepage-centre-container").addClass("hpto-bg-link");
		$("#top-banner-placeholder").addClass("hpto-bg-link");	
		
		/* Displays HPTO */
		writeHomePagetakeOver();
		
		/* target accipiter links so that background clicks through */
		var hptoLink = $("#site-take-over a").attr("href");		
		$(".hpto-bg-link").click( function() {	
			 window.open(hptoLink);	 			
		 }); 	
		
		/* display link in status bar on hover - only compatbile with Internet Explorer */
		$(".hpto-bg-link").mouseover( function() {	
			window.status = hptoLink;
		});		
	}
}
/* position HPTO depending on browser width */
function getWindowWidth() {
	//alert(isBannerServed);
	if (!noBannerServed()) {
		var windowWidth = $(window).width();	
		var windowHeight = $(window).height();	
	
		var leftRightWidth = windowWidth-1120;
		var leftRightWidthValue = leftRightWidth/2;			
		
		if (windowWidth <= 960 ){	
			//size of white page 
			$("#homepage-outer-container").css({"margin-left" : "-80px"  });				
		} else if (windowWidth < 1120) {
			// window less than 1024 
			$("#homepage-outer-container").css({"margin-left" : leftRightWidthValue+"px"  });
		} else {
			// set to auto as auto if bigger than 1024
			$("#homepage-outer-container").css({"margin-left" : "auto"  });
		}			
		$("#homepage-centre-container").css({"padding-bottom" : windowHeight+"px"  });
	}
}
// re-calculate position based on browser width if resized by user 
$(window).resize( function() { getWindowWidth() });	

// executes after DOM loaded
$(document).ready(
    function() {			
		setTimeout("createSiteTakeOverCode()", 500);					
	}
);
