//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Global JS
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//Document Ready +++++
$(function() {			
	//Detect IE and Redirect
	if($.browser.msie && location.href.indexOf("ie.html") == "-1"){
		window.location="/ui/errors/ie.html";
	}  
	//Click Action
	$("li#nav-portfolio a, li#nav-about a, li#nav-contact a,a.button-proposal,input#submit,li#nav-article a, li#nav-comments a, li#nav-respond a,a.article-list").mousedown(function(){ $(this).addClass("pressed"); });
		$("li#nav-portfolio a, li#nav-about a, li#nav-contact a,a.button-proposal,input#submit,li#nav-article a, li#nav-comments a, li#nav-respond a,a.article-list").mouseout(function(){ $(this).removeClass("pressed"); }).mouseup(function(){  $(this).removeClass("pressed"); })
	
	//Click Action for the Buttons & iPhone Icons
	$("a.send,a.cancel,a.dismiss,a.dismiss-gtalk").mousedown(function() {
		$(this).css({ backgroundPosition: 'right top'})
		}).mouseout(function() { $(this).css({ backgroundPosition: 'left top'})
		}).mouseup(function() { $(this).css({ backgroundPosition: 'left top'})
	});
	
	//Scroll To Animation
	//Main Nav - Initialization
	$("li#nav-portfolio a").addClass("enabled");
	$("li#nav-about a").addClass("enabled");
	$("li#nav-contact a").addClass("enabled");
	
	//Portfolio Nav - Only allow them to click portfolio it if it's enabled
	if($("li#nav-portfolio a").hasClass("enabled")){
		$("li#nav-portfolio a").click(function(e){
			e.preventDefault();
			//Turn off the button
			$(this).removeClass("enabled");
			//Scroll
			$.scrollTo("#portfolio", 1000, {easing:"easeOutExpo",offset:-50, onAfter:function(){ 
				$(this).addClass("enabled");
			}} );
		});
	}
	//About Nav - Only allow them to click about it if it's enabled
	if($("li#nav-about a").hasClass("enabled")){
		$("li#nav-about a").click(function(e){
			e.preventDefault();
			//Turn off the button
			$(this).removeClass("enabled");
			//Scroll
			$.scrollTo("#about", 1000, {easing:"easeOutExpo",offset:-50, onAfter:function(){ 
				$(this).addClass("enabled");
			}} );
		});
	}
	//Contact Nav - Only allow them to click contact it if it's enabled
	if($("li#nav-contact a").hasClass("enabled")){
		$("li#nav-contact a").click(function(e){
			e.preventDefault();
			//Turn off the button
			$(this).removeClass("enabled");
			//Scroll
			$.scrollTo("#contact", 1000, {easing:"easeOutExpo",offset:-30, onAfter:function(){ 
				$(this).addClass("enabled");
			}} );
		});
	}
	//Articles Nav - Initialization
	$("li#nav-article a").addClass("enabled");
	$("li#nav-comments a").addClass("enabled");
	$("li#nav-respond a").addClass("enabled");
	
	//article Nav - Only allow them to click article it if it's enabled
	if($("li#nav-article a").hasClass("enabled")){
		$("li#nav-article a").click(function(e){
			e.preventDefault();
			//Turn off the button
			$(this).removeClass("enabled");
			//Scroll
			$.scrollTo("#page", 1000, {easing:"easeOutExpo",offset:-50, onAfter:function(){ 
				$(this).addClass("enabled");
			}} );
		});
	}
	//comments Nav - Only allow them to click comments it if it's enabled
	if($("li#nav-comments a").hasClass("enabled")){
		$("li#nav-comments a").click(function(e){
			e.preventDefault();
			//Turn off the button
			$(this).removeClass("enabled");
			//Scroll
			$.scrollTo("#comments", 1000, {easing:"easeOutExpo",offset:-50, onAfter:function(){ 
				$(this).addClass("enabled");
			}} );
		});
	}
	//respond Nav - Only allow them to click respond it if it's enabled
	if($("li#nav-respond a").hasClass("enabled")){
		$("li#nav-respond a").click(function(e){
			e.preventDefault();
			//Turn off the button
			$(this).removeClass("enabled");
			//Scroll
			$.scrollTo("#respond", 1000, {easing:"easeOutExpo",offset:-30, onAfter:function(){ 
				$(this).addClass("enabled");
			}} );
		});
	}
	
	//Click Action for Various Buttons (using jQuery Live so the -/+ buttons work)
	$(".jcarousel-prev,.jcarousel-next").live("mousedown", function(){ $(this).addClass("pressed"); });
		$(".jcarousel-prev,.jcarousel-next").live("mouseout", function(){ $(this).removeClass("pressed"); }).live("mouseup", function() { $(this).removeClass("pressed"); });	
	
	//Initialize jCarousels
	$(".carousel").jcarousel({ scroll:1,visible:1,wrap:"both",easing:"easeInOutQuint" });
	//Remove the right margin on every 4th portfolio card
	$("#portfolio .container div:nth-child(4n)").addClass("last");
	//Portfolio Cards Rollover Stuff
	$("#portfolio li").mouseenter(function(){
		$(this).children("p").slideToggle("fast");
		$(this).children("em").fadeIn("fast");
	});
	$("#portfolio li").mouseleave(function(){
		$(this).children("p").slideToggle("fast");
		$(this).children("em").fadeOut("fast");
	});
	//Portfolio App Rollover Stuff
	$("div.app").mouseenter(function(){
		$(this).children("em").fadeIn("fast");
	});
	$("div.app").mouseleave(function(){
		$(this).children("em").fadeOut("fast");
	});
	//Radio Button Action
	$("#contact ul li a").click(function(){
		$(this).parent().siblings().children().removeClass("selected");
		$(this).addClass("selected");
	});
	//Initialize Shadowbox
	Shadowbox.init({ displayCounter:false, continuous:true });

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Contact Form JS
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	
	//iPhone Icon Animation
	$("#iphone-home").fadeIn(1000); 
	$(".twitter, .facebook,.gtalk, .linkedin,.labrums, .nt,.lastfm, .delicious, .lala").animate({left:0, top:0}, 600);
	$("#iphone-dock").fadeIn(600); 
	
	//Launch Email App
	$(".email").click(function(){
		$("#iphone-email").fadeIn("fast");
		$("#name").focus();
	});
	//GTalk Message Popup
	$(".gtalk").click(function(){
		$("#iphone-home-gtalk").fadeIn("fast");
	});	
	//Dismiss the success Message
	$(".dismiss").click(function(){
		$(this).parent().fadeOut("fast");
		$("#iphone-email").fadeOut("fast");
	});
	//Dismiss the GTalk Message
	$(".dismiss-gtalk").click(function(){
		$(this).parent().fadeOut("fast");
	});
	//Exit and Clear Email App with Cancel Button
	$(".cancel").click(function(){
		//Reset the form fields
		$("#name").val("");
		$("#email").val("");
		$("#message").val("");	
		//Close the app
		$("#iphone-email").fadeOut("fast");
	});
		
//Contact Form +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//Contact Form Send Button Enable/Disabled
	$("#name,#email,#message").keyup(function(){
		if ($("#name").val() != "" && $("#email").val() != "" && validEmail($("#email").val()) && $("#message").val() != ""){
			$(".send").show();
		} else {
			$(".send").hide();
		}
	});
	
	//When they click send...
	$(".send").click(function(e) {
		e.preventDefault();
		var name = $("#name").val();
		var email = $("#email").val();
		var message = $("#message").val();		
			    	
		//Validate the Form
		if(name.length > 0 && name != "NAME" && email.length > 0 && email !="EMAIL" && validEmail(email) && message.length > 0 && message != "MESSAGE"){ 
			
			//Show the sending image and text
			$("#sending-text").show();
			$("#sending").show();
			
			//Send the message to the PHP script
			$.ajax({
				type: "POST",
			    url: "../ui/scripts/contact.php",
			    data: "name="+escape(name)+"&email="+escape(email)+"&message="+escape(message),
			    success: function(msg){
			    	//Show success graphic after a delay			    	
			    	setTimeout("finish()",2000);		   
			    }
			 });
		} 		
	}); //End Contact Form
	
//Proposal Form +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	$(".button-proposal").click(function(e) {
		e.preventDefault();
		var name = $("#thename").val();
		var email = $("#theemail").val();
		var company = $("#thecompany").val();
		var website = $("#thewebsite").val();
		var scope = $("#thescope .selected").text();	
		var need = $("#theneed .selected").text();	
		var timeline = $("#thetimeline .selected").text();	
		var experience = $("#theexperience .selected").text();		
		var description = $("#thedescription").val();
		 
		 
		//Clear success from a prior form submission
		$(".success").fadeOut();
			    	
		//Validate the Form
		if(name.length > 0 && email.length > 0 && validEmail(email) && scope.length > 0 && need.length > 0 && timeline.length > 0 && experience.length > 0 && description.length > 0){ 

			//Clear Any Errors
			$(".error-thename").fadeOut("fast"); //Name
			$(".error-theemail").fadeOut("fast"); //Email
			$(".error-thescope").fadeOut("fast"); //Scope
			$(".error-theneed").fadeOut("fast"); //Need
			$(".error-thetimeline").fadeOut("fast"); //Timeline
			$(".error-theexperience").fadeOut("fast"); //Experience
			$(".error-thedescription").fadeOut("fast"); //Description
			
			//Show the loading spinner
			$(".sending").show();
			
			//Send the message to the PHP script
			$.ajax({
				type: "POST",
			    url: "../ui/scripts/proposal.php",
			    data: "name="+escape(name)+"&email="+escape(email)+"&company="+escape(company)+"&website="+escape(website)+"&scope="+encodeURIComponent(scope)+"&need="+encodeURIComponent(need)+"&timeline="+escape(timeline)+"&experience="+escape(experience)+"&description="+escape(description),
			    success: function(msg){
			    	//Hide the spinner
			    	$(".sending").hide();
			    	//Show success graphic
			    	$(".success").fadeIn();
			    	//Reset the form fields
			    	$("#thename").val("");
					$("#theemail").val("");
					$("#thecompany").val("");
					$("#thewebsite").val("");
					$("#thescope").children().children().removeClass("selected");	
					$("#theneed").children().children().removeClass("selected");	
					$("#thetimeline").children().children().removeClass("selected");		
					$("#theexperience").children().children().removeClass("selected");		
					$("#thedescription").val("");	
			    }
			 });
		
		} else { 
		//Show or Hide the Necessary Errors		
			if(name.length <= 0){ $(".error-thename").fadeIn("fast"); } else{ $(".error-thename").fadeOut("fast"); } //Name		
			checkEmail = validEmail(email); 	if(email.length <= 0 || checkEmail == false){ $(".error-theemail").fadeIn("fast"); } else{ $(".error-theemail").fadeOut("fast"); } //Email
			if(scope.length <= 0){ $(".error-thescope").fadeIn("fast"); } else{ $(".error-thescope").fadeOut("fast"); }	//Scope
			if(need.length <= 0){ $(".error-theneed").fadeIn("fast"); } else{ $(".error-theneed").fadeOut("fast"); }	 //Need
			if(timeline.length <= 0){ $(".error-thetimeline").fadeIn("fast"); } else{ $(".error-thetimeline").fadeOut("fast"); }	//Timeline
			if(experience.length <= 0){ $(".error-theexperience").fadeIn("fast"); } else{ $(".error-theexperience").fadeOut("fast"); } 	//Experience
			if(description.length <= 0){ $(".error-thedescription").fadeIn("fast"); } else{ $(".error-thedescription").fadeOut("fast"); }	 //Description
		}
	}); //End Proposal Form

}); //End Document Ready

//Uses a regular expression to verify that the email address is real (or at least has the appearance of a real address)
function validEmail(theEmail){
	var goodEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	return goodEmail.test(theEmail);
}
//Finalize iPhone Email
function finish(){
	//Hide the sending stuff
	$("#sending-text").hide();
	$("#sending").hide();	
	//Show the success div
	$("#iphone-email-success").fadeIn();
	//Reset the form fields
	$("#name").val("");
	$("#email").val("");
	$("#message").val("");	
} 	
