$(function() {
	$('div#loginWrapper').show();
	$('div#login').hide();
	$('a[href*=/login],a[href*=/close]').click(function(){
		$('div#login').slideToggle('slow');
		return false;
	});
	
	var height = $(window).height();
	if(height > 550) {		
		$('a.signup').click(function (e) {
			e.preventDefault();
			$('div#signupForm').modal();
		});		
	}	
	else { 	
		$('a.signup').click(function() {
			return true;							 
		});	
	}	
	
	$('div.bucket').hover(function(){	
		$(this).stop().animate({backgroundColor: "#e2e2e2"}, 500);	
		}, function(){	
			$(this).stop().animate({backgroundColor: "#F4F4F4"}, 500);	});		
	$('div#subNavigation ul li a').hover(function(){	
		$(this).stop().animate({marginLeft: "10px"}, 500);	
		}, function(){	
			$(this).stop().animate({marginLeft: "0"}, 500);	});
	$("#toTop").scrollToTop();	
	$('html').removeClass('fouc');
	
	
	var url = location.pathname;
	var urlArray = url.split("/");
	var section = urlArray[2];
	
	if(section == undefined) {	
		$('a[href=/features]').parent('li').addClass('selected');		
	} else {		
		$('a[href*='+section+']').parent('li').addClass('selected');
	}
	
	$('div#signupForm button.stdButton').click(function() {			
		var fields = new Array(($('input.validateName').val()),($('input.validateEmail').val()),($('input.validateTelno').val()),($('textarea#message').val()));
		var errormsg = "";
		if(fields[0] == "") {			
			var errormsg = "Please enter your name<br />";			
		}	
		if(!isValidEmailAddress(fields[1])) {			
			var errormsg = errormsg + "Your email address appears to be invalid. Please check and try again.<br />";
		}			
		if(fields[2] == "") {			
			var errormsg =  errormsg + "Please enter a phone number so that we can contact you to complete the sign up";	
		}
		if($('textarea#message').val() == "") {
			//set something in this field to pass the PHP checking
			$('#message').html(' ');
		}
		
		if(errormsg != "") {
			$('p.signupError').html(errormsg).hide().fadeIn('slow');
			return false;
		}			
		else { 
			$('p.signupError').html("");
		}
	});	
});

function isValidEmailAddress(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);
}