// 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') {
		var d = new Date();
		var y = d.getFullYear();
		var pasty = y - 100;
		var futy = y + 100;
		$('input.selectdate').each(function () {
			if ($(this).hasClass('past')) {
				$(this).datepicker({dateFormat: 'dd/mm/yy', showOn: 'both', buttonImage: '/skin/images/icon-calendar.gif', buttonImageOnly: true, yearRange:pasty+':'+y});
			} else if ($(this).hasClass('future')) {
				$(this).datepicker({dateFormat: 'dd/mm/yy', showOn: 'both', buttonImage: '/skin/images/icon-calendar.gif', buttonImageOnly: true});
			} else {
				$(this).datepicker({dateFormat: 'dd/mm/yy', showOn: 'both', buttonImage: '/skin/images/icon-calendar.gif', buttonImageOnly: true, yearRange:y+':'+futy});
			}
		});
	}

	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'
		});
		$('a.gallery-trigger').click(function () {
			$('#' + $(this).attr('rel')).trigger('click');
			return false;
		});
		$('.overlay').fancybox({
			'overlayOpacity': 0.7,
			'overlayColor':		'#000',
			'titlePosition'		: 'over',
			'height': 600,
			'width': 450,
			'type': 'iframe'
		});
	}

	if (typeof jQuery.fn.scrollTo == 'function') {
		$('.scrollto').click(function() {
			var id = $(this).attr('href');
			$.scrollTo($(id), 500);
			return false;
		});
		var url = window.location.href;
		if (url && url.hash && url.hash.length > 0) {
			$.scrollTo($(url.hash), 500);
		}
	}

	if (typeof jQuery.fn.truncate == 'function') {
		$('.truncate').truncate({'more': 'read more'});
	}

	if (typeof jQuery.fn.flashembed == 'function') {
		$('.slideshow').each(function () {
			var uid = null;
			var wmode = $(this).hasClass('transparent') ? 'transparent' : 'opaque';
			if ((uid = $(this).attr('id'))) {
				$(this).flashembed({src: '/skin/flash/slideshow.swf', wmode: wmode}, {xml: '/admin/main/bannerzone/data.php?uid=' + uid});
			}
		});
	}

	if (typeof jQuery.fn.orbit == 'function') {
		$('.jslideshow').orbit({bullets: true, bulletThumbs: true, directionalNav: false, advanceSpeed: 8000, animationSpeed: 1000});
	}

	if (typeof jQuery.fn.micromap == 'function') {
		$('.map-container').micromap();
	}

	
    $('#bp_submit').click(function() {
		var val = $('#bp_check_in_date').val();
        if (!val.match(/\d+\/\d+\/\d+/)) {
		  alert('Please choose a check-in date first by clicking on the calendar icon');
          return false;
		}
        
        var date_parts = $('#bp_check_in_date').val().split(/\//);
        var num_of_nights = $("#bp_num_of_nights").val();
        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());
        $('#res-booking-form').submit();
        return false; 
    });

	$('.slidetrigger').click(function () {
		var target = '#slidepanel-' + $(this).attr('rel');
		if ($(target).length > 0) {
			var parent = $(this).parent();
			$('.slidepanel').not(target).hide();
			$('.slidetrigger').parent().removeClass('active');
			
			if (!$(target).is(':visible')) {
				parent.addClass('active');
				$(target).slideDown('slow');
			} else {
				parent.removeClass('active');
				$(target).slideUp('fast');
			}
			return false;
		}
	});

	// This class can be used to put a click event on a entire container. When the
	// container is clicked, the first <a> with a class of "clicktarget" will be called.
	$('.clickwrap').click(function () {
		var href = $(this).find('a.clicktarget').attr('href');
		if (href) {
			location.href = href;
		}
	});

});

