$(document).ready(function() { 
	// common stuff
	$(".sidebar.right a.change").bind("click", framework.fn.reservations.changeItinerary);

	switch (framework.id.toLowerCase()) {
		case "home" : 
			// selectbox replacement
			$("#frmReservation select")
				.selectbox({
					effect: "fade", 
					speed: 150
				});
				
			// datepicker control for pickup/return
			$("#reservations>form>fieldset>ul>li.datetime>input")
				.datepicker({showOn: "both", buttonImage: "/_images/_backgrounds/calendar-icon.png", buttonImageOnly: true});
				
			// set minimum date on pickup date to today's date
			var dtCurDate			= new Date();
			
			$("#dtPickupDate").datepicker("option", "minDate", dtCurDate);
			$("#dtReturnDate").datepicker("option", "minDate", new Date(dtCurDate.getTime() + 86400000));
			// set change handler on pickup date so that when a date is chosen, the minimum date on the return date calendar is set so the user cannot set a return date prior to the pickup date
			$("#dtPickupDate").bind("change", framework.fn.datetime.setMinReturnDate);
			
			// enable tooltips for location map
			$("#additional-content>.left .map>a.right").tipTip({defaultPosition: "right", delay: 0, edgeOffset: -3})
			$("#additional-content>.left .map>a.left").tipTip({defaultPosition: "left", delay: 0, edgeOffset: -3})
			
			// set click handler for lower right-hand side bullet point items
			$("#additional-content>.right ul>li>a").bind("click", framework.fn.bullets.showBulletPointContent);
			
			// set opacity of first slider image so it's visible
			$("#slider-images>li:first")
				.css("opacity", 0)
				.animate(	{"opacity": 1}, 
							(framework.props.slider.currentSliderImageAnimSpeed * 1.5));
							
			// set up generic message wrapper
			framework.fn.validation.init_generic($("#strReturnLocation"));
			$("#strReturnLocation").bind("change", function() { 
				var objStatus			= framework.fn.reservations.checkDropFees($("#strPickupLocation").val(), $(this).val());
				
				if ((objStatus.allowed) && (objStatus.message)) {
					framework.fn.validation.show_generic(this, objStatus.message);
				} else if (!objStatus.allowed) {
					framework.fn.validation.show_generic(this, "You cannot return a vehicle rented from " + $("#strPickupLocation option:selected").text() + " to this location.");
				} else {
					framework.fn.validation.hide(this);
				}
				
				framework.fn.reservations.checkHours();
			});
			$("#strPickupLocation").bind("change", function() { $("#strReturnLocation").trigger("change"); });
			
			// set up generic message wrapper for pickup/return times
			framework.fn.validation.init_generic($("#dtPickupTime"));
			framework.fn.validation.init_generic($("#dtReturnTime"));
			$("#dtPickupTime, #dtReturnTime").bind("change", framework.fn.reservations.checkHours);

			// setup form validation handlers
			$("#frmReservation select.validate, #frmReservation input.validate").each(function() { framework.fn.validation.init($(this)); });
			$("#frmReservation").bind("submit", function() {
				var boolFormResponse			= framework.fn.validation.validate();
				var objDropFeesValid			= framework.fn.reservations.checkDropFees($("#strPickupLocation").val(), $("#strReturnLocation").val());
				var objTimesValid				= framework.fn.reservations.checkHours();
				
				if ((!boolFormResponse) || (!objDropFeesValid.allowed) || (!objTimesValid)) {
					return false;
				} else {
					return true;
				}
			});
			
			// view/change
			$("#global-message-wrap>#global-message a.open-viewchange").bind("click", function() {
				$("#viewchange-modal #frmModal>input[name='action']").val("view");
				$("#viewchange-modal h1")
					.attr("class", "view")
					.text("View your Reservation");
				
				framework.fn.reservations.viewChangeReservation();
			});
			$("#reservations>ul>li>a").bind("click", function() {
				var strCurClass					= $(this).parent("li").attr("class");
				
				$("#viewchange-modal #frmModal>input[name='action']").val(strCurClass);
				$("#viewchange-modal h1")
					.attr("class", strCurClass)
					.text($(this).attr("title"));
				
				framework.fn.reservations.viewChangeReservation();
			});
			
			// initialize the slider
			framework.props.slider.currentSliderImageCount	= $("#slider-images>li").length;
			framework.fn.slider.showSliderImage(0);
			break;

		case "home-new" : 
			// selectbox replacement
			$("#frmReservation select")
				.selectbox({
					effect: "fade", 
					speed: 150
				});
				
			// datepicker control for pickup/return
			$("#reservations>form>fieldset>ul>li.datetime>input")
				.datepicker({showOn: "both", buttonImage: "/_images/_backgrounds/calendar-icon.png", buttonImageOnly: true});
				
			// set minimum date on pickup date to today's date
			var dtCurDate			= new Date();
			
			$("#dtPickupDate").datepicker("option", "minDate", dtCurDate);
			$("#dtReturnDate").datepicker("option", "minDate", new Date(dtCurDate.getTime() + 86400000));
			// set change handler on pickup date so that when a date is chosen, the minimum date on the return date calendar is set so the user cannot set a return date prior to the pickup date
			$("#dtPickupDate").bind("change", framework.fn.datetime.setMinReturnDate);
			
			// enable tooltips for location map
			$("#additional-content>.left .map>a.right").tipTip({defaultPosition: "right", delay: 0, edgeOffset: -3})
			$("#additional-content>.left .map>a.left").tipTip({defaultPosition: "left", delay: 0, edgeOffset: -3})
			
			// set click handler for lower right-hand side bullet point items
			$("#additional-content>.right ul>li>a").bind("click", framework.fn.bullets.showBulletPointContent);
			
			// set opacity of first slider image so it's visible
			$("#slider-images>li:first")
				.css("opacity", 0)
				.animate(	{"opacity": 1}, 
							(framework.props.slider.currentSliderImageAnimSpeed * 1.5));
							
			// set up generic message wrappers
			framework.fn.validation.init_generic($("#dtPickupTime"));
			framework.fn.validation.init_generic($("#dtReturnTime"));
			framework.fn.validation.init_generic($("#strReturnLocation"));
			
			$("#strReturnLocation").bind("change", function() { 
				var objStatus			= framework.fn.reservations.checkDropFees($("#strPickupLocation").val(), $(this).val());
				
				if ((objStatus.allowed) && (objStatus.message)) {
					framework.fn.validation.show_generic(this, objStatus.message);
				} else if (!objStatus.allowed) {
					framework.fn.validation.show_generic(this, "You cannot return a vehicle rented from " + $("#strPickupLocation option:selected").text() + " to this location.");
				} else {
					framework.fn.validation.hide(this);
				}
				
				framework.fn.reservations.checkHours();
			});
			$("#strPickupLocation").bind("change", function() { $("#strReturnLocation").trigger("change"); });
			$("#dtPickupTime, #dtReturnTime").bind("change", framework.fn.reservations.checkHours);

			// setup form validation handlers
			$("#frmReservation select.validate, #frmReservation input.validate").each(function() { framework.fn.validation.init($(this)); });
			$("#frmReservation").bind("submit", function() {
				var boolFormResponse			= framework.fn.validation.validate();
				var objDropFeesValid			= framework.fn.reservations.checkDropFees($("#strPickupLocation").val(), $("#strReturnLocation").val());
				var objTimesValid				= framework.fn.reservations.checkHours();
				
				if ((!boolFormResponse) || (!objDropFeesValid.allowed) || (!objTimesValid)) {
					return false;
				} else {
					return true;
				}
			});
			
			// view/change
			$("#frmView").bind("submit", function() {
				// validation
				
				var strConfNum				= $("#frmView input[name='strConfNum']");
				var strLastName				= $("#frmView input[name='strLastName']");
				var objResults				= framework.fn.viewchange.retrieve(	$(strConfNum).val(), 
																				$(strLastName).val(), 
																				framework.fn.viewchange.callbacks.view.success, 
																				framework.fn.viewchange.callbacks.view.failure);
				
				$("#frmView div.error").slideUp();
				
				return false;
			});
			$("#frmView div.menu>a").live("click", function() {
				$("#frmVCCSubmit input[name='strConfNum']").val($("#frmView input[name='strConfNum']").val());
				$("#frmVCCSubmit input[name='strLastName']").val($("#frmView input[name='strLastName']").val());

				switch (true) {
					case $(this).hasClass("change") : 
						$("#frmVCCSubmit input[name='action']").val("change");
						break;

					case $(this).hasClass("cancel") : 
						//$("#frmVCCSubmit input[name='action']").val("cancel");
						$("#frmVCCSubmit input[name='action']").val("view");
						break;

					case $(this).hasClass("view") : 
					default : 
						$("#frmVCCSubmit input[name='action']").val("view");
						break;
				}
				
				$("#frmVCCSubmit").submit();
			});
			$("#frmChange, #frmCancel").bind("submit", function() {
				// validation 
				
				var strConfNum				= $(this).find("input[name='strConfNum']");
				var strLastName				= $(this).find("input[name='strLastName']");
				var objResults				= framework.fn.viewchange.retrieve(	$(strConfNum).val(), 
																				$(strLastName).val(), 
																				(($(this).attr("id") == "frmChange") ? framework.fn.viewchange.callbacks.change.success : framework.fn.viewchange.callbacks.cancel.success), 
																				(($(this).attr("id") == "frmChange") ? framework.fn.viewchange.callbacks.change.failure : framework.fn.viewchange.callbacks.cancel.failure));
				
				$(this).find("div.error").slideUp();
				
				return false;
			});

			/*
			$("#global-message-wrap>#global-message a.open-viewchange").bind("click", function() {
				$("#viewchange-modal #frmModal>input[name='action']").val("view");
				$("#viewchange-modal h1")
					.attr("class", "view")
					.text("View your Reservation");
				
				framework.fn.reservations.viewChangeReservation();
			});
			*/
			$("#reservations>ul>li>a").bind("click", function() {
				var strCurClass					= $(this).attr("class");
				
				$(this).parent("li").parent("ul").parent("#reservations").attr("class", strCurClass);
				$(this).parent("li").parent("ul").children("li").removeClass("selected");
				$(this).parent("li").addClass("selected");
			});
			
			// initialize the slider
			framework.props.slider.currentSliderImageCount	= $("#slider-images>li").length;
			framework.fn.slider.showSliderImage(0);
			break;

		case "facebook-home" : 
			// selectbox replacement
			$("form select")
				.selectbox({
					effect: "fade", 
					speed: 150
				});
				
			// datepicker control for pickup/return
			$("form>fieldset>ul>li.datetime>input")
				.datepicker({showOn: "both", buttonImage: "../_images/_backgrounds/calendar-icon.png", buttonImageOnly: true});
				
			// set minimum date on pickup date to today's date
			var dtCurDate			= new Date();
			
			$("#dtPickupDate").datepicker("option", "minDate", dtCurDate);
			$("#dtReturnDate").datepicker("option", "minDate", new Date(dtCurDate.getTime() + 86400000));

			// set change handler on pickup date so that when a date is chosen, the minimum date on the return date calendar is set so the user cannot set a return date prior to the pickup date
			$("#dtPickupDate").bind("change", framework.fn.datetime.setMinReturnDate);
			
			// set up generic message wrapper
			framework.fn.validation.init_generic($("#strReturnLocation"));
			$("#strReturnLocation").bind("change", function() { 
				var objStatus			= framework.fn.reservations.checkDropFees($("#strPickupLocation").val(), $(this).val());
				
				if ((objStatus.allowed) && (objStatus.message)) {
					framework.fn.validation.show_generic(this, objStatus.message);
				} else if (!objStatus.allowed) {
					framework.fn.validation.show_generic(this, "You cannot return a vehicle rented from " + $("#strPickupLocation option:selected").text() + " to this location.");
				} else {
					framework.fn.validation.hide(this);
				}
				
				framework.fn.reservations.checkHours();
			});
			$("#strPickupLocation").bind("change", function() { $("#strReturnLocation").trigger("change"); });
			
			// set up generic message wrapper for pickup/return times
			framework.fn.validation.init_generic($("#dtPickupTime"));
			framework.fn.validation.init_generic($("#dtReturnTime"));
			$("#dtPickupTime, #dtReturnTime").bind("change", framework.fn.reservations.checkHours);

			// setup form validation handlers
			$("form select.validate, form input.validate").each(function() { framework.fn.validation.init($(this)); });
			$("form").bind("submit", function() {
				var boolFormResponse			= framework.fn.validation.validate();
				var objDropFeesValid			= framework.fn.reservations.checkDropFees($("#strPickupLocation").val(), $("#strReturnLocation").val());
				var objTimesValid				= framework.fn.reservations.checkHours();
				
				if ((!boolFormResponse) || (!objDropFeesValid.allowed) || (!objTimesValid)) {
					return false;
				} else {
					return true;
				}
			});
			break;
			
		case "reservations-step1-change" : 
			var dtCurDate			= new Date();
			
			$("fieldset.itinerary select")
				.selectbox({
					effect: "fade", 
					speed: 150
				});
			$("fieldset.itinerary ul>li.dates>.datepicker.pickup")
				.datepicker({numberOfMonths: 2, showButtonPanel: false, altField: $("fieldset.itinerary input[name='dtNewPickupDate']"), minDate: dtCurDate, defaultDate: $("fieldset.itinerary input[name='dtNewPickupDate']").val(), onSelect: framework.fn.datetime.setMinReturnDate});
			$("fieldset.itinerary ul>li.dates>.datepicker.return")
				.datepicker({numberOfMonths: 2, showButtonPanel: false, altField: $("fieldset.itinerary input[name='dtNewReturnDate']"), minDate: new Date(dtCurDate.getTime() + 86400000), defaultDate: $("fieldset.itinerary input[name='dtNewReturnDate']").val()});
			break;
			
		case "reservations-step2" : 
			// setup form validation handler so the user MUST pick a car before continuing
			$("#frmReservation")
				.bind(	"submit", 
						function() { 
							if ($("#intCarChoice").val().length > 0) {
								return true;
							} else {
								return false;
							}
						});
			// set click handler for "select" button on each vehicle's <li>
			$("ul.vehicles>li button")
				.bind(	"click", 
						function() {
							var intCurCarID			= parseInt($(this).parent("span").parent("div").parent("li").attr("data-id"));
							
							if ((!isNaN(intCurCarID)) && (intCurCarID > 0)) {
								$("ul.vehicles>li[data-id != '" + intCurCarID + "']").removeClass("selected");
								$("ul.vehicles>li[data-id = '" + intCurCarID + "']").addClass("selected");
								$("#intCarChoice").val(intCurCarID);
								$("#frmReservation").submit();
							}
						});
			// drop-down for showing only cars within a specific category (only visible when "categories only" mode is false)
			$("#intCarClass")
				.bind(	"change", 
						function() { 
							var intCurCarCategory	= parseInt($(this).val());
							
							$("ul.vehicles>li").removeClass("selected");
							$("ul.vehicles>li[data-category != '" + intCurCarCategory + "']").addClass("hidden");
							$("ul.vehicles>li[data-category = '" + intCurCarCategory + "']").removeClass("hidden");
							$("#intCarChoice").val("");

							$("#btnNext").addClass("disabled");
						});
			break;
			
		case "reservations-step3" : 
			// selectbox replacement
			$("#frmReservation select")
				.selectbox({
					effect: "fade", 
					speed: 150
				});
				
			// (pseudo-)checkbox click handler
			$("fieldset.addons>ol>li>div.controls>a").bind("click", framework.fn.reservations.toggleAddon);

			// set click handler to auto-enable (or disable) addon when quantity is changed
			$("fieldset.addons>ol>li>div.qty select").bind("change", framework.fn.reservations.dropdownToggle);
			break;

		case "reservations-step4" : 
			// setup form validation handlers
			$("#frmReservation").bind("submit", framework.fn.validation.classes.checkForm);
			$("#frmReservation input.validate, #frmReservation select.validate").bind("blur change", function() { framework.fn.validation.classes.checkFields(false); });
			
			// set input mask for telephone number
			$("fieldset.form>ul>li>div>input.mask-phone-usa").mask("(999) 999-9999");
			break;
	}
});
