﻿// AJAX Windows Plugin for cms-core
cms.windows = new function() 
{
    this.list = new Array();
    this.current = null;

    this.Open = function(title, url, options)
    {
        var tmp = new _window(title, url);
        if(this.current != null)
            this.list.push(this.current);
        this.current = tmp;
        if(options && options.showLoading)
        {
            cms.Mask.css("z-index",(this.list.length*2)+99);
            cms.Mask.show();
            tmp.Show();
        }
        tmp.Load(url,options);   
    }
    this.Close = function()
    {
        if(this.current != null)
        {
            var tmp = this.current;
            tmp.Close();
            if(this.list.length > 0)
            {
                this.current = this.list.pop();
                cms.Mask.css("z-index",(this.list.length*2)+99);
                cms.Mask.show();
                
            } else {
                this.current = null;
                cms.Mask.hide();
            }
            delete tmp;
        }
    }
    this.Alert = function(msg)
    {
        var tmp = new _window("CaveCMS", "");
        if(this.current != null) this.list.push(this.current);
        this.current = tmp;
        cms.Mask.css("z-index",(this.list.length*2)+99);
        cms.Mask.show();
        tmp.content.html("<div style=\"text-align:center;\">" + msg + "<br /><input type=\"button\" value=\"Ok\" onclick=\"cms.windows.Close();\"/></div>");  
        tmp.Show();
    }
    
    this.Message = function(msg)
    {
        var tmp = new _window("CaveCMS", "");
        if(this.current != null) this.list.push(this.current);
        this.current = tmp;
        cms.Mask.css("z-index",(this.list.length*2)+99);
        cms.Mask.show();
        tmp.content.html("<table style=\"text-align:center;margin-left:auto; margin-right:auto;\"><tr><td><img src=\"/Content/images/ajaxLoading.gif\" /></td><td>&nbsp;&nbsp;&nbsp;"+msg+"</td></tr></table>");  
        tmp.Show();
    }
}
function _window(title,url)
{
    this._OnOk = null;
    this._OnCancel = null;
    this.title = title;
    
    this.Close = function()
    {
        this.container.hide();
        this.container.remove();
    }
    this.Center = function()
    {
        this.container.css({'top':($(window).height()-this.container.height())/2,'left':($(window).width()-this.container.width())/2,"z-index":(cms.windows.list.length*2)+100});
    }
    this.Load = function(url,options)
    {
        this.URL = url
        if(options != null)
        {
            options.url = url;
            options.dataTyle = "html";
            options.success = function(result) {
                cms.Mask.css("z-index",(cms.windows.list.length*2)+99);
                cms.Mask.show();
                cms.windows.current.content.html(result);
                cms.windows.current.Show();
                if(this.OnSuccess)
                    this.OnSuccess(result);        
            };
            if(options.OnOk)
                this._OnOk = options.OnOk;
            if(options.OnCancel)
                this._OnCancel = options.OnCancel;
                
            $.ajax(options);
        } else {
            $.ajax({
                url: this.URL,
                success: function(result) {
                    cms.Mask.css("z-index",(cms.windows.list.length*2)+99);
                    cms.Mask.show();
                    cms.windows.current.content.html(result);
                    cms.windows.current.Show();
                },
                error: function() {
                    alert("something didn't work");
                },
                dataType: "html"
            });
        }
    }
    this.Refresh = function() 
    {
        this.Load(this.URL);
    }
    this.Show = function() 
    {
        this.Center();
        this.container.show();
    }
 
    this.Ok = function(result)
    {
        if(this._OnOk != null)
            this._OnOk(result);
        cms.windows.Close();
    }
    
    this.Cancel = function(result)
    {
        if(this._OnCancel != null)
            this._OnCancel(result);
        cms.windows.Close();
    }
          
    var src = "<div id=\"aw"+(cms.windows.list.length)+"\" class=\"AjaxWindow\"><table cellpadding=\"0\" cellspacing=\"0\"><tr><td>";
    src += "<table cellpadding=\"0\" cellspacing=\"0\" style=\"width:100%\"><tr><td class=\"AjaxWindowTopLeft\"></td><td class=\"AjaxWindowTitle\">"+this.title+"</td><td class=\"AjaxWindowTopRight\"></td></tr></table>";
    src += "</td></tr><tr><td>";
    src += "<table cellpadding=\"0\" cellspacing=\"0\" style=\"width:100%\"><tr><td class=\"AjaxWindowLeft\"></td><td class=\"AjaxWindowContent\">&nbsp;</td><td class=\"AjaxWindowRight\"></td></tr></table>";
    src += "</td></tr><tr><td>";
    src += "<table cellpadding=\"0\" cellspacing=\"0\" style=\"width:100%\"><tr><td class=\"AjaxWindowBottomLeft\"></td><td class=\"AjaxWindowBottom\">&nbsp;</td><td class=\"AjaxWindowBottomRight\"></td></tr></table>";
    src += "</td></tr></table></div>";
    this.container = $(src);
    this.content = this.container.find(".AjaxWindowContent");
    this.content.html("<table style=\"text-align:center; width:150px; margin-left:auto; margin-right:auto;\"><tr><td><img src=\"/Content/images/ajaxLoading.gif\" /></td><td> Loading...</td></tr></table>");
    $("body").append(this.container);
}