/**
 * title: xbox.js
 * purpose: Implements boxing functionality.
 * licenses: 
 * This script uses jQuery 1.3.2 under the MIT License:
 * Copyright (c) 2009 John Resig, http://jquery.com/
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * This script uses jQuery UI 1.7.1 under the MIT License:
 * Copyright (c) 2009 Paul Bakaus, http://jqueryui.com/
 *
 * This software consists of voluntary contributions made by many
 * individuals (AUTHORS.txt, http://jqueryui.com/about) For exact
 * contribution history, see the revision history and logs, available
 * at http://jquery-ui.googlecode.com/svn/
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
function xbox(_para) {
	// constants
	this.XBOX_SHOW_TIME = 1000;
	this.XBOX_HIDE_TIME = 500;
	this.XBOX_PERF_ANIM = array_key_exists("anim",_para) && !_para["anim"] ? false : true;
	this.XBOX_DISP_ZIDX = 500;
	// properties
	this.para = _para;
	// methods
	this.init = xbox_init;
	this.show = xbox_show;
	this.hide = xbox_hide;
	this.visi = xbox_visi;
	this.jqbr = xbox_jqbr;
	this.bttn = {};
}
function xbox_init() {
	// prof
	{
		var _obj = this;
		var _prop = {};
		if(this.para["prof"]) {
			switch(parseInt(this.para["prof"])) {
			 case 1 :
			 	_prop = {
					"effe" :
						{
							"show" : "drop",
							"sopt" : {"direction":"up"},
							"hide" : "drop",
							"hopt" : {"direction":"down"}
						},
					"styl" :
						{
							"xbox" : {"background":"#e2e2e2","color":"#63665e","border":"2px solid #000000"},
							"titl" : {"padding":"2 4 2 4"}
						}
				};
				break;
			}
			jQuery.each(_prop,
				function(_k,_v) {
					if(!array_key_exists(_k,_obj.para))
						_obj.para[_k] = _v;
				}
			);
		}
	}
	return this;
}
function xbox_show(_para) {
	var _obj = this;
	// cont
	{
		if(_para["cont"]) {
			this.para["posi"] = 
				{
					"left"	: _para["cont"].offset()["left"] + _para["cont"].width() / 2 - (jQuery.sizr(_para["body"])["w"] / 2) - 1,
					"topx"	: _para["cont"].offset()["top"] + _para["cont"].height() / 2 - ((jQuery.sizr(_para["body"])["h"] + (array_key_exists("bttn",_para) ? 28 : 0)) / 2)
				}
		}
	}
	// cntr
	{
		if(this.para["cntr"]) {
			this.para["posi"] = {
				"left" : document.body.clientWidth / 2 - (jQuery.sizr(_para["body"])["w"] / 2),
				"topx" : $("body").attr("scrollTop") + (document.body.clientHeight / 2 - ((jQuery.sizr(_para["body"])["h"] + (array_key_exists("bttn",_para) ? 28 : 0)) / 2))
			}
		}
	}
	// modl
	{
		if(this.para["modl"]) {
			$("body").append('<div xmco="xbox" name="' + this.para["name"] + '" bran="modl" style="background-image:url(' + jQuery.ximg("gene","tran.png",true) + ');position:absolute;left:' + this.para["modl"].offset()["left"] + 'px;top:' + this.para["modl"].offset()["top"] + 'px;width:' + this.para["modl"].width() + 'px;height:' + this.para["modl"].height() + 'px;cursor:not-allowed;z-index:' + (this.XBOX_DISP_ZIDX - 1) + '"></div>');
		}
	}
	$("body").append('\
		<div xmco="xbox" name="' + this.para["name"] + '" bran="xbox" style="position:absolute;display:none;left:' + this.para["posi"]["left"] + 'px;top:' + this.para["posi"]["topx"] + 'px;' + jQuery.kase(this.para["styl"]["xbox"],":",";") + ';z-index:' + this.XBOX_DISP_ZIDX + '">' +
			(jQuery.trim(_para["titl"]) ? 
				'<div xmco="xbox" name="' + this.para["name"] + '" bran="titl" style="' + jQuery.kase(this.para["styl"]["titl"],":",";") + '">' + _para["titl"] + '</div>' : "") + '\
			<div xmco="xbox" name="' + this.para["name"] + '" bran="body">' + _para["body"] + '</div>\
		</div>'
	);
	this.jqbr("xbox").show(this.para["effe"]["show"],this.para["effe"]["sopt"],this.XBOX_PERF_ANIM && !array_key_exists("iopn",_para) ? this.XBOX_SHOW_TIME : 0,
		function() {
			if(_obj.para["clbk"] && _obj.para["clbk"]["asho"])
				_obj.para["clbk"]["asho"](_obj);
		}
	);
	if(_obj.para["clbk"] && _obj.para["clbk"]["dsho"])
		_obj.para["clbk"]["dsho"](this);
	// bttn
	{
		if(array_key_exists("bttn",_para)) {
			var _xml = "";
			_xml += '\
				<div style="padding:4 0 4 0">\
				<center>\
					<table cellspacing="0" cellpadding="0">\
						<tr>';
						jQuery.each(_para["bttn"],
							function(_bidx,_bdat) {
								_xml += '	<td id="xbox_bttn_' + _obj.para["name"] + '_' + _bidx + '" bttn="' + _bidx + '" style="padding:0 1 0 1">test</td>';
							}
						);
				_xml += '\
						</tr>\
					</table>\
				</center>\
				</div>';
			_obj.jqbr("xbox").append(_xml);
			$("[id^='xbox_bttn_" + _obj.para["name"] + "']").each(
				function() {
					var _bttn;
					var _prop = {
						"subj" : $("#xbox_bttn_" + _obj.para["name"] + "_" + (_bttn = $(this).attr("bttn"))),
						"labl" : _para["bttn"][_bttn]["name"],
						"prof" : _para["bttn"][_bttn]["prof"],
						"xnam" : _para["bttn"][_bttn]["xnam"]
					};
					if(array_key_exists("c_ck",_para["bttn"][_bttn]))
						_prop["c_ck"] = function() { _para["bttn"][_bttn]["c_ck"](_obj) };
					(_obj.bttn[$(this).attr("bttn")] = new xmpb(_prop)).init().draw();
				}
			);
		}
	}
}
function xbox_hide(_para) {
	var _obj = this;
	if(this.para["clbk"] && this.para["clbk"]["bhid"])
		this.para["clbk"]["bhid"](this);
	this.jqbr("xbox").hide(this.para["effe"]["hide"],this.para["effe"]["hopt"],array_key_exists("time",_para) ? _para["time"] : (this.XBOX_PERF_ANIM ? this.XBOX_HIDE_TIME : 0),
		function() {
			_obj.jqbr("xbox").add(_obj.jqbr("modl")).remove();
			if(_para && _para["ahid"])
				_para["ahid"]();
		}
	);
}
function xbox_visi() {
	return this.jqbr("xbox").size() != 0;
}
function xbox_jqbr(_bran) {
	return $("[xmco='xbox'][name='" + this.para["name"] + "'][bran='" + _bran + "']");
}
