/* Name: Inspire.js */

		$(document).ready(function()
		{
			//$.preloadCssImages();
			$(window).bind("resize", checkResize);
			checkResize();
			
			$("a.fancybox").fancybox({
				'width': 671,
				'height': 550,
				'padding': 0,
				'margin': 0,
				'overlayColor': '#000',
				'autoDimensions': false,
				'scrolling': 'no',
				'showCloseButton': false,
				'type': 'iframe',
				'centerOnScroll': false
			});			
			$("a#register").fancybox({
				'width': 720,
				'height': 471,
				'padding': 0,
				'margin': 0,
				'overlayColor': '#000',
				'autoDimensions': false,
				'scrolling': 'no',
				'showCloseButton': false,
				'type': 'iframe',
				'centerOnScroll': false
			});
			//checkVideoPage();
			
			pageInit();
		});


		function pageInit() {
			if(page == "home"){
				$('#arrow').hide();
				$('#register').animate({ height: 150}, 0);
				$('#register').animate({ height: 55}, 1500, function(){ $("#arrow").show(); });
			} 
			
			//Set CSS for Carousel Navigation
			$('.nav_holder ul li a').live('mouseenter', function() {
				//$('#bbq_flash').css('z-index', 302);
				$('#nav_bar_holder').css('z-index', 301);				
				$('.nav_holder').css('z-index', 304);
			});
			
			$('.nav_holder ul li a').live('mouseleave', function() {
				//$('#bbq_flash').css('z-index', 302);
				$('#nav_bar_holder').css('z-index', 301);				
				$('.nav_holder').css('z-index', 300);				
			});

			$('.nav_holder').live('mouseleave', function() {
				//$('#bbq_flash').css('z-index', 302);
				$('#nav_bar_holder').css('z-index', 301);			
				$('.nav_holder').css('z-index', 300);				
			});
			
			$('#send_to_friend a').click(function() {
				$('#background-scroller').animate({left: '-500px'}, 500);
				return false;
			});
			
			// change nav_bars depth depending on mouse position, fixes youtube video controls
			$(document).mousemove(function(e){
				//$('#status').html(e.pageX +', '+ e.pageY);
				if (e.pageY < 450 && e.pageX > 674) {
					$('#nav_bars').css('z-index', '299');	
				} else {
					$('#nav_bars').css('z-index', '301');	
				}				
			});
			
			//$("#container").fadeIn(1000);
			
			$("input").change(checkForm);
			
			//Initialize BBQ and Inspire Carousels
			$(function() {
				var currentRecipe = -1;				
				if ($('#caroStart').text()) {
					currentRecipe = parseInt($('#caroStart').text());
				}

				if (getQueryValue("caro") < 0 && currentRecipe < 0) {
						currentRecipe = 10;
				} else {
						if (currentRecipe < 0){
								currentRecipe = getQueryValue("caro");
						}
				}

				$("#inspire .nav_holder").jCarouselLite({
					btnPrev: "#inspire .nav_left",
					btnNext: "#inspire .nav_right",
					visible: 4,
					scroll: 3,
					circular: true,
					width: "643px",
					start: currentRecipe
				});
			});
			$(function() {
				var currentRecipe = getQueryValue("caro");			
				if (currentRecipe < 0){
					currentRecipe = 10;
				}
			
				$("#bbq .nav_holder").jCarouselLite({
					btnPrev: "#bbq .nav_left",
					btnNext: "#bbq .nav_right",
					visible: 4,
					scroll: 3,
					circular: true,
					width: "643px",	
					start: currentRecipe
				});
			});
			$(function() {
				var currentRecipe = getQueryValue("caro");			
				if (currentRecipe < 0){
					currentRecipe = 10;
				}
			
				$("#boxing .nav_holder").jCarouselLite({
					btnPrev: "#boxing .nav_left",
					btnNext: "#boxing .nav_right",
					visible: 4,
					scroll: 3,
					circular: true,
					width: "643px",	
					start: currentRecipe
				});
			});
			$(function() {
				var currentRecipe = getQueryValue("caro");			
				if (currentRecipe < 0){
					currentRecipe = 10;
				}
			
				$("#videos .nav_holder").jCarouselLite({
					btnPrev: "#videos .nav_left",
					btnNext: "#videos .nav_right",
					visible: 4,
					scroll: 2,
					circular: true,
					width: "643px",	
					start: 0
				});
			});
			
			// Carousel Navigation Click Events
			$('#boxing_tab a').click(function(){
				preload('boxing');
				$('#inspire').hide();
				$('#videos').hide();
				$('#bbq').hide();
				$('#boxing').show();
				$('#boxing').css('visibility', 'visible');
			});
			
			$('#bbq_tab a').click(function(){
				preload('bbq');
				$('#inspire').hide();
				$('#videos').hide();
				$('#bbq').show();
				$('#boxing').hide();
				$('#bbq').css('visibility', 'visible');
			});
			
			$('#video_tab').click(function(){
				$('#bbq').hide();
				$('#inspire').hide();
				$('#videos').show();
				$('#boxing').hide();
				$('#videos').css('visibility', 'visible');
			});
			
			$('#other_tab a').click(function(){
				preload('inspire');
				$('#videos').hide();
				$('#bbq').hide();
				$('#inspire').show();
				$('#boxing').hide();
				$('#inspire').css('visibility', 'visible');
			});
			
			if(page == "videos") {
				$('#boxing').hide();
				$('#inspire').hide();
				$('#videos').show();
				$('#videos').css('visibility', 'visible');
				$('#bbq').hide();
			} else if(page == "bbq") {
				$('#inspire').hide();
				$('#videos').hide();
				$('#bbq').show();
				$('#boxing').hide();
				$('#bbq').css('visibility', 'visible');
			} else if (page == "boxing") {
				$('#boxing').css('visibility', 'visible');
				$('#inspire').hide();
				$('#videos').hide();
				$('#boxing').show();
				$('#bbq').hide();
			} else {
				$('#inspire').css('visibility', 'visible');
				$('#inspire').show();
				$('#videos').hide();
				$('#boxing').hide();
				$('#bbq').hide();
			} 
			preload('nav');
			preload(page);
			
			function preload(target) {
				var baseName = '';
				var max = 0;
				switch(target) {
					case 'nav':
						baseName = 'nav-preload-home-';
						max = 3;
					break;
					case 'inspire':
						baseName = 'inspire-preload-';
						max = 10;
					break;
					case 'bbq':
						baseName = 'bbq-preload-';
						max = 10;
					break;
					case 'boxing':
					default:
						if (window.location.href.indexOf('hellmanns.ie') != -1) {
							baseName = 'ie-boxing-preload-';
						} else {
							baseName = 'uk-boxing-preload-';
						}
						max = 11;
					break;
				}
				var images = new Array();
				for (var i = 1; i <= max; i++) {
					$('#preloadHolder').append('<div class="'+(baseName+i)+'"></div>');
				}
			}
			
			
			// Submit Send to Friend Form
			$("#stf_submit_btn").click(function(){
				errors = checkForm();
				if(!errors)
				{
					//dcsMultiTrack('DCS.dcsuri', '/hellmanns/recipe/<?php echo $_REQUEST['recipe'] ?>/sent');
					$.post("php/mm/SendToFriend.php", $("form").serialize(), function(data) {
						$('#background-scroller').animate({left: '0px'}, 500);
					});
				}
				return false;
			});
			
			setTimeout("checkResize();", 2500);
		}		
		
		// Send Recipe to Friend Form Validation
		function checkForm()
		{
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			
			errors = false;
			
			$("#stf_form_terms").removeClass('error');
			$("#s_firstname").removeClass('error');
			$("#s_email").removeClass('error');
			$("#r_firstname1").removeClass('error');
			$("#r_email1").removeClass('error');
			$("#r_email2").removeClass('error');
			$("#r_email3").removeClass('error');
			
			if(!$("#confirm").is(":checked"))
			{
				$("#stf_form_terms").addClass('error');
				errors							= true;
			}
			
			if($("#s_firstname").val().length <= 0) {
				errors							= true;
				$("#s_firstname").addClass('error');
			} 
			
			if($("#s_email").val().length <= 0 || !pattern.test($("#s_email").val())) {
				errors							= true;
				$("#s_email").addClass('error');
			} 
			
			if($("#r_firstname1").val().length <= 0) {
				errors							= true;
				$("#r_firstname1").addClass('error');
			} 
			
			if($("#r_email1").val().length <= 0 || !pattern.test($("#r_email1").val())) {
				errors							= true;
				$("#r_email1").addClass('error');
			}
			
			if($("#r_email2").val().length > 0 && !pattern.test($("#r_email2").val())) {
				errors							= true;
				$("#r_email2").addClass('error');
			}
			if($("#r_email3").val().length > 0 && !pattern.test($("#r_email3").val())) {
				errors							= true;
				$("#r_email3").addClass('error');
			}
			
			return errors;
		}
		
		
		// Show Recipe		
		var currentPage;
		function setPage(page)
		{
			currentPage		= page;
			$('#product_image').addClass(page);
			$('#product_image').fadeIn('500');
			$('#' + page + '_product').show().css('visibility', 'visible');
			$('#transMap').hide();
			return false;
		}
		
		function closePage()
		{
			$('#transMap').show();
			$('#product_image').fadeOut('500', function()
			{
				$('#' + currentPage + '_product').hide().css('visibility', 'hidden');
				$('#product_image').removeClass(currentPage);
			});
		}
		
		function closeRecipe()
		{
			$("#paper").animate({right: '-383'}, 1000, function(){ });
			$("#recipe_open").fadeIn(1000);
		}
		
		function openRecipe()
		{
			$("#paper").animate({right: '0'}, 1000, function(){ });
			$("#recipe_open").fadeOut(1000);
		}
		
		size = function()
		{
			var w = 0;
			var h = 0;

			// IE
			if(!window.innerWidth)
			{
				// strict mode
				if(!(document.documentElement.clientWidth == 0))
				{
					w = document.documentElement.clientWidth;
					h = document.documentElement.clientHeight;
				}
				// quirks mode
				else
				{
					w = document.body.clientWidth;
					h = document.body.clientHeight;
				}
			}
			// w3c
			else
			{
				w = window.innerWidth;
				h = window.innerHeight;
			}
			return {width:w,height:h};
		};
		
		// Resize Container & Logo based on Window size
		function checkResize()
		{
			if(size().width < 1024 || size().height < 780)
			{
				$("#logo").addClass("small");
			} else {
				$("#logo").removeClass("small");
			}
			$("#container").css({ "height": ($(window).height() > 680) ? $(window).height()-14+"px" : "680px" });
		}
		
		var _tag=new WebTrends();
		_tag.dcsGetId();
		_tag.dcsCustom=function(){};
		_tag.dcsCollect();
		
		// Retrieve Query String Values
		function getQueryValue(term){
			url = window.location.search.substring(1);
			values = url.split("&");
			for (var i=0;i<values.length;i++) {
				value = values[i].split("=");
				if (value[0] == term) {
					return parseFloat(value[1]);
				}	
			}
			return -1;
		}
		
		// Show products page imagemap overlay
		function showProductMap(state){
			if (state){
				$("#productMap").css("display", "block");
				$("#productMap").val("top", $("#productImage").val("top"));
			} else {
				$("#productMap").css("display", "none");
			}
			//alert($("#productMap").css("display"));
		}
		
		// trace and error to the debug div (if it exists)
		function trace(str){
			if ( $('#debug').length ){
			    $('#debug').append(str+'<br/>');
			}
		}
