﻿Array.prototype.last = function() { return this[this.length - 1]; }
$(document).ready(function() {
    $('#send-friend-popup').dialog({
        modal: true,
        draggable: false,
        resizable: false,
        zIndex: 100002,
        autoOpen: false,
        minHeight: 100,
        width: 500
    });
    //Newsletter Event tracking for MVC Pages
    var popupButton = $("*[class*='PopupOpenBtn-View']");

    $(popupButton).each(function (index, value) {
        if (!isBound($(value), 'click') || $(value).attr('class').indexOf("-View") > 0) {
            $(value).click(function FormClick() {
                var formType = getFormType(value);
                _gaq.push(['_trackEvent', 'Form', 'Initiation', formType]);
            });
        }
    });
    //Newsletter Event tracking for MVC Pages
    
    $('.email').click(function() {
        $('#send-friend-form').show();
        $("#send-friend-thanks").hide();
        $("#send-email-errors").hide();
        $('#send-friend-popup').dialog('open');
    });

    //Email popup for temp wishlist users
    $('#no-account-email').dialog({
        modal: true,
        draggable: false,
        resizable: false,
        zIndex: 100002,
        autoOpen: false,
        minHeight: 100,
        width: 500
    });

    $("#no-account-email-click").click(function () {
        $('#no-account-email').dialog('open');
    });

    $("#close-no-account-email").click(function () {
        $("#no-account-email").dialog('close');
    });

    $("#close-email-popup").click(function () {
        $("#send-friend-popup").dialog('close');
    });
 
    $('#signup').dialog({
        modal: true,
        draggable: false,
        resizable: false,
        zIndex: 100002,
        title: "Choose account type.",
        autoOpen: false,
        minHeight: 100
    });
    $('.signup').click(function() {
        var urlParts = $(this).attr('href').split('?');
        if (urlParts.length > 1) {
            var param = urlParts.last();
            $('#signup li a').each(function(i) {
                var href = $(this).attr('href');
                var newHref = (href.indexOf('?') > 0) ? href + '&' + param : href + '?' + param;
                $(this).attr('href', newHref);
            });
        }

        OpenSignup();
        return false;
    });

    $("#BlankBtn").click(function() {
        $('#MoviePopup').dialog('open');
    });

    $('#MoviePopup').dialog({
        modal: true,
        draggable: false,
        resizable: false,
        zIndex: 100002,
        title: "",
        autoOpen: false,
        minHeight: 380,
        width: 640,
        close: killModalMovie
    });

    $('#NewsletterPopup').dialog({
        modal: true,
        draggable: false,
        resizable: false,
        zIndex: 100002,
        title: "",
        autoOpen: false,
        minHeight: 100,
        width: 508
    });

    $("#newsletter-signup-trigger").click(function() {
        $('#NewsletterPopup').dialog('open');
    });

    $("#show-newsletter-form-trigger").click(function() {
        $('#NewsletterIntro').slideUp('fast', function() {
            $("#NewsletterFormContainer").slideDown();
        });
    });

    $("#NewsletterSendButton").click(submitNewsletterSignup);
});

function OpenSignup() {
	$('#signup').dialog('open');
}

function UpdateCountry(url) {
	$('#Address_State').change(function() {
		if (this.value == '') return true;
		$.post(url, { state: this.value },
            function(data) {
            	$('#Address_Country').attr('value', data);
            }, 'json');
	});
}

function submitNewsletterSignup() {
    var submitButton = $("*[class*='PopupSubmitBtn-View']");
    var formType = getFormType(submitButton);
    _gaq.push(['_trackEvent', 'Form', 'Send', formType]);

    var obj = {
        EmailAddress: $("#NewsletterSignup #EmailAddress").val(),
        FirstName: $("#NewsletterSignup #FirstName").val(),
        LastName: $("#NewsletterSignup #LastName").val(),
        Zip: $("#NewsletterSignup #Zip").val(),
        State: $("#NewsletterSignup #State").val(),
        Country: $("#NewsletterSignup #Country").val()
    };
    $.post('/Partial/NewsletterSignupSend',
			obj,
			function(data) {
			    if (data.success == true) {
			        $('#NewsletterFormContainer').hide('fast', function() {
			            $("#NewsletterDisclaimer").hide();
			            $("#NewsletterThanks").show();
			            $("#NewsletterSignup #FirstName").val('');
			            $("#NewsletterSignup #LastName").val('');
			            $("#NewsletterSignup #EmailAddress").val('');
			            $("#NewsletterSignup #Zip").val('');
			            //business tracking server
			            var ebRand = Math.random() + '';
			            ebRand = ebRand * 1000000;
			            //<![CDATA[ 
			            document.write('<scr' + 'ipt src="HTTP://bs.serving-sys.com/BurstingPipe/ActivityServer.bs?cn=as&amp;ActivityID=96888&amp;rnd=' + ebRand + '"></scr' + 'ipt>');
			            //]]>
			        });
			    } else {
			        var model = data.model;
			        var errors = "";
			        for (var i = 0; i < model.length; i++) {
			            errors += "<li>" + model[i].error + "</li>";
			        }
			        $("#newsletter-signup-errors").html(errors);
			        $("#newsletter-signup-errors").show();
			    }
			},
        	"json"
		);
	return false;
}
/**EVENT TRACKING HELPER METHODS**/
var isBound = function (el, ev) {
    var isItemBound = false;

    if ($(el).data("events") != undefined) {
        $.each($(el).data("events"), function (i, e) {
            if (i === ev) {
                isItemBound = true;
            }
        });
        return isItemBound;
    }
    return isItemBound;
}

function getFormType(el) {
    var Type = "";
    var classArray = ($(el).attr("class").toString().split("_"));
    $(classArray).each(function (index, value) {
        if (value == "form" && (classArray[index - 1]).indexOf("ga") != -1) {
            Type = classArray[index + 1];
        }
    });
    return Type;
}

/**EVENT TRACKING HELPER METHODS**/
function submitEmail() {
    var obj = { EmailAddress: $("#EmailFriend_EmailAddress").val(), YourName: $("#EmailFriend_YourName").val(), Comment: $("#EmailFriend_Comment").val(), Path: escape($("#EmailFriend_Path").val()) };
    $.post('/Partial/EmailFriendSend',
			obj,
			function(data) {
			    if (data.success == true) {
			        $('#send-friend-form').hide('fast', function() {
			            $("#send-friend-thanks").show();
			            $("#EmailFriend_EmailAddress").val('');
			            $("#EmailFriend_YourName").val('');
			            $("#EmailFriend_Comment").val('');
			        });
			    } else {
			        var model = data.model;
			        var errors = "";
			        for (var i = 0; i < model.length; i++) {
			            errors += "<li>" + model[i].error + "</li>";
			        }
			        $("#send-email-errors").html(errors);
			        $("#send-email-errors").show();
			    }
			},
        	"json"
		);
}
