$(function(){	 
	/***
	 * Generic functions that come out of the box
	 */
	
	$("span.collapse-content").hide();
	$("a.collapse").click(function(){
		$(this).parents("div").children("span.collapse-content").slideToggle("slow");
		$(this).toggleClass("hide");
		($(this).hasClass("hide")) ? $(this).html("<span class='collapse-minus'>-</span> Hide") : $(this).html("<span class='collapse-plus'>+</span> Read More");
		
		return false;
	});
	
	/*E-mail Exclusives Input Clear*/
	$("input[name=EMAIL]").focus(function()
	{
		var $default = $(this).val();
		$(this).val("");		
		$(this).blur(function()
		{
			if($(this).val()=="")
				$(this).val($default);
		});
	});
	
	/*Lightbox Event Handler*/
	$("a.lightbox").prettyPhoto({
		theme: 'facebook' /* light_rounded / dark_rounded / light_square / dark_square / facebook */
	});
	
	
	/*Datepicker Event Handler*/
	$("a.datepickeropen").click(function(){		
		$("body").append('<div id="datepicker"><a href="javascript:void(0);" class="dp-close" style="float:right;">Close</a>&nbsp;</div>');		
		$("#datepicker").css({'display':'none','position':'absolute','z-index':'10000','left':$(this).offset().left,'top':$(this).offset().top});
		$("a.dp-close").click(function(){$("#datepicker").remove();});

		var opts;
		var arr = $(this).attr("rel").split("_");
		
		switch(arr[0])
		{
			case "res":
				opts = {
					minDate: 0, 
					maxDate: '+1Y',
					dateFormat:'dd-M-yy',
					onSelect: function(dateText){
						$("input[name="+arr[1]+"]").val(dateText);
						$("#datepicker").remove();
					}
					
				}				
			break;
			case "text":
				opts = {
					minDate: 0, 
					maxDate: '+1Y',	
					dateFormat:'dd-M-yy',
					onSelect: function(dateText){						
						$("."+arr[1]).val(dateText);
						$("#reservationPeriod").val( returnReservationPeriod( $.datepicker.parseDate('dd-M-yy',$("input[name='checkInDate']").val()), $.datepicker.parseDate('dd-M-yy',$("input[name='checkOutDate']").val() )) );
						$("#datepicker").remove();
					}
				}
			break;
		}
		
		$("#datepicker").datepicker(opts);		
		$("#datepicker").toggle();
		
		return false;
	});
	
	$("a.modal-link").click(function()
	{
		loadModal(780, "/layout/set/modal/"+$(this).attr("href"));
		return false;
	});
	
	/**
	 * End Generic Functions
	 */

	$(".sidebar-slides .slide").cycle(
	{
		fx: "scrollHorz",
		height:'291',
		speed: 500,
		timeout: 5000,
		pager: ".sidebar-slides ul.slide-pager",
		cleartype: false,
		cleartypeNoBg: true,
		pagerAnchorBuilder: function(idx, slide)
		{
			return '<li><a href="#">&nbsp;</a></li>';
		}
	});
});

function loadModal(mWidth,mURL)
{
	$("body").append('<div id="overlay">&nbsp;</div><div id="modal"><div id="modal-content" class="content"><img src="' + assets + 'images/spinner.gif"></div></div>');
	$("#overlay").css({width:$("body").width(),height:$(document).height()}).show();
	$("#modal").css({"top":($(document).scrollTop()+100),"left":($(window).width()/2)-(mWidth/2)});
	$("#modal").animate({"width":mWidth,"height":"50px"},250,function(){
		$("#modal-content").load(mURL,function(){
			$("#modal").animate({"height":$("#modal-content").outerHeight(true)},500,function(){
				$("#overlay").css({height:$(document).height()});
			});

			$("#overlay, #modal-content p.close a").click(function(){
				$("#modal").animate({"height":"50px"},500,function(){
					$("#modal-content").html("&nbsp");
					$("#modal").animate({"width":"0px","height":"0px"},500,function(){
						$("#modal").remove();
						$("#overlay").remove();
					});
				});
				return false;
			});
		});
	});
}

function returnReservationPeriod(input1, input2) {
    var date1 = new Date(input1);
    var date2 = new Date(input2);

    var minutes = 1000*60;
    var hours = minutes*60;
    var days = hours*24;

    var diff = Math.abs(date1.getTime() - date2.getTime());
	if ( diff > 0 )
	{
		return Math.round(diff / days);
	}
	else
	{
		return 2;
	}

    //return Math.round(diff / days);
}

