/* 
* Miscellaneous scripts utilized on all the pages throughout CheaperStays
* Last edited April 2, 2010 by Brett Somsen
*/

$(function() {
$("#fDestination").click(function() {this.select();});

$("#fDestination").autocomplete({source: "/scripts/citystate.cfm",minLength: 2,
	open: function() {
		$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
	},
	close: function() {
		$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
	}});

function unBlockPageBlocker() {
	$.unblockUI();
}

<!--- http://malsup.com/jquery/block/#page --->
$('#btn-searchhotels').click(function() { 
	$.blockUI({message: '<div style="margin: 0 0 20px 0;"><img src="/images/layout/logo.png" /></div><div style="line-height:20px;font-weight:bold;color:#999999;">Please wait while we search for results...</div><img src="/images/theme/general/loader.gif" />', css: { 
		border: 'none', 
		padding: '15px', 
		backgroundColor: '#ffffff', 
		'-webkit-border-radius': '10px', 
		'-moz-border-radius': '10px', 
		opacity: .7, 
		color: '#fff' 
	} }); 
	setTimeout($.unblockUI, 15000); 
}); 

$('.pleasewait').click(function() {  
	$.blockUI({message: '<div style="margin: 0 0 20px 0;"><img src="/images/layout/logo.png" /></div><div style="line-height:20px;font-weight:bold;color:#999999;">Updating please wait...</div><img src="/images/theme/general/loader.gif" />', css: { 
		border: 'none', 
		padding: '15px', 
		backgroundColor: '#ffffff', 
		'-webkit-border-radius': '10px', 
		'-moz-border-radius': '10px', 
		opacity: .7, 
		color: '#fff' 
	} }); 
	setTimeout($.unblockUI, 15000); 
});  

// Generate the calendar for fCheckInDate
$('input#jCheckInDate').datepicker({
	numberOfMonths: 2,
	showButtonPanel: true,
	minDate: 0,
	stepMonths: 1,
	showOn: 'button',
	buttonImage: '/images/layout/calendar.gif',
	buttonImageOnly: true,
	onSelect: function(dateText, inst) {
		var date = new Date(Date.parse(this.value)); 
		$('input#jCheckInDate').datepicker('setDate',date); 
		if ($('input#jCheckInPopup').length != 0) {$('input#jCheckInPopup').datepicker('setDate',date);}
		date.setDate(date.getDate()+2); 
		$('input#jCheckOutDate').datepicker('setDate',date);
		if ($('input#jCheckOutPopup').length != 0) {$('input#jCheckOutPopup').datepicker('setDate',date);}
	}
});		
	
// Generate the calendar for fCheckOutDate - Takes the check in date and prevents any dates previous of that from being selected
$('input#jCheckOutDate').datepicker({
	numberOfMonths: 2,
	showButtonPanel: true,
	stepMonths: 1,
	beforeShow: function() {
		var vCheckInDate = $('input#jCheckInDate').datepicker('getDate');
		$('input#jCheckOutDate').datepicker('option', 'defaultDate', vCheckInDate);
		$('input#jCheckOutDate').datepicker('option', 'minDate', vCheckInDate);},
	showOn: 'button',
	buttonImage: '/images/layout/calendar.gif',
	buttonImageOnly: true,
	onSelect: function(dateText, inst) {
		var date = new Date(Date.parse(this.value)); 
		$('input#jCheckOutDate').datepicker('setDate',date);
		if ($('input#jCheckOutPopup').length != 0) {$('input#jCheckOutPopup').datepicker('setDate',date);}
	}
});		

$('#btnAdd').click(function() {
	var num		= $('.clonedInput').length;	// how many "duplicatable" input fields we currently have
	var newNum	= new Number(num + 1);		// the numeric ID of the new input field being added
	
	// create the new element via clone(), and manipulate it's ID using newNum value
	var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
	
	// manipulate the name/id values of the input inside the new element
	newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
	newElem.children(':first').attr('id', 'roomcount' + newNum).text('').append("<strong style='font-size: 14px; line-height: 40px; color: #235b83; text-align: center; padding: 0 10px;'>Room " + newNum + ":");
	
	// insert the new element after the last "duplicatable" input field
	$('#input' + num).after(newElem);
	
	// enable the "remove" button
	$('#btnDel').css("visibility","visible");
	
	// business rule: you can only add 5 names
	if (newNum == 5)
		$('#btnAdd').css("visibility","hidden");
});

$('#btnDel').click(function() {
		var num	= $('.clonedInput').length;	// how many "duplicatable" input fields we currently have
		$('#input' + num).remove();		// remove the last element

		// enable the "add" button
		$('#btnAdd').attr('disabled','');

		// if only one element remains, disable the "remove" button
		if (num-1 == 1)
			$('#btnDel').css("visibility","hidden"); 
	});

	$('#btnDel').css("visibility","hidden"); 		

$("#dialog-roompopup").dialog({
		autoOpen: false,
		width: 440,
		height: 400,
		modal: true,
		title: "Enter Travel Details",
		buttons: { 
			"See Rates. Lowest Price Guaranteed!": function() {$('#popupform').submit(); $(this).dialog('close'); $.blockUI();},
		}
});

// Generate the calendar for fCheckInDate
$('input#jCheckInPopup').datepicker({
		numberOfMonths: 2,
		showButtonPanel: true,
		minDate: 0,
		stepMonths: 1,
		showOn: 'button',
		buttonImage: '/images/layout/calendar.gif',
		buttonImageOnly: true,
		onSelect: function(dateText, inst) {
			var date = new Date(Date.parse(this.value)); 
			$('input#jCheckInPopup').datepicker('setDate',date); 
			$('input#jCheckInDate').datepicker('setDate',date); 				
			date.setDate(date.getDate()+2); 
			$('input#jCheckOutPopup').datepicker('setDate',date);
			$('input#jCheckOutDate').datepicker('setDate',date);}
});	
	
// Generate the calendar for fCheckOutDate - Takes the check in date and prevents any dates previous of that from being selected
$('input#jCheckOutPopup').datepicker({
		numberOfMonths: 2,
		showButtonPanel: true,
		stepMonths: 1,
		beforeShow: function() {
			var vCheckInDate = $('input#jCheckInPopup').datepicker('getDate');
			$('input#jCheckOutPopup').datepicker('option', 'defaultDate', vCheckInDate);
			$('input#jCheckOutPopup').datepicker('option', 'minDate', vCheckInDate);},
		showOn: 'button',
		buttonImage: '/images/layout/calendar.gif',
		buttonImageOnly: true,
		onSelect: function(dateText, inst) {var date = new Date(Date.parse(this.value)); $('input#jCheckOutPopup').datepicker('setDate',date);}
});				

});
