/**********************************************************************************************

  CSS on Sails Framework
  Title: RJMetrics
  Author: XHTMLized (http://www.xhtmlized.com/)
  Date: April 2010

***********************************************************************************************/

function replaceFonts() {	
	//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});
	
    // font replacement for pricing page
	Cufon.replace("#banner h3.subtext", {fontFamily: "Gotham Book", textShadow: "1px 1px 1px #000"});
	Cufon.replace("#banner h3.cufon", {fontFamily: "Gotham Book", textShadow: "1px 1px 1px #000"});
    Cufon.replace("#banner div.option div.element", {fontFamily: "Gotham Book", textShadow: "0 1px 1px #eee"});
    Cufon.replace("#banner div.option div.element.highlight", {fontFamily: "Gotham Bold"});
    Cufon.replace("#banner div.option div.element a", {fontFamily: "Gotham Book"});
    Cufon.replace("#banner div.option div.element a.button.blue", {fontFamily: "Gotham Bold", textShadow: "1px 1px 1px #777"});
    Cufon.replace("#banner div.option span.title", {fontFamily: "Gotham Medium", textShadow: "0 1px 1px #eee"});
	Cufon.replace("#content h2.cufon", {fontFamily: "Gotham Book", textShadow: "1px 1px 1px #ddd"});
    
    // font replacement for signup page
	Cufon.replace("#content .leftvert", {fontFamily: "Gotham Book", textShadow: "1px 1px 1px #ddd"});
	Cufon.replace("#content .super", {fontFamily: "Gotham Book", textShadow: "0 1px 1px #eee"});
	Cufon.replace("form.plan-signup a.signup", {fontFamily: "Gotham Book", textShadow: "0 1px 1px #eee"});
    
	//startup data addition
	Cufon.replace("#header h2", {fontFamily: "Gotham Book", textShadow: "1px 1px 1px #000000"});
	Cufon.replace("#header h2 strong", {fontFamily: 'Gotham Bold', hover: true, textShadow: "1px 1px 1px #000000"});
	Cufon.replace("#header em", {fontFamily: 'Gotham Book', hover: true, textShadow: "1px 1px 1px #000000"});
	//end startup data addition
	
	Cufon.replace('#navigation ul li a, #util-nav ul li a, #startup-data-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"});
	
}

function validateEmail(email) {
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

	return email.match(re);
}

$(document).ready(function() {

	replaceFonts();

	// grabs a url parameter
	$.urlParam = function(name){
		var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
		if (!results)
		{
			return 0;
		}
		return results[1] || 0;
	}
	
	//show/hide for the login popup
	$("#show-login").click(function(){
	
		$("#login-popup").slideToggle();
		$(this).toggleClass("down", "");
		return false;
		
	});

	$('a.hoverhelp').click(function(e) {e.preventDefault();});

	$("#show-login-alt").click(function(){
	
		$("#login-popup").slideToggle();
		$(this).toggleClass("down", "");
		return false;
		
	});
		
	//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 postData = {
			message: 'Demo Request',
			demo: 1
		};
		
		if($("#login_name").length>0) {
			var nameVal = $("#login_name").val();
			
			if(nameVal == '') {
				alert('Please enter your name.')
				hasError = true;
				return false;
			}	
			
			postData.name = nameVal;
		}
		
		if($("#login_email").length>0) {
			var emailVal = $("#login_email").val();
			
			if(emailVal == '') {
				alert('Please enter your email address.')
				hasError = true;
				return false;
			} else if(!emailReg.test(emailVal)) {
				alert('Please enter a valid email address.')
				hasError = true;
				return false;
			}

			postData.email = emailVal;
		}
		
		if($("#login_phone").length>0) {
			var phoneVal = $("#login_phone").val();
			postData.phone = phoneVal;
			// no phone validation - lets make this optional for now
		}

		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",
					postData,
		   		   	function(output)
		   		   	{
		   			   if (output.success && output.instantUrl)
		   			   {  
		   				   $("#form-status").replaceWith('<div id="form-status"><h3>Click <a target="_blank" href="' + output.instantUrl + '">here</a> to access your demo account instantly. <br/><br/>You will also receive an email with this link for future reference.</h3></div>');
		   				   _gaq.push(['_trackPageview', '/demo/submitted']);
		   			   } else if(output.success && !output.instantUrl) {
   		   				   $("#form-status").replaceWith('<div id="form-status"><h3>This email is already associated with an active account. Please log in <a href="https://dashboard.rjmetrics.com">here</a>.</h3></div>');
					   } else
		   			   {
		   				   $("#form-status").replaceWith('<input type="image" src="./_ui/images/form/create-login.png" id="submit-demo" value="Create demo login" />');
		   			   }
					},
					"json"
			);
		}
		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("./contactus",
		   		   {email: emailVal, message: 'Newsletter', newsletter: true},
		   		   	function(jsonString)
		   		   	{
		   			   var output = eval('('+jsonString+')');	   
		   			   if (output.message == "success")
		   			   {
		   				   $("#form-status").html('<p>Thank you.<p>');
		   				   _gaq.push(['_trackPageview', '/newsletter/submitted']);
		   			   } else
		   			   {
		   				   $("#form-status").replaceWith('<input type="image" src="./_ui/images/form/subscribe.gif" id="submit-newsletter" value="Subscribe" />');
		   			   }
				   
					}
			);
		}
		return false;
	});
});
