﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="script.js" />
/// <reference path="jquery-1.3.2-vsdoc2.js" />
/// <reference path="slider.js" />

if (typeof GskTools == 'undefined')
{
    GskTools = function() { };
}

GskTools.PopupManagerSettings = function()
{
    /// <field type="String" name="MaskId">Id of the transparent mask which should cover main page content.</field>
    /// <field type="Integer" name="DelayTime">Interval time.</field>
    /// <field type="Integer" name="FadeTime">Fade time.</field>
    MaskId = '';
    DelayTime = 1;
    FadeTime = 500;
}

GskTools.InitializeMessagingRadio = function() {
    $('.radio-message-send').click(function() {
        if ($('.radio-message-select input:radio:checked').length == 0) {
            return false;
        }
        else {
            if (__doPostBack) {
                __doPostBack($(this).attr('name'), '');
            }
        }
    }).attr('href', '#');
}

GskTools.PopupManager = function()
{
    ///<summary>Maintain popups.</summary>
    PopupItem = function()
    {
        /// <field name="ContainerId" type="String" />
        Id = '';
        $Container = function()
        {
            /// <returns type="Object">DOM container which keeps popup.</returns>
            return $('#' + this.ContainerId);
        }
    }

    settings = new GskTools.PopupManagerSettings();
    $current = null;
    popups = new Array();
    feelingSliders = {};
    var feelingSlidersDone;

    CheckButton = function(e)
    {
        if ($(e.currentTarget).attr('disabled'))
        {
            e.preventDefault();
        }
        else
        {
            showProgressGif(true);
        }
    };

    ShowMask = function()
    {
        $mask = $('#' + settings.MaskId);
        var timeout = setTimeout(function()
        {
            //Get the screen height and width
            var maskHeight = $(document).height();
            var maskWidth = $(window).width();

            // hack - select-free its a special direct-x mask used in IE6 to hide dropdown.
            $('#select-free').css({ 'width': maskWidth, 'height': maskHeight }).
                fadeIn(200).fadeTo("fast", 0.8);

            // Set height and width to mask to fill up the whole screen
            // transition effect
            $mask.css({ 'width': maskWidth, 'height': maskHeight }).
                fadeIn(200).fadeTo("fast", 0.8);
        }, settings.DelayTime);
    };

    ShowNext = function(args)
    {
        ///<summary>Pop next popup and show it.</summary>
        if (popups.length > 0)
        {
            var item = popups.shift();
            $current = $('#' + item.Id);
            BindPopupToShow(item);
        }
    };

    CleanUp = function()
    {
    };

    OnLastClose = function(e)
    {
        ///<summary>Maintain closing action if no others popups left.</summary>
        setTimeout(function()
        {
            $current.fadeOut(300);
            $('#' + settings.MaskId).fadeOut(300);
            $('#select-free').hide();
            CleanUp();
        }, 100);
    };

    OnClose = function(e)
    {
        ///<summary>Maintain closing action if there is more popups to show.</summary>
        //Cancel the link behavior
        e.preventDefault();

        $current.fadeOut(300, function()
        {
            CleanUp();
            ShowNext(e);
        });
    };

    RedrawSliders = function()
    {
        //REDRAW ALL SLIDERS
        fdSliderController.redrawAll();
    }

    BindCloseButtons = function(item)
    {
        var id = item.Id ? item.Id : item.id;
        $closeButtons = $('#' + id + ' .close');
        if ($closeButtons.length > 0)
        {
            closeDelegate = popups.length > 0 ? OnClose : OnLastClose;
            $closeButtons.click(closeDelegate);
        }
    }

    BindPopupToShow = function(item)
    {
        $item = $('#' + item.Id);


        ///<param name="item" type="PopupItem" />
        var timeout = setTimeout(function()
        {
            BindCloseButtons(item);
            //if close button is clicked
            Sys.Application.add_load(function()
            {
                BindCloseButtons(item);
            });

            var parentIsBody = $item.parent().get(0).tagName == 'body';
            if (!parentIsBody)
            {
                $('form').append($item);
                parentIsBody = true;
            }
            //Get the window height and width
            var winH = $(window).height();
            var winW = parentIsBody ? $(window).width() : $item.parent().width();

            var itemTop = parentIsBody ? winH / 2 - $item.outerHeight() / 2 : 20;

            //Set the popup window to center
            $item.css('top', itemTop).
                css('left', winW / 2 - $item.outerWidth() / 2).
                css('zIndex', 9500);
            //transition effect
            $item.fadeIn(500, RedrawSliders);
            clearTimeout(timeout);
        }, settings.DelayTime);
    };

    EnableFeelingsButton = function()
    {
        var $closebtn = $('#' + settings.CloseFeelingsBtnId);
        $closebtn.removeClass().addClass('close').removeAttr('disabled');

        var popup = $current.get(0);
        BindCloseButtons(popup);
        //if close button is clicked
        //Sys.Application.add_load(function()
        //{
        //    BindCloseButtons(popup);
        //});
        //$closebtn.bind('click', '', function() {
        //    __doPostBack(settings.CloseFeelingsBtnId, '');
        //});
    }

    return {
        Version: function()
        {
            /// <summary>Returns information about current manager version.</summary>
            /// <returns type="String" />
            return "0.1";
        },
        Register: function(containerId)
        {
            /// <summary>Add popup to the existing popup collection.</summary>
            /// <param type="String" name="containerId">Id of control which keeps popup.</param>
            /// <returns type="Void" />
            var item = new PopupItem();
            item.Id = containerId;
            popups.push(item);
        },
        Initialize: function(settingsObject)
        {
            ///<summary>Creates handlers and other all required structures.</summary>
            ///<param type="GSKTools.PopupManagerSettings" name="settingsObject" />
            settings = settingsObject;
            if (settings.NonPostbacksSliders.length > 0)
            {
                for (i in settings.NonPostbacksSliders)
                {
                    feelingSliders[settings.NonPostbacksSliders[i]] = false;
                }

                var $closebtn = $('#' + settings.CloseFeelingsBtnId);
                $closebtn.bind('click', '', CheckButton);
                $closebtn.attr('disabled', 'disabled');
            }

            $(document).ready(
                function(sender, e)
                {
                    ShowMask();
                    ShowNext();
                });
        },
        UpdateSliderInfo: function(ctx)
        {
            if (!feelingSliders[ctx.elem.id])
            {
                feelingSliders[ctx.elem.id] = true;
            }
            var allTouched = true;
            for (i in feelingSliders)
            {
                allTouched = feelingSliders[i] & allTouched;
            }

            if (allTouched && !feelingSlidersDone)
            {
                EnableFeelingsButton();
                feelingSlidersDone = true;
            }
        }
    }
} ();

