/**
 * @package Capebreeze website
 * @author Bonsai Media / Christian Duller & Bernie Zajac
 * @version 1.0
 * @copyright 2010, Bonsai Media
*/

/* capebreeze object which contains all our functions/etc */
var cape = cape || {};

/* document ready.... magic happens here */
$(document).ready(function(){
	cape.init();
});

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////


/**
 * Initialise the Capebreeze website and various features.
 */
cape.init = function() {
	
	cape.initCufon();
	cape.initGallery();
	cape.initCalendar();
	
	// if browser is IE6 then fixed png background
	if($.browser.msie && $.browser.version < 7) cape.initPNGfix();
	
	$('.imgshadow').boxShadow( 0, 0, 15, "#000");
	$('a').initExternalLinks();
}

/** 
 * Initialise fix png
 */
cape.initPNGfix = function() {
	$("#header_hoder").pngFix(); 
	$("#anim_holder").pngFix(); 
	$("#offset_holder").pngFix(); 
	$("#sub_content").pngFix(); 
}

/** 
 * Initialise light box
 */
cape.initGallery = function(){
	var page = String(window.location).split('/');
	var lstnm = (page.length)-1;
	if(page[lstnm]== "gallery.html"){
		$('.gallery_thumb a').lightBox({fixedNavigation:true});
	} 	
}

/** 
 * Initialise Cufon text replacement
 */
cape.initCufon = function() {
	Cufon.set('fontFamily', 'font-clarendonEF').replace('.logo',{hover: true});
	Cufon.replace('.snd_frd',{hover: true});
	Cufon.set('fontFamily', 'font-clarendonHeavy').replace('.nav_list',{hover: true});
	Cufon.replace('.home_title');
	Cufon.set('fontFamily', 'font-clarendonHeavy').replace('.avl');
	Cufon.set('fontFamily', 'font-clarendonEF').replace($('.sub_page_title'));
}

/**
 * Initialise the availability calendar
 */
cape.initCalendar = function() {
	
	if (!$('#availability').length) return;
	
	// tell jQuery not to cache AJAX calls.
	$.ajaxSetup ({
		cache: false
	});
	
	var cal = 'cal1';

	// beforeShowDay callback function as used by datepicker. Checks
	// whether a particular date exists within a lookup table (unavailable_dates)
	// and if so, this means that this particular date is reserved.
	var unavailableDates = function(date) {
		if (unavailable_dates[date.toString('yyyy-MM-dd')]) return [false, 'date_booked'];
		return [true, ''];
	};
	
	// create an instance of the datepicker (inline)
	$('#availability').datepick({
		numberOfMonths: 3,
		stepMonths: 2,
		showCurrentAtPos: 1, 
		prevText: '&laquo; Back 2 months',
		nextText: 'Forward 2 months &raquo;',
		currentText: 'Jump to Today',
		beforeShowDay: unavailableDates,
		mandatory: true,
		changeMonth: false,
		changeYear: false,
		firstDay: 1,
		minDate: Date.today(),
		dateFormat: 'dd/mm/yy'
	});
	
	// function to check whether the two selected booking dates
	// do not overlap with any other existing bookings
	var bookingDateCheck = function(value, dates) {
		var start_date = dates[0];
		var end_date = dates[1];
		var bookings_exist_in_selection = false;
		if(start_date!=null){
			while (start_date.equals(end_date) !== true && bookings_exist_in_selection == false) {
				if (unavailable_dates[start_date.toString('yyyy-MM-dd')]) bookings_exist_in_selection = true; 
				start_date = start_date.add(1).days();
			}
	
			if (bookings_exist_in_selection) {
				$('#stay_dates').val('');
				alert("\nBookings exist in between your selected dates!\nPlease try alternative dates or get in touch with us.");
			}
		}
	}
	
	// stay dates selector in reservation form
	$('#stay_dates').datepick({
		rangeSelect: true,
		numberOfMonths: 3,
		stepMonths: 2,
		showCurrentAtPos: 1, 
		prevText: '&laquo; Back 2 months',
		nextText: 'Forward 2 months &raquo;',
		currentText: 'Jump to Today',
		beforeShowDay: unavailableDates,
		mandatory: true,
		changeMonth: false,
		changeYear: false,
		firstDay: 1,
		minDate: Date.today(),
		showOn: 'both',
		buttonImageOnly: true,
		buttonImage: 'images/calendar.png',
		onClose: bookingDateCheck,
		dateFormat: 'dd/mm/yy'
	});
	

	// reservation form validator
	$("#res_form").validate({
		errorLabelContainer: $('#res_errors'),
		meta: 'validate'
	});
	
}


/** 
 * Initialise jquery simple modal
 */
cape.initModal = function() {
	$('#location_div').modal({
   	overlayClose:true,
   	onOpen: function (dialog) {
		   Cufon.refresh();
           dialog.overlay.show();
           dialog.container.show();
           dialog.data.show();
           dialog.overlay.one('click', function () {
               $.modal.close();
            });
       }
     });
}

/**
 * Initialises external links (valid xHTML method), by going
 * through elements and checking if they have a "rel" attribute set
 * to "external".
 */
$.fn.initExternalLinks = function() {
	$(this).each(function() {
		obj = $(this);
		if(obj.attr('rel') == 'external') obj.attr('target', "_blank");
	});
}

/**
 * Initialises box shadow
 */
$.fn.boxShadow = function(xOffset, yOffset, blurRadius, shadowColor) {
	if (!$.browser.msie){
		return this.each(function(){
			$(this).css({
				"box-shadow" : xOffset+"px "+yOffset+"px "+blurRadius+"px "+shadowColor,
				"-o-box-shadow" : xOffset+"px "+yOffset+"px "+blurRadius+"px "+shadowColor,
				"-moz-box-shadow" : xOffset+"px "+yOffset+"px "+blurRadius+"px "+shadowColor,
				"-webkit-box-shadow" : xOffset+"px "+yOffset+"px "+blurRadius+"px "+shadowColor
			});
		});
	} else { 
		return this.each(function(){
			$(this).css({
				position: "relative",
				zoom: 1,
				zIndex: "2"
			});
	
			var div = $(document.createElement("div")).addClass('itemtoremove');
			$(this).parent().append(div);

			var _top, _left, _width, _height;
			if (blurRadius != 0) {
				$(div).css("filter", "progid:DXImageTransform.Microsoft.Blur(pixelRadius="+(blurRadius+5)+", enabled='true')");
				_top = yOffset-blurRadius+1;
				_left = xOffset-blurRadius-1;
				_width = $(this).outerWidth()+1;
				_height = $(this).outerHeight()+1;
			} else {
				_top = yOffset;
				_left = xOffset;
				_width = $(this).outerWidth();
				_height = $(this).outerHeight();
			}
		
			$(div).css({
				top: _top,
				left: _left,
				width: _width,
				height: _height,
				background:	shadowColor,
				position: "absolute",
				zIndex: 1
			});
		});
		
	}
};


$.fn.clearShadow = function() {
	if (!$.browser.msie){
	}else{
		return this.each(function(){
			$(".itemtoremove").each(function (i) {
				$(".itemtoremove").remove();
			});
		});
	}
};



