﻿// Copyright (c) 2010 PageBean, Inc.  (http://www.pagebean.com)
// All rights reserved.

$(document).ready
(
	function() {
	    onEmailProductResize(true);

	    $(".emailProduct img").click
		(
			function() {
			    $.ajax
				(
					{
					    url: "/ajax/emailProduct.aspx",
					    data: { pID: $(this).attr("id") },
					    cache: true,
					    success: function(data) {
					        $(".emailProductWrapper").html("");
					        $(".emailProductWrapper").html(data);
					        onEmailProductResize(false);
					    }
					}
				);

			    $("#emailProductOverlay").fadeTo(500, 0.45);
			    $("#emailProductDialog").delay(200).fadeIn(300);
			}
		);

	    $("#emailProductClose").live
		(
			"click",
			function() {
			    $("#emailProductOverlay").fadeOut(500);
			    $("#emailProductDialog").fadeOut(200);
			}
		)

	    $(window).resize
		(
			function() {
			    onEmailProductResize(true);
			}
		);

	    $(window).scroll
		(
			function() {
			    onEmailProductResize(true);
			}
		);

	    $("#lnkSendEmail").live
		(
			"click", function() {
			    if ($(this).hasClass("button9")) {
			        var errorMessage = "ERROR(S):<br>";
			        if ($("#txt_emailTo").val() == "") {
			            errorMessage += "You must enter at least one e-mail address for the recipient(s).<br>"
			        }
			        if ($("#txt_emailFrom").val() == "") {
			            errorMessage += "You must enter an e-mail address for the sender."
			        }
			        if (errorMessage != "ERROR(S):<br>") {
			            $("#lbl_errorMessage2").html(errorMessage);
			            $("#panel_errorMessage2").css('visibility', 'visible');

			        }
			        else {
			            $("#panel_errorMessage2").css('visibility', 'hidden');
			            $("#panel_errorMessage2").css('display', 'none');
			            $.ajax
					(
						{
						    url: "/ajax/sendEmail.aspx",
						    data:
							{
							    pID: $("#h_pID").val(),
							    productLink: $("#h_productLink").val(),
							    mailTo: $("#txt_emailTo").val(),
							    mailFrom: $("#txt_emailFrom").val(),
							    note: $("#txt_note").val(),
							    sendCopy: $("#chk_sendCopy")[0].checked
							},
						    success: function() {
						        $("#panel_errorMessage2").css('visibility', 'hidden');
						        $("#emailProductOverlay").fadeOut(500);
						        $("#emailProductDialog").fadeOut(200);
						        $("html, body").animate({ scrollTop: 0 }, 1000);
						    },
						    failure: function(msg) {
						        $("#lbl_errorMessage2").html(msg);
						        $("#panel_errorMessage2").css('visibility', 'visible');
						        $("#panel_errorMessage2").css('display', 'inline');
						    },
						    error: function(msg) {
						        var exceptionMessage = msg.responseText.substring(msg.responseText.indexOf("Exception: "));
						        exceptionMessage = exceptionMessage.substring(10, exceptionMessage.indexOf("<br>"));
						        $("#lbl_errorMessage2").html("ERROR(S):<br>" + exceptionMessage);
						        $("#panel_errorMessage2").css('display', 'inline');
						        $("#panel_errorMessage2").css('visibility', 'visible');

						    }
						}
					);
			        }
			    }
			}
		);
	}
);

function onEmailProductResize(stop) {
    if ($(window).height() > $("body").height()) {
        $("#emailProductOverlay").css("height", $(window).height() + "px");
    }
    else {
        $("#emailProductOverlay").css("height", $("body").height() + "px");
    }

    $("#emailProductOverlay").css("left", $(window).scrollLeft() + "px");

    if (stop) {
        $("#emailProductDialog").stop(true, true);
    }

    if ($("#emailProductDialog").height() < $(window).height()) {
        $("#emailProductDialog").animate({ top: ($(window).height() / 2) - ($("#emailProductDialog").height() / 2) + $(window).scrollTop() + "px" }, 250);
    }
    else {
        $("#emailProductDialog").css("top", "0px");
    }

    if ($("#emailProductDialog").width() < $(window).width()) {
        $("#emailProductDialog").animate({ left: ($(window).width() / 2) - ($("#emailProductDialog").width() / 2) + $(window).scrollLeft() + "px" }, 250);
    }
    else {
        $("#emailProductDialog").css("left", "0px");
    }
}
