/***** ONLOAD EVENTS *****/
window.onload = function () {
	if (document.getElementById("topNav")) {
		//Setup scroll div
		setupScrollDiv();
	}
	
	//Assign all mouseevents to the menu
	setupMenu();
	
	//Setup all the divs
	setupDivs();
	
	//Setup news menu
	setupNewsMenu();
	
	fixIE6Styles();

	MM_CheckFlashVersion('8,0,0,0','Content on this page requires a newer version of Adobe Flash Player. Do you want to download it now?');
}

/***** SETUP FUNCTIONS *****/

function doAltHeightChangeMem(elem,startHeight,endHeight,steps,intervals,powr) {
//Height changer with Memory by www.hesido.com
	if (elem.heightChangeMemInt) window.clearInterval(elem.heightChangeMemInt);
	var actStep = 0;
	elem.heightChangeMemInt = window.setInterval(
		function() {
			elem.currentHeight = easeInOut(startHeight,endHeight,steps,actStep,powr);
			elem.style.height = elem.currentHeight+"px";
			actStep++;
			if (actStep > steps) window.clearInterval(elem.heightChangeMemInt);
		}
		,intervals)

}

//Setup the various formated divs
function setupDivs() {
	var divTags = document.getElementsByTagName("div");
	for (i=0;i<divTags.length;i++) {
		if (divTags[i].className == "colorBoxContainerMain") {
			divTags[i].style.width = divTags[i].offsetWidth - 60 + "px";
			divTags[i].style.height = 29 + "px";	
			divTags[i].style.paddingTop= 3 + "px";
		}
		if (divTags[i].className == "colorBoxContainer") {
			divTags[i].style.width = divTags[i].offsetWidth-34 + "px";
		}
		if (divTags[i].className == "tableRow") {		
			divTags[i].style.height = divTags[i].offsetHeight + 12 + "px";
		}
	}
}

//Assign all the mouseevents to the menu
function setupMenu() {
	//Init fade animation with memory, both directions
	var animElements = document.getElementById("menu").getElementsByTagName("a");
	for(var i=0; i<animElements.length; i++) {
		if (animElements[i].className !="selected") {
			addEvent(animElements[i], "mouseout", function() {fadeTxtColRestore(this)});
			//assign events to the embedded span in artcoustics
			if (animElements[i].firstChild.nextSibling) {
				addEvent(animElements[i], "mouseout", function() {doTxtFade(this.firstChild.nextSibling,this.firstChild.nextSibling.currentbgRGB,[60,66,65],12,20,1)});
				addEvent(animElements[i], "mouseover", function() {fadeTxtColMem(this.firstChild.nextSibling, [124,122,1]);});
			}
			//Assign the right colour to the right menu item
			switch(i) {
				case 0:
					//Blue
					addEvent(animElements[i], "mouseover", function() {fadeTxtColMem(this, [246,0,255]);});
					break;    
				case 1:
					//Purple
					addEvent(animElements[i], "mouseover", function() {fadeTxtColMem(this, [0,208,240]);});
					break; 
				case 2:
					//Green
					addEvent(animElements[i], "mouseover", function() {fadeTxtColMem(this, [157,245,2]);});
					break; 			  
				case 3:
					//Yellow
					addEvent(animElements[i], "mouseover", function() {fadeTxtColMem(this, [252,247,1]);});
					break; 
				case 4:
					//Red
					addEvent(animElements[i], "mouseover", function() {fadeTxtColMem(this, [255,66,0]);});
					break;
 
				case 5:
					//white
					addEvent(animElements[i], "mouseover", function() {fadeTxtColMem(this, [255,255,255]);});
					break; 
			}
		}
	}
}

function setupScrollDiv() {

	var imgTags = document.getElementById("topNav").getElementsByTagName("img");
	var navListOffsetWidth = 0;
	var scrollToPos = 0;

	//Loop through the front images (not including the man image),
	//workout the total width and align images to vertical middle
	for (i=0;i < imgTags.length;i++) {
		if (imgTags[i].id != "man") {			
			navListOffsetWidth =navListOffsetWidth + imgTags[i].offsetWidth;
			imgTags[i].style.marginTop = ((306 - imgTags[i].offsetHeight)/2)+"px";
		}
	}

	//Set the total width
	document.getElementById("topNavContainer").style.width = navListOffsetWidth+"px";
	for (z=0;z < imgTags.length;z++) {
		if (imgTags[z].className=="selected"){
			scrollToPos = imgTags[z].offsetLeft;
		}
	}
	// Initialize scroll area
	// arguments: id of outer div, id of content div
	var wndo = new dw_scrollObj('topNav', 'topNavContainer');
	wndo.setUpScrollControls('scrollLinks');
	wndo.setUpScrollControls('scrollLinks2');

	// Initialize scrollbar
	// id of dragbar, id of track, 
	// axis (v for vertical scrolling, 'h' for horizontal)
	// horizontal offset of dragbar in track, vertical offset
	// size dragBar according to amount of content? (boolean)
	wndo.setUpScrollbar('dragBar', 'track', 'h', 0, 0, false);

	// Initialize scroll links
	// id of element within which to locate scroll controls
	wndo.setUpScrollControls('scrollbar');
	wndo.initScrollToVals(scrollToPos ,0,0.5)
		
	//Assign img swap function to the scroll arrow icons
	addEvent(document.getElementById("leftScrollArrow"), "mouseover", function() {imgSwp(this, 'tri-lft_on.gif')});
	addEvent(document.getElementById("leftScrollArrow"), "mouseout", function() {imgSwp(this, 'tri-lft.gif')});
	addEvent(document.getElementById("rightScrollArrow"), "mouseover", function() {imgSwp(this, 'tri-rt_on.gif')});
	addEvent(document.getElementById("rightScrollArrow"), "mouseout", function() {imgSwp(this, 'tri-rt.gif')});	
}		

function setupNewsMenu() {
	if (document.getElementById("topRight")){
		var rightMenuHeaders = document.getElementById("topRight").getElementsByTagName("li");
		var subAtags =  document.getElementById("topRight").getElementsByTagName("a");
		var totalHeight = 0;
		
		//Loop through all the level1 LI items, workout the totalHeight and assign the menu animation functions
		for (i=0;i<rightMenuHeaders.length;i++) {
			if (rightMenuHeaders[i].className == "level1") {
				totalHeight = totalHeight+23;
				addEvent(rightMenuHeaders[i].childNodes[0], "click", function() {doHeightChangeMem(this.parentNode, this.offsetHeight, this.nextSibling.offsetHeight+this.offsetHeight,10,10,0.9)});	
			}
		}
		
		//Loop through all the a tags, find the selected item and open the containing menu
		for (z=0;z<subAtags.length;z++) {

			if (subAtags[z].className == "selected") {
doHeightChangeMem(subAtags[z].parentNode.parentNode.parentNode, subAtags[z].parentNode.parentNode.previousSibling.offsetHeight, subAtags[z].parentNode.parentNode.offsetHeight+subAtags[z].parentNode.parentNode.previousSibling.offsetHeight,10,10,0.9);
			}
		}
		//Add the number of 12 months in height and the offset from the the first header to the totalHeight
		totalHeight = totalHeight + 220;
		//If the isotm div is taller than the rightTop div, use it's offsetHeight instead
		if (document.getElementById("iotm").offsetHeight > totalHeight) {
			totalHeight = document.getElementById("iotm").offsetHeight;
		}
		
		//Set the left content div height
//		document.getElementById("iotm").style.height = totalHeight + "px";
		//Set the right menu div height
		//Add the padding top to the total to push the right side down 
//		document.getElementById("topRight").style.height = totalHeight + 20 + "px";
	}
}

function fadeTxtColMem(elem, endColor) {
	if (!elem.currentbgRGB) elem.currentbgRGB = [115, 123, 121]; //if no mem is set, set it first;
	doTxtFade(elem,elem.currentbgRGB,endColor,4,20,1);
}

function fadeTxtColRestore(elem) {
	if (!elem.currentbgRGB) return;	//avoid error if mouseout an element occurs before the mosueover								//(e.g. the pointer already in the object when onload)
	doTxtFade(elem,elem.currentbgRGB,[115, 123, 121],12,20,1);
}

function imgSwp(elem, imgSrc) {
	elem.src = "/images/"+ imgSrc;
}

function fixIE6Styles() {


	if (navigator.appVersion.indexOf("MSIE") != -1) {
		var mysheet=document.styleSheets[0]
		var myrules=mysheet.cssRules? mysheet.cssRules: mysheet.rules
		for (i=0; i<myrules.length; i++){		
			if(myrules[i].selectorText.toLowerCase()==".inlineelems input"){
				targetrule=myrules[i]
				targetrule.style.marginTop = 0+"px";				
			}		
			if(myrules[i].selectorText.toLowerCase()==".tablerowright input"){				
				targetrule=myrules[i]
				targetrule.style.marginTop = -2+"px";				
			}
		}
	}
	if (navigator.userAgent.indexOf("Firefox") != -1 && navigator.userAgent.indexOf("Macintosh") != -1) {
		var mysheet=document.styleSheets[0]
		var myrules=mysheet.cssRules? mysheet.cssRules: mysheet.rules
		for (i=0; i<myrules.length; i++){		
			if(myrules[i].selectorText.toLowerCase()=="#menu"){
				targetrule=myrules[i]
				targetrule.style.paddingTop = 90+"px";				
			}	
			if(myrules[i].selectorText.toLowerCase()=="#maincontent a"){
				targetrule=myrules[i]
				targetrule.style.backgroundPosition= "10px 0px";				
			}	
			if(myrules[i].selectorText.toLowerCase()=="#rightnav a.leftarrow"){
				targetrule=myrules[i]
				targetrule.style.backgroundPosition= "96% 0px";				
			}	
			if(myrules[i].selectorText.toLowerCase()=="#rightnav .level1"){
				targetrule=myrules[i]
				targetrule.style.backgroundPosition= "96% 0px";				
			}	
			if(myrules[i].selectorText.toLowerCase()==".tablerowright br"){
				targetrule=myrules[i]
				targetrule.style.lineHeight= "29px";				
			}	
			if(myrules[i].selectorText.toLowerCase()==".tablerow br"){
				targetrule=myrules[i]
				targetrule.style.lineHeight= "29px";				
			}	

		}
	}
}

/***** HELPER FUNCTIONS *****/

//Adds an event on an object
function addEvent( obj, type, fn ) { 

	if ( obj.attachEvent ) { 
		obj['e'+type+fn] = fn; 
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
		obj.attachEvent( 'on'+type, obj[type+fn] ); 
	} 
	else 
		obj.addEventListener( type, fn, false ); 
} 

//Removes an event on an object
function removeEvent( obj, type, fn ) { 
	if ( obj.detachEvent ) { 
		obj.detachEvent( 'on'+type, obj[type+fn] ); 
		obj[type+fn] = null;
	} 
	else 
    		obj.removeEventListener( type, fn, false ); 
} 

function removeClassName (elem, className) {
	elem.className = elem.className.replace(className, "").trim();
}

function addCSSClass (elem, className) {
	removeClassName (elem, className);
	elem.className = (elem.className + " " + className).trim();
}
function MM_CheckFlashVersion(reqVerStr,msg){
  with(navigator){
    var isIE  = (appVersion.indexOf("MSIE") != -1 && userAgent.indexOf("Opera") == -1);
    var isWin = (appVersion.toLowerCase().indexOf("win") != -1);
    if (!isIE || !isWin){  
      var flashVer = -1;
      if (plugins && plugins.length > 0){
        var desc = plugins["Shockwave Flash"] ? plugins["Shockwave Flash"].description : "";
        desc = plugins["Shockwave Flash 2.0"] ? plugins["Shockwave Flash 2.0"].description : desc;
        if (desc == "") flashVer = -1;
        else{
          var descArr = desc.split(" ");
          var tempArrMajor = descArr[2].split(".");
          var verMajor = tempArrMajor[0];
          var tempArrMinor = (descArr[3] != "") ? descArr[3].split("r") : descArr[4].split("r");
          var verMinor = (tempArrMinor[1] > 0) ? tempArrMinor[1] : 0;
          flashVer =  parseFloat(verMajor + "." + verMinor);
        }
      }
      // WebTV has Flash Player 4 or lower -- too low for video
      else if (userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 4.0;

      var verArr = reqVerStr.split(",");
      var reqVer = parseFloat(verArr[0] + "." + verArr[2]);
  
      if (flashVer < reqVer){
        if (confirm(msg))
          window.location = "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
      }
    }
  } 
}
function showAltBox (id, posElem) {
	if (document.getElementById(id)) {
		var divElem = document.getElementById(id);
		divElem.style.left = findPos(posElem)[0] + "px";
		divElem.style.top = findPos(posElem)[1] + 40 + "px";
		divElem.style.display = "block";
	}
}

function hideAltBox (id) {
	if (document.getElementById(id)) {
		var divElem = document.getElementById(id);
		divElem.style.display = "none";
	}
}


function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}