/* ********** (C) TAXI2AIRPORT 2009 ********** */

/* ********** ****** ********* ********** */
/* ********** JQUERY FUNCTIONS ********** */
/* ********** ****** ********* ********** */

$(document).ready(function() {
	$("area").mouseover(function() {
		var country = this.className; 
		$("#map").attr("src", "gfx/eu-" + country + ".png"); // 
    });
	
	$("area").mouseout(function() {
		$("#map").attr("src", "/taxi2airport/ontwerpen/taxi2airport.com/gfx/eu.png"); // 
    });
	
	$("table.countries tr").click(function() {
		var id = this.id;
		var url = $("#" + id + " td:first a").attr("href");
		window.location = url;
	});
	
	$("div.list div div").click(function() {
		var id = this.id;
		var url = $("#" + id + " a").attr("href");
		window.location = url;
	});
	
	$("#logo").click(function() {
		$("div#en").slideToggle(); 
    });
	
	$("#title1").click(function() {
		$("#content1").slideToggle(600); 
		$("#comment1").slideToggle(150); 
    });
	
	$("#title2").click(function() {
		$("#content2").slideToggle(400); 
		$("#comment2").slideToggle(150); 
    });
	
	var showmap = true;
	$("#closemap").click(function() {
		showmap = !showmap;
		$("div.flashobject").toggle(); 
		if (showmap)
		{
			$("#showhide").attr("src", "/taxi2airport/ontwerpen/taxi2airport.com/gfx/show-c.png");
		}
		else
		{
			$("#showhide").attr("src", "/taxi2airport/ontwerpen/taxi2airport.com/gfx/hidden-c.png");
		}
    });
	
	$("div.collapse").css('cursor', 'pointer');
	$("table.countries tr").css('cursor', 'pointer');
	$("div.list div div").css('cursor', 'pointer');
	$("div.countryinfo span.button").css('cursor', 'pointer');


	/* QUOTE countryinfo */
	// see http://henrik.nyh.se/examples/truncator/
	$(function()
	{
		$('div.countryinfo').truncate({max_length: 500});
    });

	$("#foottip a").tooltip({ 
	    bodyHandler: function() { 
	        return $($(this).attr("href")).html(); 
	    }, 
	    showURL: false 
	});
	
	$("div.airports li").tooltip({ 
		bodyHandler: function() { 
	        return $('ap'+$(this).attr("ap")).html(); 
	    }, 
	    showURL: false 
	});
	
	$("form#countryform select#country").change(function(){ 
		$("form#countryform").submit();
	});
    $("form#countryform input.button").hide();

	/* QUOTE: expand/collapse return trip */
    $("#offerte-retour").change(function(){ 
		$(".retour").toggle();
		checkDate("#offerte-retourdatum", "#offerte-vertrektijdteruguur", "#offerte-vertrektijdterugminuut", "", "");
		
		// return date is empty at first, so create one if necessary
		retDate = '' + $("#offerte-retourdatum").val();
		
		setReturnDate(retDate);
		showDate("#dateReturn", "#offerte-retourdatum");
	});
	
});


/* ********** ********** ********* ********** */
/* ********** JAVASCRIPT FUNCTIONS ********** */
/* ********** ********** ********* ********** */

function setReturnDate(retDate)
{
	if (retDate == '')
	{
		d = new Date();
		m = d.getMonth() + 1;
		dd = m + '/' + d.getDate() + '/' + d.getFullYear();
		$("#dateReturn").text('Selected date: ' + dd);
		$("#offerte-retourdatum").val(dd);
		$("#offerte-vertrektijdteruguur").val($("#offerte-vertrekuur").val());
		$("#offerte-vertrektijdterugminuut").val($("#offerte-vertrekminuut").val());
	}
}

/**
 * Check for a date/time that is in the past
 * So customers won't request a quote for a ride in the past
 */
function checkDate(xDate, xHour, xMinute, timePast, dateType)
{
	selCal = $(xDate).val(); // the date that is selected in the calendar
	selYear = selCal.substring(10,6);
	selMonth = selCal.substring(0,2);
	selDay = selCal.substring(5,3);
	selHour = $(xHour).val();
	selMinute = $(xMinute).val();
	
	selDate.setYear(selYear);
	selDate.setMonth(selMonth);
	selDate.setDate(selDay);
	selDate.setHours(selHour);
	
	if ((selYear==curYear) && (selMonth==curMonth) && (selDay==curDay) && (selHour<curHour))
	{
		selHour = curHour;
		$(timePast)
			.html('<div>The date and time you selected were in the past. The time has been adjusted to ' + selHour + ':' + selMinute + ' hrs.</div>');
		$(xHour).val(selHour);
	}
	else
	{
		$("#msgTimeDepart").html('');
		$("#msgTimeReturn").html('');
	}
	
	departBeforeReturnTime (dateType);
}

/*
 * Check if the departure time is earlier than the return time
 * If both are on the same day
 */
function departBeforeReturnTime (dateType)
{	
	// departure data
	depCal = $("#offerte-vertrekdatum").val(); // the date that is selected in the calendar
	depYear = depCal.substring(10,6);
	depMonth = depCal.substring(0,2);
	depDay = depCal.substring(5,3);
	depHour = $("#offerte-vertrekuur").val();
	depMinute = $("#offerte-vertrekminuut").val();
	
	// return data
	retCal = $("#offerte-retourdatum").val(); // the date that is selected in the calendar
	retYear = retCal.substring(10,6);
	retMonth = retCal.substring(0,2);
	retDay = retCal.substring(5,3);
	retHour = $("#offerte-vertrektijdteruguur").val();
	retMinute = $("#offerte-vertrektijdterugminuut").val();
	
	setReturnDate(retCal);
	/*
	 * Departure and return day should be the same
	 * If departure time is past return time, correct the time that is not changed 
	 */
	if ( (depYear == retYear && depMonth == retMonth && depDay == retDay ) && ( depHour > retHour || ( depHour == retHour && depMinute > retMinute )))
	{
		/*
		 * dateType: depart or return 
		 * e.g. depart: this means the depart time or date has been changed
		 * then the return time should be corrected if necessary
		 * plus a message should be displayed
		 * and vice versa for return
		 */
		
//		alert('dateType: ' +dateType);
		if (dateType == '')
		{
			t4 = depHour;
			x2 = '#offerte-vertrektijdteruguur';
//			alert('depHour: ' +depHour);
			
		}
		else
		{
			if (dateType == 'depart')
			{
				t1 = 'departure';
				t2 = 'past';
				t3 = 'return';
				t4 = depHour;
				t5 = depMinute;
				x1 = '#msgTimeDepart';
				x2 = '#offerte-vertrektijdteruguur';
			}
			
			if (dateType == 'return')
			{
				t1 = 'return';
				t2 = 'before';
				t3 = 'departure';
				t4 = retHour;
				t5 = retMinute;
				x1 = '#msgTimeReturn';
				x2 = '#offerte-vertrekuur';
			}
			$("#msgTimeDepart").html('');
			$("#msgTimeReturn").html('');
			msg = '<div>The '+t1+' time you selected was '+t2+' the '+t3+' time. The '+t3+' time has been adjusted to ' + t4 + ':' + t5 + ' hrs.</div>';
			$(x1).html(msg);
		}
		
		$(x2).val(t4);
	}
	
	// show corrected times
	showDate("#dateDepart", "#offerte-vertrekdatum");
	showDate("#dateReturn", "#offerte-retourdatum");
	
}

/**
 * Show the selected date above the calendar 
 */
function showDate(xpath, date)
{
//	alert('showDate');
	$(xpath).text('Selected date: ' + $(date).val());
}

function addHour()
{
	curHour++;
	if (curHour==24)
	{
		curHour = 0;
		curDay
	}
	curDay = curDate.getDate();
	curMonth = curDate.getMonth() + 1;
	curYear = curDate.getFullYear();

}

