/*
 * @author Jason Campbell
 * @copyright 2010
 */
var mozilla,ieVer=99;

function $_$(id){
	return document.getElementById(id);
}
function $$$(container,tag){
	return container.getElementsByTagName(tag);
}

var page;


var windows=new Array();
var windowsCount=0;
var currentTab='tab_0';

function displayWindow(tab){
	currentTab=tab;
	targetWindow=tab.replace('tab','window');
	
	for(var i=0;i<windowsCount;i++){
		if(windows[i].id==targetWindow){
			$_$('tab_'+i).parentNode.style.background='#fff';
			windows[i].style.display='block';
			$_$('windows').style.height=windows[i].offsetHeight+20+'px';
		}
		else{
			$_$('tab_'+i).parentNode.style.background='#f3f3f3';
			windows[i].style.display='none';
		}
	}
	//if(page=='home')setFunctionIds();
}

function highlightTab(state,tab){
	if(state=='on'){
			$_$(tab).parentNode.style.background='#fff';
	}
	else{
		if(tab!=currentTab)
			$_$(tab).parentNode.style.background='#f3f3f3';
	}
}

function tabs(){
var bod=$$$(document,'body');
	for(i in bod){
		if(bod[i].className=='home')
			page='home';
		if(bod[i].className=='interior')
			page='interior';
	}

	var window=$$$($_$('windows'),'div');
	var tabs=$$$($_$('tabs'),'a');
	
	for(i=0;i<window.length;i++){
			if(window[i].className=='window')
			windows.push(window[i]);
	}
	
	delete window;
	for(i=0;i<windows.length;i++){
			if(windows[i].className=='window')
			windows[i].id='window_'+i;
			windowsCount++;
	}
	
	for(i=0;i<tabs.length;i++){
		tabs[i].className='tab';
		tabs[i].id='tab_'+i;
		//set tab widths
		tabs[i].parentNode.style.width=$_$('windows').offsetWidth/tabs.length+'px';
		//shrink last two tab widths for Firefox;
		if(mozilla && page=='home' && (i>tabs.length-2))
		tabs[i].parentNode.style.width=$_$('windows').offsetWidth/tabs.length-1+'px';
 		if(page=='interior')
		tabs[i].parentNode.style.width=$_$('windows').offsetWidth/tabs.length-1+'px';
 		
 		tabs[i].onmouseover=function(){highlightTab('on',this.id);}
		tabs[i].onmouseout=function(){highlightTab('off',this.id);}
		tabs[i].onmouseup=function(){displayWindow(this.id);}
	}
	
	// initially show first window
		$_$('window_0').style.display='block';
	$_$('tab_0').parentNode.style.background='#fff';
	
	//nothing has height -fucking footer wont move
		$_$('windows').style.height=$_$('window_0').offsetHeight+20+'px';
}

function addEvent(elm, evType, fn, useCapture){
	if(elm.addEventListener){//firefox, safari, chrome, opera
		elm.addEventListener(evType, fn, useCapture);
		if(navigator.userAgent.indexOf('Firefox')!=-1)
			mozilla=true;
			
		return true;
	}else if(elm.attachEvent){//ie
		ieVer=navigator.userAgent.charAt(navigator.userAgent.indexOf('MSIE')+5);
		ieVer=parseInt(ieVer);
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}else{
		elm['on' + evType] = fn;
	}
}
addEvent(window, 'load', tabs, false);

