/*
====================================================

  http://www.GlobalWebCart.com - Version 2

  Copyright 2008 Internet Specialty Services, LLC.

  ANY UN-AUTHORIZED USE OR DISTRIBUTION OF THIS
  FILE OR IT'S CONTENTS IS STRICTLY PROHIBITED

====================================================
*/

/*
	v2.08@2011-07-29 20:47
	log:
	v2.08
	1. when scroll down and refresh, and use at the same time with freze script, can't show at the right position.
	v2.07
	1. when scroll down and refresh, and use at the same time with freze script, can't show at the right position.
	v2.06
	
	v2.05
	1. when scroll down and not freeze status, after the first mousemove, not show at the right position, I fixed the bug.
	v2.04
	1. make it always show at the right positon(even it is a "fixed" object, or it's parent object is "fixed" object)
	v2.03
	1. make it always show at the right positon(fixed a bug)
	v2.02:
	1. make it always show at the right positon.
	v2.01: 
	1. Add new feature:6: 0=page, 1=main element ( normal position ), 2=main element (top right), 3=main element (bottom right), 4=main element ( bottom left )
*/


var sh_ShowHideTimer;
var sh_ShowHideID = '';

var newImageSrc = "default.jpg";
var global_WidthCenter = 0, global_Top = 0;
var oNewImage;

var sh_divBackground = null;



/*
if (window.addEventListener){
	window.addEventListener('load',CheckSHbody, false);
	}
else if (window.attachEvent){
	window.attachEvent('onload', CheckSHbody);
	}
*/





//NOT SURE IF I SHOULD KEEP THIS OR NOT
/*
function callShowHideDb(oObj){
	var eventPar = sh_SplitRelValue(oObj, 7);
	var sID = oObj.id;
	if (eventPar.length > 0 && parseInt(eventPar) >= 1 && parseInt(eventPar) <= 3) {
		oObj.onclick = new Function("sh_ShowDiv('" + sID + "')");
		}
	else {
		oObj.onmouseover = new Function("sh_ShowDiv('" + sID + "')");
		}
}*/


function CheckSHbody() {
	//var bdy = document.body.innerHTML;
	//var bdy = document.documentElement.innerHTML;

	var bdy = document.getElementById("PageDiv").innerHTML;
//alert(bdy);

bdy = bdy.replace(/name="mo_SH"/g, 'name=mo_SH');
bdy = bdy.replace(/name='mo_SH'/g, 'name=mo_SH');


	//alert(bdy);
	//alert(bdy.indexOf('name=mo_SH'));

	//if (bdy.indexOf('name=mo_SH')!=-1 || bdy.indexOf('name="mo_SH"')!=-1 || bdy.indexOf("name='mo_SH'")!=-1) {
	
	if (bdy.indexOf('name=mo_SH')!=-1) {


//var tArr = bdy.split("name=mo_SH");
//var tArrLen = tArr.length;

//alert("Array Length:"+tArrLen);

//for (i=0;i<tArrLen;i++ ){
	//alert(tArr[i]);
//	}
//alert("done looping the array");



		//alert(bdy);
		SetShowHideDB(bdy);
	}
}



function SetShowHideDB(bdy){
	var oObjs;
	if (getBrowser() == "IE6" || getBrowser() == "IE7"){
		oObjs = new Array();
		var tArr = bdy.split("name=mo_SH");
		var iObjCount = 0;
		for (var i = 0;i < tArr.length - 1;i++){
			var idPosition = tArr[i].lastIndexOf("id=");
			if (idPosition != -1){
				var wholeID = tArr[i].substring(tArr[i].lastIndexOf("id="), tArr[i].length -1);
				//alert(wholeID);
				wholeID = wholeID.replace(/"/g, '');
				wholeID = wholeID.replace(/'/g, '');
				var spacePosition = wholeID.indexOf(" ");
				if (spacePosition == -1){
					spacePosition = wholeID.length;
				}else{
					
				}
				wholeID = wholeID.substring(wholeID.indexOf("id=") + 3, spacePosition);
				//alert("|" + wholeID + "|");
				var oObjTmp = document.getElementById(wholeID);
				if (oObjTmp){
					//alert("|" + wholeID + "|");
					oObjs[iObjCount] = oObjTmp;
					iObjCount ++;
				}
				//alert(oObjs.length);
			}
		}
	}else{
		oObjs = document.getElementsByName("mo_SH");
	}
	
	//alert('oObjs.length: ' + oObjs.length);
	for (var i = 0;i < oObjs.length;i++){
		var eventPar = sh_SplitRelValue(oObjs[i], 7);
		var sID = oObjs[i].id;
		var initPos = GetElCoordinate(oObjs[i]);
		oObjs[i].lstTop = initPos.top;
		oObjs[i].fstTop = initPos.top;
		if (eventPar.length > 0 && parseInt(eventPar) >= 1 && parseInt(eventPar) <= 3){
			// onclick
			oObjs[i].onclick = new Function("sh_ShowDiv('" + sID + "')");
		}else{
			// onMouseover
			oObjs[i].onmouseover = new Function("sh_ShowDiv('" + sID + "')");
		}
	}
}

function sh_SetBackgroundSize(iWidth, iHeight){
	if (sh_divBackground){
		sh_divBackground.style.width = iWidth + "px";
		sh_divBackground.style.height = iHeight + "px";
	}
	if (sh_$("ifBackground")){
		sh_$("ifBackground").style.width = iWidth + "px";
		sh_$("ifBackground").style.height = iHeight + "px";
	}
}

function sh_SetBackground(iWidth, iHeight, iLeft, iTop, iZIndex, bUseAjax){
	if (!sh_divBackground){
		sh_divBackground = document.createElement("div");
		sh_divBackground.id = "sh_divBackground";
		
		/*var myZindex = 10;
		if (iZIndex){
			myZindex = iZIndex;
		}
		sh_divBackground.style.zIndex = myZindex;*/
		if (iZIndex){
			sh_divBackground.style.zIndex = iZIndex;
		}
		sh_divBackground.innerHTML = "<iframe id='ifBackground' src='about:blank'></iframe>";
		sh_divBackground.style.position = "absolute";
		//alert(sh_divBackground.style.zIndex );
		document.body.appendChild(sh_divBackground);
	}
	//if (!bUseAjax){
		sh_$("ifBackground").style.width = iWidth + "px";
		sh_$("ifBackground").style.height = iHeight + "px";
	//}
	
	sh_$("ifBackground").style.position = "absolute";
	
	sh_divBackground.style.display = "block";
	
	//if (!bUseAjax){
		sh_divBackground.style.width = iWidth + "px";
		sh_divBackground.style.height = iHeight + "px";
	//}
	
	sh_divBackground.style.left = iLeft + "px";
	sh_divBackground.style.top = iTop + "px";
}

function sh_HideElements(bHide, dID) {
	var el = sh_$(dID);
	if (el){
		if (bHide){
		    if (!window.__temp__){
			    window.__temp__ = new Array();
			}
		    var o = ["select","iframe","applet","object"];
		    for (var i = 0; i < o.length; i ++) {
		        var elms = document.getElementsByTagName(o[i]);
		        for (var j = 0; j < elms.length; j ++) {
		            var p = getPosition(elms[j]);
		            var e = getPosition(el);
		            if (e.left + e.width > p.left && e.left < p.left + p.width &&
		                e.top + e.height > p.top && e.top < p.top + p.height) {
		                window.__temp__[j] = elms[j];
		                elms[j].style.visibility = "hidden";
		            }
		        }
		    }
		}else{
			if (window.__temp__){
				var t = window.__temp__;
			    if (t){
			        for (var i=0; i<t.length; i++){
						if (t[i]){
							t[i].style.visibility = "visible";
						}
			        }
			    }
			}
		}
	}
}



/*
function getMaxZIndex(oObj){
	var oAllObjects = document.all ? document.all : document.getElementsByTagName("*");
	var iMaxZIndex = 1;
	for(var i = 0;i < oAllObjects.length;i++){
		var p = getPosition(oAllObjects[i]);
		var e = getPosition(oObj);
		if (e.left + e.width > p.left && e.left < p.left + p.width &&
		    e.top + e.height > p.top && e.top < p.top + p.height) {
		    var sZIndex = oAllObjects[i].style.zIndex;
			if (sZIndex.length > 0){
				var iZIndex = parseInt(sZIndex);
				if (iZIndex > iMaxZIndex){
					iMaxZIndex = iZIndex;
				}
			}
		}
	}
	
	return iMaxZIndex;
}*/



function sh_HideDiv() {
	if (sh_ShowHideID.length > 0){
		sh_$(sh_ShowHideID).style.display = "none";
		sh_ShowHideID = "";
		//sh_HideElements(false, id);
		if (sh_divBackground){
			sh_divBackground.style.display = "none";
		}
	}
}

///////////////////////////////////////////////////////////////////////
// NOTE: this functon get content string via ajax.  we need fill in this fucntion.
function GetContentByAjax(xUrl, aSync){
	var xmlhttp = false;

//xUrl = tHTTP + xUrl;
//alert(xUrl);

	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
		xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
		}

	var sBrowser = getBrowser();
	xmlhttp.open("GET", xUrl, aSync);
	if (sBrowser != "firefox"){
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				if (xmlhttp.status == 200) {
					var oObj = sh_$(infoID);
					if (oObj){
						oObj.innerHTML = xmlhttp.responseText;
					}
				}
			}
		}
	}
	xmlhttp.send(null);
	if (sBrowser == "firefox"){//alert('ff');
		if(xmlhttp.status == 200) {
			if(xmlhttp.responseText.length !== 0) {
				var oObj = sh_$(infoID);
					if (oObj){
						oObj.innerHTML = xmlhttp.responseText;
					}
			}
		}
	}
	xmlhttp.close;
}
///////////////////////////////////////////////////////////////////////
var infoID = "";

var bGot = false;
var sContent = "";
function sh_ShowDiv(sID) {//debugger;
	//alert(sID);
	var HideTime, NudgeIt = 1;
	var oD = sh_$(sID);
	
	ShowID = sh_SplitRelValue(oD, 0);
	infoID = ShowID;
	//alert(ShowID);
	HideTime = 500;
	if (sh_SplitRelValue(oD, 1).length > 0){
		HideTime = parseInt(sh_SplitRelValue(oD, 1));
	}
	
	if (sh_ShowHideID != ShowID && sh_ShowHideID != '') {
		clearTimeout(sh_ShowHideTimer);
		sh_HideDiv();
	}
	if (sh_ShowHideID == ShowID && sh_ShowHideID != '') {
		clearTimeout(sh_ShowHideTimer);
	}

	sh_ShowHideID = ShowID;
	var sD = sh_$(ShowID);
	
	// add by zk, for ajax feature to get content
	var bUseAjax = false;
	var eventPar = sh_SplitRelValue(oD, 7);
	if (eventPar.length > 0){
		if (parseInt(eventPar) >= 2 && parseInt(eventPar) <= 3){
			var contentStr = "";
			bUseAjax = true;
			//var main_path;
			//main_path = document.location.href.substring(0,document.location.href.lastIndexOf("/")+1);
			
			var ajax_file = sh_SplitRelValue(oD, 8);

			var url = tHTTP + ajax_file;
//alert('url:'+url);
			if (parseInt(eventPar) == 2){
				// 2 = click and ajax load content once
				if (!bGot){
					GetContentByAjax(url, false);
					bGot = true;
				}
				//contentStr = sContent;
			} else if (parseInt(eventPar) == 3){
				// 3 = click and ajax load content everytime
				GetContentByAjax(url, false);;
			}
			/*if (contentStr.length > 0 && sD.innerHTML != contentStr){
				sD.innerHTML = contentStr;
			}*/
		}
	}
	
	//alert(sD);
	var cord = GetElCoordinate(oD);
	sD.style.display = "block";
	sD.style.position = "absolute";

	var iWidth = GetElCoordinate(sD).width, iHeight = GetElCoordinate(sD).height;
	if (sh_SplitRelValue(oD, 2).length > 0){
		iWidth = parseInt(sh_SplitRelValue(oD, 2));
	}
	if (sh_SplitRelValue(oD, 3).length > 0){
		iHeight = parseInt(sh_SplitRelValue(oD, 3));
	}
	if (!bUseAjax){
		// width
		sD.style.width = sh_SplitRelValue(oD, 2) + "px";
		// height
		sD.style.height = sh_SplitRelValue(oD, 3) + "px";
	}
	
	var sBrowser = getBrowser();
	var iScrollTop = 0;
	if (sBrowser == "safari"){
		iScrollTop = document.body.scrollTop;
	}else{
		iScrollTop = document.documentElement.scrollTop;
	}
	
	var iLeft = cord.left - NudgeIt, iTop = cord.bottom;
	if (sh_SplitRelValue(oD, 4).length > 0){
		iTop = parseInt(sh_SplitRelValue(oD, 4));
	}
	if (sh_SplitRelValue(oD, 5).length > 0){
		iLeft = parseInt(sh_SplitRelValue(oD, 5));
	}
	var sParameter6 = sh_SplitRelValue(oD, 6);
	var iPar6 = -1;
	if (sParameter6.length > 0){
		iPar6 = parseInt(sParameter6);
		var oCoordOd = GetElCoordinate(oD);
		if (sParameter6 == "1"){
			iLeft += oCoordOd.left;
			iTop += oCoordOd.top;
		}else if (sParameter6 == "2"){
			iLeft += oCoordOd.right;
			iTop += oCoordOd.top;
		}else if (sParameter6 == "3"){
			iLeft += oCoordOd.right;
			iTop += oCoordOd.bottom;
		}else if (sParameter6 == "4"){
			iLeft += oCoordOd.left;
			iTop += oCoordOd.bottom;
		}
	}
	
	sD.style.left = iLeft + "px";
	var iAddTop = 0;
	/*if (iPar6 >= 1 && iPar6 <= 4){
		if (cord.top != oD.fstTop || sh_getPosition(oD) == "fixed"){
			iAddTop = iScrollTop;
			if (iAddTop < iScrollTop){
				iAddTop = iScrollTop;
			}
		}
	}*/
	if (oD.isKFreezed && oD.isKFreezed == true){
		iAddTop = iScrollTop;
	}else{
		iAddTop = 0;
	}//alert(oD.isKFreezed + ',' + iAddTop);
	sD.style.top = (iTop + iAddTop) + "px";
	
	oD.lstTop = cord.top;
	
	//alert(sD.style.zIndex );
	//sD.style.zIndex = 1000;
	//maxZIndex = getMaxZIndex(sD) + 1;
	//sD.style.zIndex = maxZIndex + 1;
	sD.style.zIndex = 999;
	if (bUseAjax){
		iWidth = sD.offsetWidth;
		iHeight = sD.offsetHeight;
	}
	if (getBrowser() == "IE6"){
		sh_SetBackground(iWidth, iHeight, iLeft, iTop, sD.style.zIndex - 1, bUseAjax);
	}
	
	//sh_HideElements(true, ShowID);
	/*
	var eventPar = sh_SplitRelValue(oObjs[i], 7);
	if (eventPar.length > 0 && parseInt(eventPar) == 1){
		
	}else{
		
	}*/
	
	sh_$(ShowID).onmouseover = function(){
		clearTimeout(sh_ShowHideTimer);
	}
	if (HideTime > 0){
		sh_$(ShowID).onmouseout = function(){
			clearTimeout(sh_ShowHideTimer);
			sh_ShowHideTimer = setTimeout("sh_HideDiv()", HideTime) 
		}
		oD.onmouseout = function(){
			clearTimeout(sh_ShowHideTimer);
			sh_ShowHideTimer = setTimeout("sh_HideDiv()", HideTime) 
		}
	}
}

function sh_getPosition(oObj){
	var sPos = oObj.style.position;
	var e = oObj.offsetParent;
	if (sPos.length <= 0){
		while(e && sPos.length <= 0){
			sPos = e.style.position;
			e = e.offsetParent;
		}
	}
}

function sh_$(sID){
	return document.getElementById(sID);
}

function sh_SplitRelValue(oObj, iIndex){
	var sResult = "";
	
	if (oObj){
		var aArray = oObj.getAttribute("rel").split(";");
		if (aArray){
			if (iIndex < aArray.length){
				sResult = aArray[iIndex];
			}
		}
	}
	
	return sResult;
}












// ShowImage //

var sArrowUrl = iUrl+"sys/arrowdown.gif";
var oImage = null;
var oArrow = null;
var iOriScrollLeft = 0;
var iOriScrollTop = 0;
var divBackground = null;

function si_close(oLink){
	if (oArrow){
		oArrow.style.display = "none";
	}
	if (oImage){
		oImage.style.display = "none";
	}
	if (divBackground){
		divBackground.style.display = "none";
	}
}

function si_SetBackground(iWidth, iHeight, iLeft, iTop, sPosition){
	if (!divBackground){
		divBackground = document.createElement("div");
		divBackground.id = "divBackground";
		//divBackground.style.zIndex = 10;
		divBackground.innerHTML = "<iframe id='ifDivBackground' src='about:blank'></iframe>";
		if (sPosition){
			divBackground.style.position = sPosition;
		}
		
		document.body.appendChild(divBackground);
	}
	si_$("ifDivBackground").style.width = iWidth + "px";
	si_$("ifDivBackground").style.height = iHeight + "px";
	if (sPosition){
		si_$("ifDivBackground").style.position = sPosition;
	}
	
	divBackground.style.display = "block";
	divBackground.style.width = iWidth + "px";
	divBackground.style.height = iHeight + "px";
	divBackground.style.left = iLeft + "px";
	divBackground.style.top = iTop + "px";
}

function si_InitScroll(){
	var sBrowser = getBrowser();
	if (sBrowser == "IE6"){
		iOriScrollLeft = document.documentElement.scrollLeft;
		iOriScrollTop = document.documentElement.scrollTop;
		if (window.addEventListener){
			window.addEventListener('scroll', si_ScrollForIE6, false);
			}
		else if (window.attachEvent) {
			window.attachEvent('onscroll', si_ScrollForIE6);
		}
	}
}

function si_GetIntValueByPx(sPxValue){
	return parseInt(sPxValue.replace("px", ""));
}

function si_ScrollForIE6(){
	var iNewScrollLeft = document.documentElement.scrollLeft;
	var iNewScrollTop = document.documentElement.scrollTop;
	var addLeft = iNewScrollLeft - iOriScrollLeft;
	var addTop = iNewScrollTop - iOriScrollTop;
	iOriScrollLeft = iNewScrollLeft;
	iOriScrollTop = iNewScrollTop;
	
	if (oImage){
		if (oImage.style.display != "none"){
			oImage.style.left = (si_GetIntValueByPx(oImage.style.left) + addLeft) + "px";
			oImage.style.top = (si_GetIntValueByPx(oImage.style.top) + addTop) + "px";
			
			if (oArrow){
				oArrow.style.left = (si_GetIntValueByPx(oArrow.style.left) + addLeft) + "px";
				oArrow.style.top = (si_GetIntValueByPx(oArrow.style.top) + addTop) + "px";
			}
			if (divBackground){
				divBackground.style.left = (si_GetIntValueByPx(divBackground.style.left) + addLeft) + "px";
				divBackground.style.top = (si_GetIntValueByPx(divBackground.style.top) + addTop) + "px";
			}
		}
	}
}

function si_$(sID){
	return document.getElementById(sID);
}

function si_SetActualImage(){
	//alert('test222, newImageSrc:' + newImageSrc);
	if (oImage){
		oImage.src = newImageSrc;
		oImage.width = oNewImage.width;
		oImage.height = oNewImage.height;
		
		//global_WidthCenter = iWidthCenter;
		//global_Top = iTop;
		var sBrowser = getBrowser();
		var iImageLeft = global_WidthCenter - oImage.offsetWidth / 2;
		if (iImageLeft < 0){
			iImageLeft = 0;
		}
		var iMaxLeft = document.documentElement.scrollWidth - oImage.offsetWidth - 24;
		if (iImageLeft > iMaxLeft){
			iImageLeft = iMaxLeft;
		}
		var iImageHeight = global_Top - oImage.offsetHeight;
		oImage.style.left = (iImageLeft) + "px";
		oImage.style.top = (iImageHeight) + "px";
		/*if (sBrowser == "safari"){
			iImageLeft -= 8;
			iImageHeight -= 8;
		}*/
		
		if (sBrowser == "IE6"){
			si_SetBackground(oImage.offsetWidth-2 <= 0 ? 1 : oImage.offsetWidth-2, oImage.offsetHeight-2 <= 0 ? 1 : oImage.offsetHeight-2, iImageLeft, iImageHeight);
		}
	}
}

function si_expand(oLink){
	var sImagePath = oLink.href;
	var sRelValue = oLink.getAttribute("rel");
	
	var sBrowser = getBrowser();
	var sPosition = "fixed";
	if (sBrowser == "IE6"){
		sPosition = "absolute";
	}
	
	var addLeft = document.documentElement.scrollLeft;
	var addTop = document.documentElement.scrollTop;
	
	var pMainPosition = getPosition(oLink);
	if (!oArrow){
		oArrow = new Image();
		oArrow.src = sArrowUrl;
		oArrow.style.position = sPosition;
		oArrow.style.zIndex ='1000';

		document.body.appendChild(oArrow);
	}
	oArrow.style.display = "block";

	var iWidthCenter = pMainPosition.left + pMainPosition.width / 2;
	oArrow.style.left = (iWidthCenter - oArrow.offsetWidth / 2) + "px";
	var iTop = pMainPosition.top - oArrow.offsetHeight;
	oArrow.style.top = (iTop) + "px";
	
	if (sBrowser == "IE6"){
		si_SetBackground(0, 0, 0, 0, sPosition);
	}
	if (!oImage){
		oImage = new Image();
		oImage.style.position = sPosition;
		
		document.body.appendChild(oImage);
	}
	newImageSrc = sImagePath;
	//oImage.src = sImagePath;
	oImage.src = iUrl + "sys/Loading.gif";
	oImage.width=24;
	oImage.height=24;

	oNewImage = new Image();
	oNewImage.onload = function() {
		si_SetActualImage();
	}
	oNewImage.src = newImageSrc;
	
	oImage.border = "0px";
	oImage.style.display = "block";
	if (sRelValue && sRelValue.length > 0){
		oImage.className = sRelValue;
	}else{
		oImage.className = "";
	}
	
	global_WidthCenter = iWidthCenter;
	global_Top = iTop;
	
	var iImageLeft = iWidthCenter - oImage.offsetWidth / 2;
	if (iImageLeft < 0){
		iImageLeft = 0;
	}
	var iMaxLeft = document.documentElement.scrollWidth - oImage.offsetWidth - 24;
	if (iImageLeft > iMaxLeft){
		iImageLeft = iMaxLeft;
	}
	var iImageHeight = iTop - oImage.offsetHeight;
	oImage.style.left = (iImageLeft) + "px";
	oImage.style.top = (iImageHeight) + "px";
	if (sBrowser == "safari"){
		iImageLeft -= 8;
		iImageHeight -= 8;
	}
	
	if (sBrowser == "IE6"){
		si_SetBackground(oImage.offsetWidth-2, oImage.offsetHeight-2, iImageLeft, iImageHeight);
	}
	//alert(iImageLeft);
	return false;
}





if (window.addEventListener){
	window.addEventListener('load', si_InitScroll, false);
	}
else if (window.attachEvent){
	window.attachEvent('onload', si_InitScroll);
}
