$(function(){

function emailToFriend(currentPageUrl) {
        var src = "/onlinecatalog/emailToAFriend.ktp?urlOfInterest="+currentPageUrl;
       try {
           var iframe = document.createElement("<iframe frameborder='0' scrolling='no' ></iframe>");

       }   catch(e) {
		var iframe = document.createElement("iframe");
		$(iframe).attr("scrolling", "no");
		$(iframe).attr("frameborder", "0");
        //$(iframe).css("border", "none");
       }

        $(iframe).load(function() {
			var that = this;
			window.setTimeout(function() {
				var width = 496;
				var height = 332;

				if (that.contentWindow) {
					width = $(that.contentWindow.document).find("#emailToFriend").width();
					height = $(that.contentWindow.document).find("#emailToFriend").height();
				} else {
					width = $(that.contentDocument).find("#emailToFriend").width();
					height = $(that.contentDocument).find("#emailToFriend").height();
				}

				$(iframe).attr("width", width);
				$(iframe).attr("height", height);
                $("#emailForm").attr("width", width);
                ieWidthFix();
            }, 100);
            createLightbox();

        });

        $('#lightbox-copy').html('');
        $('#lightbox-copy').append($(iframe));
        $('#lightbox-copy').children("iframe").wrapAll("<div id='emailForm' ></div>")
        $(iframe).attr("src", src);



    }

$("a.email").click(function () {emailToFriend(currentPageUrl)});
             })


function sendEmailTo(urlOfInterest, isNewProducts) {
    var firstName = $("#firstName")[0].value;
    var lastName = $("#lastName")[0].value;
    var toAddress = $("#toAddress")[0].value;
    var fromAddress = $("#fromAddress")[0].value;
    var message = $("#message")[0].value;
    $.ajax(
         {url:"/onlinecatalog/emailToFriend.ktp?firstName="+firstName+"&lastName="+lastName+"&fromAddress="+fromAddress+"&toAddress="+toAddress+"&message="+message+"&urlOfInterest=" + urlOfInterest,
         dataType:"json",
         success:function(data){
                        if(data.emailToFriend.status == "ERROR") {
                            var error = "You have errors in the following fields<br />";
                            $.each(data.emailToFriend.errors, function(i,item){
                                error += item.message + "<br />";
                            });
                            $("#emailErrorMessage").html(error);
                        } else if (data.emailToFriend.status == "SUCCESS") {
                            HideContent('email_n');
                            ShowContent('email_n_closed', isNewProducts);
                        }
                }
        // , error:function(){console.log(arguments)}
        });
}

function showEmail() {
   $("#emailErrorMessage").html("");
   ShowContent('email_n');
}

function showEmailFromNewProducts() {
   $("#emailErrorMessage").html("");
   ShowContent('email_n', true);
}

function showProductEmail(urlOfInterest, productName, imageName) {

    var url = encodeURIComponent("http://" + location.hostname + "/" + urlOfInterest);
    $("#emailErrorMessage").html("");
    $("#emailToFriendImage").attr("src", imageName);
    $("#emailToFriendImage + span").html(productName);
    $("#urlOfInterest").val( url );
    ShowContent('email_n');
    
}

function sendProductEmail() {
    var urlOfInterest = $("#urlOfInterest").val();
    sendEmailTo(urlOfInterest);
    sendOmnitureEmailToFriend(urlOfInterest);
}

function sendOmnitureEmailToFriend(urlOfInterest){

    omniture = s_gi(s_account);
    omniture.linkTrackVars='prop14,eVar26,events';
    omniture.linkTrackEvents='event11';
    omniture.prop14 = urlOfInterest;
    omniture.eVar26 = urlOfInterest;
    omniture.events = 'event11';
    omniture.tl(true, 'o', 'Send Email');

}