// Consolidated js file for home page js scripts.

// loads the homepage flash once the page has loaded
// this should keep the page load times down
function  loadHomeFlash() {
	var elementID = "homeBanner";
	if(document.getElementById(elementID)){
		var flashvars = {};
		var params = {
			wmode: "transparent",
			allowscriptaccess: "sameDomain"
		};
		var attributes = {};
		swfobject.embedSWF("flash/roulette-v4.swf", "homeBanner", "936", "380", "9.0.0","expressInstall.swf", flashvars, params, attributes);
		//mouseover event is required for FF click to activate bug when scrolling window
		document.getElementById(elementID).onmouseover = function() {
			this.style.backgroundImage = "url(/images/transparent.gif)";
			setTimeout('revertBg("'+this.getAttribute("id")+'")',1);
		}
	}
}

addLoadEvent(loadHomeFlash);


// JS to handle tabbed section at the bottom of the home page
function populateTabs() {
	
	//identify the welcome area
	var welcomeBox = document.getElementById("welcome");
	//identify base content section
	var baseContentDiv = document.getElementById("baseContent");
	//identify tabbed section
	var tabbedSectionDiv = document.getElementById("tabbedSection");
	//identify tab list
	if(tabbedSectionDiv.getElementsByTagName("ul").length > 0){
		var tabList = tabbedSectionDiv.getElementsByTagName("ul")[0];
		tabList.getElementsByTagName("a")[0].onclick = function() {
			openTab(this);
			return false;
		}
	} else {
		var tabList = document.createElement("ul");
		tabbedSectionDiv.insertBefore(tabList,tabbedSectionDiv.firstChild);
	}
	//identify div containing tab content
	var tabContent = tabbedSectionDiv.getElementsByTagName("div")[0];
	
	//first get h1 and place it at the head of the page.
	var h1Tag = welcomeBox.getElementsByTagName("h1")[0];
	var firstChildBC = baseContentDiv.firstChild;
	baseContentDiv.insertBefore(h1Tag, firstChildBC);
	
	//then get all the h2 tabs and div tabs below and append them to the tabbed ul as list items and parent tabbedSection div respectively.
	var h2Tags = welcomeBox.getElementsByTagName("h2");
	var welcomeDivs = welcomeBox.getElementsByTagName("div");
	
	for (var i=0; i < h2Tags.length; i++) {
		var tabText = h2Tags[i].childNodes[0].nodeValue;
		var newTab = document.createElement("li");
		var newTabLink = document.createElement("a");
		var newTabText = document.createTextNode(tabText);
		newTabLink.appendChild(newTabText);
		newTabLink.setAttribute("href","#");
		newTabLink.onclick = function () {
			openTab(this);
			return false;
		}
		newTab.appendChild(newTabLink);
		tabList.appendChild(newTab);
		welcomeBox.removeChild(h2Tags[i]);
		setOpacity(welcomeDivs[i].getAttribute("id"), 0);
		welcomeDivs[i].style.zIndex = "0";
		tabContent.appendChild(welcomeDivs[i]);
		i--; //since dom tree is live, you have to minus one off the iterator when an element is deleted.
	}
	document.getElementsByTagName("body")[0].removeChild(welcomeBox);
	activateRotation = setTimeout("rotateTabs()", 10000);
	//rotateTabs();
	
}

function openTab(clickedAnchor){
	
	//identify tab list
	var tabList = document.getElementById("tabbedSection").getElementsByTagName("ul")[0];
	//identify div containing tab content
	var tabContent = document.getElementById("tabbedSection").getElementsByTagName("div")[0];
	//declare tab index variable
	var tabIndex = 0;
	
	//loop through all the list items to find out what position the tab is in
	var tabItems = tabList.getElementsByTagName("li");
	var contentDivs = tabContent.getElementsByTagName("div");
	
	for (var i=0; i < tabItems.length; i++){
		if(tabItems[i].childNodes[0].childNodes[0].nodeValue == clickedAnchor.childNodes[0].nodeValue) {
			tabIndex = i;
			if(tabItems[i].className != "activeTab"){
				tabItems[i].className = "activeTab";
				contentDivs[i].style.zIndex = "10";
				fade(contentDivs[i].getAttribute("id"),0,100,1000);
			}
		} else {
			if(tabItems[i].className == "activeTab"){
				fade(contentDivs[i].getAttribute("id"),100,0,1000);
			}
			tabItems[i].className = "inactiveTab";
			contentDivs[i].style.zIndex = "0";
		}
	}	
}

function setOpacity(eID, opacityLevel) {
	var eStyle = document.getElementById(eID).style;
    eStyle.opacity = opacityLevel / 100;
    eStyle.filter = 'alpha(opacity='+opacityLevel+')';
}

function fade(eID, startOpacity, stopOpacity, duration) {
	var speed = Math.round(duration / 100);
    var timer = 0;
    if (startOpacity < stopOpacity){ // fade in
		for (var i=startOpacity; i<=stopOpacity; i++) {
            setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
			timer++;
        } return;
    } else {
		for (var i=startOpacity; i>=stopOpacity; i--) { // fade out
			setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
			timer++;
		}
	}
}

function openNextTab() {
	
	//identify tabbed section div
	var tabbedSectionDiv = document.getElementById("tabbedSection");
	//identify tab list
	var tabList = tabbedSectionDiv.getElementsByTagName("ul")[0];
	//get list items
	var tabs = tabList.getElementsByTagName("li");
	
	for (var i=0; i < tabs.length; i++){
		if(tabs[i].className == "activeTab") {
			
			if((i+1) < tabs.length) {
				openTab(tabs[i+1].getElementsByTagName("a")[0]);
			} else {
				openTab(tabs[0].getElementsByTagName("a")[0]);	
			}
			break;
		}
	}
}

function rotateTabs(){
	//call the openNextTab function at intervals
	tabRotation = setInterval("openNextTab()", 6000);
	
	//identify the parent div to set mouseover mouseout event handlers so that the
	//rotation stops while the mouse is hovering over
	var tabbedSectionDiv = document.getElementById("tabbedSection");
	
	tabbedSectionDiv.onmouseover = function() {
		clearInterval(tabRotation);	
	}
	
	tabbedSectionDiv.onmouseout = function() {
		rotateTabs();	
	}
	
}

addLoadEvent(populateTabs);








