/*
- custom content with border (auto id)
- custom content without border (auto id)
- ready content (in html) with border (content has id)
- ready content (in html) without border (content has id)
*/
var DefaultDialogBoxOptions = {
	type : 'custom-content',
	hasBorder : true,
	doEffect : false,
	hideOnOut : false,
	timeout : 1000,
	defaultBorder : true, /* border for content */
	id : '_TMP_'
};

var DefaultDialogBoxIDHelper = {
	idnum : 0,
	getid : function(id){this.idnum++;return id + this.idnum;}
}

var DialogBox = Class.create();
DialogBox.prototype = {
	initialize : function(options){
		this.options = Object.extend(Object.clone(DefaultDialogBoxOptions), options || {});
		this.content = null; //may exist or may not exist
		this.__box_id = null; //
		this.__content_id = null;
		this.timer = null; //timer.
		this._createDialog(); //create dialog
	},
	_createDialog : function(){
		var body = document.getElementsByTagName('body')[0];
		var template = null;
		if(this.options.hasBorder){
			template = this._getTemplateWithBorder();
		}else{
			template = this._getTemplateWithoutBorder();
		}
		
		if(this.options.type == 'custom-content'){
			this.id = DefaultDialogBoxIDHelper.getid(this.options.id);
		}else{
			this.id = this.options.id;
		}
			
		this.__box_id = 'dlg_' + this.id; //parent id. the biggest id :)
		this.__content_id = this.__box_id + '_c'; //will be used while updating content
		
		var templ = new Template(template);
		var opts = {'parent_id' : this.__box_id, 'content_id' : this.__content_id};		
		var html = templ.evaluate(opts);
		
		//Parent div is created on the fly ! **
		var parent_div = document.createElement('div');
		parent_div.id = this.__box_id;
		body.appendChild(parent_div);
		//***
		
		//Prepare parent div**********
		parent_div = $(this.__box_id);//ie has bug for "on the fly" created objects...
		parent_div.setStyle({position:'absolute', visibility : 'hidden'});
		if(this.options.defaultBorder){
			parent_div.setStyle({backgroundColor : '#fff', border : '1px solid #d7d7d7'});
		}
		parent_div.update(html);
		//End of Prepare parent div**********
		
		//Prepare content*********
		var content = $(this.__content_id);		
		if(this.options.type == 'ready-content'){
			content.appendChild($(this.id));
		}else if(this.options.type == 'custom-content'){
			var _tmpText = (this.content == null || typeof(this.content) == 'undefined') ? '' : this.content;
			content.update(_tmpText);
		}
		//End of Prepare content****
			
		if(this.options.hideOnOut){
			$(this.__box_id).observe('mouseover', this._onMouseOver.bind(this));
			$(this.__box_id).observe('mouseout', this._onMouseOut.bind(this));
		}
	},
	_getTemplateWithBorder : function(){
		var template = new Array; 
		template.push('<table><tr><td class="dlg_top_left">&nbsp;</td><td class="dlg_mid">&nbsp;</td><td class="dlg_top_right">&nbsp;</td></tr>');
		template.push('<tr><td class="dlg_mid">&nbsp;</td><td id="#{content_id}" class="dlg_content"></td><td class="dlg_mid">&nbsp;</td></tr>');
		template.push('<tr><td class="dlg_bot_left">&nbsp;</td><td class="dlg_mid">&nbsp;</td><td class="dlg_bot_right">&nbsp;</td></tr></table>');
		return template.join('');
	},
	_getTemplateWithoutBorder : function(){
		var template = '<div><div id="#{content_id}" class="dlg_content"></div></div>';
		return template;
	},
	_onMouseOver : function(){
		if(this.timer != null){
			clearTimeout(this.timer);
			this.timer = null;
		}
	},
	_onMouseOut : function(){
		this.timer = window.setTimeout(this.hide.bind(this), this.options.timeout);
	},
	hide : function(){
		$(this.__box_id).setStyle({visibility:'hidden'});
		//raise hide event...
		return this;
	},
	show : function(x, y){
		$(this.__box_id).setStyle({top : y + 'px', left : x + 'px', visibility: 'visible'});
		//raise shown event
		return this;
	},
	setContent : function(content){
		$(this.__content_id).update(content);
		return this;
	},
	setWidth : function(w){
		$(this.__box_id).setStyle({width : w + 'px'});
		return this;
	},
	setHeight : function(h){
		$(this.__box_id).setStyle({height : h + "px"});
		return this;
	}
}