﻿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
    });

    $('.email').click(function() {
        $('#send-friend-form').show();
        $("#send-friend-thanks").hide();
        $("#send-email-errors").hide();
        $('#send-friend-popup').dialog('open');
    });
 
    $('#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 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('');
			        });
			    } 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;
}

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"
		);
}