
/* set globals
-------------------------------------------------------------*/

var speed = 500;
var gotoURL = "http://www.draughtkeg.co.uk/";
var loadedImages = 0;
var totalImages = 0;

var loadNavAfter = 2000;
var loadNavAfter_timer = 0;


/* preload nav images functions
-------------------------------------------------------------*/

function preLoadImages() {
	totalImages = arguments.length;
	for(var i = 0; i < arguments.length; i++) {
		var objImage = new Image();
		objImage.onload = function() {
			loadedImages++;
			if(loadedImages==totalImages) {
				//console.log("All loaded");
			}
		};		
		objImage.src=arguments[i];
	}
}

preLoadImages(
		"/assets/images/music-noplay.gif",
		"/assets/images/verify-no-o.png",
		"/assets/images/verify-yes-o.png",
		"/assets/images/no.png",
		"/assets/images/nav.home.png", 
		"/assets/images/nav.inspiration.png", 
		"/assets/images/nav.storelocator.png", 
		"/assets/images/nav.the-guide.png", 
		"/assets/images/nav.win-stuff.png"
	);

function showNavigation() {
	var recall = 100;
	loadNavAfter_timer += recall;
	if(loadedImages == totalImages || loadNavAfter_timer >= loadNavAfter) {
		//$('ul.nav-mask').animate({opacity : 1}, speed);
		$('ul.nav-mask li').fadeIn(speed);
	} else {
		setTimeout('showNavigation()', recall);
	}
}


/* Flash functions
-------------------------------------------------------------*/

function replaceAnimation(name) {
	//alert(name);
	//TODO trigger transition from the flash animation to the static content.
	$('#flash-animation').empty().hide();
	$('div#fade-out').removeClass('display-after');
	$("div#fade-out").removeClass("display-after-scroll");
	$('div#fade-out').show();
	
	switch(name) {
		case 'all-in-the-head':
			writeVideoPlayer();
			break;			
		default:
			break;
	}
}

/* All in the head video embed */
function writeVideoPlayer() {
	var so = new SWFObject("/assets/swf/videoScreen.swf", "flv-player", "354", "199", "9", "#000000");
	so.useExpressInstall('/video/assets/addons/expressinstall.swf');
	so.addParam("allowfullscreen", "true");
	so.addVariable("styleURL", "/video/assets/styles/default/heineken.css");
	so.addVariable("content_video", "shine-a-light.640.low.meta.flv");
	so.write("flash-video");
}

function startMusic(name) {
	thisMovie('musicmovie').startTheMusic();
}

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
        //return document.getElementById(movieName);
    }
    else {
        return document[movieName];
    }
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/* Initialise when document is ready
-------------------------------------------------------------*/

//On docReady
$(document).ready(function() {

	$("a[rel='external']").click(function(e) {
		window.open($(this).attr("href"));
		return false;
	});
	
	var kegimage = $('img.keg-graphic');

	var browser = $.browser;
	
	// IE specific	
	if (browser.msie) {
		// prevents sprite/hover flickering, forces images to remain cached
		// document.execCommand("BackgroundImageCache",false,true);
		
		// set mask to full height
		var pageSize = getPageSizeWithScroll();
		var height = pageSize[1];
		$("div#mask").height(height + "px");
		
	}

	// VERIFY
	if ($("body").attr("id") == "verify") {
		
		$("#verify-links a.link-no").click(function(e) {
			e.preventDefault();
			$("#static-content").fadeOut(speed, function() {
				$("#verify-denied").fadeIn(speed);
			});
		});
		
		$("#music span").click(function(e) {
			if ($(this).hasClass("on")) {
				$(this).removeClass("on")
				$("#music input").removeAttr("checked", "");
			} else {
				$(this).addClass("on")
				$("#music input").attr("checked", "checked");
			}
		});
		
		$("#verify-links a.link-yes").click(function(e) {
			e.preventDefault();
			var href = $(this).attr("href");
			if ($("#music input").is(":checked")) {
				href += "&music=true";
			} else {
				href += "&music=false";
			}
			window.location.href = href;
		});
	}

	/* content fade in 
	-------------------------------------------------------------*/
	
	/* fade in handler */	
	setTimeout(function() {
	
		$('div#bg').animate({opacity : 1}, speed, function(){
		
			
			if($('#flash-animation object, #flash-animation embed').length > 0) {
				try {
					thisMovie('animations').fadeIn(speed);
				} catch (ex) {
					
				}
			}
			
			// If IE 6 or 7
			if (browser.msie && browser.version < 8) {
				
				// immediately show any divs which are supposed to fade
				$('#fade-out').animate({opacity : 1}, 1);
				$('img.keg-graphic').animate({opacity : 1}, 1);
				$('div#footer').animate({opacity : 1}, 1);
				
				// fade out the mask revealing page
				$("div#mask").fadeOut(speed);
				
			} else {
				
				if($('#fade-out').length > 0) {				
					$('#fade-out').animate({opacity : 1}, speed, function() {					
						$('img.keg-graphic').animate({opacity : 1}, speed);					
						$('div#footer').animate({opacity : 1}, speed);					
					});				
				} else {			
					$('img.keg-graphic').animate({opacity : 1}, speed);				
					$('div#footer').animate({opacity : 1}, speed);			
				}
				
			}			
			
			showNavigation();
			
		});
		
	}, speed);
	
	
	/* content fade out 
	-------------------------------------------------------------*/

	/* fade out handler */
	$("ul.nav-mask a, a.heineken-logo, .footer a[rel!='external'], #verify-links a.link-yes, body#win-stuff div.competition a, body#win-stuff div.thankyou div.intro ul a").click(function(e) {
	
		var delay = 0;
	
		e.preventDefault();
		gotoURL = $(this).attr('href');
		
		if($('#flash-animation object, #flash-animation embed').length > 0) {
			try {
			
				thisMovie('animations').fadeOut(speed);
				delay = speed;
				
			} catch(err) {
			
				//Handle errors here
				
			}
		}
		
		/* fade out content when nav item is clicked */
		
		setTimeout(function() {
	
			$('ul.nav-mask li').fadeOut(speed);
			
			$('div#footer').animate({opacity : 0}, speed);
			
			if (browser.msie && browser.version < 8) {
				$('.backup').fadeOut();
			}
			
			// if keg image exists
			
			if(kegimage.length > 0) {
	
				$('img.keg-graphic').fadeOut(speed, function() {
				
					// if fade out div exists
				
					if($('#fade-out').length > 0) {
			
						$('#fade-out').fadeOut(speed, function() {

							$('div#bg').fadeOut(speed, function(){ 
						
								$('div#bg').addClass('black');
								window.location = gotoURL;
								
							});
						
						});
						
					// if fade out div doesn't exist
						
					} else {
					
						$('div#bg').fadeOut(speed, function(){ 
						
							$('div#bg').addClass('black');
							window.location = gotoURL;
							
						});
					
					}
				
				});
				
			// if keg image doesn't exist
				
			} else {
			
				// if fade out div exists
			
				if($('#fade-out').length > 0) {
			
					$('#fade-out').fadeOut(speed, function() {
				
						$('div#bg').fadeOut(speed, function(){ 
					
							$('div#bg').addClass('black');
							window.location = gotoURL;
							
						});
					
					});
				
				// if fade out div doesn't exist
				
				} else {
				
					$('div#bg').fadeOut(speed, function(){ 
					
						$('div#bg').addClass('black');
						window.location = gotoURL;
						
					});
				
				}
			
			}
			
		}, delay);

		
	});
	
	/* set top margin
	-------------------------------------------------------------*/

	//TODO Better resize handler.
	var height = $(window).height();
	var cssObj = { margin: '10% 0 0 0' };
	if( height > 700) {
		$('div#bg').css(cssObj);
	}
	
	try {
	
		var et = new ExtendedType({ path: '/assets/et/et.php' });
		et.replace();
		
	} catch (ex) {
	
	}
	
	// FAQ
	if ($("body").attr("id") == "faq") {
		// remove duplicate section headings
		for (var i=0; i < 10; i++) {
			$("h2.faq-" + i + ":not(:first)").remove();
		}
	}
	
	$("#flash-music a").click(function(e) {
		e.preventDefault();
		if ($(this).hasClass("on")) {
			createCookie("music", "false", 30);
			try {
				thisMovie('animations').mute(true);
			} catch (ex) {
				
			}
			$(this).removeClass("on");
		} else {
			createCookie("music", "true", 30);
			$(this).addClass("on");
			try {
				thisMovie('animations').mute(false);
			} catch (ex) {
				
			}
		}
	})
	
});