_$('#startScreen').hide().setStyle({position:'absolute',left:'-647px'});
_$('#steps').empty();
_$('#contentPanel .content').empty();
HW.onload(function(){HW.FlowChart.init();});


HW.FlowChart = {
	steps:[],
	padding:40,
	width:0,
	focussed:null,
	active:true,
	cache:{},
	current:{next:null},
	first:false,
	started:false,
	init:function() {
		var obj = this;
		
		if(_$('#flowchart')) {
			
			this.content = _$('#contentPanel .content').extendObject(HW.FlowChart.ContentPanel).init();
			
			this.slide = _$('#steps').extendObject(HW.FlowChart.Slide).init();
			
			this.progress = _$('#progress').extendObject(HW.FlowChart.Progress);
			
			this.indicator = _$('#indicator').extendObject(HW.FlowChart.Indicator);
			
			this.navigation = _$('#navigation').extendObject(HW.FlowChart.Navigation).init();
			
			this.startscreen = _$('#startScreen').extendObject(HW.FlowChart.StartScreen).init();
		
			_$('.backToStart a').bind('click',function(){obj.start();},false);
			
			this.width = _$('#frame').offsetWidth;
			this.height = _$('#frame').offsetHeight;
			setTimeout(function(){obj.preloadImages();},0);
			
		}
	},
	next:function(n) {
		this.started = true;
		if(this.active) {
			this.deactivate();
			n = n||this.current.next;
			this.loadStep(n);
		}
	},
	back:function() {
		if(this.active) {
			this.deactivate();
			this.slide.back();
		}
	},
	start:function() {
		if(this.steps.length == 2) {
			this.back();
		}
		if(this.active) {
			this.deactivate();
			this.slide.start();
		}
	},
	preloadStep:function(n) {
		var obj = this;
		if(n && !this.cache[n]) {
			new HW.Ajax('ajax.php',function(r){obj.cache(r);},'steps='+n);
		}
	},
	loadStep:function(n) {
		n = n||'';
		var obj = this;
		if(this.startscreen.showing && n) {
			this.startscreen.drop();
		}
		if(this.cache[n]) {
			this.handle(this.cache[n]);
			return;
		}
		new HW.Ajax('ajax.php',function(r){obj.handle(r);},'s='+n);
	},
	handle:function(r) {
		var s = new HW.FlowChart.Step(r);
		this.cache[s.id] = r;
		this.steps.push(s);
		this.current = s;
		this.slide.addStep(s);
	},
	cache:function(r) {
		eval(r.text.substr(4,r.text.length-7));
		this.cache[response.id] = r;
	},
	preloadImages:function() {
		// preload images used to that they do not flicker when inserted
		var obj = this;
		var o = _$('body').createNode('div',null,{}).hide();
		var images = [
			'../../_images/backgrounds/yes.png',
			'../../_images/backgrounds/yes_on.png',
			'../../_images/backgrounds/yes_off.png',
			'../../_images/backgrounds/no.png',
			'../../_images/backgrounds/no_on.png',
			'../../_images/backgrounds/no_off.png',
			'../../_images/backgrounds/steps/type1_lhs.png',
			'../../_images/backgrounds/steps/type1_rhs.png',
			'../../_images/backgrounds/steps/type2_lhs.png',
			'../../_images/backgrounds/steps/type2_rhs.png',
			'../../_images/backgrounds/steps/type3.png',
			'../../_images/backgrounds/steps/type4_lhs.png',
			'../../_images/backgrounds/steps/type4_rhs.png',
			'../../_images/backgrounds/steps/type5_lhs.png',
			'../../_images/backgrounds/steps/type5_rhs.png',
			'../../_images/backgrounds/steps/info.gif'
		];
		var i=0;
		var f = function() {
			i++;
			if(i==images.length && !obj.started) {
				obj.next();
			}
		}
		images.each(function(i){
			var im = o.createNode('img',null,{src:i,onload:f});
		});
		setTimeout(function(){obj.next();},1000);
	},
	activate:function() {
		this.active = true;
		this.navigation.activate();
	},
	deactivate:function() {
		this.active = false;
		this.navigation.deactivate();
	}
}


HW.FlowChart.Slide = {
	right:0,
	width:HW.FlowChart.padding,
	down:0,
	addStep:function(s) {
		var obj = this;
		try{
			_$$('.isCurrent').each(function(o){o.removeClass('isCurrent');});
		}catch(e){HW.log(e.message);}
		this.insertBefore(s,this.firstChild);
		s.setStyle({paddingRight:'0px'});
		s.w = s.offsetWidth + HW.FlowChart.padding;
		s.setStyle({paddingRight:HW.FlowChart.padding+'px'});
		this.width += s.w;
		this.right -= s.w;
		s.l = this.width;
		
		this.setStyle({right:this.right+'px',width:this.width+'px'});
		
		s._focus(s.w,false,200+s.w*3);
		var t = this._move(0,function(){s.addClass('isCurrent');});
		if(HW.FlowChart.steps.length == 1) {
			HW.FlowChart.startscreen.enter(t);
		}
		HW.FlowChart.navigation._disable();
		HW.FlowChart.progress.set();
		
		if(s.hasClass('stepType3')) {
			_$('.yes a',s).bind('click',function(){obj.branch(s,'yes');},false).bind('mousedown',function(e){HW.cancelBubble(e);}).setStyle({cursor:'pointer'});
			_$('.no a',s).bind('click',function(){obj.branch(s,'no');},false).bind('mousedown',function(e){HW.cancelBubble(e);}).setStyle({cursor:'pointer'});
		}
		else if(!s.hasClass('stepType5')) {
			_$('a.arrowLink',s).bind('click',function(){HW.FlowChart.next();},false);
		}
		HW.FlowChart.preloadStep(s.next);
		HW.FlowChart.preloadStep(s.yes);
		HW.FlowChart.preloadStep(s.no);
	},
	init:function() {
		var obj = this;
		this.empty();
		return this;
	},
	_move:function(x,f) {
		var o = _$('#frame');
		var obj = this;
		var m = function(ob,v) {
			obj.right = v;
			obj.setStyle({right:obj.right+'px',width:obj.width+'px'});
			var r = parseInt(obj.offsetLeft);
			o.setStyle({backgroundPosition:r/2+'px bottom'});
		}
		var r = parseInt(this.style.right);
		var t = Math.min(200 + Math.abs(r-x)*3,1500);
		new HW.Animator(obj,r,x,m,t,f,true);
		return t;
	},
	back:function() {
		var obj = this;
		
		var last = HW.FlowChart.steps.pop();
		HW.FlowChart.current = HW.FlowChart.steps[HW.FlowChart.steps.length-1];
		HW.FlowChart.navigation._disable();
		
		var t = this._move(-last.w,function() {
			obj.width -= last.w;
			obj.right += last.w;
			obj.removeChild(last);
			obj.setStyle({right:'0px',width:obj.width+'px'});
			HW.FlowChart.current.selected = null;
			HW.FlowChart.current.highlight();
			HW.FlowChart.current.addClass('isCurrent');
			HW.FlowChart.progress.set();
		});
		HW.FlowChart.current._focus(-last.w,true,200+last.w*3);
		if(HW.FlowChart.steps.length == 1) {
			HW.FlowChart.startscreen.enter(t);
		}
	},
	start:function() {
		var obj = this;
		var w = 0;
		var r = [];
		while(HW.FlowChart.steps.length > 1) {
			var last = HW.FlowChart.steps.pop();
			w += last.w;
			r.push(last);
		}
		HW.FlowChart.current = HW.FlowChart.steps[0];
		HW.FlowChart.navigation._disable();
		
		var t = this._move(-w,function() {
			obj.width -= w;
			obj.right += w;
			r.each(function(step) {
				obj.removeChild(step);
			})
			obj.setStyle({right:'0px',width:obj.width+'px'});
			HW.FlowChart.current.selected = null;
			HW.FlowChart.current.highlight();
			HW.FlowChart.current.addClass('isCurrent');
			HW.FlowChart.progress.set();
		});
		HW.FlowChart.current._focus(-w,true,t);
		if(HW.FlowChart.steps.length == 1) {
			if(t > 500) {
				setTimeout(function(){HW.FlowChart.startscreen.enter(500);},t-500);
			}
			else {
				HW.FlowChart.startscreen.enter(t);
			}
		}
	},
	branch:function(s,a) {
		if(HW.FlowChart.active) {
			if(s == HW.FlowChart.current) {
				s.selected = a;
				s.highlight();
				HW.FlowChart.next(s[a]);
			}
			else if(a != s.selected) {
				var obj = this;
				var del = [];
				var b = false;
				var w = 0;
				for(var i=0,j=HW.FlowChart.steps.length;i<j;i++) {
					if(b) {
						del.push(HW.FlowChart.steps[i]);
					}
					else if(HW.FlowChart.steps[i] == s) {
						b = true;
					}
				}
				for(var i=0,j=del.length;i<j;i++) {
					w += del[i].w;
					HW.FlowChart.steps.pop();
				}
				var f = function() {
					obj.down++;
					if(obj.down == del.length) {
						obj._move(-w,function() {
							obj.width -= w;
							obj.right += w;
							for(var i=0,j=del.length;i<j;i++) {
								try {obj.removeChild(del[i]);}catch(e){}
							}
							obj.setStyle({right:'0px',width:obj.width+'px'});
							s.selected = a;
							s.highlight();
							HW.FlowChart.next(s[a]);
						});
					}
				}
				this.down = 0;
				for(var i=0,j=del.length;i<j;i++) {
					del[i].drop(f);
				}
				HW.FlowChart.content.fade(0,300);
			}
		}
	}
}

HW.FlowChart.Step = function(r,run) {
	var obj = this;
	eval(r.text.substr(4,r.text.length-7));
	var div = HW.createNode('div',null,response.newHTML);
	var o = HW.ext(div.firstChild).extendObject(this.o).init(response);
	return o;
}

HW.FlowChart.Step.prototype = {
	o:{
		next:null,
		yes:null,
		no:null,
		info:null,
		label:null,
		init:function(r) {
			for(var i in r) {
				if(i != 'newHTML') {
					this[i] = r[i];
				}
			}
			this.bindEvents();
			return this;
		},
		bindEvents:function() {
			var obj = this;
			_$('div.label',this).bind('mousedown',function(){obj._focus(0);},true).setStyle({cursor:'pointer'});
			_$('a.info',this).bind('click',function(){},false);
		},
		_focus:function(offset,force,t) {
			if(HW.FlowChart.focussed != this || force) {
				t = t||300;
				offset = offset||0;
				var x = HW.FlowChart.width - (HW.FlowChart.slide.width + HW.FlowChart.slide.right + offset) + this.l - (this.w + HW.FlowChart.padding)/2;
				var dl = x - (this.w-HW.FlowChart.padding)/2;
				var dr = x + (this.w-HW.FlowChart.padding)/2;
				if(dl < HW.FlowChart.padding && !offset) {
					var obj = this;
					var r = HW.FlowChart.slide.right - (HW.FlowChart.padding - dl);
					HW.FlowChart.slide._move(r);
					this._focus(dl - HW.FlowChart.padding);
					return;
				}
				if(dr >  HW.FlowChart.width - HW.FlowChart.padding && !offset) {
					var obj = this;
					var r = HW.FlowChart.slide.right - (HW.FlowChart.width - HW.FlowChart.padding - dr);
					HW.FlowChart.slide._move(r);
					this._focus(HW.FlowChart.padding + dr - HW.FlowChart.width);
					return;
				}
				HW.FlowChart.focussed = this;
				x = Math.max(HW.FlowChart.padding,x);
				x = Math.min(HW.FlowChart.width - HW.FlowChart.padding,x);
				HW.FlowChart.content.fade(0,300);
				HW.FlowChart.indicator._move(x,function(){HW.FlowChart.content.set()},t);
			}
		},
		drop:function(f) {
			var obj = this;
			new HW.Animator(this,0,HW.FlowChart.height,function(o,v){obj.setStyle({position:'relative',top:v+'px'})},500,f);
			this.fade(0,300);
		},
		highlight:function() {
			_$$('a.branch',this).each(function(o){o.removeClass('selected_branch').removeClass('unselected_branch');});
			this.removeClass('isSelected');
			if(this.selected) {
				this.addClass('isSelected');
				_$$('a.branch',this).each(function(o){o.addClass('unselected_branch');});
				_$('span.'+this.selected+' a.branch',this).addClass('selected_branch').removeClass('unselected_branch');
			}
		}
	}
}

HW.FlowChart.Indicator = {
	x:0,
	_move:function(x,f,t) {
		x -= this.offsetWidth/2;
		t = t||Math.abs(this.x - x)*3;
		if(Math.abs(this.x - x) > 2) {
			this.move({x:x,y:0},t,f,true);
		}
		else {
			this.setStyle({left:x+'px'});
			if(typeof f == 'function') {setTimeout(f,t);}
		}
		this.x = x;
	}
}

HW.FlowChart.ContentPanel = {
	loaded:false,
	expanded:false,
	init:function() {
		this.setFade(0).setStyle({height:'50px'});
		return this;
	},
	set:function(t,fn) {
		if(this.firstChild && this.loaded) {
			this.setStyle({height:'',overflow:''});
			this.loaded = true;
		}
		t = t||'description';
		var obj = this;
		var h1 = this.offsetHeight;
		this.setStyle({height:'',overflow:''});
		this.innerHTML = HW.FlowChart.focussed[t];
		var h2 = this.offsetHeight;
		this.setStyle({height:h1+'px',overflow:'hidden'});
		
		var dh = Math.min(Math.abs(h2-h1)*3,500);
		
		var f = function() {
			try{
				_$('p.lessInfo a',obj).bind('click',function(){obj.contract();},false);
			}catch(e){}
			try{
				_$('p.moreInfo a',obj).bind('click',function(){obj.expand();},false);
			}catch(e){}
			var w = obj.offsetWidth;
			var x = HW.FlowChart.indicator.x - w/2;
			x = Math.max(0,x);
			x = Math.min(HW.FlowChart.width - w,x);
			obj.setStyle({position:'relative',top:'0px',left:x + 'px'});
			obj.fade(100,300,function(){HW.FlowChart.activate();});
			if(typeof fn == 'function') {fn();}
		}
		
		new HW.Animator(obj,h1,h2,function(o,v){obj.setStyle({height:v+'px'});},dh,f);
	},
	expand:function() {
		if(HW.FlowChart.active) {
			HW.FlowChart.deactivate();
			var obj = this;
			this.expanded = true;
			HW.FlowChart.navigation._disable();
			this.fade(0,300,function(){
				new HW.Animator(obj,HW.FlowChart.height,10,function(o,v){_$('#frame').setStyle({height:v+'px'});},500,function(){
					obj.set('info');
					_$('#frame').bind('click',function(){
						if(obj.expanded && HW.FlowChart.active) {
							obj.contract();
						}
					},false).setStyle({cursor:'pointer'});
				});
			});
		}
	},
	contract:function() {
		if(HW.FlowChart.active) {
			HW.FlowChart.deactivate();
			var obj = this;
			_$('#frame').setStyle({cursor:''});
			this.fade(0,300,function(){
				obj.empty();
				obj.set('description',function(){
					new HW.Animator(obj,10,HW.FlowChart.height,function(o,v){_$('#frame').setStyle({height:v+'px'});},500,function(){
						obj.expanded = false;
						HW.FlowChart.navigation._disable();
					});
				});
			});
		}
	}
}

HW.FlowChart.Progress = {
	set:function(n) {
		n = n||HW.FlowChart.current.progress;
		this.setStyle({width:parseInt(n)+'%'});
	}
}

HW.FlowChart.Navigation = {
	inactiveAlpha:30,
	_disabled:{
		next:false,
		previous:false
	},
	init:function() {
		var obj = this;
		_$$('a',this).each(function(o) {
			o.bind('click',function(e){obj.handle(o);},false);
		});
		return this;
	},
	handle:function(o) {
		if(HW.hasClass(o,'previous') && !HW.hasClass(o.parentNode,'disabled')) {
			if(!this._disabled.previous) {
				HW.FlowChart.back();
			}
		}
		else if(HW.hasClass(o,'next') && !HW.hasClass(o.parentNode,'disabled')) {
			if(!this._disabled.next) {
				HW.FlowChart.next();
			}
		}
	},
	_disable:function() {
		if(!HW.FlowChart.current.next || HW.FlowChart.content.expanded) {
			HW.addClass(_$('.next',this).parentNode,'disabled');
			this._disabled.next = true;
		}
		else {
			HW.removeClass(_$('.next',this).parentNode,'disabled');
			this._disabled.next = false;
		}
		if(HW.FlowChart.steps.length == 1 || HW.FlowChart.content.expanded) {
			HW.addClass(_$('.previous',this).parentNode,'disabled');
			this._disabled.previous = true;
			_$('.backToStart').hide();
		}
		else {
			HW.removeClass(_$('.previous',this).parentNode,'disabled');
			this._disabled.previous = false;
			_$('.backToStart').show();
		}
	},
	deactivate:function() {
		this.setFade(this.inactiveAlpha);
		_$('.backToStart').setFade(this.inactiveAlpha);
	},
	activate:function() {
		this.setFade(100);
		_$('.backToStart').setFade(100);
	}
}

HW.FlowChart.StartScreen = {
	showing:true,
	init:function() {
		_$('p.footnote a',this).bind('click',function(){HW.FlowChart.next();},false);
		return this;
	},
	drop:function() {
		var obj = this;
		this.move({x:-647,y:0},1000,function(){obj.showing=false;},true);
	},
	enter:function(t) {
		var obj = this;
		this.setStyle({display:''}).move({x:0,y:0},t,function(){obj.showing=true;},true);
	}
}