if (typeof sx == "undefined") var sx = {};
if (typeof sx.core == "undefined") sx.core = {};
if (typeof sx.ajax == "undefined") sx.ajax = {};
if (typeof sx.ui == "undefined") sx.ui = {};


if (typeof sx.tools == "undefined") sx.tools = {};
sx.tools.disableElement = function(el)
{
    el.attr("disabled", true);
};
sx.tools.showEl = function(el, bool)
{
    if (bool === false)
         el.css("display", "none");
    else if (bool === true)
         el.css("display", "block");
    else
        el.css("display", el.css("display") == "block" ? "none" : "block");
};
sx.tools.enableElement = function(el, bool)
{
    if (bool === false)
    	sx.tools.disableElement(el);
    else
        el.removeAttr("disabled");
};


sx.tools.getDocHeight = function()
{
    return Math.max(
        $(document).height(), $(window).height(), document.documentElement.clientHeight
    );
};


// sx.ajax
sx.ajax.showError = function(txt)
{
    if (txt.length < 1) return;
    var msg = "ERROR:" + "\n" + txt;
    alert(msg);
};

sx.ajax.callAction = function(url, data, onsuccess, onerror)
{
    data._dummy = new Date().getTime();
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        dataType : "xml",
        success: function(xml)
        {
            var xmlError = $("response error", xml);
            if (xmlError.text())
            {
                if (onerror) {onerror(xmlError.text());}
                else
                {
                	sx.ajax.showError(xmlError.text());
                }
                return;
            }
            if (onsuccess)
            {
                var xmlData = $("response data", xml);
                onsuccess(xmlData);
            }
        },
        error : function(request, errorString)
        {
        	sx.ajax.showError ("REQUEST FAILED: " + errorString + ";txt:" + request.responseText);
            if (onerror){ onerror();}
        }
    });
};

// sx.ui
sx.ui.loading = {
	init : function()
	{
    	this.loadingbar = $("#loadingbar");
    	this.loadingbar.jqm();
	},
	show : function()
	{
		this.loadingbar.jqmShow();
	},
	hide : function()
	{
		this.loadingbar.jqmHide();
	}
};

sx.ui.infobox = {
	init : function()
	{
		this.box = $("#sxuiInfoBoxModal");
		if (this.box.length < 1)
		{
			$("body", document).append('<div id="sxuiInfoBoxModal" class="jqmWindow"><div class="hdr"><span class="title"></span><span class="close"></span></div><div class="content"></div></div>');
			this.box = $("#sxuiInfoBoxModal");
		}
		this.box.jqm();
		this.box.jqmAddClose("span.close");
		this.box._content = $("div.content:first", this.box);
		this.box._title = $("div.hdr span.title:first", this.box);
	},
    _clickOK : function(box, btn)
    {
      box.jqmHide();
    },
	_setup : function (content, title, opts)
	{
        var owner = this;
        if (!opts) opts = {};
		this.box.removeClass("success");
		this.box.removeClass("error");
		this.box.removeClass("info");

		$("div.jqmWindow div.jqpopup div.jqcontent").empty();
		$("div.jqmWindow").jqmHide();

		this.box._content.html(content);
        var btnOK = $('<button class="ok">OK</button>');
        if (typeof opts.click_ok == "function")
        {
            var fncClose = function(){
                opts.click_ok();
                owner.box.jqmHide();
                return false;
            };
            btnOK.bind("click", fncClose);
            $("span.close", this.box).unbind().bind("click", fncClose);
        }
        else
        {
            btnOK.bind("click", function(){
                owner._clickOK(owner.box, btnOK);
                return false;
            });
        }
        var ctrl = $('<div class="ctrl"><div>');
        ctrl.append(btnOK)
        this.box._content.append(ctrl);

		this.box._title.html(title);
		this.box.jqmShow();
	},
	showInfo : function(content, title, opts)
	{
		this._setup(content, title, opts);
		this.box.addClass("info");

	},
	showError : function(content, title, opts)
	{
		this._setup(content, title, opts);
		this.box.addClass("error");
	},
	showSuccess : function(content, title, opts)
	{
		this._setup(content, title, opts);
		this.box.addClass("success");
	},


	hide : function()
	{
		this.box.jqmHide();
	}
};
$(document).ready(function()
{
	sx.ui.loading.init();
	sx.ui.infobox.init();
}); //end: document.ready


