// hover fix for IE 6
sfHover = function(){
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for(var i=0; i<sfEls.length; i++){
		sfEls[i].onmouseover=function(){
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function(){
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

$(document).ready(function(){
	$('.clicknclear').click(function() {
		$(this).attr('rel', $(this).val());
		$(this).val('');
	}).blur(function() {
		if ($(this).val().length < 1)
			$(this).val($(this).attr('rel'));
	});

    $('.object-selector').click(function() {
		var obj = $.parseJSON(decodeURIComponent($(this).attr('rel')));
		window.opener.selectObj(obj);
		if (!($(this).parents('form:first').hasClass('multi-select'))) {
			window.close();
		}
		return false;
    });

	$('.printme').click(function () {
		window.print();
		return false;
	});
	
	$('.closeme').click(function () {
		if ($(this).attr('target') == '_parent')
		{
			window.opener.location.href = $(this).attr('href');
		}

		window.close();
		return false;
	});

	$('.submit').change(function () {
		$(this).parents('form:first').submit();
	});

	$('.confirm').click(function() {
		var message = $(this).attr('rel');
		if (!message)
			message = 'Are you sure? Press OK to confirm.';

		return confirm(message);
	});

	if (typeof jQuery.fn.datepicker == 'function') {
		$('input.selectdate').datepicker({dateFormat: 'dd/mm/yy', showOn: 'both', buttonImage: '/skin/images/icon-calendar.gif', buttonImageOnly: true});
	}

	if (typeof jQuery.fn.popupwindow == 'function') {
		$('.popupwindow').attr('rel', 'popupwindow').popupwindow({popupwindow: {height: 600, width:600, createnew:0, scrollbars:1, center:1}});
	}


	// FANCYBOX - Gallery
	if (typeof jQuery.fn.fancybox == 'function') {
		$('a.gallery-item').fancybox({
			'overlayOpacity': 0.7,
			'overlayColor':		'#000',
			'titlePosition'		: 'over'
		});
	}

	if (typeof jQuery.fn.scrollTo == 'function') {
		$('.scrollto').click(function() {
			var id = $(this).attr('href');
			$.scrollTo($(id), 500);
			return false;
		});
		if (url.hash && url.hash.length > 0) {
			$.scrollTo($(url.hash), 500);
		}
	}
	
    $('#bp_submit').click(function() {
        if ($('#bp_check_in_date').val() == 'Click for calendar')
          return false;
        
        var date_parts = $('#bp_check_in_date').val().split(/\//);
        var num_of_nights = ($("#bp_num_of_nights").attr('value'));
        var co_date = new Date();
        co_date.setFullYear(parseInt(date_parts[2], 10), parseInt(date_parts[1], 10) - 1, parseInt(date_parts[0], 10));
        co_date.setDate(co_date.getDate()+ parseInt(num_of_nights));
        
        function _num_fmt (num) { if (num <= 9) return '0' + num; else return '' + num; } 
        $('#bp_check_out_date').val(_num_fmt(co_date.getDate() ) + '/' + _num_fmt(co_date.getMonth() + 1) + '/' + co_date.getFullYear());
        document.resbookingform.submit();
        return false; 
    });
});
