/**
 * @author tmcdonald
 * Overlay Scripts.
 * 
 * All overlays will load into #overlayWrapper
 * All content for overlays should be loaded into #overlayContent
 * Add use cssClass property to style overlays
 * Always specify an overlay width
 */
 if (!window.HLD) {window.HLD = {}; }

$(document).ready(function(){
	HLD.initialiseOverlays();
});
/*
 * For binding standard overlays to a domElement
 * -Use onBeforeLoad to manipulate overlay content before load.
 * -Load all content into #overlayContent
 */
HLD.Overlay = function(domElement, width, top, cssClass, fixed, onBeforeLoad, onLoad, onClose,args){
	if($.browser.msie && parseInt($.browser.version.substr(0,1))<7){var ie6=true;}
	if (!fixed||(ie6)){ var fixed=false; }
	if(fixed==false&&$.browser.msie){
		left=$(window).width()/2;
	} else {
		left="50%";
	}
	overlayArgs={
		target:"#overlayWrapper",
		mask: {color : '#fff'},
		top: top,
		left: left,
		fixed:fixed,
		onBeforeLoad:function()
			{	
				$("#overlayWrapper").width(width).css("left","50%").addClass(cssClass);
				//align to center by calculating width of overlay (including padding)
				var overlayPadding=parseInt($("#overlayWrapper").css('paddingLeft'));
				var overlayMarginLeft=(width/2)+overlayPadding;
				$("#overlayWrapper").css('margin-left',-overlayMarginLeft+"px");
				if (onBeforeLoad) {
					onBeforeLoad();
				}
				Cufon.refresh();
			},
		onLoad:	function(){
			//fixes IE bug with fade-ins leaving behind a filter attribute.
			if ($.browser.msie) {
				$("#overlayWrapper").get(0).style.removeAttribute('filter');
			}
			if (onLoad) {
				onLoad();
			}
		},
		onClose:function()
			{
				$("#overlayWrapper").removeClass(cssClass);
				$("#overlayContent").removeClass(cssClass);
				if (onClose) {
					onClose();
				}				
			},
		closeOnClick:true
		}
	overlayArgs=$.extend(overlayArgs,args)
	domElement.overlay(overlayArgs);
};
/*
 * For binding standard overlays to a domElement but inserting content from a filename
 * -files should be located in /resources/html
 */
function HLDhtmlOverlay(domElement, filename, width, top, cssClass, fixed, onBeforeLoad, onLoad, onClose) {
	HLD.htmlOverlay(domElement, filename, width, top, cssClass, fixed, onBeforeLoad, onLoad, onClose);
}
HLD.htmlOverlay = function(domElement, filename, width, top, cssClass, fixed, onBeforeLoad, onLoad, onClose){

	newOnBeforeLoad =	function()
			{
				$("#overlayContent").load(hld_resource_path + "/html/" + filename,
					function(){Cufon.refresh();}	
				);
				if (onBeforeLoad) {
					onBeforeLoad();
				}
			}
	HLD.Overlay(domElement, width, top, cssClass, fixed, newOnBeforeLoad, onLoad, onClose);

};

/*
 * For loading standard overlays without binding to anything
 * -Good for loading overlays on page load
 */
HLD.loadOverlay = function(width, top, cssClass, fixed, onBeforeLoad, onLoad, onClose,args){
	//remove previous overlay data as to load in new overlay data
	$("#overlayWrapper").removeData('overlay');
	if($.browser.msie && parseInt($.browser.version.substr(0,1))<7){var ie6=true;}
	if (!fixed||(ie6)){ var fixed=false; }
	if(fixed==false&&$.browser.msie){
		left=$(window).width()/2;
	} else {
		left="50%";
	}
	overlayArgs={
		load:true,
		mask: {color : '#fff'},
		top: top,
		fixed:fixed,
		left:left,
		onBeforeLoad:function()
			{
				$("#overlayWrapper").width(width).css("left","50%").addClass(cssClass);
				//align to center by calculating width of overlay (including padding)
				var overlayPadding=parseInt($("#overlayWrapper").css('paddingLeft'));
				var overlayMarginLeft=(width/2)+overlayPadding;
				$("#overlayWrapper").css('margin-left',-overlayMarginLeft+"px").css('filter', '');
				if (onBeforeLoad) {
					onBeforeLoad();
				}
				Cufon.refresh();
			},
		onLoad:	function(){
			//fixes IE bug with fade-ins leaving behind a filter attribute.
			if ($.browser.msie) {
				$("#overlayWrapper").get(0).style.removeAttribute('filter');
			}
			if (onLoad) {
				onLoad();
			}
		},
		onClose:function()
			{
				$("#overlayWrapper").removeClass(cssClass);
				$("#overlayContent").removeClass(cssClass);
				
				if (onClose) {
					onClose();
				}		
			},
		closeOnClick:true
		}
	overlayArgs=$.extend(overlayArgs,args)
	$("#overlayWrapper").overlay(overlayArgs);
};

/*
 * For loading standard overlays without binding to anything. Content loaded from html file
 * -files should be located in /resources/html
 */
HLD.loadHtmlOverlay = function(filename, width, top, cssClass, fixed, onBeforeLoad, onLoad, onClose){

	newOnBeforeLoad =	function()
			{
				$("#overlayContent").load(hld_resource_path + "/html/" + filename,
					function(){Cufon.refresh();	
				}
				);
				if (onBeforeLoad) {
					onBeforeLoad();
				}
			}
	HLD.loadOverlay(width, top, cssClass, fixed, newOnBeforeLoad, onLoad, onClose);

};

//Initialise overlays here
HLD.initialiseOverlays = function(){
	HLD.htmlOverlay($("#australiaDisclaimer"), "DisclaimerMessage.html", 400, "30%", 'disclaimerMessage');
	HLD.htmlOverlay($("#newzealandDisclaimer"), "DisclaimerMessage_nz.html", 820, "30%", 'disclaimerMessage');
	HLD.htmlOverlay($(".footerOptions img"), "DisclaimerMessage.html", 400, "30%", 'disclaimerMessage',true);
}

function loadThunderOverlay(){
	HLD.loadHtmlOverlay('thunder.html', 907, 70, 'thunderBoxOverlay', true, function(){
	}, function(){
		setTimeout(function(){makeScrollable();},1000);
		function makeScrollable(){
			$('.scrollable').scrollable({
				items: '.panes',
				onSeek: function(){
					var api = $('.scrollable').data('scrollable');
					var currentIndex = api.getIndex();
					var currentSlide = $('ul.tabs li').eq(currentIndex).children('a').attr('class');
					pageTracker._trackPageview('/vehicles/ute/thunder/' + currentSlide);
				}
			}).navigator({
				navi: 'ul.tabs'
			});
		}
		pageTracker._trackPageview('/vehicles/ute/thunder/overview');
		$('.needToKnow').hover(function(){
			if ($.browser.msie) {
				$('.pullOut').show();
			}
			else {
				$('.pullOut').fadeIn();
			}
		},function(){
			if ($.browser.msie) {
				$('.pullOut').hide();
			}
			else {
				$('.pullOut').fadeOut();
			}
		});	}
		);
}



