$(function() {
	$("#country").bind("change", function() {
		var c = $("#country").val();
		$.get("/modules/booking/getdata.php" , { q: c } , function(data) {
			$("#city, #dropCity").empty();
			$("#city").prepend(data);
			$("#dropCity").prepend(data);
		})
	});
	
	$("#city").bind("change", function() {
		var c = $("#city").val();
		$("#dropCity").selectOptions(c);
	});
});

// faq stuff
$(function() {
	$(".faqs dd").hide();
	$(".faqs dt").css("cursor","pointer");
	$(".faqs dt").bind("click", function() {
		var qaid = $(this).attr("id");
		qaid = qaid.replace("_q_", "_a_");
		// leave them open, better user experience
		//$(".faqs dd").slideUp(400);
		$("#" + qaid).slideToggle(800);
	});
});

var profiles = {
	info: {
		height:600,
		width:800,
		status:1,
		scrollbars:1,
		resizable:1,
		center:1
	},
	newWin: {
		height:800,
		width:1000,
		status:1,
		scrollbars:1,
		resizable:1,
		location:1,
		toolbar:1,
		center:1
	},
	popWin: {
		height:850,
		width:700,
		status:1,
		scrollbars:1,
		resizable:1,
		center:1
	}
};

// zebra stripe tables
$(function()
{
	$(".leasing_table tr:nth-child(even)").addClass("dark");	
});

$(function()
{
		$(".popupwindow").popupwindow(profiles);
		$(".leasing_table td:not(.vehicle) a:not(.enquire)").attr("rel","popWin").popupwindow(profiles);
		$(".leasing_table td.vehicle a").attr("rel","info").popupwindow(profiles);
		$(".leasing_table td a.enquire").attr("rel","newWin").popupwindow(profiles);
});



/*
 * Selects an option by value
 *
 * @name     selectOptions
 * @author   Mathias Bank (http://www.mathias-bank.de)
 * @param    value specifies, which options should be selected
 * @example  jQuery("#myselect").selectOptions("val1");
 *
 */
jQuery.fn.selectOptions = function(value) {
	this.each(
		function()	{
			if(this.nodeName.toLowerCase() != "select") return;
			
			// get number of options
			var optionsLength = this.options.length;
			
			for(var i = 0; i<optionsLength; i++) {
				if (this.options[i].value == value) {
					this.options[i].selected = true;
				};
			}
		}
	)
	return this;
}
	
/*$(function() {
	$("#city1,#city2").focus( function() { if ( $(this).val()=="[Enter city name]" ) $(this).val(""); } );
	$("#time1,#time2").focus( function() { $(this).val(""); } );
});*/

/*function findValue(li, step) {
	var fld1 = "#city";
	var fld2 = "#country";
	
	switch ( step ) {
		case 1:
			fld1 = "#city";
			fld2 = "#country";
		break;
		case 2:
			fld1 = "#dropCity";
		break;
	}
	
	if( li == null ) {
		// empty the hidden fields
		switch ( step ) {
			case 1:
				$(fld1).val("");
				$(fld2).val("");
			break;
			case 2:
				$(fld1).val("");
			break;
		}
		return;
	}

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
	
	// split sValue into parts and populate hidden fields 
	switch ( step ) {
		case 1:
			var fld1Val = sValue.substring(0, sValue.indexOf('['));
			$(fld1).val($.trim(fld1Val));
			var fld2Val = sValue.substring(sValue.indexOf('[')+1, sValue.indexOf(']'));
			$(fld2).val($.trim(fld2Val));
		break;
		case 2:
			var fld1Val = sValue.substring(0, sValue.indexOf('['));
			$(fld1).val($.trim(fld1Val));
		break;
	}
}

function selectItem(li) {
	findValue(li);
}

function selectItem1(li) {
	findValue(li, 1);
}

function selectItem2(li) {
	findValue(li, 2);
}

function selectItem3(li) {
	findValue(li, 3);
}

function selectItem4(li) {
	findValue(li, 4);
}

function formatItem(row) {
	return row[0] + row[1] ;
}*/

$(document).ready(function() {
/*	$("#city1").autocomplete(
			"/modules/booking/getdata.php",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:1,
				maxItemsToShow:10,
				onItemSelect:selectItem1,
				selectFirst:true
			}
		);

	$("#city2").autocomplete(
			"/modules/booking/getdata.php",
			{
				delay:10,
				minChars:2,
				matchSubset:1,
				matchContains:1,
				cacheLength:1,
				maxItemsToShow:10,
				onItemSelect:selectItem2,
				selectFirst:true
			}
		);
		
	$("#time1").autocompleteArray(
			["00:00","00:30","01:00","01:30","02:00","02:30","03:00","03:30","04:00"
			 ,"04:30","05:00","05:30","06:00","06:30","07:00","07:30","08:00","08:30"
			 ,"09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00"
			 ,"13:30","14:00","14:30","15:00","15:30","16:00","16:30","17:00","17:30"
			 ,"18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30","22:00"
			 ,"22:30","23:00","23:30"],
			{
				delay:10,
				minChars:1,
				matchContains:1,
				cacheLength:10,
				maxItemsToShow:10,
				onItemSelect:selectItem3,
				selectFirst:true
			}
		);
	
	$("#time2").autocompleteArray(
			["00:00","00:30","01:00","01:30","02:00","02:30","03:00","03:30","04:00"
			 ,"04:30","05:00","05:30","06:00","06:30","07:00","07:30","08:00","08:30"
			 ,"09:00","09:30","10:00","10:30","11:00","11:30","12:00","12:30","13:00"
			 ,"13:30","14:00","14:30","15:00","15:30","16:00","16:30","17:00","17:30"
			 ,"18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30","22:00"
			 ,"22:30","23:00","23:30"],
			{
				delay:10,
				minChars:1,
				matchContains:1,
				cacheLength:10,
				maxItemsToShow:10,
				onItemSelect:selectItem4,
				selectFirst:true
			}
		);*/

if ( typeof $().datePicker == 'function' ) {
	$(function() {
			$('.date-pick').datePicker({clickInput:true});
			// set the earliest number of days from today that a booking can be made
			var daysGrace = 1;
			// new start date object
			
			var startDate = new Date();
			if ( typeof startDate.addDays == 'function' ) {
				startDate.addDays(daysGrace);
				// set the start date
				$('.date-pick').dpSetStartDate(startDate.asString());
				// set the end date for collections
				var endDate1 = new Date();
				var m1 = endDate1.getMonth();
				//if ( m1 >= 3 &&  m1 <= 11 )
					endDate1.addYears(2);
				var ed1 = '31/03/' + endDate1.getFullYear();
				$('#date1').dpSetEndDate(ed1);
				// set the end date for returns
				var endDate2 = new Date();
				//if ( m1 >= 3 &&  m1 <= 11 )
					endDate2.addYears(2);
				var ed2 = '30/04/' + endDate2.getFullYear();
				$('#date2').dpSetEndDate(ed2);
				// bind the two together
				$('#date1').bind(
					'dpClosed',
					function(e, selectedDates)
					{
						var d = selectedDates[0];
						if (d) {
							$('#date2').dpSetStartDate(d.addDays(1).asString());
						}
					}
				);
				$('#date2').bind(
					'dpClosed',
					function(e, selectedDates)
					{
						var d = selectedDates[0];
						if (d) {
							$('#date1').dpSetEndDate(d.addDays(-1).asString());
						}
					}
				);
			}
		});
	
	$("form").submit( function() {
		   var sdate = $('#date1').val();
		   var stime = $('#time1').val();
		   var edate = $('#date2').val();
		   var etime = $('#time2').val();
		   
		   var sarr = sdate.split('/');
		   var s = sarr[2] + '-' + sarr[1] + '-' + sarr[0] + 'T' + stime + ':00';
		   
		   var earr = edate.split('/');
		   var e = earr[2] + '-' + earr[1] + '-' + earr[0] + 'T' + etime + ':00';
		   
		   $('#start').val(s);
		   $('#end').val(e);
		 } );
	
	$("#gcarslease").attr("scrolling","auto");
}

	// select the current page in the nav
	if ( $('#navigation2') ) {
		var pathname = window.location.pathname;
		$('#navigation2 li.tab').each(function(){
			if ( pathname.indexOf( $(this).children("a").attr("href") ) == 0  ) {
				$("#navigation2 li.tab").children("p").fadeOut(100);
				$("#navigation2 li.tab").removeClass('selected');
				$(this).children("p").fadeIn(500);
				$(this).addClass("selected");
			}
		});
	}
});

// nav functions
$(function() {
	$.browser.msie6 = $.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent);
	if ( $('#navigation2') && !$.browser.msie6 ) {
		$("#navigation2 li.tab").hover(function(){
			if ( $(this).attr("class").indexOf('selected') == -1 ) {
				$("#navigation2 li.tab").children("p").fadeOut(100);
				$("#navigation2 li.tab").removeClass('selected');
				$(this).children("p").fadeIn(500);
				$(this).addClass('selected');
			}
		});
	}
});

