$(function(){
	
	var oBooking = {};
	lunchSet = 0;
	
	$.ajaxSetup({
	async: false,
	cache: false,
	type: "POST",
	error: function(x,e){
							if(x.status==0){
								jAlert('Sorry, your internet connection appears to have gone offline');
							}else if(x.status==404){
								jAlert('Sorry, the requested resource was not found.');
							}else if(x.status==500){
								jAlert('Sorry, there was an internal server error.');
							}else if(e=='parsererror'){
								jAlert('Sorry, there was an error in parsing your data.');
							}else if(e=='timeout'){
								jAlert('Sorry, your request timed out, please try again.');
							}else {
								jAlert('Sorry, an unknown error occured, please try again.');
							}
							
							$("#submit-booking").removeAttr("disabled");	
						}
	});	
	
	/*--------------------------------------------
	| Close the containers & setup arrays
	--------------------------------------------*/
	$(".step-titles").each(function(){
		$(this).next().wrapAll('<div class="newWrap"></div>');
		$(this).next().css({height:0,overflow:'hidden'});
	});
	
	var backTrack=new Array();
		backTrack[0]=true; //set to true as default, as it is open by default
		backTrack[1]=false;
		backTrack[2]=false;
		backTrack[3]=false;
		backTrack[4]=false;
	
	var containerId=new Array();
		containerId[0]='step-one';
		containerId[1]='step-two';
		containerId[2]='step-three';
		containerId[3]='step-four';
		containerId[4]='step-five';
	
	/*--------------------------------------------
	| open first container
	--------------------------------------------*/
	var paddingT = $(".step-titles:first").next().children(".step-wrapper").css("paddingTop");
	var paddingT = parseFloat(paddingT);
	
	var paddingB = $(".step-titles:first").next().children(".step-wrapper").css("paddingBottom");
	var paddingB = parseFloat(paddingB);
	
	var divHeight = $(".step-titles:first").next().children(".step-wrapper").height()+paddingT+paddingB;
	$(".step-titles:first").next().animate({height:divHeight},500, function(){$(this).css({overflow: 'visible'})});
	$(".step-titles:first").css({cursor:'pointer'});
	
	/*--------------------------------------------
	| if next button is click, open next
	| container and close currently open
	--------------------------------------------*/
	$("div.next-button a").live("click",function(){
				
		var formId = $(this).parents("form:first").attr("id");
		
		if (updateBooking(formId) == 1)
		{
			
			var paddingT = $(this).parents(".newWrap").next(".step-titles").next(".newWrap").children(".step-wrapper").css("paddingTop");
			var paddingT = parseFloat(paddingT);
			
			var paddingB = $(this).parents(".newWrap").next(".step-titles").next(".newWrap").children(".step-wrapper").css("paddingBottom");
			var paddingB = parseFloat(paddingB);
			
			var divHeight = $(this).parents(".newWrap").next(".step-titles").next(".newWrap").children(".step-wrapper").height()+paddingT+paddingB;
			
			$(this).parents(".newWrap").next(".step-titles").next(".newWrap").animate({height:divHeight},500, function(){$(this).css({overflow: 'visible'})});
			$(this).parents(".newWrap").animate({height:0},500, function(){$(this).css({overflow: 'hidden'})});
			
			// check id against array to get array position, then set corresponding backTrack to true, to allow "backtracking"
			var stepId = $(this).parents(".newWrap").next(".step-titles").next(".newWrap").children(".step-wrapper").attr("id");
			var arrayIndex = $.inArray(stepId,containerId);
			backTrack[arrayIndex]=true;

			//makes the title look like a link if allowed
			$(".step-titles").each(function(){
				var stepId = $(this).next().children(".step-wrapper").attr("id");
				var arrayIndex = $.inArray(stepId,containerId);
				
				if(backTrack[arrayIndex] == true){
					$(this).css({cursor:'pointer'});	
				}
			});
		}
		document.location.href = '#book-your-party';
		return false;
	});
	
	/*--------------------------------------------
	| backtracking (click on title to open, only
	| if opened once before)
	--------------------------------------------*/
	$(".step-titles").click(function(){
		var stepId = $(this).next().children(".step-wrapper").attr("id");
		var arrayIndex = $.inArray(stepId,containerId);
				
		if(backTrack[arrayIndex] == true){
			if ( $(this).next(".newWrap").height() == 0 ) {
				var paddingT = $(this).next().children(".step-wrapper").css("paddingTop");
				var paddingT = parseFloat(paddingT);
				
				var paddingB = $(this).next().children(".step-wrapper").css("paddingBottom");
				var paddingB = parseFloat(paddingB);
				
				var divHeight = $(this).next().children(".step-wrapper").height()+paddingT+paddingB;
				
				$(".newWrap").animate({height:0},500, function(){$(this).css({overflow: 'hidden'})});
				
				$(this).next(".newWrap").animate({height:divHeight},500, function(){$(this).css({overflow: 'visible'})});
			}
			
			//save current last step data so that it can be reloaded
			if (arrayIndex == 3) {
				updateBooking('booking-five');
			}
			
			//re forces next button when the user back tracks, needed to ensure changes are saved
			$.each(backTrack, function (i, val) {
				if (i > arrayIndex && backTrack[i] == true) {
					backTrack[i] = false;
				}
			});
		}
	});
	
	function updateBooking(formId)
	{
		oBooking.rtnValue = 0;
		
		switch(formId)
		{
			case 'booking-one':
					
				var booking = $("#booking-one").serialize();
				
				$.ajax({
					url: "/lovetoparty/book-your-christmas-party/ajax/update.ajax.php",
					data: booking,
					success: function (msg) {
						
						if (msg == '') {
							$("#step-two").load('/lovetoparty/book-your-christmas-party/steps/step2.ajax.php', function () { 
								oBooking.rtnValue = 1; 
							});
						} else {
							jAlert(msg, 'Before you can proceed');
							return 0;
						}
						
					}
				});
									
			break;
			case 'booking-two':
				
				oBooking.rtnValue = 1;
				
			break;
			case 'booking-three':
								
				if ($("#booking_date_hour").val() > 15 && lunchSet == 1) {
					
					jAlert('You have selected a lunchtime menu but your party time is for the evening. Please change your time if necessary.');
				}
				
				var booking = $("#booking-three").serialize();
				
				$.ajax({
					url: "/lovetoparty/book-your-cocktail-masterclass/ajax/update.ajax.php",
					data: booking,
					success: function (msg) {
						if (msg == '') {
							$("#xmas-matrix").children().remove();
							oBooking.rtnValue = 1;
						} else {
							jAlert(msg, 'Before you can proceed');
							return 0;
						}
					}
				});
				
			break;
			case 'booking-four':
				
				$("#step-five").load("/lovetoparty/book-your-christmas-party/steps/step5.ajax.php", function () { oBooking.rtnValue = 1; });

			break;
			case 'booking-five':
				
				var booking = $("#booking-five").serialize();
				
				$.ajax({
					url: "/lovetoparty/book-your-christmas-party/ajax/update.ajax.php",
					data: booking,
					success: function (msg) {
						oBooking.rtnValue = 1;
					}
				});
				
			break;
		}
		
		return oBooking.rtnValue;
	}
		
	$(".addoption").live("click", function(){
		$.ajax({
			url: "/lovetoparty/book-your-christmas-party/ajax/addoption.ajax.php",
			data: {option_id: $(this).attr("id")},
			success: function (msg) {
				$("#step-two").load('/lovetoparty/book-your-christmas-party/steps/step2.ajax.php');
			}
		});
		
		return false;
	});
	
	$(".removeoption").live("click", function(){
		$.ajax({
			url: "/lovetoparty/book-your-christmas-party/ajax/removeoption.ajax.php",
			data: {option_id: $(this).attr("id")},
			success: function (msg) {
				$("#step-two").load('/lovetoparty/book-your-christmas-party/steps/step2.ajax.php');
			}
		});
		
		return false;
	});
	
	$(".adddrinkoption").live("click", function(){
		var optionId = $(this).attr("id");
		var optionQuantity = $("#quantity_" + optionId);
		var optionData = {option_id: optionId}
		
		if (optionQuantity.length) {		
			var optionQuantity = optionQuantity.attr("value");
			if(optionQuantity > 0) {
				optionData.quantity = optionQuantity;
			} else {
				jAlert('Please specify a numeric quantity');
				return false;
			}
		}
		
		$.ajax({
			url: "/lovetoparty/book-your-christmas-party/ajax/addoption.ajax.php",
			data: optionData,
			success: function (msg) {
				$("#drink-options").load('/lovetoparty/book-your-christmas-party/steps/drink-options.ajax.php');
			}
		});
		
		return false;
	});
	
	$(".removedrinkoption").live("click", function(){
		$.ajax({
			url: "/lovetoparty/book-your-christmas-party/ajax/removeoption.ajax.php",
			data: {option_id: $(this).attr("id")},
			success: function (msg) {
				$("#drink-options").load('/lovetoparty/book-your-christmas-party/steps/drink-options.ajax.php');
			}
		});
		
		return false;
	});
		
	$("#booking_date_hour").change(function(){
		
		var booking = $("#booking-three").serialize();
		
		$.ajax({
			url: "/lovetoparty/book-your-christmas-party/ajax/update.ajax.php",
			data: booking,
			success: function (msg) {
				$("#xmas-matrix").load('/lovetoparty/book-your-christmas-party/steps/matrix.ajax.php');
				$(".jcarousel-container").css({position:'absolute'}); //ie6 fixes, ridiculous way to do it, but only way that worked
		$("#party-dates").css({position:'relative'}); //ie6 fixes
			}
		});
		$(".jcarousel-container").css({position:'absolute'}); //ie6 fixes, ridiculous way to do it, but only way that worked
		$("#party-dates").css({position:'relative'}); //ie6 fixes
		
		$("div#xmas-matrix").parents("div.newWrap").css({height: 'auto'});
		
		return false;
	});
	
	$("#booking_date_date").change(function(){
		
		var booking = $("#booking-three").serialize();
		
		$.ajax({
			url: "/lovetoparty/book-your-christmas-party/ajax/update.ajax.php",
			data: booking,
			success: function (msg) {
				$("#xmas-matrix").load('/lovetoparty/book-your-christmas-party/steps/matrix.ajax.php');
				$(".jcarousel-container").css({position:'absolute'}); //ie6 fixes, ridiculous way to do it, but only way that worked
				$("#party-dates").css({position:'relative'}); //ie6 fixes
			}
		});
		
		$(".jcarousel-container").css({position:'absolute'}); //ie6 fixes, ridiculous way to do it, but only way that worked
		$("#party-dates").css({position:'relative'}); //ie6 fixes
		
		$("div#xmas-matrix").parents("div.newWrap").css({height: 'auto'});
		
		return false;
				
	});
	
	$("#no_people").change(function(){
		
		var booking = $("#booking-three").serialize();
		
		$.ajax({
			url: "/lovetoparty/book-your-christmas-party/ajax/update.ajax.php",
			data: booking,
			success: function (msg) {
				$("#xmas-matrix").load('/lovetoparty/book-your-christmas-party/steps/matrix.ajax.php');
				$(".jcarousel-container").css({position:'absolute'});
				$("#party-dates").css({position:'relative'});
			}
		});
		
		$(".jcarousel-container").css({position:'absolute'}); //ie6 fixes, ridiculous way to do it, but only way that worked
		$("#party-dates").css({position:'relative'}); //ie6 fixes
		
		$("div#xmas-matrix").parents("div.newWrap").css({height: 'auto'});
		
		return false;
				
	});
	
	$("#yes-drinks").click( function () {
		$("#drink-options").load('/lovetoparty/book-your-christmas-party/steps/drink-options.ajax.php', function () { 
			$("div#drink-options").parents("div.newWrap").css({height: 'auto'}); 
			$("#yes-drinks img").attr("src", "/assets/images/xmasmatrix/yes-on.gif");
		});
		return false;
	});
	
	$("#submit-booking").live("click", function() {
		
		$("#submit-booking").attr("disabled", "true");
		
		var booking = $("#booking-five").serialize();
				
		$.ajax({
			url: "/lovetoparty/book-your-christmas-party/ajax/save.ajax.php",
			data: booking,
			success: function (msg) {
				
				if(msg == 'success') {
					window.location = "/lovetoparty/book-your-christmas-party/thankyouxmas.php";
				} else {
					$("#submit-booking").removeAttr("disabled");	
					jAlert(msg, 'Before you can proceed');
				}
				
			}
		});
	});
	
	jQuery.fn.exists = function(){return jQuery(this).length>0;}

	$(".date").datepicker({
		dateFormat: 'dd/mm/yy',
		minDate: new Date(2010, 11 - 1, 1),
		maxDate: new Date(2011, 1 - 1, 31),
		defaultDate:  new Date(2010, 12 - 1, 1) 
	});
	
});//end jQuery

window.setInterval("updateActive()", 600000);

function updateActive() {
	$.ajax({url: "/lovetoparty/book-your-christmas-party/ajax/session.ajax.php"});
};
