﻿/**
 * 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://yourdomain/sendEmail.php";

var valid=true;

jQuery(function(){
	//jQuery('ul#menuUl').superfish();
	jQuery('ul.sf-menu').superfish();
 	Cufon.replace('h1');
	Cufon.replace('h2');
	Cufon.replace('h3');
	Cufon.replace('caption');
	Cufon.replace('#header #quote li');

	setSearchInputClickHandler();
	validateSendEmailForm();
	

//	positionUlChildren();
	
	jQuery("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'light_rounded' /* light_rounded / dark_rounded / light_square / dark_square */
	});
	
	setInfo();
	
});

//activate the submenu classes//
jQuery(function(){
	jQuery(".submenu ul").parent().addClass("arrow");
});

jQuery(function(){
	jQuery('div.accordion> div').hide();
	jQuery('div.first').show();
	jQuery('div.accordion> h3').click(function() {
		var $nextDiv = jQuery(this).next();
		var $visibleSiblings = $nextDiv.siblings('div:visible');
		if ($visibleSiblings.length ) {
			$visibleSiblings.slideUp('medium', function() {
				$nextDiv.slideToggle('medium');
			});
		}
		else {
			$nextDiv.slideToggle('medium');
		}
	});
});

jQuery(function(){
	jQuery('div.accordion2> div').hide();
	jQuery('div.first').show();
	jQuery('div.accordion2> h3').hover(function() {
		var $nextDiv = jQuery(this).next();
		var $visibleSiblings = $nextDiv.siblings('div:visible');
		if ($visibleSiblings.length ) {
			$visibleSiblings.slideUp('medium', function() {
				$nextDiv.slideToggle('medium');
			});
		}
		else {
			$nextDiv.slideToggle('medium');
		}
	});
});

jQuery(function(){
	jQuery('#quote').innerfade({
		animationtype: 'fade',
		speed:2000,
		timeout: 6000,
		type: 'sequence',
		containerheight: 'auto',
		runningclass: 'innerfade'
	});
});

/**
 *	Removes the text in the search text box when clicked on it.
 */
function setSearchInputClickHandler(){
	jQuery("#searchInput").click(function(){
		if(searchClicked==false){
			this.value='';
			searchClicked=true;
		}
	});
}

/**
 *	Validates the send email form.
 */
function validateSendEmailForm(){
    jQuery("#sendButton").click(function(){
    
		
		//clear previous messages
		jQuery("#nameError").hide();
		jQuery("#emailError").hide();
		jQuery("#questionError").hide();
		valid=true;  
		
		//verify whether the name text box is empty
		if(document.getElementById("nameTextBox").value=="" || document.getElementById("nameTextBox").value==null){
			jQuery("#nameError").show();
			valid=false;
		}

		//verify whether the question text area is empty
		if(document.getElementById("questionTextArea").value=="" || document.getElementById("questionTextArea").value==null){
			jQuery("#questionError").show();
			valid=false;
		}
		
		//verify whether the inserted email address is valid
		var email = document.getElementById("emailTextBox").value;
		if(!isValidEmailAddress(email)) {
			jQuery("#emailError").show();
			valid=false;
		}
		
		//verify whether the email text box is empty
		if(document.getElementById("emailTextBox").value=="" || document.getElementById("emailTextBox").value==null){
			jQuery("#emailError").show();
			valid=false;
		}
		
		
		
		var name=document.getElementById("nameTextBox").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 = 'name='+ name + '&question=' + question + '&email=' + email + '&emailToSend=' + emailToSend;    

			jQuery.ajax({  
				type: "POST",  
				url: urlToPhp,  
				data: dataString,  
				success: function() {  
				jQuery("label#message").show();
				jQuery("label#message").append("<br/><br/>");
				jQuery("#submitForm").each(function(){
					this.reset();
				});
				}
			}); 
		}
    });
}

/**
 *	Positions the dropdown children of the menu.
 */
function positionUlChildren(){
	jQuery("#menu ul li").each(function(i){
		var childUl=jQuery(this).find("ul");
		var left=jQuery(this).find("a").offset().left-jQuery("#menu").offset().left;
		childUl.css({left:left});
		
		
		childUl.hover(function(){
			jQuery(this).parent("li").find("a").addClass("selected");
		},function(){
			jQuery(this).parent("li").find("a").removeClass("selected");
		});
	});
	
	jQuery("#menu ul li ul li").each(function(i){
		var childUl=jQuery(this).find("ul");
		var left=jQuery(this).find("a").offset().left-jQuery("#menu").offset().left+327;
		var top=jQuery(this).offset().top;
		
		childUl.css({left:left});
	});
	
}

function setInfo(){
	jQuery("#portfolio div.portfolioItem").hover(function(){
		var info=jQuery(this).find("div.portfolioItemInfo");	
		info.stop().animate({bottom:"4px"}, 500);
	},
	function(){
		var info=jQuery(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})?)jQuery)|(@\[?((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})\]?jQuery)/i);
	return pattern.test(emailAddress);
}

jQuery(document).ready(function(){

	jQuery(".sidebarBox .page_item ul").not('ul.sitemap ul').hide();
	jQuery(".sidebarBox .current_page_item ul:first").slideDown();
	jQuery(".sidebarBox .current_page_item, .sidebarBox .current_page_ancestor ").parents("ul, li")
		.map(function () { 
			jQuery(this).slideDown();
		});
	jQuery(".current_page_parent").not(":has(ul)").addClass("current_page_item");
	
	jQuery("ul.sitemap ul").css("display","block");
});
