function $(anId){
	return document.getElementById(anId);
}
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; 
 } 
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}
	
(aigagJS = function(){
		// private
		var searchTextClicked = false;
		//var currGlobalNavSelected=$("globalNavLink1");
	return {
		currGlobalNavSelected:$("globalNavLink1"),
		evenHeight: function(eleId,subClass,arr) {
			if ($(eleId) != null) {
				if (arr == null) {
					arr = $(eleId).getElementsByTagName("div");
				}
				var newArr = [];
				var maxHeight = 0;
				for (var i = 0; i < arr.length; i++) {
					if (arr[i].className.indexOf(subClass) >= 0) {
						newArr.push(arr[i]);
						if (arr[i].offsetHeight > maxHeight) {
							maxHeight = parseInt(arr[i].offsetHeight) / 13;
						}
					}
				}
				for (var j = 0; j < newArr.length; j++) {
					newArr[j].style.height = maxHeight + "em";
				}
			}
		},
		initMiscFunc: function() {
			if ($("email")!=null) {
				$("email").onclick=function() {
					window.location.href="mailto:?subject="+document.title+"&body="+document.location;
				}
			}
			if ($("print")!=null) {
				$("print").onclick=function() {
					window.print();
				}
			}
			if ($("pdf")!=null) {
				$("pdf").onclick=function() {
					window.open();
				}
			}
		},
		initGlobalNav: function() {
			try {
				arr = $("globalNavLinks").getElementsByTagName("ul")[0].getElementsByTagName("li");
				
			} catch(e) {return;}
			if (arr.length!=0 && $("subNavContainer")!=null) {
				var newArr=[];
				for (var i = 0; i < arr.length; i++) {
					if (arr[i].id.indexOf("globalNavLink")>=0) {
						newArr.push(arr[i]);
					}
				}
				
				for (var j=0;j<newArr.length;j++) {
					arr[j].onmouseover=function(e) {
						aigagJS.currGlobalNavSelected = this;
						var num = this.id.toString().substring(13,14);
						
						if ($("subNav")!=null) {
							var subNavArr = $("subNav").getElementsByTagName("div");
							for (var k=0;k<subNavArr.length;k++) {
								subNavArr[k].style.display="none";
							}
							$("subNav").style.display="block";
						}
						var el = $("subNavLinks"+num);
						if (el != null) {
							el.style.display = "block";
							$("globalNavIndicator").style.display="block";
							
						}
						$("globalNavIndicator").style.display = "block";
						$("globalNavIndicator").style.left = findPos(this)[0] - (parseInt(document.body.clientWidth - $("body_wrapper").offsetWidth)/2)+parseInt((this.offsetWidth/2))-20+"px" ;
						
					}
				}
			}
		},
		initVideoLightBox: function() {
			if ($("videoContent")!=null) {
				if ($("lightBox") != null && $("overlay") != null) {
					var d1=$("lightBox");
					var d2=$("overlay");
					d1.parentNode.removeChild(d1);
					d2.parentNode.removeChild(d2);
					document.body.appendChild(d1);
					document.body.appendChild(d2);
					d2.style.height=$("body_wrapper").offsetHeight+"px";
					d2.style.width=document.body.clientWidth+"px";
					addEvent(d2, 'click', function(){
							$("lightBox").style.display = "none";
							$("overlay").style.display = "none";
						})
					var numVideo = 0;
					var i = 1;
					while ($("video" + i + "Play") != null) {
						i++;
					}
					numVideo=i-1;
					i=1;
					function initVideoLightBoxHelper() {
						$("lightBox").style.display = "block";
							$("overlay").style.display = "block";
							$("lightBox").style.left=parseInt(document.body.clientWidth/2)-parseInt($("lightBox").offsetWidth/2)+"px";
							if (document.documentElement.scrollTop>=0) {
								d1.style.top = (parseInt(document.documentElement.scrollTop) + 20) + "px";
							}
							else {
								d1.style.top = (parseInt(window.pageYOffset) + 20) + "px";
							}
					}
					while ($("video" + i + "Play") != null) {
						$("video" + i + "Play").onclick= function(){
							initVideoLightBoxHelper();
							aigagJS.videoLightBoxContent(this.id.toString().substring(5,6),numVideo);
						};
						$("video" + i + "Thumb").onclick= function(){
							initVideoLightBoxHelper();
							aigagJS.videoLightBoxContent(this.id.toString().substring(5,6),numVideo);
						};
						i++;
					}
					if ($("closeLightBox")!=null) {
						addEvent($("closeLightBox"), 'click', function(){
							$("lightBox").style.display = "none";
							$("overlay").style.display = "none";
						})
					}
				}
			}
		},
		videoLightBoxContent:function(videoId,totalNumVideo) {
			if (videoId == 1) {
				$("prevClip").style.display = "none";
			} else {
				$("prevClip").style.display="inline";
				$("prevClip").onclick=function(){
					aigagJS.videoLightBoxContent(parseInt(videoId)-1,totalNumVideo)
				};
			}
			if (videoId==totalNumVideo) {
				$("nextClip").style.display="none";
			} else {
				$("nextClip").style.display="inline";
				$("nextClip").onclick=function(){
					aigagJS.videoLightBoxContent(parseInt(videoId)+1,totalNumVideo)
				};
			}
			if (totalNumVideo==0) {
				$("nextClip").style.display="none";
				$("prevClip").style.display = "none";
			}
			
			
			/*
			 Change video, title, description here
			 Example: 
			$("videoMedia").title="Video" + videoId;
			 */
			if ($("videoMedia")) $("videoMedia").title="Video " + videoId;
			
			
		},
		resetSizes: function() {
			aigagJS.evenHeight("subFeatures","homepageFeatures",null);
			if ($("additionalFeatures") != null && $("additionalFeatureModule1") != null && $("additionalFeatureModule2") != null && $("additionalFeatureModule3") != null) {
				aigagJS.evenHeight("additionalFeatures", "additionalFeatureMoeduleBody", [$("additionalFeatureModule1").getElementsByTagName("div")[1], $("additionalFeatureModule2").getElementsByTagName("div")[1], $("additionalFeatureModule3").getElementsByTagName("div")[1]]);
				//aigagJS.evenHeight("additionalFeatures", "additionalFeatureMoeduleHeader", [$("additionalFeatureModule1").getElementsByTagName("div")[0], $("additionalFeatureModule2").getElementsByTagName("div")[0], $("additionalFeatureModule3").getElementsByTagName("div")[0]]);
			}
			if ($("bodyModuleContent")) {
				if (!$("bodyModuleContent4")) {
					$("bodyModuleContent1").style.width=parseInt($("bodyModuleContent1").offsetWidth)+75+"px";
					$("bodyModuleContent2").style.width=parseInt($("bodyModuleContent2").offsetWidth)+75+"px";
					$("bodyModuleContent3").style.width=parseInt($("bodyModuleContent3").offsetWidth)+75+"px";
				}
			}
		},
		init: function() {
			aigagJS.resetSizes();
			/*
			$("searchText").addEventListener("focus",function(){
				if (!aigagJS.searchTextClicked) {
					$("searchText").text="";
					aigagJS.searchTextClicked=true;
				}
				},false)
			*/
			/*
			$("globalNavIndicator").style.left = findPos($("globalNavLink1"))[0] - (parseInt(document.body.clientWidth - $("body_wrapper").offsetWidth)/2)+parseInt(($("globalNavLink1").offsetWidth/2))-20+"px" ;
			addEvent($("searchText"),'focus',function(){
				if (!aigagJS.searchTextClicked) {
					$("searchText").value="";
					aigagJS.searchTextClicked=true;
				}
				});
				*/
			function initSizes() {
				$("textSizer").style.fontSize = "12px";
				$("textSizer").style.lineHeight = "12px";
				$("textSizerS").style.cursor = "pointer";
				$("textSizerM").style.cursor = "pointer";
				$("textSizerL").style.cursor = "pointer";
			}
			if ($("textSizerS") != null & $("textSizerM") != null && $("textSizerL") != null) {
				addEvent($("textSizerS"), 'click', function(){
					var oldNavIndicatorLoc=findPos(aigagJS.currGlobalNavSelected)[0];
					var oldNavIndicatorLeft=parseInt($("globalNavIndicator").style.left.substring(0,$("globalNavIndicator").style.left.length-2));
					var oldWidth=aigagJS.currGlobalNavSelected.offsetWidth;
					document.body.style.fontSize = "80%";
					initSizes();
					var newNavIndicatorLoc=findPos(aigagJS.currGlobalNavSelected)[0];
					var newWidth=aigagJS.currGlobalNavSelected.offsetWidth;
					$("textSizerS").style.cursor = "default";
					$("globalNavIndicator").style.left = oldNavIndicatorLeft - (oldNavIndicatorLoc - newNavIndicatorLoc) - (oldWidth-newWidth)/2 +"px";
				})
				addEvent($("textSizerM"), 'click', function(){
					var oldNavIndicatorLoc=findPos(aigagJS.currGlobalNavSelected)[0];
					var oldNavIndicatorLeft=parseInt($("globalNavIndicator").style.left.substring(0,$("globalNavIndicator").style.left.length-2));
					var oldWidth=aigagJS.currGlobalNavSelected.offsetWidth;
					document.body.style.fontSize = "95%";
					initSizes();
					var newNavIndicatorLoc=findPos(aigagJS.currGlobalNavSelected)[0];
					var newWidth=aigagJS.currGlobalNavSelected.offsetWidth;
					$("textSizerM").style.cursor = "default";
					$("globalNavIndicator").style.left = oldNavIndicatorLeft - (oldNavIndicatorLoc - newNavIndicatorLoc) - (oldWidth-newWidth)/2 +"px";
				})
				addEvent($("textSizerL"), 'click', function(){
					var oldNavIndicatorLoc=findPos(aigagJS.currGlobalNavSelected)[0];
					var oldNavIndicatorLeft=parseInt($("globalNavIndicator").style.left.substring(0,$("globalNavIndicator").style.left.length-2));
					var oldWidth=aigagJS.currGlobalNavSelected.offsetWidth;
					document.body.style.fontSize = "110%";
					initSizes();
					var newNavIndicatorLoc=findPos(aigagJS.currGlobalNavSelected)[0];
					var newWidth=aigagJS.currGlobalNavSelected.offsetWidth;
					$("textSizerL").style.cursor = "default";
					$("globalNavIndicator").style.left = oldNavIndicatorLeft + (newNavIndicatorLoc - oldNavIndicatorLoc) + (newWidth-oldWidth)/2 +"px";
				})
			};
			if ((!$("leftNav") || $("leftNav").style.display=="none") && $("icons")) {
				$("icons").className="template2";
			}
			aigagJS.initVideoLightBox();
			aigagJS.initMiscFunc();
			aigagJS.initGlobalNav();
		}
	}
}()).init();
