/**********************************************************************************************

  CSS on Sails Framework
  Title: RJMetrics
  Author: XHTMLized (http://www.xhtmlized.com/)
  Date: April 2010

***********************************************************************************************/

$(document).ready(function() {
	
	//font replacement
	Cufon.replace("#banner h2", { fontFamily: "Gotham Book", textShadow: "1px 1px 1px #000" });
	Cufon.replace("#banner h2 strong, ul#slider-menu li a", { fontFamily: 'Gotham Bold', hover: true, textShadow: "1px 1px 1px #000" });
	Cufon.replace("a.big-button, a.plain-green-button, a.read-more, a.grey-button, ul#slider-menu li a, #quad .item h3", { fontFamily: 'Gotham Bold', hover: true });
	
	Cufon.replace('#navigation ul li a, #util-nav ul li a, #showcase ul.tabs li a', { fontFamily: 'Gotham Medium', hover: true });
	
	Cufon.replace("#banner h4", { fontFamily: "Gotham Book" });
	
	Cufon.replace('.gotham', { fontFamily: 'Gotham Medium', textShadow: "1px 1px 1px #000" });
	
	//show/hide for the login popup
	$("#show-login").click(function(){
	
		$("#login-popup").slideToggle();
		$(this).toggleClass("down", "");
		return false;
		
	});
	
	//login popup alternative (on hover)
	// $("#show-login-alt").mouseenter(function(){
	// 
	// 	$("#login-popup").slideToggle();
	// 	$(this).toggleClass("down", "");
	// 	
	// });
	
	$("#show-login-alt").click(function(){
	
		$("#login-popup").slideToggle();
		$(this).toggleClass("down", "");
		return false;
		
	});
	
	// $("#login-popup").mouseleave(function(){
	// 
	// 	$("#login-popup").slideToggle();
	// 	$("#show-login-alt").toggleClass("down", "");
	// 	
	// });
	
	//slider menu
	$("ul#slider-menu li a").click(function(){
		
		var li = $(this).parent().attr('id');
		var screen = "#" + li + "-screen";
		
		$("#slider").scrollTo( $(screen), 500, {offset: {left:-40} });
		
		$("ul#slider-menu li").each(function(){
			$(this).removeClass("active");
		});
		
		$("li#" + li).addClass("active");

		return false;
		
	});
	
	//non-slider menu
	$("ul#showcase-menu li a").click(function(){
		
		var li = $(this).parent().attr('id');
		var screen = "#" + li + "-content";
		
		$("div.showcase-content").hide();
		
		$(screen).show();

		return false;
	});
	
	$("input.text").focus(function(){
		
		field_clearer(this);
		
	});
	
	function oc(a){

		var o = {};

		for(var i=0;i<a.length;i++){
			o[a[i]]='';
		}

		return o;

	}
	
	function field_clearer(clicked){

		var defaults = ['Name', 'Email','Phone Number'];

		if($(clicked).val() in oc(defaults) ){
			$(clicked).val("").removeClass("error");
		}

	}
		
	$("#submit-demo").click(function(){
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		var nameVal = $("#login_name").val();
		var emailVal = $("#login_email").val();
		
		if(nameVal == '') {
			alert('Please enter your name.')
			hasError = true;
			return false;
		}		
		if(emailVal == '') {
			//$("#login_email").after('<span class="error">You forgot to enter the email address to send to</span>');
			alert('Please enter your email address.')
			hasError = true;
			return false;
		} else if(!emailReg.test(emailVal)) {
			//$("#login_email").after('<span class="error">Enter a valid email address to send to.</span>');
			alert('Please enter a valid email address.')
			hasError = true;
			return false;
		}

		if(hasError == false) {
			$(this).replaceWith('<div id="form-status" style="margin-left: 30px; margin-top: 15px"><img src="./_ui/images/ajax-loader.gif" alt="Loading" id="loading" /></div>');
			$.post("./contactus.php",
		   		   { name: nameVal, email: emailVal, message: 'Demo Request', demo: 1 },
		   		   	function(jsonString)
		   		   	{
		   			   var output = eval('('+jsonString+')');	   
		   			   if (output.message == "Success")
		   			   {
		   				   $("#form-status").replaceWith('<div id="form-status"><h3>Your password is on its way.</h3></div>');
		   				   pageTracker._trackPageview('/demo/submitted');
		   			   } else
		   			   {
		   				   $("#form-status").replaceWith('<input type="image" src="./_ui/images/form/create-login.png" id="submit-demo" value="Create demo login" />');
		   			   }
				   
					}
			);
		}
		return false;
	});
	
	$("#submit-signup").click(function(){
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		var nameVal = $("#login_name").val();
		var emailVal = $("#login_email").val();
		var phoneVal = $("#login_phone").val();
		if(nameVal == '') {
			alert('Please enter your name.')
			hasError = true;
			return false;
		}		
		if(emailVal == '') {
			//$("#login_email").after('<span class="error">You forgot to enter the email address to send to</span>');
			alert('Please enter your email address.')
			hasError = true;
			return false;
		} else if(!emailReg.test(emailVal)) {
			//$("#login_email").after('<span class="error">Enter a valid email address to send to.</span>');
			alert('Please enter a valid email address.')
			hasError = true;
			return false;
		}

		if(hasError == false) {
			$(this).replaceWith('<div id="form-status"><img src="./_ui/images/ajax-loader.gif" alt="Loading" id="loading" /></div>');
			$.post("./contactus.php",
		   		   { name: nameVal, email: emailVal, message: 'Contact Me', number: phoneVal },
		   		   	function(jsonString)
		   		   	{
		   			   var output = eval('('+jsonString+')');	   
		   			   if (output.message == "Success")
		   			   {
		   				   $("#form-status").html('<h3>Thank you. We will contact you within 24 hours.</h3>');
		   				   pageTracker._trackPageview('/signup/submitted');
		   			   } else
		   			   {
		   				   $("#form-status").replaceWith('<input type="image" id="submit-signup" src="./_ui/images/form/contact-me.png" value="Contact Me" class="right" />');
		   			   }
				   
					}
			);
		}
		return false;
	});
	
	$("#submit-newsletter").click(function(){
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		var emailVal = $("#newsletter_email").val();

		if(emailVal == '') {
			//$("#login_email").after('<span class="error">You forgot to enter the email address to send to</span>');
			alert('Please enter your email address.')
			hasError = true;
			return false;
		} else if(!emailReg.test(emailVal)) {
			//$("#login_email").after('<span class="error">Enter a valid email address to send to.</span>');
			alert('Please enter a valid email address.')
			hasError = true;
			return false;
		}

		if(hasError == false) {
			$(this).replaceWith('<div id="form-status"><img src="./_ui/images/ajax-loader.gif" alt="Loading" id="loading" /></div>');
			//$("#sendEmail li.buttons").append('<img src="/images/template/loading.gif" alt="Loading" id="loading" />');
			$.post("./newsletter.php",
		   		   { email: emailVal },
		   		   	function(jsonString)
		   		   	{
		   			   var output = eval('('+jsonString+')');	   
		   			   if (output.message == "Success")
		   			   {
		   				   $("#form-status").html('<p>Thank you.<p>');
		   				   pageTracker._trackPageview('/newsletter/submitted');
		   			   } else
		   			   {
		   				   $("#form-status").replaceWith('<input type="image" src="./_ui/images/form/subscribe.gif" id="submit-newsletter" value="Subscribe" />');
		   			   }
				   
					}
			);
		}
		return false;
	});
});