

$(document).ready(function(){
	$('#txtSearch').autofill({
		//value: 'Keyword(s)'
		value: 'Name, Sport, School'
	});
	
	$('#userName').autofill({
		value: 'Enter Email'		
	});
	$('#password').autofill({
		value: 'Password'
	});
	
	$('#L_userName').autofill({
		value: 'Enter Email'		
	});
	$('#L_password').autofill({
		value: 'Password'
	});
	
	$('#userName').focus( function() {	
		clearTimeout($(this).data('timeout'));		
		$("#loginExp").slideDown("normal", function() { $("#loginExp").show(); });	
		
	});
	
	$('#login').click( function() {						
		var t = setTimeout(	function() { doSlideUp() }, 9500);
		$("#loginExp:hidden").slideDown("normal", function() { $("#loginExp:hidden").show(); });	
		
	});

	
	$('#userName').change( function() {		
		$("#loginExp").show();
	});
	
	
									 
	$("#userName").blur(function() { 						 
		var t = setTimeout(	function() { doSlideUp() }, 2500);
		$(this).data("timeout", t)									 
	} );	
	
	
	$("#login input").focus(function() {
		$(this).data('focused', true);			  
	});
	$("#login input").blur(function() { 
		$(this).data('focused', false);						 
									 
	} );	
	
	$("#password").live("keydown",function(event) {
        if (event.which === $.ui.keyCode.ENTER) {			
            $("#MainLoginForm").submit()
        }
    });
	
	
	$(".popup .close").click(function() {
		$(".popup").fadeOut("normal");			
	});
	
	
	$("#B_forgot").click(function() {
		if(!$("#loginShareBar #forgotpw").length) {						  
			$("#forgotpw").appendTo("#loginShareBar");
		}
		$('#forgotpw').css("top", "35px");
	   	$('#forgotpw').css("left", "6px");
		showForgotWin();		
		return false;
	 });
	
	
	
	$("#B_forgotIn").click(function() {
		$('#forgotpw').css("top", "300px");
		$('#forgotpw').css("left", "-90px");
		showForgotWin();		
		return false;								
	});
	

	
	$("#B_forgotPswd_send").click(function() {
		var email = $("#forgotPswdEmail").val();
		var emailConfirm = $("#forgotPswdConfirm").val();
		
		 $.ajax({
			type: "POST",
			url: "/forgotPswd.do",
			data: { "email": email, "emailConfirm": emailConfirm },
			dataType: "xml",
			success: function(xml) {
				var responseStatus = $(xml).find("response").attr("status");
				if (responseStatus != 0) {
					var errMsg = ''
					if (responseStatus == 2 || responseStatus == 5)
						errMsg = ":<br />" + $(xml).find("response").attr("message");
					$("#forgotPswd_response").html("<strong style='color:#cc0000'>Password Check Failed" + errMsg + "</strong>");	
					$("#forgotPswd_response").show();
				}
				else {
					$("#forgotPswd_fields").hide();
					$("#forgotPswd_response").show();
					$("#forgotPswd_response").html("<strong>Password Sent Successfully</strong><br /><br />");
				}
			},
			error: function() {
				$("#forgotPswd_fields").hide();
				$("#forgotPswd_response").show();
				$("#forgotPswd_response").html("<strong style='color:#cc0000'>Password Check Failed</strong><br /><br />");	
			}
		});
	});
	
	/* BEGIN ATE UPDATE */
	$(".B_sendATE").click(function() {
		
		var email = $("#ateEmail").val();
		var question = $("#ateQuest").val();
		
		 $.ajax({
			type: "POST",
			url: "/Ask.do",
			data: { "askEmail": email, "askQuestion": question },
			dataType: "xml",
			success: function(xml) {
				var responseStatus = $(xml).find("response").attr("status");
				if (responseStatus != 0) {
					var errMsg = ''
					//if (responseStatus == 2 || responseStatus == 5)
					errMsg = $(xml).find("response").attr("message");
					$("#ATE_response").html("<strong style='color:#cc0000'>" + errMsg + "</strong>");	
					$("#ATE_response").show();
				}
				else {
					$("#ATE_response").show();
					$("#ATE_response").html("<strong>Thank you for your question.</strong><br /><br />");
					$("#ateEmail").val("");
					$("#ateQuest").val("");
				}
			},
			error: function() {
				$("#ATE_response").show();
				$("#ATE_response").html("<strong style='color:#cc0000'>Sorry, there was a problem accepting your submission.</strong><br /><br />");	
			}
		});
	});
	/* END ATE UPDATE */
	
	
	$("#B_tellAFriend").click(function() {
		$(".popup").hide();
		$("#tellFnd_fields:hidden").show();
		$("#tellFnd_response").hide();
		$("#tellFnd_fields").find("input").val("");
		$("#tellPopup").fadeIn();
		$("#B_tellFnd_send").data("clicked",false);	
		return false;
	});
	
	$("#B_tellFnd_send").click(function() {

		var myName = $("#tellFndMyName").val();
		var friendName = $("#tellFndFName").val();
		var friendEmail = $("#tellFndFEmail").val();
		
		 $.ajax({
			type: "POST",
			url: "/taf.do",
			data: { "fromName": myName, 
	                        "toName": friendName, 
	                        "toEmail": friendEmail },
			dataType: "xml",
			success: function(xml) {
				var responseStatus = $(xml).find("response").attr("status");
				if (responseStatus != 0) {
					var errMsg = ''
					if (responseStatus == 2 || responseStatus == 5)
						errMsg = "<br />" + $(xml).find("response").attr("message");
					$("#tellFnd_response").html("<strong style='color:#cc0000'>" + errMsg + "</strong>");	
					$("#tellFnd_response").show();
				}
				else {
					//pageTracker._trackPageview("/Tell a friend sent/");
					$("#tellFnd_fields").hide();
					$("#tellFnd_response").show();
					$("#tellFnd_response").html("<strong>Tell a Friend Sent Successfully</strong><br /><br />");
					$("#B_tellFnd_send").data("clicked",false);	
				}
			},
			error: function() {
				$("#tellFnd_fields").hide();
				$("#tellFnd_response").show();
				$("#tellFnd_response").html("<strong style='color:#cc0000'>Tell a Friend Failed</strong><br /><br />");	
			}
		});
 	});
	
	$("#B_abuse").click(function() {
		$(".popup").hide();
		$("#RF_message").val("");
		$("#RF_fields").find("input[name!=RF_subject]").val("");
		$("#RF_fields").find("textarea").val("");
		$("#RF_fields").show();
		$("#RF_response").hide();
		$('#abuseForm').show("slide", { direction: "down" }, 1000);
		return false;
	});
	
	$("#B_abuse_send").click(function() {
			var fromName = encodeURIComponent($("#RF_name").val());
			var fromEmail = $("#RF_email").val();
			var subject = encodeURIComponent($("#RF_subject").val());
			var abuseDescription = encodeURIComponent($("#RF_message").val());
		
		 $.ajax({
			type: "POST",
			url: "/reportAbuse.do",
			data: { "fromName": fromName, "fromEmail": fromEmail, "subject" : subject, "abuseDescription": abuseDescription },
			dataType: "xml",
			success: function(xml) {
				var responseStatus = $(xml).find("response").attr("status");
				if (responseStatus != 0) {
					var errMsg = ''
					if (responseStatus == 2)
						errMsg = ": " + $(xml).find("response").attr("message");
					$("#RF_response").html("<strong style='color:#cc0000'>Message Sending Failed" + errMsg + "</strong>");	
					$("#RF_response").show();
				}
				else {
					$("#RF_fields").hide();
					$("#RF_response").show();
					$("#RF_response").html("<strong>Message Sent Successfully</strong><br /><br />");
				}
			},
			error: function() {
				$("#RF_fields").hide();
				$("#RF_response").show();
				$("#RF_response").find("span").html("<strong style='color:#cc0000'>Message Sending Failed</strong><br /><br />");	
			}
		});
	});
	
	
	$(".share").click(function() {
		pageTracker._trackPageview('/share button clicked/');						   
	});
	
	$('.zip').autofill({
		value: 'zip'
	});
	
});


function showZoom(elem)
{
	elem.show('scale', {percent: 100}, 200, null);
}


function hideZoom(elem)
{
	//jquery is really messing up here, so, sorry, this was my solution	
	var in_html = elem.html();	
	elem.hide('scale', {percent: 0}, 200, function(){elem.html(in_html);});
}
	


function doSlideUp() {
	
	if( !$('#login input').data("focused") && !$('#password').data("focused") && !$('#lbgo').data("focused")) {
		$("#loginExp").slideUp()
	} else
		setTimeout(	function() { doSlideUp() }, 3500);
}

function showForgotWin() {
	$(".popup").hide();		
	$('#loginExp').fadeOut();
	$("#forgotPswd_fields").find("input").val("");
	$("#forgotPswd_fields").show();
	$("#forgotPswd_response").hide();
	$('#forgotpw').slideDown("slow");	
}


