/**
 *	@author sonny.cabali
 *	@version 1.0
 *	@email: sonny.cabali@gmail.com
 *
 *	Link2Support Dialog
 *	Dialog wrapper based on Windows Prototype
 *
 *	Copyright (c) 2007
 *
 *	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.
 */

var L2SDialog = Class.create();
L2SDialog.prototype = {

	/**
	 * default width and height
	 *
	 */
	_width: 400,
	_height: 200,

	/**
	 * default Ok and Cancel Label
	 *
	 */
	_okLabel: "Yes",
	_cancelLabel: "No",

	/**
	 * default button class
	 *
	 */
	_className: "alphacube",

	/**
	 * default timeout for info
	 * 5 secs
	 */
	_timeOut: null,

	/**
	 * funciton initialize
	 * @param Object data
	 *
	 * Constructor
	 */
	initialize: function(data){
		/*
		 * set content
		 */
		if(typeof data.content != "undefined"){
			this._content = data.content;
		}
		else{
			this._content = "";
		}

		/*
		 * set top position
		 */
		if(typeof data.top != "undefined"){
			this._top = data.top;
		}
		else{
			this._top = null;
		}

		/*
		 * set left position
		 */
		if(typeof data.left != "undefined"){
			this._left = data.left;
		}
		else{
			this._left = null;
		}

		/*
		 * set the dialog width
		 */
		if(typeof data.width != "undefined"){
			this._width = data.width;
		}

		if(typeof data.height != "undefined"){
			this._height = data.height;
		}

		/*
		 * set Ok button Label
		 */
		if(typeof data.okLabel != "undefined"){
			this._okLabel = data.okLabel;
		}

		/*
		 * set Cancel Button Label
		 */
		if(typeof data.cancelLabel != "undefined"){
			this._cancelLabel = data.cancelLabel;
		}

		/*
		 * set button class
		 */
		if(typeof data.className != "undefined"){
			this._className = data.className;
		}

		/*
		 * set Ok Event
		 */
		if(typeof data.onOk != "undefined"){
			this._onOk = data.onOk;
		}
		else{
			this._onOk = null;
		}

		/*
		 * set Cancel Event
		 */
		if(typeof data.onCancel != "undefined"){
			this._onCancel = data.onCancel;
		}
		else{
			this._onCancel = null;
		}

		/*
		 * set Progress bar
		 */
		if(typeof data.showProgress != "undefined"){
			this._showProgress = data.showProgress;
		}
		else{
			this._showProgress = true;
		}

		/*
		 * set timeout for info message
		 */
		if(typeof data.timeout != "undefined"){
			this._timeOut = data.timeout;
		}
	},

	/**
	 * function Alert
	 *
	 * Opens window message with one button
	 */
	Alert: function(){
		/*
		 * construct the parameter
		 */
		var param = "{ width: "+this._width+", height: "+this._height+
			", className: \""+this._className+"\", okLabel: \""+this._okLabel+"\""+
			"_PARAM_}";

		/*
		 * set the position
		 */
		var _param = new Array();
		if(this._top != null)
			_param.push("top: "+this._top);
		if(this._left != null)
			_param.push("left: "+this._left);
		if(this._onOk != null)
			_param.push("onOk: "+this._onOk);

		/*
		 * join the parameters
		 */
		if(_param.length > 0)
			param = param.gsub("_PARAM_", ", "+_param.join(", "));
		else
			param = param.gsub("_PARAM_", "");

		if(this._content instanceof Object)
			eval("Dialog.alert("+Object.toJSON((this._content))+","+param+");");
		else
			eval("Dialog.alert("+(this._content).inspect()+","+param+");");
	},

	/**
	 * funciton Confirm
	 *
	 * Opens window message with two button
	 */
	Confirm: function(){
		/*
		 * construct the parameter
		 */
		var param = "{ width: "+this._width+", height: "+this._height+
			", className: \""+this._className+"\", okLabel: \""+this._okLabel+"\", "+
			"cancelLabel: \""+this._cancelLabel+"\"_PARAM_}";

		/*
		 * set the position
		 */
		var _param = new Array();
		if(this._top != null)
			_param.push("top: "+this._top);
		if(this._left != null)
			_param.push("left: "+this._left);
		if(this._onOk != null)
			_param.push("onOk: "+this._onOk);
		if(this._onCancel != null)
			_param.push("onCancel: "+this._onCancel);

		/*
		 * join the parameters
		 */
		if(_param.length > 0)
			param = param.gsub("_PARAM_", ", "+_param.join(", "));
		else
			param = param.gsub("_PARAM_", "");

		if(this._content instanceof Object)
			eval("Dialog.confirm("+Object.toJSON((this._content))+","+param+");");
		else
			eval("Dialog.confirm("+(this._content).inspect()+","+param+");");
	},

	/**
	 * function Info
	 *
	 * Opens dialog box with no button
	 */
	Info: function(){
		/*
		 * construct the parameter
		 */
		var param = "{ width: "+this._width+", height: "+this._height+
			", className: \""+this._className+"\", showProgress: \"true\""+
			"_PARAM_}";

		/*
		 * set the position
		 */
		var _param = new Array();
		if(this._top != null)
			_param.push("top: "+this._top);
		if(this._left != null)
			_param.push("left: "+this._left);

		/*
		 * join the parameters
		 */
		if(_param.length > 0)
			param = param.gsub("_PARAM_", ", "+_param.join(", "));
		else
			param = param.gsub("_PARAM_", "");

		if(this._content instanceof Object)
			eval("Dialog.info("+Object.toJSON((this._content))+","+param+");");
		else
			eval("Dialog.info("+(this._content).inspect()+","+param+");");

		if(this._timeOut !=  null)
			setTimeout(Dialog.closeInfo(), this._timeOut);
	},

	/**
	 * function close
	 *
	 * Close the current dialog
	 */
	close: function(){
		Dialog.closeInfo();
	},

	/**
	 * function closeAll
	 *
	 * Close all the dialog
	 */
	closeAll: function(){
		Windows.closeAll();
	},

	/**
	 * function setCancelLabel
	 * @param String label
	 *
	 * Set the label of the cancel button
	 */
	setCancelLabel: function(label){
		this._cancelLabel = label;
	},

	/**
	 * function setClassName
	 * @param String classname
	 *
	 * Set the Class name
	 */
	setClassName: function(classname){
		this._className = classname;
	},

	/**
	 * function setContent
	 * @param String content
	 *
	 * Set the content of the dialog
	 */
	setContent: function(content){
		this._content = content;
	},

	/**
	 * function setLeft
	 * @param Integer left
	 *
	 * Set the horizontal position of the dialog
	 */
	setLeft: function(left){
		this._left = left;
	},

	/**
	 * function setOkLabel
	 * @param String label
	 *
	 * Set the String
	 */
	setOkLabel: function(label){
		this._okLabel = label;
	},

	/**
	 * function setOnCancelEvent
	 * @param Object evnt
	 *
	 * Set the Cancel Button Event
	 */
	setOnCancelEvent: function(evnt){
		this._onCancel = evnt;
	},

	/**
	 * function StOnOkEvent
	 * @param Object evnt
	 *
	 * Set the Ok Button Event
	 */
	setOnOkEvent: function(evnt){
		this._onOk = evnt;
	},

	/**
	 * function setTop
	 * @param Integer top
	 *
	 * Set the vertical position of the dialog
	 */
	setTop: function(top){
		this._top = top;
	},

	/**
	 * function setInfoMessage
	 * @param String message
	 *
	 * Set the message in the dialog
	 */
	setInfoMessage: function(message){
		Dialog.setInfoMessage(message);
	},

	/**
	 * function showLoading
	 *
	 * create a loading screen
	 */
	 showLoading: function(data){

		 var _content = "Processing...";
		 var _height = 60;
		 var _width = 180;

		 if(typeof data != "undefined"){
			if(typeof data.content != "undefined" && typeof data.content == "string")
				_content = data.content;
			if(typeof data.width != "undefined" && typeof data.width == "number")
				_width = data.width;
			if(typeof data.height != "undefined" && typeof data.height == "number")
				_height = data.height;
		 }

		 (new L2SDialog({content: _content, showProgress: true, width: _width, height: _height})).Info();
	 },

	/**
	 * function showProgress
	 * @param Boolean bool
	 *
	 * Toggles the progress indicator
	 */
	showProgress: function(bool){
		this._showProgress = bool;
	}
};
L2SDialog.showLoading = function(data){
		 L2SDialog.prototype.showLoading(data);
};
L2SDialog.close = function(){
		L2SDialog.prototype.close();
};
L2SDialog.closeAll = function(){
		L2SDialog.prototype.closeAll();
};
