$(document).ready(function()
{	
	$(document).mousedown(function(e){
			if($("ul.resFormDD")){
				$("ul.resFormDD").fadeOut({duration:200});
			}
		})
	
	//this initializes the about drop down
	$('#aboutLinkHead').hover(
		function(){
			$('#aboutDd').fadeIn(200);	   
		},
		function(){
			$('#aboutDd').fadeOut(200);	
		}
	);
	
	//this initializes the fares drop down
	$('#selectedFare').click(function(e){showDDFare(e, 'ul#fareFormDD');return false;});
	
	//these initialize the form dropdowns
	$('#puStateField').click(function(e){showDD(e, 'ul#puStateDD');return false;});
	$('#destStateField').click(function(e){showDD(e, 'ul#destStateDD');return false;});
	$('#hoursField').click(function(e){showDD(e, 'ul#hoursDD');return false;});
	$('#minutesField').click(function(e){showDD(e, 'ul#minutesDD');return false;});
	$('#amField').click(function(e){showDD(e, 'ul#amDD');return false;});
	$('#passengersField').click(function(e){showDD(e, 'ul#passengersDD');return false;});
	$('#subjectField').click(function(e){showDD(e, 'ul#subjectDD');return false;});
	
	
	//this initializes the form submit validation
	$("#quickResForm").validate({
		errorElement: "em",
		errorPlacement: function(error, element) {
			 error.appendTo(element.parent("li").children("label")[0]);
		   },  
		//this is the ajax form submit function   
		submitHandler: function(form){
				var data = new String("");
				var confMsg = new String("");
				var inputs = $('input');
				$("#wrapper").append('<div id="confBox"><h5>Please Confirm the following Information:</h5><ul></ul></div>');		
				var topVal = $("#formDiv").position().top - 200 + "px";
				var leftVal = $("#formDiv").position().left + 50 + "px";
				$("#confBox").css({top:topVal, left:leftVal});	
				inputs.each(function(i){
							key = $(this).attr('name');
							value = escape($(this).val());
							txt = $(this).parent();
							txt = $('label', txt);
							txt = $(txt).text();
							data += key + "=" + value + "&" ;
							
							if (txt.indexOf('*') != -1){
								txt = txt.substring(0, txt.indexOf('*'));
							}
							
							if (value != 'submit'){
								if(txt != 'Special Instructions (ADA)'){
								confMsg += "<li><span>" + txt +":</span><strong> " + unescape(value) + "</strong></li>";}
							}
							});
				$("#confBox ul").append(confMsg + '<li class="buttons"><a href="#" id="cancel"><img src="images/cancel_btn.png" alt="Cancel" /></a><a href="#" id="submitForm"><img src="images/submit_btn.png" alt="Submit" /></a></li>');
					
					$('a#cancel').click(function(){$("#confBox").remove();return false;});
					$('a#submitForm').click(function(){
						$.ajax({
						   type:"POST",
						   url:"proc/res_proc.php",
						   data: data,
						   success: function(resp){
							  	$("div#confBox").fadeOut({duration:200}); 
								$("div#formDiv").html('<div id="confMessage">' + resp + '</div>');   
								}
							});
							return false;});
						}
					});
					//end validate and submit
	
	
	//this is the submission function for the contact form
	$("#contactForm").validate({
		errorElement: "em",
		errorPlacement: function(error, element) {
			 error.appendTo(element.parent("li").children("label")[0]);
		   },
		   
		//this is the ajax contact form submit function   
		submitHandler: function(form){
				var data = new String("");
				var inputs = $('input');
				inputs.push($('#messageField'));
				
				//sets the data to send
				$(inputs).each(function(i){
							key = $(this).attr('name');
							value = escape($(this).val());
							data += key + "=" + value + "&" ;
							});
				$.ajax({
					type:"POST",
					url:"proc/contact_proc.php",
					data:data,
					success: function(resp){
						$("div#formDiv").html('<div id="confMessage">' + resp + '</div>');	
					}
				});
			}
		});//end submit contact form
	
	
	//this initializes the datepicker
	$('#dateField').datepicker({showAnim: 'fadeIn', duration:200, showOn: 'both'});
	
	
	//this is for the fare form
	if($('#fareForm')){
		var cityArray = {}	
	}
});


//this shows the drop down field
function showDD(e, d){
	var leftVal = e.pageX-50;
	var topVal = e.pageY;
	$(d).css({left:leftVal, top:topVal}).fadeIn({duration:250});
	$("li a", d).click(function(){ddPick(this, e.target); return false;});
	return false;
}

function showDDFare(e, d){
	var leftVal = e.pageX-50 + "px";
	var topVal = 200 + "px";
	var winHeight = $(window).height()- 250 + "px";
	$(d).css({left:leftVal, top:topVal, height:winHeight}).fadeIn({duration:250});
	return false;
}

function ddPick(e, n){
	$(n).val($(e).text());
	$('ul.resFormDD').fadeOut({duration:200});
}

function hideDD(d){
	
}

//this is the function that shows the fare
function showFare(e,n){
	
	$('#selectedFare').text(e);
	
	//this is where the rate per mile is set
	var rate = 2.5;
	
	//this is the cost of the meter drop
	var mDrop = 2.5;
	
	var fare = (n * rate) + mDrop;
	$("#locBox h5.return").text(e);
	$("#milesBox h5.return").text(n);
	$("#fareBox h5.return").text('$' + fare.toFixed(2));
	
	$("div.box").show();
	$('ul#fareFormDD').hide();
	
	return false;
}



				  
