/* 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:not(INPUT.bookingdate)').each(function() {
        $(this).datePicker({ startDate: '01/01/2007' }).dpSetOffset(0, 24);
    });
    $('INPUT.bookingdate').each(function() {
        $(this).datePicker({ minDate: 0, maxDate: 365 }).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;
}


/* 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();
	});
}

function toggleDrop(r,t){
	$(t).toggleClass('hidden');
	if(!$(t).hasClass('hidden')){
		$(document).bind('mouseup', function temp01(){ toggleDrop(r,t); });
		$(r).bind('mouseup',function() { return false; });
		$(t).css({ left: $(r).offset().left + 'px', top: $(r).offset().top + $(r)[0].offsetHeight + 'px' });
	} else {
		$(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 ( $('> iframe.bgiframe', this).length == 0 )
				this.insertBefore( document.createElement(html), this.firstChild );
		});
	}
	return this;
};
})(jQuery);

function showBox(t1,ref){
	if( $('DIV.id-'+t1).length > 0 ){
		$('#popupbox').remove();
	} else {
		$('#popupbox').remove();
		$('#footer').after('<div id="popupbox" class="font02 id-' + t1 + '"><div id="popupbox-a"></div></div>');
		$('#popupbox').css({
		left: $(ref).offset().left - $('#box01').offset().left - $('#popupbox').width(),
			backgroundPosition: '100% ' + (0 - 100 + $(ref).offset().top - $('#popupbox').offset().top + ($(ref)[0].offsetHeight / 2) ) + 'px'
		});
		$('#popupbox-a').html($('#'+t1).html());
	}
	return false;
}

function clickForgotPassword(e) {
	var t = e.title || e.name || null;
	var a = e.href || e.alt;
	var g = e.rel || false;
	tb_show(t,a,g);
	e.blur();
	return false;
}

/* Booking */

function SetDefaultBookingPeriod(checkInID, checkOutID, days) {
    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, days);
    }
}

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, days) {
    var checkInDateArr = checkInString.split('/');
    if (checkInDateArr.length == 3) {
        try {
            var checkOutDate = new Date(checkInDateArr[2], checkInDateArr[1] - 1, checkInDateArr[0]);
            checkOutDate.setDate(checkOutDate.getDate() + days);
            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 '';
}

function ConfirmDeletion() {
    return confirm('Are you sure you wish to delete this record?');
}

function openHelp(url) {
    var str = ",height=600,innerHeight=600,width=800,innerWidth=800";
    if (window.screen) {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;

        var xc = (aw - 800) / 2;
        var yc = (ah - 600) / 2;

        str += ",left=" + xc + ",screenX=" + xc;
        str += ",top=" + yc + ",screenY=" + yc;
    }
    window.open(url, '', 'scrollbars=yes,menubar=no' + str + ',resizable=yes,toolbar=no,location=no,status=no');
    return false;
}
