Shadowbox.init({ players: ["img", "iframe"], continuous: true, autoplayMovies: true });

$(function() {	
	// SETUP RESIZE TIMER LOOKING FOR A CHANGE
	var resizeTimer = null;
	$(window).bind("resize", function() {
		if (resizeTimer) {clearTimeout(resizeTimer);}
		resizeTimer = setTimeout(setupBackgrounds, 100);
	});
	
	
	// MOVE ALL BACKGROUNDS IN RELATION TO CURRENT SCREEN SIZE
	function setupBackgrounds() {
		var PageHeight = $(".navigation").outerHeight() + $(".wrapper").outerHeight() + $(".footer").outerHeight();
		var PageWidth = $("html").outerWidth();
		var WrapperWidth = $(".wrapper").outerWidth();
		var WrapperHeight = $(".wrapper").outerHeight();
		
		$(".tractor").css({ height: PageHeight + "px" });
		$(".seabed").css({ height: PageHeight + "px" });
		$(".sub").css({ height: PageHeight + "px" });
		$(".poppies1").css({ height: PageHeight + "px" });
		$(".poppies2").css({ height: PageHeight + "px" });
		$(".tree1").css({ height: PageHeight + "px" });
		$(".tree2").css({ height: PageHeight + "px" });
		$(".fish").css({ height: PageHeight + "px" });
		$(".bubbles").css({ height: PageHeight + "px" });
		
		var CloudHorz1 = (PageWidth / 2) + (WrapperWidth / 2) - 200; // MIDDLE RIGHT
		var CloudHorz2 = (PageWidth / 2) - (WrapperWidth / 2) - 330; // MIDDLE LEFT
		var CloudHorz3 = (PageWidth / 2) + (WrapperWidth / 2) - 150; // TOP RIGHT
		var BackgroundHorz = (PageWidth / 2) - (WrapperWidth / 2);
		var PlaneHorz = (PageWidth / 2) + (WrapperWidth / 2) - 140;
		var TractorHorz = (PageWidth / 2) - 510;
		var TractorVert = PageHeight - 270;
		var SeabedHorz = (PageWidth / 2) - 10;
		var SeabedVert = PageHeight - 135;
		var SubHorz = (PageWidth / 2) + 240;
		var SubVert = PageHeight - 240;
		var PoppiesHorz1 = (PageWidth / 2) - 560;
		var PoppiesHorz2 = (PageWidth / 2) - 580;
		var PoppiesVert1 = PageHeight - 150;
		var PoppiesVert2 = PageHeight - 165;
		var TreeHorz1 = (PageWidth / 2) - 570;
		var TreeVert1 = PageHeight - 340;
		var TreeHorz2 = (PageWidth / 2) - 510;
		var TreeVert2 = PageHeight - 400;
		var FishHorz = (PageWidth / 2) + 80;
		var FishVert = PageHeight - 330;
		var BubblesHorz = (PageWidth / 2) + 280;
		var BubblesVert = PageHeight - 400;
		
		$(".cloud1").css({ backgroundPosition: CloudHorz1 + "px 150px" });
		$(".cloud2").css({ backgroundPosition: CloudHorz2 + "px 180px" });
		$(".cloud3").css({ backgroundPosition: CloudHorz3 + "px 50px" });
		$(".bkg_layer").css({ top: "80px", left: BackgroundHorz + "px", height: WrapperHeight + 160 + "px" });
		$(".plane").css({ backgroundPosition: PlaneHorz + "px 230px" });
		$(".tractor").css({ backgroundPosition: TractorHorz + "px " + TractorVert + "px" });
		$(".seabed").css({ backgroundPosition: SeabedHorz + "px " + SeabedVert + "px" });
		$(".sub").css({ backgroundPosition: SubHorz + "px " + SubVert + "px" });
		$(".fish").css({ backgroundPosition: FishHorz + "px " + FishVert + "px" });
		$(".bubbles").css({ backgroundPosition: BubblesHorz + "px " + BubblesVert + "px" });
		$(".poppies1").css({ backgroundPosition: PoppiesHorz1 + "px " + PoppiesVert1 + "px" });
		$(".poppies2").css({ backgroundPosition: PoppiesHorz2 + "px " + PoppiesVert2 + "px" });
		$(".tree1").css({ backgroundPosition: TreeHorz1 + "px " + TreeVert1 + "px" });
		$(".tree2").css({ backgroundPosition: TreeHorz2 + "px " + TreeVert2 + "px" });
		$(".footer").css({ top: WrapperHeight + 100 + "px", left: ((PageWidth / 2) - (WrapperWidth / 2)) + "px" });
		
		$(".navigation").css({ top: "20px", left: (PageWidth / 2) - (WrapperWidth / 2) + "px" });
	};
	
	
	// NAVIGATION BUTTONS CLICKABLE
	$("ul.navigation li").click(function() {
		window.location = $(this).find("a").attr("href");
		return false;
	});
	
	
	// FAQS
	$(".faq h3").click(function() {
		if ($(this).next().css("display") != "none") {
			$(".faq p:visible").slideUp(300, function() {
				setupBackgrounds();
			});
						
		} else {
			$(".faq p:visible").slideUp(300);
			$(this).next().slideDown(200, function() {
				setupBackgrounds();
			});
		}
	});
	
	
	// PREVIEW SLIDESHOWS
	$(".slideshow").cycle({ fx: "fade" });
	
	// BASKET
	$("#Products :input").change(function() {
		var QuantityDVD = $("#QuantityDVD").val();
		var QuantityDVDArray = QuantityDVD.split("-");
		
		QuantityDVD = QuantityDVDArray[1];
		
		// CHECK WHICH MODE WE ARE IN
		if ($("#QuantityDVD").parent().parent().attr("id") == "PrimaryPurchase") {
			var FilmCount = $("#PrimaryPurchase input.films:checked").length;
			
		} else {
			var FilmCount = $("#SecondaryPurchase input.films:hidden").length;
		}
		
		var DVDAddons = "";
		$("input.addon:checked").each(function() {
			DVDAddons += $(this).attr("id") + "|";
		});
		
		$("input.addon:hidden").each(function() {
			DVDAddons += $(this).attr("id") + "|";
		});
		
		var QuantityOther = "";
		$("#AdditionalPurchase :input").each(function() {
			QuantityOther += $(this).val() + "|";
		});
		
		$.post("/assets/p_products.php", { QuantityDVD: QuantityDVD, FilmCount: FilmCount, DVDAddons: DVDAddons, QuantityOther: QuantityOther }, function(TotalHTML) {
			$("#BasketTotal").html(TotalHTML);
		});
		
		if (FilmCount == 0) {
			$("#AdditionalPurchase").hide();
			$("#Details").hide();
			$("#Payment").hide();
			$("input.addon").attr("checked", "");
			$("input.addon").attr("disabled", "disabled");
			
			setupBackgrounds();
		
		} else {
			$("#AdditionalPurchase").show();
			$("#Details").show();
			$("#Payment").show();
			$("input.addon").attr("disabled", "");
			
			setupBackgrounds();
		}
	});
	
	// SETUP BACKGROUND POSITIONS
	setupBackgrounds();
});


function formOrdering() {
	var Name = $("#Name").val();
	var Address = $("#Address").val();
	var Telephone = $("#Telephone").val();
	var Terms = $("#Terms").attr("checked");
	
	if (Name == "") {
		alert("Please enter your name.");

	} else if (Name.length < 6) {
		alert ("Please enter your full name!");
		
	} else if (Address == "") {
		alert("Please enter your address.");

	} else if (Address.length < 6) {
		alert ("Please enter your delivery address correctly!");
		
	} else if (Telephone == "") {
		alert("Please enter a contact telephone number.");

	} else if (Telephone.length < 6) {
		alert ("Please enter your full telephone number.");
		
	} else if (Terms != true) {
		alert ("Please agree to the terms & conditions to continue your purchase.");
		
	} else {
		$("#BasketForm").submit();
		$("#Submit").attr("disabled", "disabled");
	}
	
	return false;
}
