﻿/// <reference name="MicrosoftAjax.js"/>



Type.registerNamespace("TUI.TipsCommunity");



TUI.TipsCommunity.SpecialCommandsDialogHandler = function()
{
    TUI.TipsCommunity.SpecialCommandsDialogHandler.initializeBase(this);
    this._scrapbookChoiceClickedDelegate;
}
TUI.TipsCommunity.SpecialCommandsDialogHandler.prototype = 
{
    initialize: function()
    {
        TUI.TipsCommunity.SpecialCommandsDialogHandler.callBaseMethod(this, 'initialize');
        this._scrapbookChoiceClickedDelegate = Function.createDelegate(this, this.scrapbookChoiceClicked);
    },
    dispose: function()
    {
        TUI.TipsCommunity.SpecialCommandsDialogHandler.callBaseMethod(this, 'dispose');
    },
    showAddToScrapbookDialog: function(tipTargetId)
    {
        var
            dialog = this.get_dialogAddToScrapbook(),
            $dialog = $(dialog.get_element()),
            $sections = $dialog.find('.scrapbook-choice, .new-scrapbook, .existing-scrapbooks'),
            existingScrapbooksList = $dialog.find('.existing-scrapbooks select').get(0);
        
        $dialog.find('.validator').hide();
        $dialog.find('input.associated-entity').val(tipTargetId);
        $sections.hide();

        if (existingScrapbooksList.options.length == 1)
        {
            $sections.eq(1).show();
            $dialog.find('.add-to-new').show();
            $dialog.find('.add-to-old').hide();
        }
        else
        {
            $sections.eq(0).find(':radio').click( this._scrapbookChoiceClickedDelegate ).eq(1).attr('checked', true);
            $sections.eq(0).show().end().eq(2).show();
            $dialog.find('.add-to-old').show();
            $dialog.find('.add-to-new').hide();
        }
        
        dialog.show();
    },
    scrapbookChoiceClicked: function(e)
    {
        var $dialog = $(this.get_dialogAddToScrapbook().get_element());
        
        $dialog.find(".new-scrapbook, .existing-scrapbooks").hide();
        
        switch (e.target.value)
        {
            case "AddToNewScrapbook" :
                $dialog.find(".new-scrapbook").show();
                $dialog.find('.add-to-new').show();
                $dialog.find('.add-to-old').hide();
                break;
            case "AddToExistingScrapbook" :
                $dialog.find(".existing-scrapbooks").show();
                $dialog.find('.add-to-old').show();
                $dialog.find('.add-to-new').hide();
                break;
        }
    },
    getAddToScrapbookLoginRoundTripUrl: function(tipTargetId)
    {
        var
            regexUriQuery = /^[^?#]+\?([^#]+)/i, // URI query
            regexUriFragment = /#.+/i,
            hasQuery = regexUriQuery.test(location.href),
            hasFragment = regexUriFragment.test(location.href),
            queryParams = String.format("{0}addToScrapbook=true&tipTargetId={1}", hasQuery ? "&" : "?", tipTargetId),
            urlReturn,
            urlRedirect;
        
        if (hasFragment)
        {
            urlReturn = location.href.replace
            (
                regexUriFragment,
                function(group, index, original)
                {
                    return queryParams + group;
                }
            );
        } 
        else
        {
            urlReturn = location.href + queryParams;
        }
        
        urlRedirect = String.format("{0}?returnUrl={1}", this.get_loginUrl(), encodeURIComponent(urlReturn));
            
        return urlRedirect;
    },
    get_loginUrl: function() { return this._loginUrl; },
    set_loginUrl: function(value) { this._loginUrl = value; },
    get_dialogAddToScrapbook: function() { return this._dialogAddToScrapbook; },
    set_dialogAddToScrapbook: function(value) { this._dialogAddToScrapbook = value; }    
}
TUI.TipsCommunity.SpecialCommandsDialogHandler.registerClass('TUI.TipsCommunity.SpecialCommandsDialogHandler', Sys.Component);



if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
