﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="jquery.intellisense.js"/>
/// <reference path="jquery.validate.js"/>

/// <reference path="Kleenex.Core.js"/>

Kleenex.HomePageMailingListSignup = function(clientId)
{
    this.ClientID = clientId;
    this.Selector = "#" + clientId;
    this.Initialize();
};
Kleenex.HomePageMailingListSignup.prototype =
{
    Name : 'Kleenex.HomePageMailingListSignup',
    __typeName : 'Kleenex.HomePageMailingListSignup',
    __class : true,
    AttachEvents : function()
    {
        var __app = this;
        var context = $(this.Selector);
        
        var revertDisplay = function(timeout)
        {
            setTimeout(function() 
            {
                $('.MailingListFailure', context).hide();
                $('.MailingListThankYou', context).hide();
                $('.MailingListProcessing', context).hide();
                $('.MailingListInvalidEmail', context).hide();
                $('.EmailAddress', context).val('');
                $('.signup', context).fadeIn();
                $('.EmailAddress', context).focus().blur();
            }, timeout);
        };
        
        var onSuccess = function(result)
        {
            $('.MailingListProcessing', context).hide();
            if(result)
            {
                $('.signup', context).fadeOut(250, function()
                {
                    $('.MailingListThankYou', context).fadeIn();
                    revertDisplay(7000);
                });
            }
            else
            {
                $('.signup', context).fadeOut(250, function()
                {
                    $('.MailingListFailure', context).fadeIn();
                    revertDisplay(7000);
                });
            }
        };
        
        var validateEmailAddress = function(emailAddress)
        {
            var AtPos = emailAddress.indexOf('@');
            var StopPos = emailAddress.lastIndexOf('.');

            if (emailAddress === '') {
                return false;
            }
            if (AtPos == -1 || StopPos == -1) {
                return false;
            }
            if (StopPos < AtPos) {
                return false;
            }
            if (StopPos - AtPos == 1) {
                return false;
            }
            return true;
        };
        
        var handleSubscribe = function()
        {
            var emailAddress = $('.EmailAddress', context).val();
            
            if(validateEmailAddress(emailAddress))
            {
                switch($('#list').val())
                {
                    case 'StayConnected':
                        __app.MailingListService.KleenexMailingListOptIn(emailAddress, onSuccess);
                        break;
                    case 'MovieReminder':
                        __app.MailingListService.OptInMovieReminder(emailAddress, onSuccess);
                        break;
                }
                
                $('.signup', context).fadeOut(250, function()
                {
                    $('.MailingListProcessing', context).fadeIn(100);
                });
            }
            else
            {
                $('.signup', context).fadeOut(250, function()
                {
                    $('.MailingListInvalidEmail', context).fadeIn();
                    revertDisplay(5000);
                });
            }
        };
        
        $('a.mailingListButton', context).click(handleSubscribe);
    },
    Display : function()
    {
        var __app = this;
    }
};

Kleenex.Extend(Kleenex.HomePageMailingListSignup, Kleenex.UserControl);
