var Dialog = {};
Dialog.Box = Class.create();
Object.extend(Dialog.Box.prototype, {
  initialize: function(pcMessage) {

		$$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'hidden' });
		 
		if(!document.getElementById("msgbox")) var boxDiv = document.createElement("div");
		else boxDiv = document.getElementById("msgbox");
		boxDiv.id = "msgbox";
		
		//var imgDiv = document.createElement("div");
		//var msgDiv = document.createElement("div");
		//if(!document.getElementById("diagimage")) { 
		//	var boxImg = document.createElement("img");
		//	boxImg.id = "diagimage";
		//	imgDiv.appendChild(boxImg);
		//} 
		//else {
		//  var boxImg = document.getElementById("diagimage");
		//}
		//if(!document.getElementById("diagmessage")) { 
		//	var boxText = document.createTextNode(pcMessage);
		//	boxText.id = "diagmessage";
		//	msgDiv.appendChild(boxText);
		//} 
		//else {
		//  var boxText = document.getElementById("diagmessage");
		//}		
		//imgDiv.id = "msgimg";
		//msgDiv.id = "pcMessage";
		//if (alerttype == "ERROR") boxImg.src = "img/dialog-error.png";
		//boxText = pcMessage;
		//msgDiv.appendChild(boxText);
		//boxDiv.appendChild(imgDiv);
		//boxDiv.appendChild(msgDiv);	
		
		boxDiv.innerHTML = pcMessage;
		
		if(!document.getElementById("msgbox")) { 
			document.body.appendChild(boxDiv);		
		}
		
				
		this.createOverlay();
		this.dialog_box = $("msgbox");
    this.dialog_box.show = this.show.bind(this);
    this.dialog_box.persistent_show = this.persistent_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 = '80px';
    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%',
				height: '500px',
      	backgroundColor: '#000',
      	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);
  },
	
  getPageSize: function() {
	        
	  var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	},	

  show: function() {
		var arrayPageSize = this.getPageSize();
		this.overlay.style.height = arrayPageSize[1] + 'px';		
    this.moveDialogBox('out');
    this.overlay.onclick = this.hide.bind(this);
    this.selectBoxes('hide');
    new Effect.Appear(this.overlay, {duration: 0.2, from: 0.0, to: 0.7});
    this.dialog_box.style.display = '';
  	this.dialog_box.style.left = '0px';
    var e_dims = Element.getDimensions(this.dialog_box);
  	this.dialog_box.style.left = ((this.winWidth()/2) - (e_dims.width)/2) + 'px';
  },
  
  persistent_show: function() {
    this.overlay.style.height = this.winHeight()+'px';
    this.moveDialogBox('out');
    this.selectBoxes('hide');
    new Effect.Appear(this.overlay, {duration: 0.1, from: 0.0, to: 0.3});
    this.dialog_box.style.display = '';
  	this.dialog_box.style.left = '0px';
    var e_dims = Element.getDimensions(this.dialog_box);
  	this.dialog_box.style.left = ((this.winWidth()/2) - (e_dims.width)/2) + 'px';
  },

  hide: function() {
  	this.selectBoxes('show');
    new Effect.Fade(this.overlay, {duration: 0.1});		
		this.dialog_box.style.display = 'none';
		this.moveDialogBox('back');
    $A(this.dialog_box.getElementsByTagName('input')).each(function(e){if(e.type!='submit')e.value=''});
    $$('select', 'object', 'embed').each(function(node){ node.style.visibility = 'visible' });          
  },

  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)})
  },

  winWidth: function() { return document.body.offsetWidth || window.innerWidth || document.documentElement.clientWidth || 0; },
  winHeight: function() { return document.body.offsetHeight || window.innerHeight || document.documentElement.clientHeight || 0; }

}); 


