﻿/// <reference path="../jquery-1.4.2-vsdoc.js" />
/// <reference path="../jquery.tools.min.js" />
/// <reference path="TranceSoft.js" />
/// <reference path="TranceSoft.Utils.js" />

TranceSoft.Overlay = function ()
{
    var module = {};
    var ordered = new Array();
    var overlays = {};
    var options = { maskId: 'mask' };

    function Sort()
    {
        for (i in overlays)
        {
            ordered.push(overlays[i]);
        }
    }

    function ShowQueue()
    {
        var overlay = ordered.pop();
        if (overlay) { Show(overlay); }
    }

    function Bind(overlay)
    {
        var $overlay = $('#' + overlay.elementId);
        var api = $overlay.data("overlay");
        if (api) { api.load(); }
        else
        {
            $overlay.overlay(
                    {
                        load: true,
                        closeOnClick: overlay.maskClose,
                        mask: {
                            color: '#000000',
                            loadSpeed: 500,
                            opacity: 0.6
                        }
                    });
            $overlay.find('iframe').each(function ()
            {
                var $this = this;
                var href = $this.src;
                $this.src = '';
                setTimeout(function () { $this.src = href; }, 1);
            });
        }
    }

    function Show(overlay)
    {
        if (overlay)
        {
            var $overlay = $('#' + overlay.elementId);
            $overlay.overlay(
                    {
                        load: true,
                        closeOnClick: overlay.maskClose,
                        mask: {
                            color: '#000000',
                            loadSpeed: 500,
                            opacity: 0.6
                        }
                    });
            $overlay.find('iframe').each(function ()
            {
                var $this = this;
                var href = $this.src;
                $this.src = '';
                setTimeout(function () { $this.src = href; }, 1);
            });
        }
    };

    function Initialize()
    {
        Sort();
        ShowQueue();
    }

    $(window).load(Initialize);

    module.Register = function (context)
    {
        /// <summary>Register new overlay</summary>
        overlays[context.name] = context;
    };
    module.BindOnDemand = function (context)
    {
        if (context.triggerSel)
        {
            $(context.triggerSel).live('click',
            function () { Bind({ name: context.name || context.triggerId, elementId: context.elementId }); });
        }
        else
        {
            $('#' + context.triggerId).live('click',
            function () { Bind({ name: context.name || context.triggerId, elementId: context.elementId }); });
        }
    };
    module.Mask = function (maskId) { options.maskId = maskId; };

    return module;
} ();
