// JavaScript Document
function mouseOver(e){
		if(e){
			_source = e.target;
		}else{
			_source = window.event.srcElement;
		}
		switch(_source.tagName){
		case 'LI':if(_source.className!='selected'){
					_source.className = 'hover';
				  }	
				  break;
		case 'A':if(_source.parentNode.tagName=='LI'&&_source.parentNode.className!='selected'){
					_source.parentNode.className = 'hover';
				 }
				 break;
		}
	}
	
function mouseOut(e){
	if(e){
		_source = e.target;
	}else{
		_source = window.event.srcElement;
	}
		switch(_source.tagName){
		case 'LI':if(_source.className!='selected'){
					_source.className = '';
				  }
				  break;
		case 'A':if(_source.parentNode.tagName=='LI'&&_source.parentNode.className!='selected'){
					_source.parentNode.className = '';
				 }
				 break;
		}
}

function mouseDown(e){
	if(e){
		_source = e.target;
	}else{
		_source = window.event.srcElement;
	}
		switch(_source.tagName){
		case 'LI':var _a = _source.getElementsByTagName('A');
				  if(_a.length==1){
					document.location.href = _a[0].href;  
				  }
				  break;
		}
}

function TIAC(){
	this.topNav;
	this.leftNav;
	this.breadCrumbs;
	this.pageTitle;
	this.init=function(){
		//alert('init');
		menu.linkNodes();
		this.topNav = 'nav';
		this.leftNav = 'navbar';
		this.breadCrumbs = 'breadcrumbs';
		this.pageTitle = 'pageTitle';
			
		this.checkLoaded();
	}

	this.init2=function(){
		menu.buildTop();
		if(menu.currentNode){
			menu.buildLeft(menu.currentNode);
			var _title = util.dE(this.pageTitle);
			if(_title){
				_title.innerHTML = menu.currentNode.name;
			}
			menu.breadCrumbs();
			document.title = menu.currentNode.name;
		}
	}
	
	this.checkLoaded=function(){
		var _test = util.dE(this.topNav);
		if(!_test){
			window.setTimeout('tiac.checkLoaded()',10);	
		}else{
			this.init2();	
		}
	}
}
tiac = new TIAC();

function MENU(){
	this.structureAA=[];
	this.currentNode;	
	this.getRoot=function(){
		var _items=[];
		try{
			_items = data.structure.items;	
		}catch(e){
			alert('error getting root');
			_items=[];	
		}
		return _items;
	}
	
	this.linkNodes=function(_item){
		var _items=[];
		try{
			if(_item){
				_items = _item.children;	
			}else{
				_items = this.getRoot();	
			}
		}catch(e){
			alert('error linking nodes');
			_items=[];	
		}
		if(_items.length>0){
			for(var x in _items){
				if(_item){
					_items[x].parent = _item;
				}
				if(_items[x].children.length>0){
					this.linkNodes(_items[x]);
				}
				this.structureAA['N'+_items[x].id]=_items[x];
			}
		}
	}
	
	this.buildTop=function(){
		var _items = this.getRoot();
		var _target = util.dE(tiac.topNav);
		var _htm = [];
		try{
			if(_items.length>0){
				_htm[_htm.length]='<ul>';
				for(var i=0;i<_items.length;i++){
					_htm[_htm.length]='<li id="'+_items[i].id+'"><a href="'+_items[i].href+'">'+_items[i].name.toLowerCase()+'</a></li>';
				}
				_htm[_htm.length]='</ul>';
			}
			_target.innerHTML = _htm.join('');
		}catch(e){
			alert('error building navigation');
		}
	}
	
	this.buildLeft=function(_node){
		var _items = _node.children;
		var _target = util.dE(tiac.leftNav);
		if(!_target){
			var _sectionButton = util.dE(this.currentNode.id);
			if(_sectionButton){
				_sectionButton.className = 'selected';	
			}
			return;
		}
		var _htm = [];
		var _section;
		var _sections=[];
		this.buildSubMenu=function(_node){
			var _items = _node.children;
			var _htm = [];
			_htm[_htm.length]='<ul>';
			for(var i=0;i<_items.length;i++){
				_htm[_htm.length]='<li id="'+_items[i].id+'"><a href="'+_items[i].href+'">'+_items[i].name+'</a></li>';
				if(_sections['N'+_items[i].id]&&_items[i].children.length>0){
					_htm[_htm.length]=this.buildSubMenu(_items[i]);			
				}
			}
			_htm[_htm.length]='</ul>';
			return _htm.join('');
		}
		try{
			var _n = _node;
//			alert(_n.id);
			_sections['N'+_n.id]=_n;
//			alert('build the sub menu');
			while(_n.parent){
				_n = _n.parent;	
				_sections['N'+_n.id]=_n;
				//alert(_n.name);
			}
			_section = _n;
			// HIGHLIGHT SECTION
			var _sectionButton = util.dE(_section.id);
			if(_sectionButton){
				_sectionButton.className = 'selected';	
			}
			_htm[_htm.length]=this.buildSubMenu(_section);
			_target.innerHTML = _htm.join('');
			var _nodeButton = util.dE(_node.id);
			if(_nodeButton){
				_nodeButton.className = 'selected';	
			}
		}catch(e){
			alert('error building sub navigation');
			alert(e.message);
		}
	}
		
	this.setCurrent=function(_id){
		if(this.structureAA['N'+_id]){
			this.currentNode = this.structureAA['N'+_id];	
		}
	}
	
	this.breadCrumbs=function(){
		var _target = util.dE(tiac.breadCrumbs);
		if(!_target){
			return;	
		}
		var _htm=[];
		var _n = this.currentNode;
		_htm[_htm.length]='<a href="'+_n.href+'">'+_n.name+'</a>';
		while(_n.parent){
			_n = _n.parent;
			_htm[_htm.length]='<a href="'+_n.href+'">'+_n.name+'</a>';
		}
		_htm.reverse();
		_target.innerHTML = '<b>Location: </b>'+_htm.join(' -> ');
	}
}
menu = new MENU();

document.onmouseover=mouseOver;
document.onmouseout=mouseOut;
document.onmousedown=mouseDown;
document.onload=tiac.init();
