$(function() {
	// legal popup
	$('#ftrLegal a[rel="legal"]').click(function(e) {
		e.preventDefault();
		
		var height	= 500;
		var width	= 650;
		window.open(this.href, 'legal_popup', 'height='+height+', left='+(screen.width-width)/2+', location=no, resizable=no, scrollbars=yes, status=no, toolbar=no, top='+(screen.height-height)/2+', width='+width);
	});
	
	// current url
	$('#currentURL').attr('value',window.location.pathname);
	
	// side nav collapsing / expanding
	$('#leftNav ul li.closed.parent > a').click(function(e) {
		if($(this).next('ul').is(':visible')) {
			$(this).next('ul').hide();	
		} else if($(this).next('ul').is(':hidden')) {
			$(this).next('ul').show();
		}
		
		e.preventDefault();
	});
	
	// #mid image alignment class
	$('#mid img[align=right]').addClass('img-right');
	
	// share dropdown
	$('#showShare').click(function() {
		$('#friendFormContainer').show();
	});
	
	$('#friendFormContainer .cancel').click(function() {
		$('#friendFormContainer').hide();
	});
	
	
	
	// share form validation
	$('#friendForm .submit').click(function(e) {
		if($('#friendForm #yourName').val() == "") {
			alert('Please enter your name');
			
			$('#friendForm #yourName').focus();
		} else if($('#friendForm #senderEmail').val() == "") {
			alert('Please enter your email address');
			
			$('#friendForm #senderEmail').focus();
		} else if(!isValidEmail($('#friendForm #senderEmail').val())) {
			alert('Please enter a valid email address');
			
			$('#friendForm #senderEmail').focus();
		} else if($('#friendForm #friendName').val() == "") {
			alert('Please enter your friends name');
			
			$('#friendForm #friendName').focus();
		} else if($('#friendForm #friendEmail').val() == "") {
			alert('Please enter your friends email address');
			
			$('#friendForm #friendEmail').focus();
		} else if(!isValidEmail($('#friendForm #friendEmail').val())) {
			alert('Please enter a valid email address');
			
			$('#friendForm #friendEmail').focus();
		} else {
			$('#friendForm').submit();
		}
	});
	
	function isValidEmail(email){
		var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		return filter.test(email);
	}
});
