window.onload = initHomepageTabs;

/*
addLoadEvent(initHomepageTabs);

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}*/
var currentPane = 1;

function initHomepageTabs(){
	if(document.getElementById('tab-heading')){
		var tabs = document.getElementById('tab-heading');
		var li = tabs.getElementsByTagName('li');
		var tabCount = 0;
		var tabWidth = 0;
		var tabNumber = 0;
		while(li[tabCount]){
			if(li[tabCount].id != ''){
				li[tabCount].onmouseover = tabOver;
				li[tabCount].onmouseout = tabOut;
				li[tabCount].onclick = tabOn;
				li[tabCount].tabNum = tabNumber + 1;
				tabNumber++;
			}
			tabWidth += li[tabCount].offsetWidth;
			tabCount++;
		}
		var availablePixels = tabs.offsetWidth - tabWidth;
		var chunk = Math.floor(availablePixels/tabNumber);
		availablePixels = availablePixels % tabNumber;
		tabCount = 0;
		while(li[tabCount]){
			if(li[tabCount].id != ''){
				innerDivs = li[tabCount].childNodes;
				if(availablePixels){
					innerDivs[0].style.width = innerDivs[0].offsetWidth + chunk + 1 + 'px';
					availablePixels--;
				}
				else{
					innerDivs[0].style.width = innerDivs[0].offsetWidth + chunk + 'px';
				}
			}
			tabCount++;
		}
	}
	//if(document.getElementById('tab-overlay')) {
		//document.getElementById('tab-overlay').style.display ='none'; //fixes the width adjustment of the homepage tabs
	//}
	
}

function tabOver(){
	var nodeChildren;
	if(this.className != 'on'){
		nodeChildren = this.childNodes;
		nodeChildren[1].className = 'right-hover';
		this.className = 'hover';
	}
}
function tabOut(){
	var nodeChildren;
	if(this.className != 'on'){
		nodeChildren = this.childNodes;
		nodeChildren[1].className = 'right';
		this.className = '';
	}
}
function tabOn(){
	var nodeChildren;
	if(this.className != 'on'){
		var i = 0;
		var t = document.getElementById('tab-heading').getElementsByTagName('li');
		while(t[i]){
			if(t[i].id != ''){
				if((t[i].id != this.id) && (t[i].className == 'on')){
					t[i].className = '';
					t[i].firstChild.nextSibling.className = 'right';
				}
			}
			i++;
		}
		nodeChildren = this.childNodes;
		nodeChildren[1].className = 'right-on';
		this.className = 'on';
		if(document.getElementById('block'+this.tabNum)){
			if(document.getElementById('block'+currentPane)){
				document.getElementById('block'+currentPane).className = 'block hide';
				document.getElementById('block'+this.tabNum).className = 'block';
				currentPane = this.tabNum;
			}
		}
	}
	return false;
}


