var Dialog = {};
Dialog.Box = Class.create();
Object.extend(Dialog.Box.prototype, {
	initialize: function(id) {
		this.createOverlay();

		this.dialog_box = $(id);
		this.dialog_box.show = this.show.bind(this);
		this.dialog_box.hide = this.hide.bind(this);

		this.parent_element = this.dialog_box.parentNode;

		this.dialog_box.style.position = "absolute";

		var e_dims = Element.getDimensions(this.dialog_box);
		var b_dims = Element.getDimensions(this.overlay);
		this.dialog_box.style.left = ((b_dims.width/2) - (e_dims.width/2)) + 'px';
		
		this.dialog_box.style.top = "0px";
		this.dialog_box.style.zIndex = this.overlay.style.zIndex + 1;

	},

	createOverlay: function() {
		if($('dialog_overlay')) {
			this.overlay = $('dialog_overlay');
		} else {
			this.overlay = document.createElement('div');
			this.overlay.id = 'dialog_overlay';
			Object.extend(this.overlay.style, {
				position: 'absolute',
				top: 0,
				left: 0,
				zIndex: 90,
				width: '100%',
				backgroundColor: '#000',
				filter: 'alpha(opacity=60)',
				mozOpacity: '.60',
				opacity: '.60',
				display: 'none'
			});
			document.body.insertBefore(this.overlay, document.body.childNodes[0]);
		}
	},

	moveDialogBox: function(where) {
		Element.remove(this.dialog_box);
		if(where == 'back')
		this.dialog_box = this.parent_element.appendChild(this.dialog_box);
		else
		this.dialog_box = this.overlay.parentNode.insertBefore(this.dialog_box, this.overlay);
	},

	show: function() {
		this.overlay.style.height = Element.getDimensions(document.body).height+'px';
		this.moveDialogBox('out');
		//this.overlay.onclick = this.hide.bind(this);
		this.selectBoxes('hide');
		this.overlay.show();
		
		var xyPoint = this.getXYPositions();
		var whPoint = this.getWidthHeight();
		var dialog_dims = Element.getDimensions(this.dialog_box);
				
		this.dialog_box.style.top = (xyPoint[1] + (whPoint[1]/2) - (dialog_dims.height/2)) + 'px';
		this.dialog_box.style.left = ((whPoint[0]/2) - (dialog_dims.width/2)) + 'px';
		this.dialog_box.style.display = '';
	},

	hide: function() {
		this.selectBoxes('show');
		this.overlay.hide();
		this.dialog_box.style.display = 'none';
		this.moveDialogBox('back');
		$A(this.dialog_box.getElementsByTagName('input')).each(function(e){if(e.type!='submit')e.value=''});
	},

	selectBoxes: function(what) {
		$A(document.getElementsByTagName('select')).each(function(select) {
			Element[what](select);
		});

		if(what == 'hide')
		$A(this.dialog_box.getElementsByTagName('select')).each(function(select){Element.show(select)})
	},
	
		/**
		Return an array of size 2 with position 0 = x and position 1 = y
	**/
	getXYPositions: function(){  
		  var point = new Array(2);
		  if( typeof( window.pageYOffset ) == 'number' ) {
		    //Netscape compliant
		    point[1] = window.pageYOffset;
		    point[0] = window.pageXOffset;
		  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		    //DOM compliant
		    point[1] = document.body.scrollTop;
		    point[0] = document.body.scrollLeft;
		  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		    //IE6 standards compliant mode
		    point[1] = document.documentElement.scrollTop;
		    point[0] = document.documentElement.scrollLeft;
		  } else{
		  	point[1] = 0;
		  	point[0] = 0;
		  }
		  
		  return point;
	},
		/**
		Return an array of size 2 with position 0 = width and position 1 = height
	**/
	getWidthHeight: function() {
		var wh = new Array(2);
		  if( typeof( window.innerWidth ) == 'number' ) {
		    //Non-IE
		    wh[0] = window.innerWidth;
		    wh[1] = window.innerHeight;
		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
		    wh[0] = document.documentElement.clientWidth;
		    wh[1] = document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    //IE 4 compatible
		    wh[0] = document.body.clientWidth;
		    wh[1] = document.body.clientHeight;
		  }
	  
	 	return wh;
	}
});