var $polaImg;
var mainSSInterval;
var mainSSIntervalDelay = 8000;
var currentMainSSImgIndex = 1;
var emailsNeeded, emailsSent;
var mainSSNumImgs = 1;

function changeMainSSSlide() {
	/* Sets the boxes to filled / unfilled based on which image you're seeing. */
	$("div.mainSSButtons a").removeClass("current");
	$("div.mainSSButtons a[rel=" + currentMainSSImgIndex + "]").addClass("current");

	var offset = (currentMainSSImgIndex - 1) * 830;
	$("#mainSSImages").animate({ marginLeft: "-" + offset + "px" }, 1200 );
}

function mainSSNext() {
	currentMainSSImgIndex++;
	if (currentMainSSImgIndex == (mainSSNumImgs + 1))
		currentMainSSImgIndex = 1;
	changeMainSSSlide();
}

function polaSSChange(imgNum) {
	$("#polaImg").attr("src", "./images/home/pola" + imgNum + ".jpg");
	$("#debug-display").html("DIRECT: " + imgNum + " : " + "./images/home/pola" + imgNum + ".jpg");
}

$(function() {
	$polaImg = $("#polaImg");
	mainSSInterval = setInterval("mainSSNext()", mainSSIntervalDelay);
	mainSSNumImgs = $("#mainSSImages img").length;
	
	/* The boxes under the home page images.  Click to change the image. */
	$("div.mainSSButtons a").click(function(event) {
		event.preventDefault();
		clearInterval(mainSSInterval);
		currentMainSSImgIndex--;
		currentMainSSImgIndex = $(this).attr("rel");
		changeMainSSSlide();
	});
	$("#mainSSPrev").click(function(event) {
		event.preventDefault();
		clearInterval(mainSSInterval);
		currentMainSSImgIndex--;
		if (currentMainSSImgIndex == 0)
			currentMainSSImgIndex = mainSSNumImgs;
		changeMainSSSlide();
	});
	$("#mainSSNext").click(function(event) {
		event.preventDefault();
		clearInterval(mainSSInterval);
		mainSSNext();
	});
	
	
	// SLIDESHOW (bottom left)
	$("#polaSSPrev").click(function(event) {
		event.preventDefault();
		var index = $polaImg.attr("src").indexOf("pola");
		var curImgNum = $polaImg.attr("src").substring(index + 4, index + 6);
		curImgNum--;
		if (curImgNum == 0)
			curImgNum = 13;
		$polaImg.attr("src", "./images/home/pola" + curImgNum + ".jpg");
		$("#debug-display").html("PREV: " + curImgNum + " : " + "./images/home/pola" + curImgNum + ".jpg");
	});
	$("#polaSSNext").click(function(event) {
		event.preventDefault();
		var index = $polaImg.attr("src").indexOf("pola");
		var curImgNum = $polaImg.attr("src").substring(index + 4, index + 6);
		curImgNum++;
		if (curImgNum == 14)
			curImgNum = 1;   
		$polaImg.attr("src", "./images/home/pola" + curImgNum + ".jpg");
		$("#debug-display").html("NEXT: " + curImgNum + " : " + "./images/home/pola" + curImgNum + ".jpg");
	});
	
	// SUBMIT EMAIL
	$("#submitEmailBtn").click(function(event) {
		event.preventDefault();
		
		var email = $("#emailTxt").val().trim();
		if (email == "" || !_isValidEmail(email))
		{
			$("#emailDivMsg").html("Invalid Email!");
			// alert causes click to fire twice in firefox when enter key is pressed.. very odd.
			/*alert('You\'ve entered an invalid email address. Please re-enter your email address.');*/
			return;
		}
		
		$.post(
			"../scripts/php/form_handler.php",
			{ action:"mailing_list", email:$("#emailTxt").val(), keyid:"phuCuvexazezuyUfRaX5w8am" }, 
			function(data) {
				// if the script returns an error, display it
				if (data)
					alert("There was an error adding your email to our systems. We apologize for this inconvenience! data:" + data);
				else {
					//_openPopUp("./coupon.php", "coupon", 617, 540);
					//window.location = "./coupon.php";
					showOverlay();
					$("#couponContents").load("./coupon.php");
					$("#couponDiv").css("display", "block");
					$("#emailDivMsg").html("");
				}
			}
		);
	});
	$("#closeCouponLnk").click(function(event) {
		event.preventDefault();
		$("#overlay").fadeOut();
		$("#couponDiv").fadeOut();
	});
	
	// TELL A FRIEND
	$("#tellAFriendLnk").click(function(event) {
		event.preventDefault();
		$("#tafFormDiv").css("display", "block");
		$("#tafThanksDiv").css("display", "none");
		
		// position the div and fade it in
		var windowHeight = _getWindowHeight();
		var topOffset = (windowHeight - 500) / 2;
		$("#tafDiv").css("top", topOffset);
		$("#tafDiv").fadeIn("normal");
		
		showOverlay();
	});
	$("#closeTAFLnk").click(function(event) {
		event.preventDefault();
		$("#overlay").fadeOut();
		$("#tafDiv").fadeOut();
	});	
	
	$("#tafForm").validate({
		submitHandler: function(form) {
			$("#submitMsg").html("Submiting...");
		
			/*
			// sign user up for mailing list?
			$.post(
				"/scripts/php/form_handler.php", 
				{ action:"mailing_list", email:$("#sender_email").val(), keyid:"xxx" }
			);*/
			
			var senderName = $("#sender_first_name").val();
			var senderEmail = $("#sender_email").val();
			emailsNeeded = 1;
			emailsSent = 0;
	
			// send email to friend
			sendEmailtoFriend(senderName, senderEmail, "recipient_name", "recipient_emails");
		}
	});
	
	// generic ajax error handler
	$("#stayTuned").ajaxError(function(event, request, settings){
		alert("There was an unexpected error. We apologize for this inconvenience!");
	});	
	$("#emailTxt").keydown(function(event) {
		if (event.keyCode == 13) {
			event.preventDefault();
			$("#submitEmailBtn").click();
		}
	});
});

function showOverlay() {
	// overlay: can't simply fade in.. IE will make opacity 100%
	$("#overlay")
	.css({"filter":"alpha(opacity=0)", "opacity":"0", "-moz-opacity":"0", "display":"block"})
	.fadeTo("normal", 0.80);
}

function sendEmailtoFriend(senderName, senderEmail, recipientNameInputID, recipientEmailInputID) {
	var recName = ($("#" + recipientNameInputID).val() != "") ? $("#" + recipientNameInputID).val() : "Friend";
	
	$.post(
		"../scripts/php/form_handler.php",
		{ 
			action:"send_email", 
			email_template:"http://www.maxravestores.com/welcome.html", 
			email_subject:"Welcome to Maxrave - Fun Fashion, Fun Prices", 
			sender_first_name:senderName,
			sender_last_name:$("#sender_last_name").val(),
			sender_email:senderEmail,
			sender_date_birth:$("#sender_date_birth").val(),
			sender_zip:$("#sender_zip").val(),
			recipient_name:recName, 
			recipient_emails:$("#" + recipientEmailInputID).val(), 
			keyid:"phuCuvexazezuyUfRaX5w8am"
		}, 		
		// callback only executed if the response has a successful response code			
		function(data, textStatus) {
			emailsSent++;
			
			// if this is the last email that needs to be sent, clear the fields and show the thank you message
			if (emailsNeeded == emailsSent) {
				$("#submitMsg").html("");
				$("#sender_first_name").val("First Name");
				$("#sender_last_name").val("Last Name");
				$("#sender_email").val("Your Email");
				$("#sender_zip").val("Zip");
				$("#sender_date_birth").val("B-Day (mm/dd/yyyy)");
				$("#recipient_emails").val("Your Friend's Email (separated by commas)");
        $("#recipient_name").val("Friends name");
				
				// HIDE ENTRY FORM, SHOW THANK YOU
				$("#tafFormDiv").css("display", "none");
				//$("#tafDiv").css("background", "#fff url(../images/bg/tellafriend_THANKS.jpg) no-repeat 0 0")
				$("#tafThanksDiv").css("display", "block");
			}
		}
	);
	
	/* // test
	emailsSent++;
	
	// if this is the last email that needs to be sent, clear the fields and show the thank you message
	if (emailsNeeded == emailsSent) {
		$("#submitMsg").html("");
		$("#sender_name").val("");
		$("#sender_email").val("");
		$("#recipient_name").val("");
		$("#recipient_email").val("");
		$("#recipient_name2").val("");
		$("#recipient_email2").val("");
		$("#recipient_name3").val("");
		$("#recipient_email3").val("");
		$("#recipient_name4").val("");
		$("#recipient_email4").val("");
		
		// SHOW THANK YOU
		$("#tafFormDiv").css("display", "none");
		$("#tafThanksDiv").css("display", "block");
	}*/
}
