﻿function getViewportWidth() {
	var viewportwidth;
 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined') {
		  viewportwidth = window.innerWidth;
	 } else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
	 	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
		   viewportwidth = document.documentElement.clientWidth;
	 } else {
	 	// older versions of IE
		   viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	 }
	 return viewportwidth;
}
function getViewportHeight() {
	var viewportheight;
 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerHeight != 'undefined') {
		  viewportheight = window.innerHeight;
	 } else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientHeight != 'undefined' && document.documentElement.clientHeight != 0) {
	 	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
		   viewportheight = document.documentElement.clientHeight;
	 } else {
	 	// older versions of IE
		   viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	 }
	 return viewportheight;
}
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, null).getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}
function positionElement(objName, anchorEdgeArr) {
	var obj = document.getElementById(objName);
	obj.className = "flexcroll";
	var Anchors = ""
	
	for (var i=0; i<anchorEdgeArr.length; i++) {
		anchorEdge = anchorEdgeArr[i];
		Anchors = Anchors + anchorEdge;
		switch (anchorEdge.toLowerCase()) {
			case "t":
				//Get the relevant styles that affect this measurement and position
				var TopStyle = getStyle(obj, "top");
				var BottomStyle = getStyle(obj, "bottom");
				var objHeight = getStyle(obj, "max-height");
				if (objHeight == null) { objHeight = getStyle(obj, "height"); }
	
				if (TopStyle!="" && BottomStyle!="" && objHeight!="") {
					//Reset the tmp values
					var tmpTop = 0;
					var tmpBottom = 0;
					var tmpHeight = 0;
					var WinHeight = getViewportHeight();
	
					//Determine the numeric objHeight value
					if (objHeight.search(/%/i)>-1) {
						var percent = parseFloat(objHeight.replace(/%/,"")) / 100;
						tmpHeight = WinHeight * percent;
					} else if (objHeight.search(/px/i)>-1) {
						tmpHeight = parseFloat(objHeight.replace(/px/,""));
					}
					
					//Determine the numeric Top value
					if (TopStyle.search(/%/i)>-1) {
						var percent = parseFloat(TopStyle.replace(/%/,"")) / 100;
						tmpTop = Math.abs(WinHeight * percent);
					} else if (TopStyle.search(/px/i)>-1) {
						tmpTop = Math.abs(parseFloat(TopStyle.replace(/px/,"")));
					}
	
					// Determine the numeric Bottom Value
					if (BottomStyle.search(/%/i)>-1) {
						var percent = parseFloat(BottomStyle.replace(/%/,"")) / 100;
						tmpBottom = Math.abs(WinHeight * percent);
					} else if (BottomStyle.search(/px/i)>-1) {
						tmpBottom = Math.abs(parseFloat(BottomStyle.replace(/px/,"")));
					}
					
					var strPaddingTop = "";
					var PaddingTop = 0;
					var strPaddingBottom = "";
					var PaddingBottom = 0;
					strPaddingTop = getStyle(obj, "padding-top");
					strPaddingBottom = getStyle(obj, "padding-bottom");
					if (strPaddingTop.search(/em/i)>-1) {
						PaddingTop = parseFloat(strPaddingTop.replace(/em/,"")) * 16;
					} else if (strPaddingTop.search(/px/i)>-1) {
						PaddingTop = Math.abs(parseFloat(strPaddingTop.replace(/px/,"")));
					}
					if (strPaddingBottom.search(/em/i)>-1) {
						PaddingBottom = parseFloat(strPaddingBottom.replace(/em/,"")) * 16;
					} else if (strPaddingBottom.search(/px/i)>-1) {
						PaddingBottom = Math.abs(parseFloat(strPaddingBottom.replace(/px/,"")));
					}
	
					var CalculatedObjHeight = tmpHeight;
					var CalulatedWinHeight = WinHeight - tmpTop - tmpBottom - PaddingTop - PaddingBottom;
					
					if (CalculatedObjHeight < CalulatedWinHeight) {
						obj.className = obj.className + " bottomOff";
					}
				}
				break;
				
			case "r":
				//Get the relevant styles that affect this measurement and position
				var leftStyle = getStyle(obj, "left");
				var rightStyle = getStyle(obj, "right");
				var objwidth = getStyle(obj, "max-width");
				if (objwidth == null) { objwidth = getStyle(obj, "width"); }
	
				if (leftStyle!="" && rightStyle!="" && objwidth!="") {
					//Reset the tmp values
					var tmpleft = 0;
					var tmpright = 0;
					var tmpwidth = 0;
					var Winwidth = getViewportWidth();
	
					//Determine the numeric objwidth value
					if (objwidth.search(/%/i)>-1) {
						var percent = parseFloat(objwidth.replace(/%/,"")) / 100;
						tmpwidth = Winwidth * percent;
					} else if (objwidth.search(/px/i)>-1) {
						tmpwidth = parseFloat(objwidth.replace(/px/,""));
					}
					
					//Determine the numeric left value
					if (leftStyle.search(/%/i)>-1) {
						var percent = parseFloat(leftStyle.replace(/%/,"")) / 100;
						tmpleft = Math.abs(Winwidth * percent);
					} else if (leftStyle.search(/px/i)>-1) {
						tmpleft = Math.abs(parseFloat(leftStyle.replace(/px/,"")));
					}
	
					// Determine the numeric right Value
					if (rightStyle.search(/%/i)>-1) {
						var percent = parseFloat(rightStyle.replace(/%/,"")) / 100;
						tmpright = Math.abs(Winwidth * percent);
					} else if (rightStyle.search(/px/i)>-1) {
						tmpright = Math.abs(parseFloat(rightStyle.replace(/px/,"")));
					}
					
					var strPaddingleft = "";
					var Paddingleft = 0;
					var strPaddingright = "";
					var Paddingright = 0;
					strPaddingleft = getStyle(obj, "padding-left");
					strPaddingright = getStyle(obj, "padding-right");
					if (strPaddingleft.search(/em/i)>-1) {
						Paddingleft = parseFloat(strPaddingleft.replace(/em/,"")) * 16;
					} else if (strPaddingleft.search(/px/i)>-1) {
						Paddingleft = Math.abs(parseFloat(strPaddingleft.replace(/px/,"")));
					}
					if (strPaddingright.search(/em/i)>-1) {
						Paddingright = parseFloat(strPaddingright.replace(/em/,"")) * 16;
					} else if (strPaddingright.search(/px/i)>-1) {
						Paddingright = Math.abs(parseFloat(strPaddingright.replace(/px/,"")));
					}
	
					var CalculatedObjwidth = tmpwidth;
					var CalulatedWinwidth = Winwidth - tmpleft - tmpright - Paddingleft - Paddingright;
					
					if (CalculatedObjwidth < CalulatedWinwidth) {
						obj.className = obj.className + " leftOff";
					}
				}
				break;
				
			case "b":
				//Get the relevant styles that affect this measurement and position
				var TopStyle = getStyle(obj, "top");
				var BottomStyle = getStyle(obj, "bottom");
				var objHeight = getStyle(obj, "max-height");
				if (objHeight == null) { objHeight = getStyle(obj, "height"); }
	
				if (TopStyle!="" && BottomStyle!="" && objHeight!="") {
					//Reset the tmp values
					var tmpTop = 0;
					var tmpBottom = 0;
					var tmpHeight = 0;
					var WinHeight = getViewportHeight();
					
					//Determine the numeric objHeight value
					if (objHeight.search(/%/i)>-1) {
						var percent = parseFloat(objHeight.replace(/%/,"")) / 100;
						tmpHeight = WinHeight * percent;
					} else if (objHeight.search(/px/i)>-1) {
						tmpHeight = parseFloat(objHeight.replace(/px/,""));
					}
					
					//Determine the numeric Top value
					if (TopStyle.search(/%/i)>-1) {
						var percent = parseFloat(TopStyle.replace(/%/,"")) / 100;
						tmpTop = Math.abs(WinHeight * percent);
					} else if (TopStyle.search(/px/i)>-1) {
						tmpTop = Math.abs(parseFloat(TopStyle.replace(/px/,"")));
					}
	
					// Determine the numeric Bottom Value
					if (BottomStyle.search(/%/i)>-1) {
						var percent = parseFloat(BottomStyle.replace(/%/,"")) / 100;
						tmpBottom = Math.abs(WinHeight * percent);
					} else if (BottomStyle.search(/px/i)>-1) {
						tmpBottom = Math.abs(parseFloat(BottomStyle.replace(/px/,"")));
					}
					
					var strPaddingTop = "";
					var PaddingTop = 0;
					var strPaddingBottom = "";
					var PaddingBottom = 0;
					strPaddingTop = getStyle(obj, "padding-top");
					strPaddingBottom = getStyle(obj, "padding-bottom");
					if (strPaddingTop.search(/em/i)>-1) {
						PaddingTop = parseFloat(strPaddingTop.replace(/em/,"")) * 16;
					} else if (strPaddingTop.search(/px/i)>-1) {
						PaddingTop = Math.abs(parseFloat(strPaddingTop.replace(/px/,"")));
					}
					if (strPaddingBottom.search(/em/i)>-1) {
						PaddingBottom = parseFloat(strPaddingBottom.replace(/em/,"")) * 16;
					} else if (strPaddingBottom.search(/px/i)>-1) {
						PaddingBottom = Math.abs(parseFloat(strPaddingBottom.replace(/px/,"")));
					}
	
					var CalculatedObjHeight = tmpHeight;
					var CalulatedWinHeight = WinHeight - tmpTop - tmpBottom - PaddingTop - PaddingBottom;
					
					if (CalculatedObjHeight < CalulatedWinHeight) {
						obj.className = obj.className + " topOff";
					}
				}
				break;
				
			case "l":
				//Get the relevant styles that affect this measurement and position
				var leftStyle = getStyle(obj, "left");
				var rightStyle = getStyle(obj, "right");
				var objwidth = getStyle(obj, "max-width");
				if (objwidth == null) { objwidth = getStyle(obj, "width"); }
	
				if (leftStyle!="" && rightStyle!="" && objwidth!="") {
					//Reset the tmp values
					var tmpleft = 0;
					var tmpright = 0;
					var tmpwidth = 0;
					var Winwidth = getViewportWidth();
	
					//Determine the numeric objwidth value
					if (objwidth.search(/%/i)>-1) {
						var percent = parseFloat(objwidth.replace(/%/,"")) / 100;
						tmpwidth = Winwidth * percent;
					} else if (objwidth.search(/px/i)>-1) {
						tmpwidth = parseFloat(objwidth.replace(/px/,""));
					}
					
					//Determine the numeric left value
					if (leftStyle.search(/%/i)>-1) {
						var percent = parseFloat(leftStyle.replace(/%/,"")) / 100;
						tmpleft = Math.abs(Winwidth * percent);
					} else if (leftStyle.search(/px/i)>-1) {
						tmpleft = Math.abs(parseFloat(leftStyle.replace(/px/,"")));
					}
	
					// Determine the numeric right Value
					if (rightStyle.search(/%/i)>-1) {
						var percent = parseFloat(rightStyle.replace(/%/,"")) / 100;
						tmpright = Math.abs(Winwidth * percent);
					} else if (rightStyle.search(/px/i)>-1) {
						tmpright = Math.abs(parseFloat(rightStyle.replace(/px/,"")));
					}
					
					var strPaddingleft = "";
					var Paddingleft = 0;
					var strPaddingright = "";
					var Paddingright = 0;
					strPaddingleft = getStyle(obj, "padding-left");
					strPaddingright = getStyle(obj, "padding-right");
					if (strPaddingleft.search(/em/i)>-1) {
						Paddingleft = parseFloat(strPaddingleft.replace(/em/,"")) * 16;
					} else if (strPaddingleft.search(/px/i)>-1) {
						Paddingleft = Math.abs(parseFloat(strPaddingleft.replace(/px/,"")));
					}
					if (strPaddingright.search(/em/i)>-1) {
						Paddingright = parseFloat(strPaddingright.replace(/em/,"")) * 16;
					} else if (strPaddingright.search(/px/i)>-1) {
						Paddingright = Math.abs(parseFloat(strPaddingright.replace(/px/,"")));
					}
	
					var CalculatedObjwidth = tmpwidth;
					var CalulatedWinwidth = Winwidth - tmpleft - tmpright - Paddingleft - Paddingright;
					
					if (CalculatedObjwidth < CalulatedWinwidth) {
						obj.className = obj.className + " rightOff";
					}
				}
				break;
				
			default :
				// Error: Bad anchorEdge value.
				alert("Error. 'anchorEdge' values are ['t', 'r', 'b', 'l'].");
		}
	}
}