$(document).ready(function(){
	$('#emailpage').toggle(function(){
		resetEmailForm();
		$('#wg_emailToFriend').fadeIn();
	},function(){
		$('#wg_emailToFriend').fadeOut();
	});
	$('#emailClose a').click(function(event){
		event.preventDefault();
		$('#emailpage').click();
	});
	var alert = [];

	$('#wg_emailToFriendForm textarea').focus(function(){
		if( !$(this).val().length || $(this).val().length == 0 ){
			var emailText = emailTemplate($('#wg_emailToName').val(),$('#wg_emailFromName').val());
			$(this).css({'height':'180px','width':'400px'}).val(emailText);	
			$('#wg_emailToFriend').width('410px');
		}
	});
	
	$('#wg_emailToFriendForm').submit(function(){
		alert = [];
		loading();
		if($('#wg_emailToName').val().length === 0){ alert.push("Your Friend's Name is missing");}
		if($('#wg_emailToEmail').val().length === 0){ alert.push("Your Friend's Email is missing");}
		if($('#wg_emailFromName').val().length === 0){ alert.push("Your Name is missing");		}

		if (alert.length > 0){
			var alertStr = '';
			for( var i= 0; i<alert.length; i++){
					alertStr += '<dd>'+alert[i]+'</dd>';
			}
			$('#wg_emailToFriendError').html('<dl>'+alertStr+'</dl>').show();
			return false;
		}
		$('#wg_link').val(window.location.href);
		
		var postVar = ''; var first  = true;
		$(this).find('input, textarea').each(function(){if ($(this).attr('type') === 'submit')return;	postVar += (first === true ? '' : '&')   + $(this).attr('name')+'='+$(this).val();first = false;});

		$.ajax({ 
		  type: "POST", 
		  url: $(this).attr('action'), 
		  data: postVar , 
		  success: postResult
		});
		return false;
	});
		
		
	function loading(){
		$('#wg_emailToFriendForm').find('input[type=submit]').hide();
		$('#wg_emailToFriendError').html('<img src="/img/loading.gif">').show();
		return null;
	}
	
	function stopLoading(){
		$('#wg_emailToFriendError').html('');
		$('#wg_emailToFriendForm').find('input[type=submit]').show();
		return null;
	}
	
	function postResult(msg){
		stopLoading();
		if (msg.indexOf('Email Error') == '-1') {
			postResDisplay('Success!<br><br>'+msg);
			hideForm();
		}
		else postResDisplay('<span id="postError">Oops!<br><br>'+msg+'</span>');
		return null;
	}	
	
	function resetEmailForm(){
		$('#wg_emailToFriend').find('input, textarea').show().not('input[type=submit]').val('');
		$('#wg_emailToFriend textarea').css({'height':'','width':''}); 
		$('#wg_emailToFriend').css({'width':''});
		$('#wg_emailToFriend label').show();
		$('#wg_emailToFriendError').html('');
		return null;
	}
	
	function hideForm(){
		$('#wg_emailToFriend').find('input, textarea, label').hide();
	}
	
	function postResDisplay(result){
		stopLoading();
		$('#wg_emailToFriendError').html(result).show();
		return null;
	}
	
	function emailTemplate(friend,me){
		return "Hi " +friend+",\n\n"+
		"I found this great site for planning trips to and via Singapore. I thought you would find it useful.\n\n" +
		"Check it out at:\n" +
		window.location.href+"\n\n" +
		"regards,\n" +
		me;
	}
});
