$(document).ready(function(){
	//###############   Form focus & default text   ###############
	$("form input.required:not([type=password]), form textarea.required").each(function(i) {
		$ParentForm = $(this).parents("form");
		if ($ParentForm.attr("name") == "search")
			DefaultValue = "Enter your search term";
		else
			DefaultValue = "required";
		$(this).attr("notequal", DefaultValue);
		if ($(this).val() == "") {$(this).val(DefaultValue).addClass("empty");}
		$(this).focus(function () {
			if ($(this).parents("form").attr("name") == "search")
				DefaultValue = "Enter your search term";
			else
				DefaultValue = "required";
			if ($(this).val() == DefaultValue) {
				$(this).val("").removeClass("empty");
			}
		}).blur(function () {
			if ($(this).val() == "") {
				if ($(this).parents("form").attr("name") == "search")
					DefaultValue = "Enter your search term";
				else
					DefaultValue = "required";

				$(this).val(DefaultValue).addClass("empty");
			}
		});
	});

	$("form .box").click(function() {
		$Option = $(this).find("input");
		$Option.attr('checked',true);
		GetItemPrice($Option);
	});

	//###############   Form Updates relating to Price change   ###############
	$("select[name='tuition-hours'], select[name='rental-duration']").change(function () {
		//###   Dropdown box selection   ###
		GetItemPrice($(this));
	});
	$("input[name='quantity']").keyup(function () {
		//###   Free Entry   ###
		GetItemPrice($(this));
	});
	$("input[name='tuition-packages'], input[name='rental-options'], input[name='clinic-options[]']").bind(($.browser.msie ? "click" : "change"), function () {
		//###   Checkbox & Radio Selection   ###
		GetItemPrice($(this));
	});


	//###############   Clinics Datepicker   ###############
	// Done in specific page script for date variables

	//###############   Bookings Datepicker   ###############
	if ($("#module-holiday-date").length > 0 ) {
		$("#module-holiday-date").datepicker({
			numberOfMonths: 3,
			dateFormat: 'dd/mm/yy',
			minDate: '0',
			showOn: 'button',
			buttonImage: '/themes/site_themes/angulo/images/bookings-calendar.png',
			buttonImageOnly: true,
			showButtonPanel: true,
			beforeShowDay: BookingDateShow,
			onSelect: BookingDateSelected
		});
	}

	//###############   Reveal Date Remove Option - Rollover Action   ###############
	$("#booking-dates-form .arrive, #booking-dates-form .leave").parent().hoverIntent(function() {
		$(this).children("a").fadeIn("slow");
	}, function() {
		$(this).children("a").fadeOut("fast");
	});
	//###############   Delete Date from Basket - Trigger Action   ###############
	$("#booking-dates-form .remove-date").click(function() {
		$(this).hide().prev().html("<br />");
		holidayDates[ $(this).parent().parent().find("div").index( $(this).parent() ) ] = "";
		return false;
	});

	//###############   Reveal Item Remove Option - Rollover Action   ###############
	$(".sidebar .booking .remove-item").parent().hoverIntent(function() {
		$(this).children(".remove-item").fadeIn("slow");
	}, function() {
		$(this).children(".remove-item").fadeOut("fast");
	});
	//###############   Delete Item from Basket - Trigger Action   ###############
/*	$(".sidebar .booking .remove-item").click(function() {
		AjaxComms("delete", $(this));
		return false;
	});*/
});

function GetItemPrice( $fieldID ) {
	var $Form = $fieldID.parents("form")
	var EntryID = $Form.find("input[name='entry_id']").val();

	if (EntryID.length > 0) {
		//###   Send form via AJAX   ###
		$.ajax({
			url: "/ajax/price/" + EntryID,
			type: "POST",
			data: $Form.serialize(),
			dataType: "html",
			success: function (html) {
				//alert(html);
				$Form.find(".price").html(html);
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				//alert(textStatus + errorThrown);
			},
			complete: function (XMLHttpRequest, textStatus) {
				//alert(textStatus);
			}
		});
	}
}

function twoDigits(number) {
	number = parseInt(number);
	if (number < 10)
		return "0"+number;
	else
		return number;
}

var holidayDates = new Array("","");
function BookingDateShow(date) {
	return [true, ""];
}
function BookingDateSelected(dateText, inst) {
//	var rawDate = twoDigits(dateText.getDate()+1) + twoDigits(dateText.getMonth()+1) + dateText.getFullYear();
//	var formattedDate = twoDigits(dateText.getDate()+1) +"-"+ twoDigits(dateText.getMonth()+1) +"-"+ dateText.getFullYear();
	var rawDate = parseInt( dateText.split("/").reverse().join("") );
	
	if (holidayDates[0] == "") {
		holidayDates[0] = rawDate;
		$(".sidebar .date .arrive").html("Arrive: " + dateText);
	} else if (rawDate == holidayDates[0]) {
		holidayDates[0] = "";
		$(".sidebar .date .arrive").html("Arrive: ");
	} else if (rawDate < holidayDates[0]) {
		holidayDates[0] = rawDate;
		$(".sidebar .date .arrive").html("Arrive: " + dateText);
	} else if (rawDate == holidayDates[1]) {
		holidayDates[1] = "";
		$(".sidebar .date .leave").html("Leave: ");
	} else {
		holidayDates[1] = rawDate;
		$(".sidebar .date .leave").html("Leave: " + dateText);
	}

	//###   Save it?   ###
	if (holidayDates[0] != "" && holidayDates[1] != "") {
		$("#module-holiday-date").val(holidayDates[0] + "-" + holidayDates[1]);
		$("#booking-dates-form input[name=from]").val( $(".sidebar .date .arrive").html() );
		var inputVal = $(".sidebar .date .leave").html();
		$("#booking-dates-form input[name=to]").val( inputVal );

//			data: $("#booking-dates-form").serialize() + "&" + holidayDates[0] + " to " + holidayDates[1] + "&action=" + $("#booking-dates-form").attr("action"),
//alert($("#booking-dates-form").serialize() + "&" + holidayDates[0] + " to " + holidayDates[1] + "&action=" + $("#booking-dates-form").attr("action"));
		//###   Send form via AJAX   ###
		$.ajax({
			url: $("#booking-dates-form").attr("action"),
			type: "POST",
			data: $("#booking-dates-form").serialize(),
			dataType: "html",
			success: function (html) {
				$("#booking-dates-form").find(".form-success").slideDown("slow");
				setTimeout('$("#booking-dates-form").find(".form-success").fadeOut("slow");', 5000);
			}
		});
	}
}

function AjaxComms(Action, $Element) {

	//###   Hide trigger   ###
	$Element.hide();

	//###   Send form via AJAX   ###
	$.ajax({
		url: $Element.attr("href"),
		type: "POST",
		dataType: "html",
		success: function (html) {
			//alert("Success: " +  html);

			$Element.parent().parent().remove();
//NEED TO UPDATE TOTAL PRICE
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			//alert("Error: " + textStatus + " - " + errorThrown);
		},
		complete: function (XMLHttpRequest, textStatus) {
			//alert("Complete: " + textStatus);

			//###   Show trigger again   ###
			$Element.fadeIn("slow");
		}
	});
}
