HW.Navigation = function(xmlPath) {
	this.selectors = {
		topNav:'#tabnavigation .tabnavlft',
		breadCrumb:'#bcLft',
		leftNav:'#sidebar1 .Lftnav',
		sitemap:'#sitemapContainer'
	}
	if(!xmlPath) {
		return;
	}
	var obj = this;
	new HW.Ajax(xmlPath,function(r){obj.handle(r.xml);});
}

HW.Navigation.prototype = {
	structure:{current:null},
	root:null,
	handle:function(xml) {
		var obj = this;
		
		var root = xml.getElementsByTagName('site');
		root = root.length?root[0]:null;
		
		if(root === null) {return ;}
		
		this.root = root.getAttribute('root');
		
		this.getChildren(root,this.structure);
		
		HW.onload(function(){
			obj.topLevelNav();
			obj.breadCrumb();
			obj.leftNav();
			obj.sitemap();
		});
	},
	getChildren:function(node,dataHolder) {
		var children = node.childNodes;
		if(!dataHolder.children) {
			dataHolder.children = [];
		}
		for(var i=0,j=children.length;i<j;i++) {
			if(children[i].nodeType == 1 && children[i].tagName == 'page') {
				var o = {
					url:	children[i].getAttribute('url'),
					name:	children[i].getAttribute('name'),
					isHome:	children[i].getAttribute('isHome'),
					inTopNav:	children[i].getAttribute('inTopNav'),
					children:[],
					parent:dataHolder
				};
				this.getChildren(children[i],o);
				dataHolder.children.push(o);
				if(self.location.pathname == this.root+children[i].getAttribute('url')) {
					this.structure.current = o;
				}
			}
		}
	},
	topLevelNav:function() {
		var selector = this.selectors.topNav;
		var c = _$(selector);
		if(c && c.nodeType == 1) {
			this.empty(c);
			var arr = this.structure.children;
			for(var i=0,j=arr.length;i<j;i++) {
				if(arr[i].inTopNav != 'false') {
					var a = HW.createNode('a',c,'',{href:this.root+arr[i].url});
					HW.createNode('span',a,arr[i].name);
					if(this.isDescendent(this.structure.current,arr[i])) {
						HW.addClass(a,'selected');
					}
				}
			}
		}
	},
	breadCrumb:function() {
		var selector = this.selectors.breadCrumb;
		var c = _$(selector);
		if(c && c.nodeType == 1 && this.structure.current) {
			this.empty(c);
			HW.createNode('a',c,'Home',{href:this.root});
			var a = [];
			var o = this.structure.current;
			while(o.parent && !o.isHome) {
				a.push(o);
				o = o.parent;
			}
			for(var i=a.length;i>0;i--) {
				c.innerHTML += ' / ';
				if(i-1 >0) {
					HW.createNode('a',c,a[i-1].name,{href:this.root+a[i-1].url});
				}
				else {
					c.innerHTML += a[i-1].name;
				}
			}
		}
	},
	leftNav:function() {
		var obj = this;
		var selector = this.selectors.leftNav;
		var c = _$(selector);
		if(c && c.nodeType == 1 && this.structure.current) {
			this.empty(c);
			
			var node = this.structure.current;
			
			while(node.parent.parent) {
				node = node.parent;
			}
			
			HW.createNode('h1',c,node.name);
			var drawChildren = function(node,level) {
				var str = '';
				for(var i=0;i<level;i++) {
					str += '&gt;';
				}
				for(var i=0,j=node.children.length;i<j;i++) {
					HW.createNode('a',c,str+' '+node.children[i].name,{href:obj.root+node.children[i].url});
					if(node.children[i].children.length) {
						drawChildren(node.children[i],level+1);
					}
				}
			}
			
			drawChildren(node,0);
			
		}
	},
	sitemap:function() {
		var selector = this.selectors.sitemap;
		var c = _$(selector);
		if(c && c.nodeType == 1) {
			var obj = this;
			this.empty(c);
			var drawChildren = function(ch,p) {
				if(ch.length) {
					var ul = HW.createNode('ul',p);
					for(var i=0,j=ch.length;i<j;i++) {
						var li = HW.createNode('li',ul);
						var a = HW.createNode('a',li,ch[i].name,{href:obj.root+ch[i].url});
						drawChildren(ch[i].children,li);
					}
				}
			}
			drawChildren(this.structure.children,c);
		}
	},
	empty:function(node) {
		while(node.firstChild) {
			node.removeChild(node.firstChild);
		}
	},
	isDescendent:function(a,b) {
		while(a) {
			if(a.url == b.url) {return true;}
			a = a.parent;
		}
		return false;
	}
}

new HW.Navigation('xml/navigation.xml');