﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="../jquery.intellisense.js"/>

/// <reference path="../Kleenex.Core.js"/>

Kleenex.Feel.CardDetail = function(clientId, addImageUrl, removeImageUrl, loginUrl) {
    this.ClientID = clientId;
    this.AddImageUrl = addImageUrl;
    this.RemoveImageUrl = removeImageUrl;
    this.LoginUrl = loginUrl;
    this.Selector = "#" + clientId;
    this.Initialize();
};

Kleenex.Feel.CardDetail.prototype =
{
    Name: 'Kleenex.Feel.CardDetail',
    __typeName: 'Kleenex.Feel.CardDetail',
    __class: true,
    AttachEvents: function() {
        var __app = this;
        var context = $(this.Selector);
        var cardId = $('.Key', context).text();

        var handleInappropriateContent = function() {
            this.blur();

            var onSuccess = function() {
                $('div.FlagContentContainer', context).fadeOut(function() {
                    $('div.FlagContentContainer', context).text('Flagged for review.').fadeIn();
                });
            };

            __app.FeelCardService.FlagInappropriate(cardId, onSuccess);
        };
        $('div.FlagContentContainer a', context).click(handleInappropriateContent);

        var handleGiveATissue = function() {
            this.blur();

            var onSuccess = function(count) {
                if (count > 0) {
                    $('span.TissueCount', context).text('' + count + '');
                }
            };

            __app.FeelCardService.GiveATissue(cardId, onSuccess);
        };
        $('div.GiveATissue a', context).click(handleGiveATissue);

        var handleFavoriteCard = function() {
            this.blur();

            var app = Kleenex.GetApplication();
            if (!app.IsUserAuthenticated()) {
                Kleenex.ShowModal(__app.LoginUrl, false);
                return;
            }

            var onSuccess = function(isFavorite) {
                var text;
                var imageUrl;
                if (isFavorite) {
                    text = 'Remove from Favorites';
                    imageUrl = __app.RemoveImageUrl;
                }
                else {
                    text = 'Add to Favorites';
                    imageUrl = __app.AddImageUrl;
                }
                $('div.Favorites a img', context).attr('alt', text).attr('src', imageUrl);
            };
            __app.FeelCardService.ToggleCardFavorite(cardId, onSuccess);
        };
        $('div.Favorites a', context).click(handleFavoriteCard);

        var handleConfirmDeleteFavorite = function() {
            var onSuccess = function(isFavorite) {
                if (!isFavorite) {
                    hideConfirmation();
                    $('td.CardRemoveTitle', context).text('Removed!');
                    setTimeout(function() {
                        context.slideUp(500);
                    }, 1500);
                }
            };
            __app.FeelCardService.RemoveCardFavorite(cardId, onSuccess);
            hideConfirmation();
        };

        var showConfirmation = function() {
            // prepare the modal if it is not in place already
            if ($('#DeleteCardFavorite').length === 0) {
                var modal = $('.ConfirmDeleteFavorite:first').html();
                $('form:first').prepend('<div id="DeleteCardFavorite">' + modal + '</div>');
                $('#DeleteCardFavorite div.Close a').click(hideConfirmation);
                $('#DeleteCardFavorite .CancelFavoriteDelete').click(hideConfirmation);
            }
            __app.ShowMask();

            // ensure the context is preserved
            $('#DeleteCardFavorite .ConfirmFavoriteDelete').unbind().click(handleConfirmDeleteFavorite);

            var scrollTop = $(window).scrollTop();
            $('#DeleteCardFavorite').css('top', (scrollTop + 30) + 'px').show();
        };

        var hideConfirmation = function() {
            $('#DeleteCardFavorite').hide();
            __app.HideMask();
        };

        var handleRemoveFavoriteCard = function() {
            this.blur();
            showConfirmation();
        };
        $('td.CardRemoveTitle a', context).click(handleRemoveFavoriteCard);

        var toggleCardDetailView = function() {
            $(this).blur();
            $('.MyAccountCardListing', context).slideToggle(500, function() {
                var visible = $('.MyAccountCardListing', context).is(':visible');
                if (!visible) {
                    $('a.cardStateToggle', context).text('View');
                }
                else {
                    $('a.cardStateToggle', context).text('Close');
                }
            });
        };
        $('a.cardStateToggle', context).click(toggleCardDetailView);
    },
    Display: function() {
        var __app = this;
    },
    ChangeLoginStatus: function(isAuthenticated) {
        var __app = this;
        var cardId = $(__app.Selector + ' .Key').text();

        var onSuccess = function(isFavorite) {
            if (isFavorite) {
                var text = 'Remove from Favorites';
                var imageUrl = __app.RemoveImageUrl;
                $(__app.Selector + ' div.Favorites a img').attr('alt', text).attr('src', imageUrl);
            }
        };

        this.FeelCardService.IsFavoriteCard(cardId, onSuccess);
    }
};

Kleenex.Extend(Kleenex.Feel.CardDetail, Kleenex.UserControl);
