﻿var Window = null;

function _CaveWindowManager()
{
    this.Windows = new Array();
    this.Themes = new Array();
    this.Mask = $("<div id=\"Mask\" />");

    this.CloseAll = function() {
        while (this.Windows.length) {
            Window.Close();
        }
    }
    
    $.ajax({
        url:"/Content/CaveWin.htm",
        dataType: "html",
        success: function(result) {
            CWM.Themes["default"] = result;
        }
    });
}

var CWM = new _CaveWindowManager();

function CaveWindow(title,url,options)
{
    var _title;
    var _content;
    var _body;
    var _theme = "default";
    var _resize = false;
    var _Result = null;

    this.Close = function() {
        _body.fadeOut("fast",function(){$(this).remove()});
        CWM.Windows.pop();
        delete Window;
        if (CWM.Windows.length > 0) {
            Window = CWM.Windows[CWM.Windows.length - 1];
            CWM.Mask.css("z-index",(CWM.Windows.length*2)+99);
        } else {
            Window = null;
            CWM.Mask.hide();
        }
    }
    this.Result = function(result) {
        if (!!_Result)
            _Result(result);
    }
    this.Show = function()
    {
        _body.show();
    }
    this.Load = function(url)
    {
        _content.load(url);
    }

    this.render = function() {
        _body = $("<div class=\"window\" id=\"CW" + (CWM.Windows.length + 1) + "\">" + CWM.Themes[_theme] + "</div>");
        if (!!options && !!options.Type)
            _body.addClass(options.Type);
        $("body").append(_body);
        
        _title = _body.find(".header");
        _content = _body.find(".content");

        if (!!_title && title) {
            _title.text(title);
        }
        if (!!_content && url != undefined) {
            if(!options)
                options = {};
            if (!!options.ShowLoading) {
                var loading = $("<div id=\"Loading\" class=\"window\"><div class=\"message\"><div class=\"content\">Loading...</div></div></div>");
                $("body").append(loading);
                loading.css({left:($(window).width()-loading.width())/2,top:($(window).height()-loading.height())/2});
                loading.css("z-index",(CWM.Windows.length*2)+101).fadeIn("fast");
                CWM.Mask.css("z-index",((CWM.Windows.length+1)*2)+99).show();
                _resize = true;
            }
            options.url = url;
            options.dataType = "html";           
            options.success = function(result) {
                _content.html(result);
                _body.css({top:($(window).height()-_body.height())/2,left:($(window).width()-_body.width())/2});
                _body.css("z-index",(CWM.Windows.length*2)+100);
                if(!!options && !!options.Type)
                    _body.addClass(options.Type);
                if (_resize) {
                    $("#Loading").animate({height:_body.height(),width:_body.width(),top:($(window).height()-_body.height())/2,left:($(window).width()-_body.width())/2},500,function() {
                        _body.show();
                        CWM.Mask.css("z-index",(CWM.Windows.length*2)+99);
                        $("#Loading").fadeOut("fast",function(){$(this).remove();});        
                    });
                } else {
                    CWM.Mask.css("z-index",(CWM.Windows.length*2)+99).show();
                    _body.fadeIn("fast");
                }
            }
            options.error = function(result) {
                alert("an error occured: " + result);
            }
            $.ajax(options);
        }
        Window = this;
    }
    
    if (!!options) { 
        if(!!options.Theme)
            _theme = options.Theme;
        if (!!options.OnResult)
            _Result = options.OnResult;
    }
    
    if (CWM.Themes[_theme] == undefined) {
        $.ajax({
            url: options.Theme,
            dataType: "html",
            success: function(result) {
                CWM.Themes[_theme]= result;
                Window.render();
            }
        });
    } else {
        this.render();
    }
    CWM.Windows.push(this);
    Window = this;    
    return this;
}