/*

	Allows modal window in IE and as close as possible in Moz

	Include this file in parent and dialouge box

*/

function modalWindow(width,height){
	var id = modalWindow.stack.push(this) - 1
	var win
	var args
	var FlagResize = true

	this._onclose = function(value){
		window.onfocus = function(){  }
		modalWindow.current = false
		this.onclose(value)
	}
	this.onclose = function(value){}
	this.getDialogArguments = function(){ return args }
	this.resize = function(win){
		if( FlagResize ){
			FlagResize = false
			win.innerHeight = win.innerHeight - (win.outerHeight - win.innerHeight)
		}
	}
	this.open = function(url,_args,override){
		try{
			FlagResize = true
			args = _args
			if(!window.showModalDialog || override){
				var LeftPosition=(screen.width-width)/2
				var TopPosition=(screen.height-height)/2
				
				// WEIRD MOZ BUG ON RELATIVE LOCATION? ?!?!
				if(url.indexOf('://') == -1 && url.charAt(0) != '/'  && url.charAt(0) != '.'){
					var base = String(window.location)
					base = base.substring(0, base.lastIndexOf('/')+1 )
					url = base + url
				}	
				
				modalWindow.current = win = window.open(url,'ModalWindow_'+ id +'_'+ new Date().valueOf(),'top='+TopPosition+',left='+LeftPosition+',width='+(width || 300)+',height='+ (height||180) +',modal,dialog')
				window.onfocus = function(){ modalWindow.current.focus() }
			}else{
				// Ahh some thing IE is better at then Moz
				var rv = window.showModalDialog( url , args , "dialogWidth:"+(width||300)+"px; dialogHeight: "+(height||180)+"px; status: no; help:  no")
				this.onclose( rv )
			}
		}catch(e){ alert("A window has been blocked by your popup blocker\n\nPlease allow popups for this site (" + window.location.toString().split('/').slice(2,3).join('/')+")" ) }
	}
}
modalWindow.stack = []
modalWindow.current = false

// Set up modal window features for Moz
if(window.opener && window.name.split('_')[0] == 'ModalWindow'){
	if(!window.returnValue) window.returnValue = false;
	var _creator = window.opener.modalWindow.stack[ window.name.split('_')[1] ]
	dialogArguments = _creator.getDialogArguments()
	window.onunload = function(){ _creator._onclose(returnValue) }
	_creator.resize( window )

	// Moz stop selection
	document.onmousedown = function(e){
		var el = e.target
		if(el.tagName != 'INPUT' && el.tagName != 'TEXTAREA' && el.tagName != 'SELECT' && el.tagName != 'BUTTON')
			return false
		return true
	}
	document.oncontextmenu = function(e){
		var el = e.target
		if((el.tagName == 'INPUT' && el.type == 'text') || el.tagName == 'TEXTAREA' || el.tagName == 'SELECT')
			return true
		return false
	}
}

