/*
====================================================

  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

====================================================
*/



var oElementNeedSet = null;
var sEleID = 'SetCorner';

var widthPercent = -1;
var heightPercent = -1;

var bRight = true;
var bBottom = true;

var bAddNewDiv = false;

function GetIntValueByPx(sPxValue){
	return parseInt(sPxValue.replace("px", ""));
}
function GetIntValueByPer(sPerValue){
	return parseInt(sPerValue.replace("%", ""));
}

function ProcessIE6(sBrowser){//debugger
	var oWidth = 0;
	var oHeight = 0;
	var oBorderTBWidth = 0;
	var oBorderLRWidth = 0;

	if (oElementNeedSet.tagName == "IMG"){
		oWidth = oElementNeedSet.width;
		oHeight = oElementNeedSet.height;
		oBorderTBWidth = GetIntValueByPx(oElementNeedSet.border) * 2;
		oBorderLRWidth = GetIntValueByPx(oElementNeedSet.border) * 2;
	}else{
		oBorderTBWidth = GetIntValueByPx(oElementNeedSet.style.borderTopWidth) 
							+ GetIntValueByPx(oElementNeedSet.style.borderBottomWidth);
		oBorderLRWidth = GetIntValueByPx(oElementNeedSet.style.borderLeftWidth) 
							+ GetIntValueByPx(oElementNeedSet.style.borderRightWidth);

		if (oBorderTBWidth.toString() == "NaN"){
			oBorderTBWidth = 0;
			}

		if (oBorderLRWidth.toString() == "NaN"){
			oBorderLRWidth = 0;
			}

		if (oElementNeedSet.style.width.indexOf("px") >= 0){
			oWidth = GetIntValueByPx(oElementNeedSet.style.width);
			if (widthPercent != -1){
				oWidth = widthPercent * document.documentElement.clientWidth / 100;
				oElementNeedSet.style.width = (oWidth - oBorderTBWidth) + "px";
			}
		} else if (oElementNeedSet.style.width.indexOf("%") >= 0){
			oWidth = GetIntValueByPer(oElementNeedSet.style.width) * document.documentElement.clientWidth / 100;
			widthPercent = GetIntValueByPer(oElementNeedSet.style.width);
	//alert(oWidth+'-'+oBorderTBWidth);
			//if (oBorderTBWidth != 'NaN') {
				oElementNeedSet.style.width = (oWidth - oBorderTBWidth) + "px";
			//	}
			//else {
			//	oElementNeedSet.style.width = oWidth + "px";
			//	}
//return;
			if (widthPercent >= 100){
				AppendWidthDiv();
			}
		}

		if (oElementNeedSet.style.height.indexOf("px") >= 0){
			oHeight = GetIntValueByPx(oElementNeedSet.style.height);
			if (heightPercent != -1){
				oHeight = heightPercent * document.documentElement.clientHeight / 100;
				oElementNeedSet.style.height = (oHeight - oBorderLRWidth) + "px";
			}
		} else if (oElementNeedSet.style.height.indexOf("%") >= 0){
			oHeight = GetIntValueByPer(oElementNeedSet.style.height) * document.documentElement.clientHeight / 100;
			heightPercent = GetIntValueByPer(oElementNeedSet.style.height);
			oElementNeedSet.style.height = (oHeight - oBorderLRWidth) + "px";
		} else{
			oHeight = oElementNeedSet.clientHeight;
			oElementNeedSet.style.height = (oHeight - oBorderLRWidth) + "px";
		}
	}
	//debugger
	var addLeft = document.documentElement.scrollLeft;
	var addTop = document.documentElement.scrollTop;
	var eleLeft = (document.documentElement.clientWidth  - oWidth - oBorderLRWidth) * (bRight ? 1 : 0) + addLeft;
	if (eleLeft < 0)
		eleLeft = 0;
	var eleTop = (document.documentElement.clientHeight - oHeight - oBorderTBWidth) * (bBottom ? 1 : 0) + addTop;
	if (eleTop < 0)
		eleTop = 0;
	//alert("eleLeft: " + eleLeft + "; eleTop: " + eleTop);
	oElementNeedSet.style.left = eleLeft + "px";
	oElementNeedSet.style.top = eleTop + "px";
}

function AppendWidthDiv(){
	if (bAddNewDiv == false){
		var oNewDiv = document.createElement("div");
		oNewDiv.style.height = oElementNeedSet.style.height;
		document.body.appendChild(oNewDiv);
		bAddNewDiv = true;
	}
}

function SetPosition() {
	var sBrowser = getBrowser();
	if (sBrowser == "IE6"){
		ProcessIE6(sBrowser);
	}else{
		if (bRight){
			oElementNeedSet.style.right = 0 + "px";
		}else{
			oElementNeedSet.style.left = 0 + "px";
		}
		if (bBottom){
			oElementNeedSet.style.bottom = 0 + "px";
		}else{
			oElementNeedSet.style.top = 0 + "px";
		}
		
		if (oElementNeedSet.tagName == "DIV"){
			if (oElementNeedSet.style.width.indexOf("%") >= 0){
				if (GetIntValueByPer(oElementNeedSet.style.width) >= 100){
					
					if (sBrowser != "IE6"){
						AppendWidthDiv();
					}
					
					oElementNeedSet.style.right = 0 + "px";
					oElementNeedSet.style.left = 0 + "px";
				}
			}
			if (oElementNeedSet.style.height.indexOf("%") >= 0){
				if (GetIntValueByPer(oElementNeedSet.style.height) >= 100){
					
				}
			}
		}
	}
}

function CornerLoad(sElementID) {//debugger
	//alert('sElementID: " + sElementID);
	if (oElementNeedSet == null) {
		//oElementNeedSet = document.getElementById(sEleID);
		oElementNeedSet = document.getElementById(sElementID);
		oElementNeedSet.style.display="block";
		}

		
	// set the value of bRight and bBottom
	var sRel = oElementNeedSet.attributes.getNamedItem("rel").value;
	if (sRel.length > 0){
		var aRel= sRel.split(";");
		for (var i = 0;i < aRel.length;i++) {
			var aRelParameter = aRel[i].split(":");
			if (aRelParameter.length == 2 && aRelParameter[0] == "position") {
				var aPosition = aRelParameter[1].split("-");
				if (aPosition.length == 2) {
					if (aPosition[0] == "left") {
						bRight = false;
						}
					if (aPosition[1] == "top") {
						bBottom = false;
						}
					}
				}
			}
		}

	var sBrowser = getBrowser();
	if (sBrowser == "IE6") {
		SetPosition();
		oElementNeedSet.style.position = "absolute";
		
		window.onscroll = function() {
			SetPosition();
			}
	}
	else {
		SetPosition();
		oElementNeedSet.style.position = "fixed";
	}
		
	window.onresize = SetPosition;
}


function getBrowser() {
	var sResult = "other";
	var s = navigator.userAgent.toLowerCase();//alert(s);
	var a = new Array("msie", "firefox", "safari", "opera", "netscape");
	for(var i = 0; i < a.length; i ++) {
		if(s.indexOf(a[i]) != -1) {
			sResult = a[i];
			}
		}

	if (sResult == "msie") {
		sResult = "IE6";
		if (window.XMLHttpRequest) {
			sResult = "IE7";
			}
		}
	return sResult;
}

function ShowDiv(sDivID){
	var oEle = document.getElementById(sDivID);
	if (oEle)
		oEle.style.display = "block";
}

function HideDiv(sDivID){
	var oEle = document.getElementById(sDivID);
	if (oEle)
		oEle.style.display = "none";
}
