﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="jquery-1.2.6-vsdoc.js"/>

SCOTT.CSC.BlogEmailSubscribe = function(clientId) {
    this.ClientID = clientId;
    this.Selector = '#' + clientId;
    this.Initialize();
};

SCOTT.CSC.BlogEmailSubscribe.prototype =
{
    Name: 'SCOTT.CSC.BlogEmailSubscribe',
    __typeName: 'SCOTT.CSC.BlogEmailSubscribe',
    __class: true,
    AttachEvents: function() {
        var __app = this;
        var context = $(this.Selector);

        var updateSubscription = function(optIn) {
            SCOTT.CSC.WebServices.Users.SetBlogEmailSubscription(optIn, function(subscribed) {
                __app.UpdateDisplay(subscribed);
            });
        };

        $('a.subscribe', context).click(function() {
            if (SCOTT.CSC.Utility.RequireAuthentication(function() {
                updateSubscription(true);
            })) {
                updateSubscription(true);
            }
        });

        $('a.unsubscribe', context).click(function() {
            updateSubscription(false);
        });
    },
    UpdateDisplay: function(subscribed) {
        var context = $(this.Selector);
        if (subscribed) {
            $('a.subscribe', context).fadeOut(250, function() {
                $('a.unsubscribe', context).fadeIn(250);
            });
        }
        else {
            $('a.unsubscribe', context).fadeOut(250, function() {
                $('a.subscribe', context).fadeIn(250);
            });
        }
    },
    ChangeLoginStatus: function(isAuthenticated) {
        var __app = this;

        SCOTT.CSC.WebServices.Users.GetBlogEmailSubscription(function(subscribed) {
            __app.UpdateDisplay(subscribed);
        });
    }
};

SCOTT.CSC.Extend(SCOTT.CSC.BlogEmailSubscribe, SCOTT.CSC.UserControl);
