$(document).ready(function() {

	// toggle text inside a form on and off
	$('input[type=text], textarea').focus(function(){
		var formOrigValue = $(this).attr('origvalue');
		var formValue = $(this).attr('value');
		if (formValue == formOrigValue) {
			$(this).attr('value', '')
		}
	});
	$('input[type=text], textarea').blur(function(){
		var formOrigValue = $(this).attr('origvalue');
		var formValue = $(this).attr('value');
		if (formValue == '') {
			$(this).attr('value',formOrigValue);
		}
	});
	
	// Gallery
	$('#gallery a').lightBox({fixedNavigation:true});
	
	
	// Amenities Icons
	$('#amenities li a').hover(
		function() {
			var thisAmenity = $(this).attr('amenity');
			$('.amenityName').html(thisAmenity);
		}, 
		function() {
			$('.amenityName').html('Hover over an icon to see the amenity');
		}
	);


	/* PRIMA */
	// Show Contact Menu
	$('nav ul li.contactBtn > a').click(function(event){
		if ($(this).hasClass('open')) {
			$(this).next('contact').slideUp('fast');
			$(this).removeClass('open');
		} else {
			if ($('#cmsHighlightElement')) {
				$('#cmsHighlightElement').fadeOut(200);
			}
			$(this).next('contact').slideDown('fast');
			$("#contactForm").show();
			$("#contactMessage").hide();
			$("#contactName").val("Name");
			$("#contactEmail").val("Email");
			$("textarea#contactComments").val("Your Message");
			$(this).addClass('open');		
		}
	});
	
	// hides contact menu when clicked outside of it
	$(window).click(function() {
		if ($('#cmsBar').length) {
		} else {
			$('li.contactBtn contact').slideUp('fast');
			$('li.contactBtn a.open').removeClass('open');		
		};
	});	
	// prevents hiding of contact menu when clicked inside
	$('nav ul li.contactBtn > a, nav ul li contact').click(function(event){
		event.stopPropagation();
	});	
	/* SECONDI */
	// Show any accordion panels
	$('li.accordionDisplay > a').live('click',function(){
		if ($(this).hasClass('open')) {
			$(this).next('section').slideUp();
			$(this).removeClass('open');
			$(this).find('.arrow').attr('pos','down');
		} else {
			$(this).next('section').slideDown();
			$(this).addClass('open');
			$(this).next('section').find('h3').hide();
			$(this).find('.arrow').attr('pos','up');			
		}
	});
	/* TRIFECTA */
	// Tabs functionality 
	//tab control
	var isTabOpen = "no";
	$('#tabs li a').click(function(){		
		var openThisTabNoHash = $(this).attr("rel");
		var openThisTab = "#" + openThisTabNoHash;
		if ($(this).hasClass('open')) {
			$(openThisTab).slideUp('fast');
			$(this).removeClass('open');
			var isTabOpen = "no";
		}
		else {
			$(this).parent('li').siblings('li').children('a').removeClass('open');
			$(this).addClass('open');
			if (isTabOpen == "no") {
				$(openThisTab).slideDown('fast');
				var isTabOpen = "yes";
			}
			else {
				$(openThisTab).siblings('.tabContent').hide();
				$(openThisTab).show();
				var isTabOpen = "yes";				
			}
		};
	});

//	// Design includes Layout and Theme
//
//	//Preview Theme
//	$(".selectTheme").live('click',function(){
//		var themeId = $(this).attr('id');
//		var parameter = "themeId="+themeId;
//		$.ajax({
//			type: "POST",
//			url: "/cms-design/preview_theme",
//			data: parameter,
//			dataType: "json",
//			success: function(data){
//				$('#themeId').attr('href' , '/css/theme_'+data.themeId+'.css');
//			}
//		});
//	});
//
//	//Preview Layout
//	$(".selectLayout").live('click',function(){
//		var layoutId = $(this).attr('id');
//		var parameter = "layoutId="+layoutId;
//		$.ajax({
//			type: "POST",
//			url: "/cms-design/preview_layout",
//			data: parameter,
//			dataType: "json",
//			success: function(data){
//				window.location.reload();
//			}
//		});
//	});
//
//	//Save Theme
//	$("#saveTheme").live('click',function(){
//		$.ajax({
//			url: "/cms-design/save_theme",
//			dataType: "json",
//			success: function(data){
//				window.location.reload();
//			}
//		});
//
//	});
//
//	//Cancel Theme
//	$("#cancelTheme").live('click',function(){
//		$.ajax({
//			url: "/cms-design/cancel_theme",
//			dataType: "json",
//			success: function(data){
//				$('#themeId').attr('href' , '/css/theme_'+data.themeId+'.css');
//			}
//		});
//	});
//
//	//Save Layout
//	$("#saveLayout").live('click',function(){
//		$.ajax({
//			url: "cms-design/save_layout",
//			dataType: "json",
//			success: function(data){
//				$("#savecancelLayout").slideUp();
//			}
//		});
//	});
//
//	//Cancel Layout
//	$("#cancelLayout").live('click',function(){
//		$.ajax({
//			url: "cms-design/cancel_layout",
//			dataType: "json",
//			success: function(data){
//				window.location.reload();
//			}
//		});
//	});


$(".event").live("click",function(){
	var calendarDay = $(this).attr("calendarDay");
	var calendarMonth = $(this).attr("calendarMonth");
	var calendarYear = $(this).attr("calendarYear");
	$.ajax({
		type: "POST",
		url: "/event/eventdate/"+calendarYear+"/"+calendarMonth+"/"+calendarDay,
		async: false,
		success: function(data){
			$("#eventList").hide();
			$("#eventList").html(data);
			$("#eventList").fadeIn();
		}

	});
	return false;
});
	
	
$("#selectMonthEvent").live("change",function(){
	var value = $(this).val();
	if (value != 'NULL'){
		$.ajax({
			type: "POST",
			url: "event/eventdate/"+value,
			async: false,
			success: function(data){
					$("#eventList").hide();
					$("#eventList").html(data);
					$("#eventList").fadeIn();
					$.ajax({
						type: "POST",
						url: "event/eventcalendar/"+value,
						async: false,
						success: function(data){
							$(".month").hide();
							$(".month").html(data);
							$(".month").fadeIn();
						}
					});
				}
			});
	}
	return false;
	});


	

//Contact Us
	$("#contactSubmit").click(function(){
		$(".contactError").hide();
		var name = $("#contactName").val();
		if(name == "Name") name = "";
		var email = $("#contactEmail").val();
		if(email == "Email") email = "";
		var comment = $("textarea#contactComments").val();
		if(comment == "Your Message") comment = "";
		var error = 0;
		var contactNameError = "";
		var contactEmailError = "";
		var contactCommentError = "";
		contactNameError = errorMessage('Name',name,'required');
		contactEmailError = errorMessage('Email',email,'required');
		if(contactEmailError === ''){contactEmailError  = validateEmail(email);}
		contactCommentError = errorMessage('Comment',comment,'required');
		if(contactNameError !== ''){
			$("#contactNameError").html(contactNameError);
			$("#contactNameError").slideDown('slow');
			error = 1;
		}
		if(contactEmailError !== ''){
			$("#contactEmailError").html(contactEmailError);
			$("#contactEmailError").slideDown('slow');
			error = 1;
		}
		if(contactCommentError !== ''){
			$("#contactCommentsError").html(contactCommentError);
			$("#contactCommentsError").slideDown('slow');
			error = 1;

		}
		if(error === 1)
			return false;
		else{
			var parameter = "name="+name+"&email="+email+"&comment="+comment;
			$.ajax({
				type: "POST",
				url: "landing_page/send_form",
				data: parameter,
				dataType: "json",
				beforeSend: function(){
					$("#contactMessage").show();
					$("#contactMessage").html("Please wait while we process your request");
				},
				success: function(data){
					$("#contactForm").slideUp("slow");
					$("#contactMessage").html(data.message);
					$("#contactName").val("");
					$("#contactEmail").val("");
					$("textarea#contactComments").val("");
				}
			});
//			setTimeout(function(){
//				$("#contactMessage").fadeOut('fast');
//			},2000);
			return false;

		}

	});

	
});



function errorMessage(Subject,Field,Required){
	if(Field == "" && Required=='required'){
		return Subject+" is required";
	}
	else{
		return "";
	}
}

// function to validates an email address
function validateEmail(thisEmail)
{
	//initiate returnMessage variable
	var returnMessage="";

	if(thisEmail == '')
	{
		// alert the user
		returnMessage = "Please type in a valid email address.";
	}
	else
	{
		// function to check email address vilidity
		function emailCheck(emailStr)
		{
			/* The following pattern is used to check if the entered e-mail address
			   fits the user@domain format.  It also is used to separate the username
			   from the domain. */
			var emailPat=/^(.+)@(.+)$/
			/* The following string represents the pattern for matching all special
			   characters.  We don't want to allow special characters in the address.
			   These characters include ( ) < > @ , ; : \ " . [ ]    */
			var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
			/* The following string represents the range of characters allowed in a
			   username or domainname.  It really states which chars aren't allowed. */
			var validChars="\[^\\s" + specialChars + "\]"
			/* The following pattern applies if the "user" is a quoted string (in
			   which case, there are no rules about which characters are allowed
			   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
			   is a legal e-mail address. */
			var quotedUser="(\"[^\"]*\")"
			/* The following pattern applies for domains that are IP addresses,
			   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
			   e-mail address. NOTE: The square brackets are required. */
			var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
			/* The following string represents an atom (basically a series of
			   non-special characters.) */
			var atom=validChars + '+'
			/* The following string represents one word in the typical username.
			   For example, in john.doe@somewhere.com, john and doe are words.
			   Basically, a word is either an atom or quoted string. */
			var word="(" + atom + "|" + quotedUser + ")"
			// The following pattern describes the structure of the user
			var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
			/* The following pattern describes the structure of a normal symbolic
			   domain, as opposed to ipDomainPat, shown above. */
			var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


			/* Finally, let's start trying to figure out if the supplied address is
			   valid. */

			/* Begin with the coarse pattern to simply break up user@domain into
			   different pieces that are easy to analyze. */
			var matchArray=emailStr.match(emailPat);
			//alert(emailStr);
			var checkMatch='-' + matchArray + '-';
			if (checkMatch == "-null-")
			{
			  /* Too many/few @'s or something; basically, this address doesn't
				 even fit the general mould of a valid e-mail address. */
				returnMessage = "The email address seems incorrect (check @ and .'s).";
			}
			else
			{
				var user=matchArray[1];
				var domain=matchArray[2];

				// See if "user" is valid
				if (user.match(userPat)==null)
				{
					// user is not valid
					returnMessage = "The email's username doesn't seem to be valid (before the @).";
				}

				/* if the e-mail address is at an IP address (as opposed to a symbolic
				   host name) make sure the IP address is valid. */
				var IPArray=domain.match(ipDomainPat)
				if (IPArray!=null) {
					// this is an IP address
					  for (var i=1;i<=4;i++) {
						if (IPArray[i]>255) {
							returnMessage = "The email's destination IP address is invalid.";
						}
					}
				}

				// Domain is symbolic name
				var domainArray=domain.match(domainPat)
				if (domainArray==null) {
					returnMessage = "The email's domain name doesn't seem to be valid (after the @).";
				}

				/* domain name seems valid, but now make sure that it ends in a
				   three-letter word (like com, edu, gov) or a two-letter word,
				   representing country (uk, nl), and that there's a hostname preceding
				   the domain or country. */

				/* Now we need to break up the domain to get a count of how many atoms
				   it consists of. */
				var atomPat=new RegExp(atom,"g")
				var domArr=domain.match(atomPat)
				var len=domArr.length
				if (domArr[domArr.length-1].length<2 ||
					domArr[domArr.length-1].length>4) {
				   // the address must end in a two letter or three letter word.
				   returnMessage = "The email must end in a four-letter domain, three-letter domain, or two letter country.";
				}

				// Make sure there's a host name preceding the domain.
				if (len < 2) {
				   returnMessage="This email is missing a hostname!";
				}
			}
		}
		// call the validation function and return its result
		val=emailCheck(thisEmail);
		// if it returns val=no_submit, stop form
	}

	return returnMessage;
}


