/**
 * 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(){

	$('.post_box').each(function(){
	$(this).find('p:last').css({marginBottom:10});
	});
	
		Cufon.replace('h1');
		Cufon.replace('h2');
		Cufon.replace('h3');
		Cufon.replace('h4');
		Cufon.replace('h5');
		Cufon.replace('h6');
	
	
	$('ul#menuUl').superfish();

});

/**
 * 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="send_email.php";
				
				var dataString = 'name='+ name + '&question=' + question + '&email=' + email;  

				$.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
}


$(document).ready(function(){

	var deg=0;

	/* Storing all the images into a variable */

	var images	= $('#stage img').removeClass('default').addClass('animationReady');
	var dim		= { width:images.width(),height:images.height()};
	
	var cnt = images.length;
	
	/* Finding the centers of the animation container: */
	var centerX = $('#stage').width()/2;
	var centerY = $('#stage').height()/2 - dim.height/2;

	function rotate(step,total){
		// This function loops through all the phone images, and rotates them
		// with "step" degrees (10 in this implementation) until total has reached 0
	
		/* Increment the degrees: */
		deg+=step;
		
		var eSin,eCos,newWidth,newHeight,q;
		
		/* Loop through all the images: */
		for(var i=0;i<cnt;i++){
			
			/* Calculate the sine and cosine for the i-th image */
			
			q = ((360/cnt)*i+deg)*Math.PI/180;
			eSin		= Math.sin(q);
			eCos		= Math.cos(q);
			
			/*
			/	With the sine value, we can calculate the vertical movement, and the cosine 
			/	will give us the horizontal movement.
			*/
			
			q = (0.6+eSin*0.4);
			newWidth	= q*dim.width;
			newHeight	= q*dim.height;
			
			/*
			/	We are using the calculated sine value (which is in the range of -1 to 1)
			/	to calculate the opacity and z-index. The front image has a sine value
			/	of 1, while the backmost one has a sine value of -1.
			*/
			
			images.eq(i).css({
				top			: centerY+15*eSin,
				left		: centerX+200*eCos,
				opacity		: 0.8+eSin*0.2,
				marginLeft	: -newWidth/2,
				zIndex		: Math.round(80+eSin*20)
			}).width(newWidth).height(newHeight);
		}

		total-=Math.abs(step);
		if(total<=0) return false;
		
		// Setting the function to be run again in 40 seconds (equals to 25 frames per second):
		setTimeout(function(){rotate(step,total)},40);
	
	}
	
	// Running the animation once at load time (and moving the iPhone into view):
	rotate(10,360/cnt);
	
	$('#phoneCarousel .previous').click(function(){
		// 360/cnt lets us distribute the phones evenly in a circle
		rotate(-10,360/cnt);
	});
	
	$('#phoneCarousel .next').click(function(){
		rotate(10,360/cnt);
	});
});
