/**
 * usage:  
 * 
 * var popup = new S100.Popup(parentElement, contentElement, options)
 * popup.show();
 * 
 * if the dialog is the first level dialog, use document.body as the parent. 
 * 
 * later to close the popup:
 * 
 * popup.hide()
 * 
 * 
 * @author bran
 */

if (!window.S100) 
	var S100 = {};
	
S100.Popup = Class.create();
S100.Popup.prototype = {
	initialize: function(parentPopup, div2Show, options) {
		this.parentPopup = parentPopup;
		this.div2Show = div2Show;
		
		this.options = Object.extend(
			{width: 500}, // default width
			options||{}
		);
		
		if (!document.getElementsByTagName){ return; }
		
		this.__overlayId = 'icer-inlinepopup-overlay' + "-" + div2Show.id;
		this.__shimId = this.__overlayId + "-shim";
		this.shimmed = (document.all && !window.opera)?true:false;
		this.__overlayIndex = 1978 + (+parentPopup.style.zIndex);

		if (typeof this.options.customCallBack == 'function')
			    this.customCallBack = options.customCallBack;

		if (!$(this.__overlayId)) {
			var overlay = document.createElement('div');
			overlay.setAttribute('id', this.__overlayId);
		//	overlay.style.cssText = 'position:absolute; top: 0; left: 0; display: hidden; width: 100%; height: 100%;background-color: #FFF; filter:alpha(opacity=70); -moz-opacity: 0.7; opacity: 0.7; z-index:' + __overlayIndex;
		// how about 100% transparent
			overlay.style.cssText = 'position:absolute; top: 0; left: 0; display: hidden; width: 100%; height: 100%;background-color: #FFF; filter:alpha(opacity=0); -moz-opacity: 0; opacity: 0; z-index:' + this.__overlayIndex;	
			
			
			//document.body.appendChild(overlay);
			this.parentPopup.appendChild(overlay);
			/**
			 * Uses an iframe shim to mask system controls for IE v5.5 or higher as suggested in
			 * see http://dotnetjunkies.com/weblog/jking/posts/488.aspx
			 */
			if (this.shimmed) {
				var shim = document.createElement('<iframe id="' + this.__shimId + '" src="about:blank" scrolling="no" frameborder="0" style="-moz-opacity:0;filter:alpha(opacity=0);opacity:0;position:absolute; top:0px; left:0px; display:none;z-index:' + (this.__overlayIndex-1) + '"></iframe>');
				//document.body.appendChild(shim);
				this.parentPopup.appendChild(shim);
			}
			
			/* register ESC keypress */
			Event.observe(document, 'keydown', function(e) {
				if (Event.KEY_ESC == e.keyCode) {
					this.hide();
				}
			}.bindAsEventListener(this), false);
		}
	},

//	initDialog : function(options) {
//		if (!this.dialog) {
//			var msgbox = document.createElement('div');
//			Element.addClassName(msgbox, 'icer-ip-msgbox');
//			dialog = this.decorate(msgbox, options);
//			dialog.msgbox = msgbox;
//			
//			var nav = document.createElement('div');
//			Element.addClassName(nav, 'icer-ip-nav');
//			dialog.body.appendChild(nav);
//			
//			var okBtn = document.createElement('button');
//			Element.addClassName(okBtn, 'icer-ip-dialog-ob');
//			nav.appendChild(okBtn);
//			dialog.okBtn = okBtn;			
//	
//			var cancelBtn = document.createElement('button');
//			Element.addClassName(cancelBtn, 'icer-ip-dialog-cb');			
//			nav.appendChild(cancelBtn);
//			dialog.cancelBtn = cancelBtn;
//
//			document.body.appendChild(dialog);
//		}
//	},
	
	/**
	 * wrap the content in a panel with header and optionally buttons
	 */
	decorate : function(ele, options) {
		var element = $(ele);
		if (!element['__ipopup']) {
			var	ipopup = document.createElement('div');
			ipopup.id = 'icer-ip-' + element.id;
			Element.addClassName(ipopup, 'icer-ip');
			if (options.popupClass)
				Element.addClassName(ipopup, options.popupClass);

			var head = document.createElement('div');
			Element.addClassName(head, 'icer-ip-head');

			var bar = document.createElement('div');
			Element.addClassName(bar, 'icer-ip-bar');

			if (options && options.closeButton)  {
				var closeBtn = document.createElement('span');
				closeBtn.className = 'icer-ip-closebtn';
				closeBtn.innerHTML = 'Close';
				bar.appendChild(closeBtn);				
				Event.observe(closeBtn, 'click', function() {this.hide();}.bindAsEventListener(this), false);
			}

			var body = document.createElement('div');
			ipopup.body = body;
			body.id = 'icer-ip-' + element.id + '-body';
			Element.addClassName(body, 'icer-ip-body');

			head.appendChild(bar);
			ipopup.appendChild(head);

			body.appendChild(element);
			ipopup.appendChild(body);
			document.body.appendChild(ipopup);
						
			element['__ipopup'] = ipopup;
		}
		return element['__ipopup'];
	},
	
	/**
	 * show the window
	 */
	show: function(){
		// no decoration!
//		this.ipopup = this.decorate(this.div2Show, this.options);
		this.ipopup = this.div2Show;

		var ws = this.getWinSize(window);
		var l, t;
		
		$(this.div2Show).style.display = 'block';
		this.ipopup.style.width = this.options.width + 'px';
		if (this.options.height)
		   this.ipopup.style.height = this.options.height + 'px';
		var dim = Element.getDimensions(this.ipopup);

		if (this.options.left)
			l = this.options.left;
		else if (this.options.right)
			l = screen.availWidth - this.options.right - dim.width;
		else 
			l = (ws[0] < dim.width)? 0 : (ws[0]-dim.width)/2 + ws[2];

		if (this.options.top)
			t = this.options.top;
		else
			t = (ws[1] < dim.height)? 0 : (ws[1]-dim.height)/2 + ws[3];
	
		this.ipopup.style.left = l + 'px';
		this.ipopup.style.top = t + 'px';
		this.ipopup.style.position = 'absolute';
		this.ipopup.style.zIndex = (+this.__overlayIndex) + 1;
		this.div2Show.style.zIndex = (+this.__overlayIndex) + 1;
		
		this.ipopup.style.display = 'block';
		
		if (this.options.onPopup) {
			this.options.onPopup.apply($(ele));
		}

	
		this._showOverlay();
		
		//this.rounded();		
	},

	_showOverlay : function() {

		var ps = this.getPageSize();
		
		with ($(this.__overlayId).style) {
			height = ps[1]+"px";
			display = "block";
		}

		if (this.shimmed) {
			with ($(this.__shimId).style) {
				width = ps[0] + "px";
				height = ps[1] + "px";
				display = "block";
			}
		}
	},

	hide : function() {
		Element.hide(this.ipopup);
		Element.hide($(this.__overlayId));
		if (this.shimmed)
			Element.hide($(this.__shimId));
	},
	
	
	submit : function() {
		this.hide();
		this.customCallBack();
	},

	
	getWinSize : function(w) {
		var w = !w ? window : w;
		if (w.innerWidth)
			return [w.innerWidth-16,w.innerHeight,w.pageXOffset,w.pageYOffset];
		else if (w.document.compatMode=='CSS1Compat')
			with (w.document.documentElement) return [clientWidth,clientHeight,scrollLeft,scrollTop];
		else
			with (w.document.body) return [clientWidth,clientHeight,scrollLeft,scrollTop];
	},
	
	getPageSize : function(w) {
		var w = !w ? window : w;
		if (w.innerHeight && w.scrollMaxY) {
			return [w.document.body.scrollWidth, w.innerHeight + w.scrollMaxY];
		} else if (w.document.body.scrollHeight > w.document.body.offsetHeight){ // all but Explorer Mac
			with (w.document.body) return [scrollWidth, scrollHeight];
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			with (w.document.body) return [offsetWidth, offsetHeight];
		}
	}
}

