HW.onload(function(){HW.HomePage.init();});

HW.HomePage = {
	active:true,
	cache:{},
	transition:400,
	init:function(){
		if(_$('#homepageTool')) {
			this.navigation = _$('#homepageTool .navigation ul').extendObject(HW.HomePage.Navigation).init();
			this.current = _$('#homepageTool .content div.contentInner');
			this.content = _$('#homepageTool .content');
		}
	},
	change:function(qs) {
		if(this.active) {
			this.active = false;
			var obj = this;
			qs = qs.split('?').join('');
			if(!this.cache[qs]) {
				new HW.Ajax('ajax.php',function(r){obj.handle(r,qs)},qs);
			}
			else {
				this.handle(this.cache[qs],qs);	
			}
		}
	},
	handle:function(r,qs) {
		var obj = this;
		if(qs) {
			this.cache[qs] = r;
		}
		var d = this.content.createNode('div',r.text).setStyle({zIndex:'0'});
		this._new = HW.ext(d.firstChild).setFade(0).hide();
		this.content.appendChild(this._new);
		d.remove();
		var f1 = function() {
			obj.current = HW.ext(obj._new);
			obj.current.setStyle({zIndex:'1'});
			obj._new = null;
			obj.active = true;
		}
		var f2 = function() {
			obj._new.setStyle({overflow:'',height:''});
			obj._new.fade(100,obj.transition,f1);
		}
		var f3 = function(){
			obj.navigation.update('?'+qs);
			var h1 = obj.current.offsetHeight;
			var h2 = HW.getTotalSize(obj._new);
			obj.current.remove();
			obj._new.setStyle({height:h1+'px',overflow:'hidden'}).show();
			new HW.Animator(obj._new,h1,h2,function(o,v){obj._new.setStyle({height:v+'px'});},obj.transition,f2);
		}
			
		this.current.fade(0,this.transition,f3);
	}
}


HW.HomePage.Navigation = {
	init:function() {
		var a = null;
		this.links = _$$('a',this).each(function(a){a.extendObject(HW.HomePage.Navigation.Link).init()});
		_$$('li',this).each(function(li){if(li.hasClass('selected')){a=li;li.removeClass('selected');}});
		this.indicator = this.createNode('li').extendObject(HW.HomePage.Navigation.Indicator).init();
		var lnk = a?_$('a',a):this.links[0];
		this.update(lnk.search,true);
		return this;	
	},
	update:function(qs,onload) {
		var obj = this;
		_$$('li',this).each(function(li){
			if(!li.hasClass('indicator')) {
				var a = _$('a',li);
				if(a && a.search == qs) {
					var h = li.offsetTop;
					if(onload) {
						obj.indicator.setStyle({top:h+'px',left:'0px'}).set(a);
					}
					else {
						obj.indicator.set({innerHTML:a.innerHTML,href:''}).setStyle({zIndex:'1'});
						obj.indicator.move({x:0,y:h},HW.HomePage.transition,function(){obj.indicator.set(a);},true);
					}
				}
			}
		});
	}
}

HW.HomePage.Navigation.Link = {
	init:function() {
		var href = this.search;
		this.bind('click',function(){HW.HomePage.change(href);},false);
		return this;	
	}
}

HW.HomePage.Navigation.Indicator = {
	init:function() {
		this.addClass('indicator');
		this.lnk = this.createNode('a','&nbsp;').bind('click',function(){},false);
		return this;	
	},
	set:function(a) {
		this.setStyle({zIndex:'3'});
		this.lnk.innerHTML = a.innerHTML;
		this.lnk.href = a.href;
		return this;
	}
}