// resource: http://luke.breuer.com/tutorial/javascript-modal-dialog.aspx
var _hideType = 'none'; // none | iframe | replace
var _rulesAdded = false;
var _mbox = false;

function $(id)
{
	return document.getElementById(id);
}

function sm(id,w,h,modal) // show modal
{
	if (modal==null) modal = true;
	_mbox = assertBodyIsParent(id);
	if (_mbox)
	{
		// make sure it's part of the 
		var bg = assertBodyIsParent('mBG');
		if (modal && bg)
		{
			bg.style.display='block';
			bg.style.zIndex=10001;
		}

		_mbox.style.display='block';
		_mbox.style.width=w+'px';
		_mbox.style.height=h+'px';
		_mbox.style.zIndex=10002;
	
		// special < IE7 -only processing for windowed elements, like select	
		if (window.XMLHttpRequest==null)
		{
			if (_hideType=='iframe') $('mIFrame').style.display='block';
			else if (_hideType=='replace') ReplaceSelectsWithSpans();
		}
	
		// call once to center everything
		OnWindowResize();
		
		if (window.attachEvent) window.attachEvent('onresize', OnWindowResize);
		else if (window.addEventListener) window.addEventListener('resize', OnWindowResize, false);
		else window.onresize = OnWindowResize;
		
		// we won't bother with using javascript in CSS to take care
		//   keeping the window centered
		if (document.all) document.documentElement.onscroll = OnWindowResize;
	} else {
		alert("Modal box not found with id: "+id);
	}
}

function assertBodyIsParent(id)
{
	var node = $(id);
	if (node)
	{
		if (document.body != node.parentNode)
		{
			node.parentNode.removeChild(node);
			document.body.appendChild(node);
		}
	}
	return node;
}

function hm() // hide modal
{
	if (_mbox)
	{
		_mbox.style.display = $('mBG').style.display = 'none';
		_mbox = false;
		
		// special IE-only processing for windowed elements, like select	
		if (document.all)
		{
			if (_hideType == 'iframe') $('mIFrame').style.display = 'none';
			else if (_hideType == 'replace') RemoveSelectSpans();
		}
		
		if (window.detachEvent) window.detachEvent('onresize', OnWindowResize);
		else if (window.removeEventListener) window.removeEventListener('resize', OnWindowResize, false);
		else window.onresize = null;
	}
}

function pageWidth() {return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}

function OnWindowResize()
{
	if (_mbox)
	{  
		var x = ((pageWidth()) / 2)- (_mbox.offsetWidth / 2);
		

		// we only need to move the dialog based on scroll position if
		// we're using a browser that doesn't support position: fixed, like < IE 7
		var left = window.XMLHttpRequest == null ? document.documentElement.scrollLeft : 0;
		var top = window.XMLHttpRequest == null ? document.documentElement.scrollTop : 0;
		
		_mbox.style.top = '140px';
		_mbox.style.left = x +'px';
	}
}

/* These functions deal with IE's retardedness in not allowing divs to 
 * cover select elements by replacing the select elements with spans. */
function RemoveSelectSpans()
{
	var selects = document.getElementsByTagName('select');
	
	for (var i = 0; i < selects.length; i++)
	{
		var select = selects[i];
		
		if (select.clientWidth == 0 || select.clientHeight == 0 || 
			select.nextSibling == null || select.nextSibling.className != 'selReplace')
		{continue;}
			
		select.parentNode.removeChild(select.nextSibling);
		select.style.display = select.cachedDisplay;
	}
}

function ReplaceSelectsWithSpans()
{
	var selects = document.getElementsByTagName('select');
	
	for (var i = 0; i < selects.length; i++)
	{
		var select = selects[i];
		
		if (select.clientWidth == 0 || select.clientHeight == 0 || 
			select.nextSibling == null || select.nextSibling.className == 'selReplace')
		{
			continue;
		}
			
		var span = document.createElement('span');
		
		// this would be "- 3", but for that appears to shift the block that contains the span 
		//   one pixel down; instead we tolerate the span being 1px shorter than the select
		span.style.height = (select.clientHeight - 4) + 'px';
		span.style.width = (select.clientWidth - 6) + 'px';
		span.style.display = 'inline-block';
		span.style.border = '1px solid rgb(200, 210, 230)';
		span.style.padding = '1px 0 0 4px';
		span.style.fontFamily = 'Arial';
		span.style.fontSize = 'smaller';
		span.style.position = 'relative';
		span.style.top = '1px';
		span.className = 'selReplace';
		
		span.innerHTML = select.options[select.selectedIndex].innerHTML + 
			'<img src="images/custom_drop.gif" alt="drop down" style="position: absolute; right: 1px; top: 1px;" />';
		
		select.cachedDisplay = select.style.display;
		select.style.display = 'none';
		select.parentNode.insertBefore(span, select.nextSibling);
	}
}

function GetWinW()
{
	var width =
		document.documentElement && document.documentElement.clientWidth ||
		document.body && document.body.clientWidth ||
		document.body && document.body.parentNode && document.body.parentNode.clientWidth || 0;
	return width;
}
function GetWinH()
{
    var height =
		document.documentElement && document.documentElement.clientHeight ||
		document.body && document.body.clientHeight ||
  		document.body && document.body.parentNode && document.body.parentNode.clientHeight || 0;
  	return height;
}

// TESTING ONLY
function OnDocumentDblClick()
{
	sm('mWin');
}
function initMDiv()
{
	document.ondblclick = OnDocumentDblClick;
	$('mWin').onclick = hm;
}
//addEvent(window,'load',initMDiv);
