function pageWidth() {
	return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}
function pageHeight() {
	return window.innerHeight != null ? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null ? document.body.clientHeight:null;
}
function posLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft?document.body.scrollLeft:0;
}
function posTop() {
	return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;
}
function scrollFix(){
	$('transBG').style.top = posTop()+'px';
	$('transBG').style.left = posLeft()+'px';
	var divs = $('modalBox').getElementsByTagName('div');
	var content = divs[0];
	if (content)
	{
		var tp = posTop() + ((pageHeight() - content.offsetHeight) / 2) - 12;
		var lt = posLeft() + ((pageWidth() - content.offsetWidth) / 2) - 12;
		$('modalBox').style.top = (tp<0 ? 0 : tp) + 'px';
		$('modalBox').style.left = (lt<0 ? 0 : lt) + 'px';
	}
}
function sizeFix(){
	$('transBG').style.height=pageHeight()+'px';
	$('transBG').style.width=pageWidth()+'px';
}
function specVis(visiblity){
	tag = document.getElementsByTagName('select');
	for(i=tag.length-1;i>=0;i--) tag[i].style.visibility = visiblity;
	if (visiblity=='hidden' ){	
		tag = $$('#modalBox select');
		for(i=tag.length-1;i>=0;i--) tag[i].style.visibility = "visible";
	}
	tag = document.getElementsByTagName('iframe');
	for(i=tag.length-1;i>=0;i--) tag[i].style.visibility = visiblity;
	tag = document.getElementsByTagName('object');
	for(i=tag.length-1;i>=0;i--) tag[i].style.visibility = visiblity;
}
function showModal(src){
	window.onscroll = scrollFix; 
	window.onresize = sizeFix;
	$('modalBox').innerHTML = $(src).innerHTML;
	$('transBG').style.height = pageHeight() + 'px';
	$('transBG').style.width = pageWidth() + 'px';
	$('transBG').style.top = posTop() + 'px';
	$('transBG').style.left = posLeft() + 'px';
	$('transBG').style.display = 'block';
	$('modalBox').style.top = '0px';
	$('modalBox').style.left = '-10000px';
	specVis('hidden');
	var divs = $('modalBox').getElementsByTagName('div');
	var content = divs[0];
	content.style.display = 'block'; 
	$('modalBox').style.display = 'block';
	var tp = posTop() + ((pageHeight() - content.offsetHeight) / 2) - 12;
	var lt = posLeft() + ((pageWidth() - content.offsetWidth) / 2) - 12;
	
	Event.observe(document, 'keypress', function(event){ return  mdivhandler(event)});
 	$('modalBox').style.width = content.offsetWidth + 'px';
	$('modalBox').style.height = content.offsetHeight + 'px';
	$('modalBox').style.top = (tp<0 ? 0 : tp) + 'px';
	$('modalBox').style.left = (lt<0 ? 0 : lt) + 'px';

	return false;
}
function hideModal(){
	window.onscroll = null; 
	window.onresize = null;
	$('transBG').style.display = 'none';
	$('modalBox').style.display = 'none';
	specVis('visible');
	document.onkeypress='';
	return false;
}
function initModal(){
	var Body = document.getElementsByTagName('body')[0];
	var TransBGFragment = document.createDocumentFragment();
	var TransBG = document.createElement('div');
	var ModalBox = document.createElement('div');
	ModalBox.setAttribute('id','modalBox');
	ModalBox.className = 'modalbox';
	TransBG.setAttribute('id','transBG');
	TransBG.className = 'transbg';
	TransBGFragment.appendChild(TransBG);
	TransBGFragment.insertBefore(ModalBox,TransBG.nextSibling);
	Body.insertBefore(TransBGFragment,Body.firstChild);
}

window.onload = initModal;              

function mdivhandler(e){
	e = (!e) ? window.event : e;
	var target = (!e.target) ? e.srcElement : e.target;
	if (target.nodeType == 3) target = target.parentNode;
	code = (e.charCode) ? e.charCode :
       ((e.keyCode) ? e.keyCode :
       ((e.which) ? e.which : 0));
  // check to see if the event was keyup
  	if (e.type == "keyup") isKeyUpDownPressed =false;
	if (code ==27)
	{
		hideModal();
	}
}
