/**
 * @author      Jimmy Pellitteri
 * @base		base on http://roland.devarea.nl/work/dialog/
 * @since		20/12/2010
 * @version     1.0 (28/12/2010)
 * TODO : 		- Confirm box
 **/
var Dialog = Class.create({
	content:null,
	contentDefault:null,
	className:'dialog-error',
	classNameDefault:null,
	_inserted:false,
	_exec:false,
	_open:false,
	_elements:{
		overlay:['div', 'dialog-overlay'],
		container:['div', 'dialog-container', 'dialog-base'],
		close:['div', 'dialog-close'],
	    content:['div', 'dialog-content']
	},
	initialize:function(content, className){
		this.contentDefault = this.content = content;
		this.classNameDefault = this.className = className || this.className;
		this.load();
		this.attachEvents();
	},
	Browser:{
		IE6:(Prototype.Browser.IE && parseInt(navigator.appVersion) == 4 && navigator.userAgent.toLowerCase().indexOf('msie 6.') != -1),
		IE8:(Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 8)
	},
	load:function(){
		if(!!Dialog.prototype._exec) return;
		Dialog.prototype._exec = true;
		var e = this._elements;
		for(var x in e){
			var d = e[x],
			    a = {style:'display:none'};
			if(d[1]) a['id'] = d[1];
			//~ if(d[2]) a['className'] = d[2];
			switch(d[0]){
				case 'a': a['href'] = 'javascript:;'; break;
			}
			var el = new Element(d[0], a);
			if(d[2]) {
				el.addClassName(d[2]);
			}
			this._elements[x] = el;
		}
		this._insertElements();
	},
	_insertElements: function(){
		if(!!Dialog.prototype._inserted) return;
		Dialog.prototype._inserted == true;
		var elems = this._elements;
			$(document.body)
			.insert({top: elems['overlay']})
			.insert({top: elems['container'].insert(elems['close']).insert(elems['content'])});
	},
	view:function(){
		var view = document.viewport,
		    dim  = view.getDimensions(),
			data = {width:dim.width, height:dim.height};
		if(this.Browser.IE6){
			var scroll = view.getScrollOffsets();
			data.top  = scroll.top;
			data.left = scroll.left;
		}
		return data;
	},
	elem:function(elem){
		return this._elements[elem];
	},
	attachEvents:function(){
		Event.observe(window, 'resize', this.setDimensions.bindAsEventListener(this));
		if(this.Browser.IE6){
			Event.observe(window, 'scroll', this.setScroll.bindAsEventListener(this));
		}
		this.elem('close').observe('click', function(){
			this.close();
		}.bindAsEventListener(this));
		this.elem('overlay').observe('click', function(){
			this.close();
		}.bindAsEventListener(this));
	},
	setDimensions:function(){
		if(this.Browser.IE6){
			this.elem('overlay').setStyle('width:'+this.view().width+'px;height:'+this.view().height+'px');
			this.setScroll();
		}
	},
	setScroll:function(){
		var v = this.view(),
			c = this.elem('container'),
			d = c.getDimensions(),
			t = v.top + parseInt((v.height - d.height) / 2),
			l = v.left + parseInt((v.width - d.width) / 2);
		this.elem('overlay').setStyle('margin:'+v.top+'px 0 0 '+v.left+'px');
	},
	setContent:function(){
		if(Object.isElement(this.content)){
			this.content.show();
		}
		this.elem('content').update(this.content);
	    this.setDimensions();
	},
	open:function(content, className){
		this.content = content || this.contentDefault;
		var oldClassName = this.className;
		this.className = className || this.classNameDefault;
		if(this.Browser.IE6){
			$$('select').select(function(el){ return el.visible(); }).invoke('hide').invoke('addClassName', 'dialog-hideselect');
		}	
		var o = this.elem('overlay'), c = this.elem('container'), t = this.elem('close'), v = this.elem('content');
		[o, c, t, v].invoke('show');
		o.setOpacity(0.75);
		c.removeClassName(oldClassName);
		c.addClassName(this.className);
		this._open = true;
		this.setContent();
	},
	close:function(){
		for(var x in this._elements) this._elements[x].writeAttribute('style', 'display:none');
		if(this.Browser.IE6){
			$$('select.dialog-hideselect').invoke('show').invoke('removeClassName', 'dialog-hideselect');
		}
		this._open = false;
	}
});
