/* Fix sub-menu div height
-------------------------------------------------- */
window.onload=function()
	{
		sameHeight(new Array('left','center','right'));
	}

	function sameHeight(divs) // Bring in an array of your divs
	{
		var highest = 0;
		var heighttu = 0;
		for(i = 0; i < divs.length; i++) // Loop through the divs
		{
			// Check to see if this div is the highest?
			try
			{
				if(document.getElementById(divs[i]).offsetHeight > highest)
				{
					// Yes its the highest so set the highest value to this div's height
					highest = document.getElementById(divs[i]).offsetHeight;
				}
			}
			catch (e) { }
		}
		// Loop through divs and set their hieght all the same
		for(i = 0; i < divs.length; i++)
		{
			try
			{
				document.getElementById(divs[i]).style.height = highest+"px";
			}
			catch (e) { }
		}
	
		try
			{
			// now get offset hieght again and we may found we have gone higher this is because of padding
			if(document.getElementById(divs[0]).offsetHeight > highest)
			{
				// correct the height
				highest = highest - (document.getElementById(divs[0]).offsetHeight - highest);
				// Correct divs
				for(i = 0; i < divs.length; i++)
				{
					document.getElementById(divs[i]).style.height = highest+"px";
				}
			}
		}
		catch (e) { }
	}

/* IE6 flicker fix
-------------------------------------------------- */
try { document.execCommand("BackgroundImageCache", false, true); } catch(err){}
jQuery.fn.hasClass = function(c) { return this.is('.'+c) };
function g(r){ return document.getElementById(r); }

/* Init */

$(document).ready(function() {
	$('INPUT.onfocusclear').bind('focus',function () {
		if(!this.oldvalue){
			this.oldvalue = this.value;
		}
		if(this.value == this.oldvalue){
			this.value = '';
			$(this).removeClass('defaultvalue');
		}
	}).bind('blur',function () {
		if(this.value == ''){
			this.value = this.oldvalue;
			$(this).addClass('defaultvalue');
		}
	});
	$('.ihasahover').hover(function(){ $(this).addClass('hover'); },function(){ $(this).removeClass('hover'); });

	// Add calendar functionality to input fields

	Date.format = 'dd/mm/yyyy';
	$('INPUT.wdate').each(function() {
		$(this).datePicker({ startDate : '01/01/2008'}).dpSetOffset(0,24);
	});
	$('DIV.quickbook INPUT.wdate').each(function() {
		$(this).dpSetPosition($.dpConst.POS_TOP,$.dpConst.POS_RIGHT).dpSetOffset($(this)[0].offsetHeight+2,-4);
	});

	// Make input elements look cool
	if(jQuery.fn.makenice){ $('SELECT.makenice, INPUT.makenice, TABLE.bookform SELECT, TABLE.bookform INPUT[type="text"], TABLE.smallform SELECT, TABLE.smallform INPUT[type="text"], TABLE.smallform INPUT[type="password"]').makenice(); };
});

/* Language dropdown */

function toggleLang(ref){
	$('#languages').toggleClass('hidden');
	if(!$('#languages').hasClass('hidden')){
		$(document).bind('mouseup', toggleLang);
		$('#currentlang').bind('mouseup',function() { return false; });
		$('#languages')[0].style.left = $(ref).offset().left + 'px';
		$('#languages')[0].style.top = $(ref).offset().top + ref.offsetHeight + 'px';
	} else {
		$(document).unbind('mouseup', toggleLang);
	}
}

/* Tabs switcher */

function showTab(ref,set){
	$('#'+set+' > LI > A').each(function(i,a){
		$(a).removeClass('active');
		$($(a).attr('href')).addClass('hidden');
	});
	$(ref).addClass('active');
	$($(ref).attr('href')).fadeIn(500).removeClass('hidden').css({display: ''});
	return false;
}

/* Tooltip actions. */

function showTooltip(ref,txt,opt){
	$('body').append('<div id="tooltip"><div id="tooltip-ending"></div><div id="tooltip-inner">' + txt + '</div></div>');
	$('#tooltip').bgiframe();
	if(opt){ helper = opt; } else { helper = ref; }
	fit = $(document).width() - $(helper).offset().left - $('#tooltip').width();
	$('#tooltip')[0].style.top = $(helper).offset().top + $(helper).height() + 'px';
	if( fit < 0 ){
		$('#tooltip')[0].style.left = $(helper).offset().left + fit + 'px';
		$('#tooltip-ending')[0].style.backgroundPosition = fit - fit - ($(document).width() - $(ref).offset().left) + 'px 0';
	} else {
		$('#tooltip')[0].style.left = $(helper).offset().left + 'px';
	}
	$(ref).bind('mouseout',function() {
		$('#tooltip').unbind('mouseout');
		$('#tooltip').remove();
		$('IFRAME.bgiframe').remove();
	});
}

/* Booking calendar */

function getNewDate(date, days, id) {
	if((date != '') && (days != '')) {
		var olddate = new Date(date.substring(6, 10), date.substring(3, 5) - 1, date.substring(0, 2));
		olddate.setDate(olddate.getDate() + eval(days));
		var day = (olddate.getDate() + '').length == 1 ? ("0" + olddate.getDate()) : olddate.getDate();
		var month = olddate.getMonth() + 1;
		month = (month + '').length == 1 ? ("0" + month) : month;
		$('#' + id)[0].value = day + '/' + month + '/' + olddate.getFullYear();
	}
}
function getDateDif(date1, date2, id) {
	if((date1 != '') && (date2 != '')) {
		var d1 = new Date(date1.substring(6, 10), date1.substring(3, 5) - 1, date1.substring(0, 2));
		var d2 = new Date(date2.substring(6, 10), date2.substring(3, 5) - 1, date2.substring(0, 2));
		d11 = d1.getTime();
		d12 = d2.getTime();
		var one_day = 1000 * 60 * 60 * 24;
		var dif = d12 - d11;
		var result = Math.ceil(dif / one_day);
		if (result >= 0) $('#' + id)[0].value = result;
		else $('#' + id)[0].value = 0;
	}
}
function checkDates(date1, date2, id1, id2) {
	if((date1 != '') && (date2 != '')) {
		var d1 = new Date(date1.substring(6, 10), date1.substring(3, 5) - 1, date1.substring(0, 2));
		var d2 = new Date(date2.substring(6, 10), date2.substring(3, 5) - 1, date2.substring(0, 2));
		var next = new Date();
		next.setDate(d1.getDate() + 1);
		var month = next.getMonth() + 1;
		var nextdate = next.getDate() + '/' + month + '/' + next.getFullYear();
		d11 = d1.getTime();
		d12 = d2.getTime();
		var one_day = 1000 * 60 * 60 * 24;
		var dif = d12 - d11;
		var result = Math.ceil(dif / one_day);
		if (result <= 0) {
			$('#' + id1)[0].value = nextdate;
			$('#' + id2)[0].value = '1';
		}
	}
}
function form_minus(ref) {
	if (ref.value > 1) { ref.value = ref.value - 1; }
}
function form_plus(ref) {
	if (ref.value < 1) { ref.value = 0; }
	ref.value = eval(ref.value) + 1;
}

/* Booking */

function SetNextWeek(checkInID, checkOutID){
	var checkInDateCtl = $('#'+checkInID)[0];
	var checkOutDateCtl = $('#'+checkOutID)[0];

	//check if check-in date is after check-out date
	if(CompareDates(checkInDateCtl.value, checkOutDateCtl.value) == true){
		checkOutDateCtl.value = ChangeDate(checkInDateCtl.value);
	}
}

function CompareDates(checkInString, checkOutString){
	var checkInArr = checkInString.split('/');
	var checkOutArr = checkOutString.split('/');
	if(checkInArr.length == 3 && checkOutArr.length == 3){
		try {
			var checkInDate = new Date(checkInArr[2], checkInArr[1], checkInArr[0]);
			var checkOutDate = new Date(checkOutArr[2], checkOutArr[1], checkOutArr[0]);
			if(checkInDate >= checkOutDate) return true;
		} catch(err) {
			return false;
		}
	} else if(checkInArr.length == 3 && checkOutArr.length == 1) {
		return true;
	}
}

function ChangeDate(checkInString){
	var checkInDateArr = checkInString.split('/');
	if(checkInDateArr.length == 3){
		try {
			var checkOutDate = new Date(checkInDateArr[2], checkInDateArr[1] - 1, checkInDateArr[0]);
			checkOutDate.setDate(checkOutDate.getDate() + 7);
			var checkOutDay = checkOutDate.getDate();
			var checkOutMonth = checkOutDate.getMonth() + 1;
			var checkOutYear = checkOutDate.getFullYear();

			//check for proper month, year
			if(checkOutMonth == 0){
				checkOutMonth = 12;
				checkOutYear -= 1;
			}
			if(checkOutDay < 10) checkOutDay = '0' + checkOutDay;
			if(checkOutMonth < 10) checkOutMonth = '0' + checkOutMonth;
			return checkOutDay + '/' + checkOutMonth + '/' + checkOutYear;
		} catch(err) {
			return '';
		}
	} else return '';
}

/* Tooltip */

function showTooltip(ref,txt){
	$('body').append('<div id="tooltip"><div id="tooltip-inner"></div></div>');
	$('#tooltip').bgiframe();
	$('#tooltip-inner')[0].innerHTML = txt;
	$('#tooltip')[0].style.top = $(ref).offset().top + 'px';
	$('#tooltip')[0].style.left = $(ref).offset().left + 'px';
	$('#tooltip').bind('mouseout',function tmp() {
		$('#tooltip').unbind('mouseout', tmp);
		$('#tooltip').remove();
		$('IFRAME.bgiframe').remove();
	});
}