var $j = jQuery.noConflict();


/* 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 */

$j(document).ready(function() {
	$j('INPUT.onfocusclear').bind('focus',function () {
		if(!this.oldvalue){
			this.oldvalue = this.value;
		}
		if(this.value == this.oldvalue){
			this.value = '';
			$j(this).removeClass('defaultvalue');
		}
	}).bind('blur',function () {
		if(this.value == ''){
			this.value = this.oldvalue;
			$j(this).addClass('defaultvalue');
		}
	});
	$j('.ihasahover').hover(function(){ $j(this).addClass('hover'); },function(){ $j(this).removeClass('hover'); });

	// Add calendar functionality to input fields

	Date.format = 'dd/mm/yyyy';
	$j('INPUT.wdate').each(function() {
		$j(this).datePicker({ startDate : '01/01/2008'}).dpSetOffset(0,24);
	});
	$j('DIV.quickbook INPUT.wdate').each(function() {
		$j(this).dpSetPosition($.dpConst.POS_TOP,$.dpConst.POS_RIGHT).dpSetOffset($j(this)[0].offsetHeight+2,-4);
	});

	// Make input elements look cool
	if(jQuery.fn.makenice){ $j('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){
	$j('#languages').toggleClass('hidden');
	if(!$j('#languages').hasClass('hidden')){
		$j(document).bind('mouseup', toggleLang);
		$j('#currentlang').bind('mouseup',function() { return false; });
		$j('#languages')[0].style.left = $j(ref).offset().left + 'px';
		$j('#languages')[0].style.top = $j(ref).offset().top + ref.offsetHeight + 'px';
	} else {
		$j(document).unbind('mouseup', toggleLang);
	}
}

/* Tabs switcher */

function showTab(ref,set){
	$j('#'+set+' > LI > A').each(function(i,a){
		$j(a).removeClass('active');
		$j($j(a).attr('href')).addClass('hidden');
	});
	$j(ref).addClass('active');
	$j($j(ref).attr('href')).fadeIn(500).removeClass('hidden').css({display: ''});
	return false;
}

/* Tooltip actions. */

function showTooltip(ref,txt,opt){
	$j('body').append('<div id="tooltip"><div id="tooltip-ending"></div><div id="tooltip-inner">' + txt + '</div></div>');
	$j('#tooltip').bgiframe();
	if(opt){ helper = opt; } else { helper = ref; }
	fit = $j(document).width() - $j(helper).offset().left - $j('#tooltip').width();
	$j('#tooltip')[0].style.top = $j(helper).offset().top + $j(helper).height() + 'px';
	if( fit < 0 ){
		$j('#tooltip')[0].style.left = $j(helper).offset().left + fit + 'px';
		$j('#tooltip-ending')[0].style.backgroundPosition = fit - fit - ($j(document).width() - $j(ref).offset().left) + 'px 0';
	} else {
		$j('#tooltip')[0].style.left = $j(helper).offset().left + 'px';
	}
	$j(ref).bind('mouseout',function() {
		$j('#tooltip').unbind('mouseout');
		$j('#tooltip').remove();
		$j('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;
		$j('#' + 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) $j('#' + id)[0].value = result;
		else $j('#' + 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) {
			$j('#' + id1)[0].value = nextdate;
			$j('#' + 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 = $j('#'+checkInID)[0];
	var checkOutDateCtl = $j('#'+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){
	$j('body').append('<div id="tooltip"><div id="tooltip-inner"></div></div>');
	$j('#tooltip').bgiframe();
	$j('#tooltip-inner')[0].innerHTML = txt;
	$j('#tooltip')[0].style.top = $j(ref).offset().top + 'px';
	$j('#tooltip')[0].style.left = $j(ref).offset().left + 'px';
	$j('#tooltip').bind('mouseout',function tmp() {
		$j('#tooltip').unbind('mouseout', tmp);
		$j('#tooltip').remove();
		$j('IFRAME.bgiframe').remove();
	});
}

function toggleDrop(r,t){
	$j(t).toggleClass('hidden');
	if(!$j(t).hasClass('hidden')){
		$j(document).bind('mouseup', function temp01(){ toggleDrop(r,t); });
		$j(r).bind('mouseup',function() { return false; });
		$j(t).css({ left: $j(r).offset().left + 'px', top: $j(r).offset().top + $j(r)[0].offsetHeight + 'px' });
	} else {
		$j(document).unbind('mouseup');
	}
}

/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * $LastChangedDate: 2007-07-21 18:44:59 -0500 (Sat, 21 Jul 2007) $
 * $Rev: 2446 $
 *
 * Version 2.1.1
 */

(function($){
$.fn.bgIframe = $.fn.bgiframe = function(s) {
	// This is only for IE6
	if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
		s = $.extend({
			top     : 'auto', // auto == .currentStyle.borderTopWidth
			left    : 'auto', // auto == .currentStyle.borderLeftWidth
			width   : 'auto', // auto == offsetWidth
			height  : 'auto', // auto == offsetHeight
			opacity : true,
			src     : 'javascript:false;'
		}, s || {});
		var prop = function(n){return n&&n.constructor==Number?n+'px':n;},
		    html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
		               'style="display:block;position:absolute;z-index:-1;'+
			               (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
					       'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
					       'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
					       'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
					       'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
					'"/>';
		return this.each(function() {
			if ( $j('> iframe.bgiframe', this).length == 0 )
				this.insertBefore( document.createElement(html), this.firstChild );
		});
	}
	return this;
};
})(jQuery);
