﻿/**
 * This is the main JS file. It initializes some plugins and contains the functionality
 * for the send email form.
 * 
 * Author: Pexeto
 * http://pexeto.com/
 */

var searchClicked=false;

var nullNameError="Please insert your name";
var nullEmailError="Please insert your email";
var nullQuestionError="Please insert your question";
var invalidEmailError="Please insert a valid email address";

var urlToPhp = "http://www.pousadadosciprestes.com.br/wp-content/themes/Sintagma_wp/sendEmail.php";

var valid=true;

$(function(){
	//$('ul#menuUl').superfish();
	$('ul.sf-menu').superfish();
	
 	Cufon.replace('h1');
	Cufon.replace('h2');
	Cufon.replace('h3');
	Cufon.replace('caption');
	Cufon.replace('#menu li a');
	Cufon.replace('#footer li a');
	Cufon.replace('#footer p');
		
	setSearchInputClickHandler();
	validateSendEmailForm();

//	positionUlChildren();
		
	$("a[class^='fancyMe']").fancybox({
		'autoDimensions'	: false,
		'width'				: 450,
		'height'			: 338,
		'titleShow'     	: true,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'overlayOpacity'	: '0.5',
		'opacity'			: 'true'
	});

	$("a[class^='fancyMap']").fancybox({
		'autoScale'			: false,
		'autoDimensions'	: false,
		'width'				: 807,
		'height'			: 1200,
		'titleShow'     	: true,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'overlayOpacity'	: '0.5',
		'opacity'			: 'true'
	});
	
	$("a[href^='uploads']").fancybox();
	
	setInfo();
	
});

/**
 *	Removes the text in the search text box when clicked on it.
 */
function setSearchInputClickHandler(){
	$("#searchInput").click(function(){
		if(searchClicked==false){
			this.value='';
			searchClicked=true;
		}
	});
}

/**
 *	Validates the send email form.
 */
function validateSendEmailForm(){
    $("#sendButton").click(function(){
    
		
		//clear previous messages
		$("#nameError").hide();
		$("#emailError").hide();
		$("#questionError").hide();
		valid=true;  
		
		//verify whether the name text box is empty
		if(document.getElementById("nameTextBox").value=="" || document.getElementById("nameTextBox").value==null){
			$("#nameError").show();
			valid=false;
		}

		//verify whether the question text area is empty
		if(document.getElementById("questionTextArea").value=="" || document.getElementById("questionTextArea").value==null){
			$("#questionError").show();
			valid=false;
		}
		
		//verify whether the inserted email address is valid
		var email = document.getElementById("emailTextBox").value;
		if(!isValidEmailAddress(email)) {
			$("#emailError").show();
			valid=false;
		}
		
		//verify whether the email text box is empty
		if(document.getElementById("emailTextBox").value=="" || document.getElementById("emailTextBox").value==null){
			$("#emailError").show();
			valid=false;
		}
		
		
		
		var name=document.getElementById("nameTextBox").value;
		var email=document.getElementById("emailTextBox").value;
		var achou=document.getElementById("achouTextBox").value;
		var question=document.getElementById("questionTextArea").value;

		//if the inserted data is valid, then sumbit the form
		if(valid==true){
			urlToPhp=document.getElementById("url").value;
			var emailToSend=document.getElementById("emailToSend").value;
			
			var dataString = 'nameTextBox='+ name + '&questionTextArea=' + question + '&emailTextBox=' + email + '&achouTextBox=' + achou;     

			$.ajax({  
				type: "POST",  
				url: urlToPhp,  
				data: dataString,  
				success: function() {  
				$("label#message").css('display','block');
				$("#submitForm").each(function(){
					this.reset();
				});
				}
			}); 
		}
		
		return false;
    });
}

/**
 *	Positions the dropdown children of the menu.
 */
function positionUlChildren(){
	$("#menu ul li").each(function(i){
		var childUl=$(this).find("ul");
		var left=$(this).find("a").offset().left-$("#menu").offset().left;
		childUl.css({left:left});
		
		
		childUl.hover(function(){
			$(this).parent("li").find("a").addClass("selected");
		},function(){
			$(this).parent("li").find("a").removeClass("selected");
		});
	});
	
	$("#menu ul li ul li").each(function(i){
		var childUl=$(this).find("ul");
		var left=$(this).find("a").offset().left-$("#menu").offset().left+327;
		var top=$(this).offset().top;
		
		childUl.css({left:left});
	});
	
}

function setInfo(){
	$("#portfolio div.portfolioItem").hover(function(){
		var info=$(this).find("div.portfolioItemInfo");	
		info.stop().animate({bottom:"4px"}, 500);
	},
	function(){
		var info=$(this).find("div.portfolioItemInfo");
		info.stop().animate({bottom:"-85px"}, 500);
	});
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

