var pages=new Array("overview","features","libraries","downloads","licensing");
var contents_path = './content/';
var siteName='SevenHats';
var MenuFade = new Array();
var HeaderFade;
var ContentFade;
var CurrentPage="";
var ExpectedHash="";
var currentMenuItemOpacity = 0.4;

function _initiate(){
		
		ContentFade = new Fx.Style('PageContent', 'opacity', {duration:150});
		HeaderFade = new Fx.Style('PageHeader', 'opacity', {duration:150});
		for(var i=0; i<pages.length; i++){
			MenuFade.push(new Fx.Style('menu::'+pages[i], 'opacity', {duration:250}));
			MenuFade[i].set(currentMenuItemOpacity);
		}
		
		handleHistory();
		window.setInterval("handleHistory()", 50);
		
		_setCurrentPage();
		_showContent(true);
}

function _showContent(initial){
	if(!initial){
		ContentFade.start(1,0);
		HeaderFade.start(1,0);
	} else {
		ContentFade.set(0);
		HeaderFade.set(0);
	}
	if (pages.toString().indexOf(CurrentPage)==-1){ CurrentPage=pages[0];	}
	setTimeout('_loadContent("'+CurrentPage+'")',250);
	
	for(var i=0; i<pages.length; i++){
		var menu_item=$('menu::'+pages[i]);
		if(menu_item){
			if(pages[i]!=CurrentPage){
				menu_item.getElementsByTagName('a')[0].className='';
				if(menu_item.style.opacity<1){ MenuFade[i].start(currentMenuItemOpacity, 1); }
				
			}else if(pages[i]==CurrentPage){
				menu_item.getElementsByTagName('a')[0].className='current';
				if(menu_item.style.opacity>currentMenuItemOpacity){	MenuFade[i].start(1, currentMenuItemOpacity); }
			}
		}
	}
	
}

function _setCurrentPage(){
	
	ExpectedHash=window.location.hash;
	
	if(window.location.hash.length>1){
		for(var i=0;i<pages.length;i++){
			if(pages[i]==window.location.hash.substring(1)){
				CurrentPage=pages[i];
				return true;
			}
		}
	}
	
	CurrentPage=pages[0];
	
	//window.location.hash=pages[0];

}

function _loadContent(){
	
	new Ajax(contents_path+CurrentPage+'.html', {
		update: $('PageContent'),
		onComplete: function(){ document.title=siteName + ': ' + CurrentPage.charAt(0).toUpperCase() + CurrentPage.substr(1).toLowerCase();
								$('PageHeader').innerHTML=CurrentPage;
								ContentFade.start(0, 1);
								HeaderFade.start(0, 1);
							}
	}).request();
	
}

function handleHistory(){
  if ( window.location.hash != ExpectedHash ) {
	_setCurrentPage();
	_showContent(false);
  }
}