﻿/// <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.ArticleTags = function(clientId, themeNumber, catnum) {
    this.ClientID = clientId;
    this.Selector = '#' + clientId;
    this.ThemeNumber = themeNumber;
    this.Catnum = catnum;
    this.Timeout = undefined;
    this.Initialize();
};

SCOTT.CSC.ArticleTags.prototype =
{
    Name: 'SCOTT.CSC.ArticleTags',
    __typeName: 'SCOTT.CSC.ArticleTags',
    __class: true,
    AttachEvents: function() {
        var __app = this;
        var context = $(this.Selector);

        var addTag = function() {
            this.blur();
            if (!__app.RequireAuthentication()) {
                return false;
            }
            clearTimeout(__app.Timeout);
            $('div.Tags .Form', context).css('height', '');
            $('div.Tags .Form', context).show();
            $('div.Tags .Thanks', context).hide();
            $('div.Tags', context).slideDown(250, function() {
                $('input.addATagInput', context).focus();
            });

            return false;
        };

        $('a.AddTag', context).click(addTag);

        var onTagging = function() {
            var onSuccess = function() {
                $('.Tags', context).css('height', 58);
                $('.Tags .Form', context).fadeOut(500, function() {
                    $('.Tags .Form input.addATagInput', context).val('');
                    $('.Tags .Thanks span', context).hide();
                    $('.Tags .Thanks', context).show(10, function() {
                        $('.Tags .Thanks span', context).fadeIn(250, function() {
                            __app.Timeout = setTimeout(function() {
                                $('.Tags .Thanks', context).fadeOut(500);
                                __app.Timeout = setTimeout(function() {
                                    $('.Tags', context).slideUp(500);
                                }, 500);
                            }, 1500);
                        });
                    });
                });
            };

            var text = $('input.addATagInput', context).val();

            if (text !== '') {
                __app.TagsService.SaveArticleTags(text, __app.ThemeNumber, __app.Catnum, onSuccess);
            }
        };

        $('img.addATagSubmitBtn', context).mousedown(onTagging).css('cursor', 'pointer');

        // handle enter key
        $('.Tags .Form input.addATagInput', context).keydown(function(ev) {
            if (ev.keyCode === 13) {
                $(this).blur();
                onTagging();
                return false;
            }
            else {
                return true;
            }
        });

        $('.addATagCaption .textLink').click(function() {
            $('.Tags', context).slideUp(500);
            $('.Tags .Form input.addATagInput', context).val('');
        });
    }
};

SCOTT.CSC.Extend(SCOTT.CSC.ArticleTags, SCOTT.CSC.UserControl);
