/*

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

function rootpath()
{
	try
	{
		if (_axorootpath != undefined)
		{
			return _axorootpath;
		}
	}
	catch (ex) {}
	
	return "";
}
var indicator_img_path = rootpath() + "images/indicator.gif";
var blank_htm_path = rootpath() + "/blank.htm";

function init_ibox() 
{
	createIbox(document.getElementsByTagName("body")[0]); //create our ibox
}

function showCustomIBox(url, title, height, width)
{
    url = unescape(url);
    if (url.indexOf('?') >= 0)
    {
		url += '&';
    }
    else
    {
		url += '?';
    }
    url += 'zzz=' + new Date().getTime();
    
    showBG();
    showIbox(url, title, height, width);	// show ibox
    return false;
}

function showCustomIBoxNoClose(url, title, height, width)
{
    showIbox(url, title, height, width);
}

var box_w_visible = false;
showBG = function() {
	var box_w = getElem('ibox_w');

	var opacity_level = 8;
	box_w_visible = false;
	box_w.style.opacity = 0;
	box_w.style.filter = 'alpha(opacity=0)';

	for (var i=0;i<=opacity_level;i++) {setTimeout('setOpacity('+i+')',10*i);} // from quirksmode.org
	
	setOpacity = function (value)	{
		var box_w = getElem('ibox_w');
		box_w.style.opacity = value/10;
		box_w.style.filter = 'alpha(opacity=' + value*10 + ')';

		if (value == opacity_level) {box_w_visible = true;} 
	}
	
	box_w.style.display = "";
	var pagesize = new getPageSize();
	var scrollPos = new getScrollPos();
	var ua = navigator.userAgent;
	if(ua.indexOf("MSIE ") != -1)
	{
	    //IE
	    box_w.style.width = pagesize.width+'px';
	} 
	else
	{
	    //other
	    //box_w.style.width = pagesize.width-20+'px';
	    box_w.style.width = pagesize.width+'px';
	}
	//box_w.style.height = document.body.scrollHeight+50+'px';
	box_w.style.height = pagesize.height+scrollPos.scrollY+'px';
	hideSelects();
	oldKeyPress = document.getElementsByTagName('body')[0].onkeypress;
	document.getElementsByTagName('body')[0].onkeypress = keyHandler;
}

resizeBG = function() {
    if(!box_w_visible)
        return;
        
	var box_w = getElem('ibox_w');
	var pagesize = new getPageSize();
	var scrollPos = new getScrollPos();
	var ua = navigator.userAgent;
	if(ua.indexOf("MSIE ") != -1)
	{
	    //IE
	    box_w.style.width = pagesize.width+'px';
	} 
	else
	{
	    //other
	    //box_w.style.width = pagesize.width-20+'px';
	    box_w.style.width = pagesize.width+'px';
	}
	//box_w.style.height = document.body.scrollHeight+50+'px';
	box_w.style.height = pagesize.height+scrollPos.scrollY+'px';
}

hideBG = function() {
	var box_w = getElem('ibox_w');
	box_w.style.display = "none";
	bg_opacity = 0;
	box_w_visible = false;
	showSelects();
}

showIndicator = function() {
	var ibox_p = getElem('ibox_progress');
	ibox_p.style.display = "";
	posToCenter(ibox_p);
	ibox_p.onclick = function() {hideIbox();hideIndicator();}
}

hideIndicator = function() {
	var ibox_p = getElem('ibox_progress');
	ibox_p.style.display = "none";
	ibox_p.onclick = null;
}

createIbox = function(elem) {

	// a trick on just creating an ibox wrapper then doing an innerHTML on our root ibox element
	var strHTML = "<div id=\"ibox_w\" style=\"display:none;\"></div>";
	strHTML +=	"<div id=\"ibox_progress\" style=\"display:none;\"><img src=\""+indicator_img_path+"\" alt=\"Loading...\"";
	strHTML +=	"style=\"width:128px;height:128px;\"/></div>";
	strHTML +=	"<div id=\"ibox_wrapper\" style=\"display:none\">";
	strHTML +=	"<div id=\"ibox_header\"><div id=\"ibox_title\" style=\"float:left;\"></div><div class=\"iBox_Close\" style=\"float:right;\" onclick=\"closeIbox();\">X</div>&nbsp;</div>";
	strHTML +=	"<div id=\"ibox_content\" style=\"clear:both; overflow: auto\"><iframe id=\"ibox_frame\" frameborder=\"0\" src=\"" + blank_htm_path + "\"></iframe></div>";
	strHTML +=  "</div>";

	var docBody = document.getElementsByTagName("body")[0];
	docBody.style.overflowY = "auto";
	var ibox = document.createElement("div");
	ibox.setAttribute("id","ibox");
	ibox.innerHTML = strHTML;
	elem.appendChild(ibox);

}

setIboxTitle = function(title)
{
    var ibox_title = getElem('ibox_title');
	if(title != "") {ibox_title.innerHTML = title;} else {ibox_title.innerHTML = "&nbsp;";}
}

var ibox_w_height = 0;
showIbox = function(url, title, height, width)
{
    var ibox = getElem('ibox_wrapper');
    var ibox_content = getElem('ibox_content');
    var ibox_header = getElem('ibox_header');
    var ibox_type = 0;

	// set title here
	setIboxTitle(title);
	
	// file checking code borrowed from thickbox
	var urlString = /\.jpg|\.jpeg|\.png|\.gif|\.html|\.htm|\.php|\.cfm|\.asp|\.aspx|\.ashx|\.jsp|\.jst|\.rb|\.txt/g;
	var urlType = url.match(urlString);
	
	if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif'){
		ibox_type = 0;
		
		showIndicator();
		
		var imgPreloader = new Image();
		
		imgPreloader.onload = function(){
			
			var pagesize = new getPageSize();
			
			var x = pagesize.width - 100;
			var y = pagesize.height - 100;

			if(imgPreloader.width > x) { 
				imgPreloader.height = imgPreloader.height * (x/imgPreloader.width); 
				imgPreloader.width = x; 
				if(imgPreloader.height > y) { 
					imgPreloader.width = imgPreloader.width * (y/imgPreloader.height); 
					imgPreloader.height = y; 
				}
			} 

			else if(imgPreloader.height > y) { 
				imgPreloader.width = imgPreloader.width * (y/imgPreloader.height); 
				imgPreloader.height = y; 
				if(imgPreloader.width > x) { 
					imgPreloader.height = imgPreloader.height * (x/imgPreloader.width); 
					imgPreloader.width = x;
				}
			}
		
			hideIndicator();	
			var strHTML = "<a href=\"javascript:void(null);\"><img src=\""+url+"\" style=\"width:"+imgPreloader.width+"px;height:"+imgPreloader.height+"px;border:0;\"/></a>";
			setIBoxContent(strHTML);	
			
			// set width and height
			ibox.style.height = imgPreloader.height+'px';
			ibox.style.width = imgPreloader.width+'px';

			ibox.style.display = "";
			ibox.style.visibility = "hidden";
			posToCenter(ibox); 
			ibox.style.visibility = "visible";
		}
		
		imgPreloader.src = url;
		

		
	} else if(url.indexOf("#") > 0) {
			var strHTML = "";
			ibox_type = 1;

			var elemSrcId = url.substr(url.indexOf("#") + 1,1000);
			var elemSrc = getElem(elemSrcId);
			
			if(elemSrc) {
				strHTML = elemSrc.innerHTML;
			}

			setIBoxContent(strHTML);
	
			if(height > 0) {ibox.style.height = height + 'px';} 
			else {ibox.style.height = '280px';}
			
			if(width > 0) {ibox.style.width = width + 'px';} 
			else {ibox.style.width = '450px';}

		
			ibox.style.display = "";
			ibox.style.visibility = "hidden";
			posToCenter(ibox); 
			ibox.style.visibility = "visible";

	}else if(urlType=='.htm'||urlType=='.html'||urlType=='.php'||
			 urlType=='.asp'||urlType=='.aspx'||urlType=='.jsp'||
			 urlType=='.jst'||urlType=='.rb'||urlType=='.txt'||
			 urlType=='.cfm'||urlType=='.ashx') {
			
	
			ibox_type = 2;
	
			showIndicator();
			
			//setIBoxContent(response);
			
			getElem('ibox_frame').src = url;
			
			hideIndicator();
			
	        if(height > 0) {ibox.style.height = height + 'px';} 
	        else {
	            ibox.style.height = '280px';  //try to make the ibox only as tall as it needs to be
	        }
			
	        if(width > 0) {ibox.style.width = width + 'px';} 
	        else {
	        ibox.style.width = '450px';
	        }
	        
	        // set content height based on wrapper height - header height
	        var hh = 22;
	        if (ibox_header.style.height)
	        {
				hh = ibox_header.style.height;
	        }
	        ibox_content.style.height = (height - hh) + 'px';

			ibox.style.display = "";
			ibox.style.visibility = "hidden";
			posToCenter(ibox); 
			ibox.style.visibility = "visible";
			
			/*
			
			http.open('get',url,true);
	
			http.onreadystatechange = function() {
				if(http.readyState == 4){
					var response = http.responseText;
					

					
				}
			}
			
			http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			http.send(null);
			
			*/

			
	} 
	
	ibox.style.opacity = 0;
	ibox.style.filter = 'alpha(opacity=0)';	
	var ibox_op_level = 10;
		
	for (var i=0;i<=ibox_op_level;i++) {setTimeout('setIboxOpacity('+i+')',30*i);}

	setIboxOpacity = function (value)	{
		var ibox = getElem('ibox_wrapper');
		ibox.style.opacity = value/10;
		ibox.style.filter = 'alpha(opacity=' + value*10 + ')';
	}
			
	if(ibox_type == 1 || ibox_type == 2) 
	{
		ibox.onclick = null;
	} 
	else 
	{
		ibox.onclick = hideIbox;
	}

	window.onscroll = maintPos;

}

maintPos = function() {
	var ibox = getElem('ibox_wrapper');
	var box_w = getElem('ibox_w');
	var pagesize = new getPageSize();
	var ua = navigator.userAgent;
	
	if(ua.indexOf("MSIE ") != -1) {box_w.style.width = pagesize.width+'px';} 
	else {box_w.style.width = pagesize.width-20+'px';}
	box_w.style.height = document.body.scrollHeight+50+'px';
	posToCenter(ibox);
}

hideIbox = function()
{
	hideBG();
	var ibox = getElem('ibox_wrapper');
	ibox.style.display = "none";
	clearIboxContent();
	window.onscroll = null;
}

hideSelects = function()
{
    var docRoot = document.getElementsByTagName("select");
    for(var i = 0; i < docRoot.length; i++)
    {
        docRoot[i].style.visibility = 'hidden';
    }
}

showSelects = function()
{
    var docRoot = document.getElementsByTagName("select");
    for(var i = 0; i < docRoot.length; i++)
    {
        docRoot[i].style.visibility = 'visible';
    }
}

posToCenter = function(elem) {
	var scrollPos = new getScrollPos();
	var pageSize = new getPageSize();
	var emSize = new getElementSize(elem);
	
    var x = Math.round(pageSize.width/2) + scrollPos.scrollX;
    var y = Math.round(pageSize.height/2) + scrollPos.scrollY;
    if (emSize.width)
    {
		x -= (emSize.width /2);
		y -= (emSize.height /2);
	}

	elem.style.left = x+'px';
	elem.style.top = y+'px';

}

getScrollPos = function() {
	var docElem = document.documentElement;
	this.scrollX = self.pageXOffset || (docElem&&docElem.scrollLeft) || document.body.scrollLeft;
	this.scrollY = self.pageYOffset || (docElem&&docElem.scrollTop) || document.body.scrollTop;
}

getPageSize = function() {
		var docElem = document.documentElement
		this.width = self.innerWidth || (docElem&&docElem.clientWidth) || document.body.clientWidth;
		this.height = self.innerHeight || (docElem&&docElem.clientHeight) || document.body.clientHeight;
}

getElementSize = function(elem) {
	this.width = elem.offsetWidth ||  elem.style.pixelWidth;
	this.height = elem.offsetHeight || elem.style.pixelHeight;
}

setIBoxContent = function(str)
{
	clearIboxContent();
//	getElem('ibox_content').innerHTML = str;	// can't do this anymore - DS:6/29/06 11:22am
}

clearIboxContent = function()
{
	getElem('ibox_frame').src = '';
//	getElem('ibox_content').innerHTML = "";
}


getElem = function(elemId) {
	return document.getElementById(elemId);	
}

parseQuery = function(query) {
   var Params = new Object ();
   if (!query) return Params; 
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;

   }
   
   return Params;
}

/********************************************************
 Make this IE7 Compatible ;)
 http://ajaxian.com/archives/ajax-on-ie-7-check-native-first
*********************************************************/
createRequestObject = function() {
	var xmlhttp;
		/*@cc_on
	@if (@_jscript_version>= 5)
			try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
					try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
					catch (E) {xmlhttp = false;}
			}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
			try {xmlhttp = new XMLHttpRequest();} catch (e) {xmlhttp = false;}
	}
	return xmlhttp;
}

var http = createRequestObject();

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}
addEvent(window, 'load', init_ibox);

var oldKeyPress;

function keyHandler(e)
{
    // MSIE or Firefox?
    var kC  = (window.event) ? event.keyCode : e.keyCode; 
    var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE // MSIE : Firefox
    if(kC==Esc)
    {
        hideIbox();
        document.getElementsByTagName('body')[0].onkeypress = oldKeyPress;
    }
}