﻿/// <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.CardDetail = function(clientId, addImageUrl, removeImageUrl, loginUrl)
{
    this.ClientID = clientId;
    this.AddImageUrl = addImageUrl;
    this.RemoveImageUrl = removeImageUrl;
    this.LoginUrl = loginUrl;
    this.Selector = "#" + clientId;
    this.Initialize();
};
Kleenex.CardDetail.prototype =
{
    Name : 'Kleenex.CardDetail',
    __typeName : 'Kleenex.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.CardService.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.CardService.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 = 'Von den Favoriten entfernen';
                    imageUrl = __app.RemoveImageUrl;
                }
                else
                {
                    text = 'Add to Favorites';
                    imageUrl = __app.AddImageUrl;
                }
                $('div.Favorites a img', context).attr('alt', text).attr('src', imageUrl);
            };
            __app.CardService.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.CardService.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 hidden = ('none' == $('.MyAccountCardListing', context).css('display'));
                if (hidden)
                {
                    $('a.cardStateToggle', context).text('Ansicht');
                }
                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 = 'Von den Favoriten entfernen';
                var imageUrl = __app.RemoveImageUrl;
                $(__app.Selector + ' div.Favorites a img').attr('alt', text).attr('src', imageUrl);
            }
        };
        
        this.CardService.IsFavoriteCard(cardId, onSuccess);
    }
};

Kleenex.Extend(Kleenex.CardDetail, Kleenex.UserControl);