﻿/**
 * This is the main JS file. It initializes some plugins and contains the functionality
 * for the send email form.
 * 
 * Author: Pexeto
 * http://pexeto.com/
 */


/**
 * Initialize the plugins and do the main settings
 */
$(function(){

	//CUFON FONT REPLACEMENT
		Cufon.replace('h1');
		Cufon.replace('h2');
		Cufon.replace('h3');
		Cufon.replace('h4');
		Cufon.replace('h5');
		Cufon.replace('h6');
		Cufon.replace('.big_letter');
	
	//LOAD SUPREFISH DROP-DOWN MENU PLUGIN
	$('#menu ul').superfish();
	
	//ADD THE DROPDOWN POINTER TO THE MENUS
	$('#menu ul li ul').not('#menu ul li ul ul').prepend('<li><div class="dropdown_pointer"></div></li>');
	
	set_submit_comment();
	
	//LOAD PORTFOLIO FUNCTIONALITY
	portfolioSetter.init();
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'dark_rounded' /* light_rounded / dark_rounded / light_square / dark_square */
	});
	
	$('.post_box').each(function(){
		$(this).find('p:last').css({marginBottom:10});
	});
	
	//INIT IMAGE FADER
	perception_fader.init();
	
	//SET THE SEARCH BUTTON CLICK HANDLER
	$('#search_button').click(function(event){
		event.preventDefault();
		$('#searchform').submit();
	});
	
	//remove the right margin of the last column of the four-column layout
	$('.columns_wrapper .four_column').eq(3).css({marginRight:0});

});

/**
 * Contains the functionality of the send email form. Makes the validation and sends the message.
 */
perception_contact_form={
	set:function(){
		this.setSendButtonClickHandler();
		this.setInputClickHandler();
	},
	
	setSendButtonClickHandler:function(){
		$("#send_button").click(function(event){
			
			event.preventDefault();	
			valid=true;  
			$("#name_text_box").removeClass('invalid');
			$("#email_text_box").removeClass('invalid');
			$("#question_text_area").removeClass('invalid');
			$('#invalid_input').hide();
			$('#sent_successful').hide();
			
			//verify whether the name text box is empty
			var name=$("#name_text_box").val();
			if(name=='' || name==null){
				$("#name_text_box").addClass('invalid');
				valid=false;
			}
			
			//verify whether the inserted email address is valid
			var email = $("#email_text_box").val();
			if(!perception_contact_form.isValidEmailAddress(email)) {
				$("#email_text_box").addClass('invalid');
				valid=false;
			}
			
			//verify whether the question text area is empty
			var question=$("#question_text_area").val();
			if(question=='' || question==null){
				$("#question_text_area").addClass('invalid');
				valid=false;
			}
			
			if(!valid){
				$('#invalid_input').show();
			}else{
				//the data is valid, sumbit the form
				urlToPhp=$("#url").val();
				emailToSend=$("#email_to_send").val();
				
				var dataString = 'name='+ name + '&question=' + question + '&email=' + email + '&emailToSend=' + emailToSend;  

				$.ajax({  
					type: "POST",  
					url: urlToPhp,  
					data: dataString,  
					success: function() {
					$('#sent_successful').show();
					$("#submit_form").each(function(){
						this.reset();
					});
					}
				}); 
			}
	    });
	},
	
	setInputClickHandler:function(){
		$('.form_input').click(function(){
			$(this).removeClass('invalid');
		});
	},
	
	/**
	 * Checks if an email address is a valid one.
	 * 
	 * @param emailAddress
	 *            the email address to validate
	 * @return true if the address is a valid one
	 */
	isValidEmailAddress:function(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);
	}
};



function set_piecemaker_slider(){
	AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
			'width', '100%',
			'height', '100%',
			'src', 'piecemaker',
			'quality', 'high',
			'pluginspage', 'http://www.adobe.com/go/getflashplayer_de',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'noscale',
			'wmode', 'transparent',
			'devicefont', 'false',
			'id', 'piecemaker',
			'bgcolor', 'none',
			'name', 'piecemaker',
			'menu', 'true',
			'allowFullScreen', 'false',
			'allowScriptAccess','sameDomain',
			'movie', 'piecemaker',
			'salign', ''
			); //end AC code
}

function set_submit_comment(){
	$('#submit_comment_button').click(function(event){
		event.preventDefault();
		$('#commentform').submit();
	});
}

function init_slider(){
	if (!($.browser.msie && $.browser.version.substr(0,1)<7)){
		window.onload=function(){
			$('.loading').hide();
			$('#slider_container').slider();
		};
	}else{
		 $(document).ready(function() {
	    	 $('.loading').hide();
	  		$('#slider_container').slider();
	      });    
	}
}


