

// modal_dialog.js
// modal dialogs
// copyright xThink 2007
// This code is the property of xThink.



var ModalDialog = Class.create();

ModalDialog.prototype = {
  
  initialize: function(id, className) {
  	// create an overlay object
  	this.overlay = new Overlay();
  	// create a div element and add it to the body
  	this.dialog = document.createElement("div");
  	Element.extend(this.dialog);
  	this.dialog.setAttribute("id", id);
  	this.dialog.className = "modalDialog " + className;
  	this.dialog.setStyle({opacity: 1});
  	this.dialog.hide();
  	
  	var bodyEl = document.getElementsByTagName('body').item(0);
  	bodyEl.appendChild(this.dialog);
  	this.center();
  },
  
  
  setContent: function(html) {
  	this.dialog.update(html)
  },
  
  center: function() {
  	var size = this.dialog.getDimensions();
		var width = size.width;
    var height = size.height;
    var scrollTop = Browser.getScrollTop();
		var top = (Browser.getClientHeight()-height)/2 + Browser.getScrollTop() + scrollTop;
    var left = (Browser.getClientWidth()-width)/2 + Browser.getScrollLeft();
    this.dialog.setStyle({top: top/2 + "px", left: left + "px", width: width + "px"});
	},
	
  show: function() {
  	this.overlay.show();
  	this.dialog.show();
	},
	
	hide: function() {
		this.dialog.hide()
		this.overlay.hide();
	}
	
}
	
  
// overlay


var Overlay = Class.create();

Overlay.prototype = {

  initialize: function() {
  	// create a div element and add it to the body
  	this.overlayElement = $('bodyOverlay');
		if (!this.overlayElement) {
			this.overlayElement = document.createElement('div');
			Element.extend(this.overlayElement);
      this.overlayElement.setAttribute('id','bodyOverlay');
      this.overlayElement.style.display = 'none';
      this.overlayElement.style.position = 'absolute';
      this.overlayElement.style.background = '#000';
      this.overlayElement.style.top = '0';
      this.overlayElement.style.left = '0';
      var bodyEl = document.getElementsByTagName('body').item(0);
      bodyEl.appendChild(this.overlayElement);
		}
		this.initDimensions();
		this.initOpacity(0.5);
	},
	
	initDimensions: function() {
		var width = Math.max(Browser.getDocumentWidth(), Browser.getClientWidth());
		var height = Math.max(Browser.getDocumentHeight(), Browser.getClientHeight());
		this.overlayElement.style.width = width + "px";
    this.overlayElement.style.height = height + "px";
	},
	
	
	
	initOpacity: function(opacity) {
  	var overlayElement = this.overlayElement;
  	overlayElement.setStyle({ opacity: opacity });
  	/*
		if (opacity == 1) {
			overlayElement.setStyle({ opacity: (/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ?  0.999999 : null });
			if (/MSIE/.test(navigator.userAgent)) {
      	overlayElement.setStyle({filter: overlayElement.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'')});
      }
  	} else {  
    	if (opacity < 0.00001) opacity = 0;  
			overlayElement.setStyle({opacity: opacity});
			if (/MSIE/.test(navigator.userAgent)) {
				overlayElement.setStyle({ filter: overlayElement.getStyle('filter').replace(/alpha\([^\)]*\)/gi,'') + 'alpha(opacity=' + opacity*100 + ')' });
			}
		}
		*/
	},
	
	show: function() {
		this.overlayElement.show();
	},
	
	hide: function() {
		this.overlayElement.hide();
	}

}

var Browser = {
};

Browser.getDocumentWidth = function() {
	return (document.documentElement && document.documentElement.scrollWidth) || document.body.scrollWidth;
}

Browser.getDocumentHeight = function(){
	return(document.documentElement&&document.documentElement.scrollHeight)||document.body.scrollHeight;
}

Browser.getClientWidth = function(){
	return (window.innerWidth || (document.documentElement && document.documentElement.clientWidth) || (document.body && document.body.clientWidth) || 0);
}

Browser.getClientHeight = function(){
	return(window.innerHeight || (document.documentElement && document.documentElement.clientHeight) || (document.body && document.body.clientHeight) || 0);
}

Browser.getScrollTop=function(){
	return(document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop) || 0;
}

Browser.getScrollLeft=function(){
	return(document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft) || 0;
}

