//$Id$
/**
 * 

 * 
 * Description	jQuery scripts file.
 * 
 * @license		GNU General Public License
 * @author		James Gordon - Ambition Design
 * @link		http://www.ambitiondesign.co.uk
 * @email		james@ambitiondesign.co.uk
 * 
 * @file		scripts.jquery.js
 * @version		1.3
 * @date		07/22/2011
 * 
 * Copyright (c) 2011
 */
 (function($) { /* some code that uses $ */ 

$(document).ready(function(){
	$("a[rel=external]").attr('target', '_blank');
	
	//comment form hide
	$("#commentform").hide();
	$("#reply-title").html("Would you like to leave a comment?").click(function(){showHideCommentsForm();});
	
	function showHideCommentsForm(){
		if ($("#commentform").is(":hidden")){
			//$("#contactformtext").hide();
			$("#reply-title").html("Leave a comment");
			//pageTracker._trackEvent('Comment Form','Form Open');
			$("#commentform").slideDown(500, function(){
				$("#commentform #author").focus();
			});
			
		}else{
			$("#reply-title").html("Would you like to leave a comment?");
			$("#commentform").slideUp(500);
		}
	}
	
	//contact form hide
	$("#contactform").hide();
	
	$("a[href=#contactform]").click(function(event){
	//	event.preventDefault();
		showHideContactForm();
		$("#contactform").scrollTop(0);
	
	})

	function showHideContactForm(){
		if ($("#contactform").is(":hidden")){
			$("#contactformtext").hide();
			$("#contactform").slideDown(500, function(){
				$("#contactformtext").fadeIn(500);
				//pageTracker._trackEvent('Contact Form','Form Open');
				_gaq.push(['_trackEvent', 'Contact Form', 'Form Open', window.location.pathname]);
			});
			$("#contactform #name").focus();
		}else{
			$("#contactformtext").fadeOut(200, function(){
				$("#contactform").slideUp(500);
			});
		}
	}
	
	$("#contact_form").live('submit', function(){
		//need to validate prior to submit
		if (validate("name","text") && validate("email","email") && validate("comments","text")){
			var sendData = $("#contact_form").serialize();
			//pageTracker._trackEvent('Contact Form','Submit');
			_gaq.push(['_trackEvent', 'Contact Form', 'Form Submit', window.location.pathname]);
			_gaq.push(['_trackPageview', '/thank-you.html']);
			//submit
			$.post("/wp-content/themes/ambitiondesign2011/mail/sendmail.php", sendData, function(data){
				$("#contactform").html(data);
			});
		}
		return false;
	});
		
	function validate(id, type){		var value = $("#contact_form #"+id).val();
		switch(type){
			case "text":
				if (value==null||value==""){
					$("#contact_form #"+id).addClass("required");
    				$("#contact_form #"+id).parent().append("<span class=\"error\">This field is required.</span>");
    				return false;
    			}else{ 
    				 $("#contact_form #"+id).removeClass("required");
    				 $("#contact_form #"+id).parent().find('span').remove();
			  		return true;   
			  	}
			break;
					
			case "email":
				if (value==null||value==""){
					$("#contact_form #"+id).addClass("required");
    				$("#contact_form #"+id).parent().append("<span class=\"error\">This field is required.</span>");
    				return false;
    			}else{
    				apos=value.indexOf("@");
 					dotpos=value.lastIndexOf(".");
  					if (apos<1||dotpos-apos<2){
  						$("#contact_form #"+id).addClass("required");
    					$("#contact_form #"+id).parent().append("<span class=\"error\">A valid email address is required.</span>");
  						return false;
  					}else {
  						$("#contact_form #"+id).removeClass("required");
    					$("#contact_form #"+id).parent().find('span').remove();
  						return true;
  					}
  				}
			break;
		}
	}
	
	//call the functions for the homepage portfolio slider
	$("#work #portfolio").easySlider({
				auto: true, 
				continuous: true,
				numeric: true,
				speed: 1200,
				pause: 10000
			});
			
	$("#work #portfolio ul li").hover( 
		function(){
			$(this).find("a").css("bottom","0px");
		}, function(){
			$(this).find("a").css("bottom","-30px");
	});
	

});

})(jQuery)
