var fulldates = [new Date(2001, 1-1, 1), new Date(2011, 9-1, 2), new Date(2011, 9-1, 3), new Date(2011, 9-1, 4), new Date(2011, 9-1, 15), new Date(2011, 9-1, 17), new Date(2011, 9-1, 23), new Date(2011, 9-1, 24), new Date(2011, 9-1, 25),
new Date(2011, 10-1, 6), new Date(2011, 10-1, 7), new Date(2011, 10-1, 8), new Date(2011, 10-1, 30), new Date(2011, 10-1, 31), 
new Date(2011, 11-1, 1)];

var dbledates = [new Date(2001, 1-1, 1), new Date(2011, 9-1, 5), new Date(2011, 9-1, 6), new Date(2011, 9-1, 9), new Date(2011, 9-1, 18), new Date(2011, 9-1, 29), new Date(2011, 9-1, 30)];

// twin booked - new Date(2011, 9-1, 16)

// CYCLE
$(document).ready(function() {
    $('.photos').cycle({
		fx: 'fade' 
	});
});

$(document).ready(function() {
    $('.photos2').cycle({
		fx: 'fade' 
	});
});

$(document).ready(function() {
    $('.photos3').cycle({
		fx: 'fade' 
	});
});

// DATEPICKER
$(function() {
		$("#datepicker").datepicker({dateFormat: 'dd/mm/yy'});
});

$(function() {
		$("#datepicker2").datepicker({dateFormat: 'dd/mm/yy'});
});


$(function(){
    $('#availDate').datepicker({
        numberOfMonths: [1,1],
        dateFormat: 'dd/mm/yy',
        beforeShowDay: highlightDays
	});
    function highlightDays(date) {
        for (var i = 0; i < fulldates.length; i++) {
              if (fulldates[i].getTime() == date.getTime()) {
                 return [true, 'unavailable'];
              }
         }
		for (var i = 0; i < dbledates.length; i++) {
              if (dbledates[i].getTime() == date.getTime()) {
                 return [true, 'doubleonly'];
              }
         }
         return [true, ''];

    }         
});




//  BUTTONS
// submit
$(function() {
	$(".submitbutton").click(function() {
		var name = $("input#name").val();
		if (name == "" ) {
			$("input#name").focus();
			$('#submitMessage').html("<div id='msg'></div>");
			$('#msg').html("<h4>Please enter your name</h4>");	
			alert("Please enter your name");
			return false;
		}
		var email1 = $("input#email1").val();
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(email1) == false) {
			$('#submitMessage').html("<div id='regMsg'></div>");
			$('#regMsg').html("<h4>Please enter a valid email address</h4>");
			alert("Please enter a valid email address");
			$("input#email").focus();
			return false;
		}
		var email2 = $("input#email2").val();
		if (email1 != email2) {
			$('#submitMessage').html("<div id='msg'></div>");
			$('#msg').html("<h4>Email addresses do not match</h4>");	
			alert("Email addresses do not match");
			$("input#email").focus();
			return false;
		}
		var arrival = $("input#datepicker").val();
		var departure = $("input#datepicker2").val();
		var message = $("textarea#message").val();
		var guests = $("select#guests").val();
		var detailsString = "data=" + name + "|" + email1 + "|" + guests + "|" + arrival + "|" + departure + "|" + message;
		$.ajax({
			type: "POST",
			url: "sendenq.php",
			dataType: "text",
			data: detailsString,
			async: false
		})
		$('#submitMessage').html("<div id='msg'></div>");
		$('#msg').html("<h4>Message sent - We will get back to you ASAP - Thanks</h4>");
		alert("Message sent - We will get back to you ASAP - Thanks");
		$("input#name").focus();
		return false;
	});	
}); 

//POPUPS
//0 means disabled; 1 means enabled;  
var popupStatus = 0; 
var label = ''; 

function loadPopup(){  
//loads popup only if it is disabled  
	if(popupStatus==0){  
		//
 		$("#backgroundPopup").css({  
		   	"opacity": "0.7"  
   		});  
   		$("#backgroundPopup").fadeIn("slow");  
		if (label == 'price') {
   			$("#pricePopup").fadeIn("slow");
		}
		if (label == 'find') {
   			$("#findPopup").fadeIn("slow");
		}
		if (label == 'break') {
   			$("#breakPopup").fadeIn("slow");
		}
		if (label == 'contact') {
   			$("#contactPopup").fadeIn("slow");
		}
		if (label == 'things') {
   			$("#thingsPopup").fadeIn("slow");
		}
   		popupStatus = 1;  
   	}  
}

function disablePopup(){  
//disables popup only if it is enabled 
	if(popupStatus==1){  
		$("#backgroundPopup").fadeOut("slow"); 
		
		if (label == 'price') {
 			$("#pricePopup").fadeOut("slow"); 
		}
		if (label == 'find') {
 			$("#findPopup").fadeOut("slow"); 
		}
		if (label == 'break') {
 			$("#breakPopup").fadeOut("slow"); 
		}
		if (label == 'contact') {
 			$("#contactPopup").fadeOut("slow"); 
		}
		if (label == 'things') {
 			$("#thingsPopup").fadeOut("slow"); 
		}
		popupStatus = 0;  
	}  
}

//centering popup  
function centerPopup(){  
//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	if ( label == 'price' ) {
		var popupWidth = $("#pricePopup").width();  
		//centering  
		$("#pricePopup").css({  
			"position": "absolute",  
			"top": 300,  
			"left": windowWidth/2-popupWidth/2
		});
	}
	if ( label == 'find' ) {
		var popupWidth = $("#findPopup").width();  
		//centering  
		$("#findPopup").css({  
			"position": "absolute",  
			"top": 300,  
			"left": windowWidth/2-popupWidth/2
		});
	}
	if ( label == 'break' ) {
		var popupWidth = $("#breakPopup").width();  
		//centering  
		$("#breakPopup").css({  
			"position": "absolute",  
			"top": 300,  
			"left": windowWidth/2-popupWidth/2
		});
	}
	if ( label == 'contact' ) {
		var popupWidth = $("#contactPopup").width();  
		//centering  
		$("#contactPopup").css({  
			"position": "absolute",  
			"top": 300,  
			"left": windowWidth/2-popupWidth/2
		});
	}
	if ( label == 'things' ) {
		var popupWidth = $("#thingsPopup").width();  
		//centering  
		$("#thingsPopup").css({  
			"position": "absolute",  
			"top": 300,  
			"left": windowWidth/2-popupWidth/2
		});
	}

//only need force for IE6  
	$("#backgroundPopup").css({  
		"height": windowHeight  
	});  
}

$(document).ready(function(){  
	//LOADING POPUP  
	//Click the button event!  
	$("#pricebutton").click(function(){ 
		label = 'price'; 
		//centering with css  
		centerPopup();  
		//load popup  
		loadPopup();  
	}); 
	$("#findbutton").click(function(){ 
		label = 'find'; 
		//centering with css  
		centerPopup();  
		//load popup  
		loadPopup();  
	});
	$("#breakbutton").click(function(){ 
		label = 'break'; 
		//centering with css  
		centerPopup();  
		//load popup  
		loadPopup();  
	});
	$("#contactbutton").click(function(){ 
		label = 'contact'; 
		//centering with css  
		centerPopup();  
		//load popup  
		loadPopup();  
	});
	$("#thingsbutton").click(function(){ 
		label = 'things'; 
		//centering with css  
		centerPopup();  
		//load popup  
		loadPopup();  
	});

	//CLOSING POPUP  
	//Click the x event!  
	$("#pricePopupClose").click(function(){  
		disablePopup();  
	}); 
	$("#findPopupClose").click(function(){  
		disablePopup();  
	});  
	$("#breakPopupClose").click(function(){  
		disablePopup();  
	});  

 	//Click out event!  
	$("#backgroundPopup").click(function(){  
		disablePopup();  
	});  
});
