var Tour = (function(){
	function constructor(items,tour,nav,controller){
		this.items = items;
		this.tour = tour;
		this.nav = nav;
		this.controller = controller;
		this.pos;
		this.time = 250;
		this.length = items.length;
		this.positionController = new TweenController(this,"pos",this.time,"easeSin");
		this.current = 0;
		this.previous;
		this.btns;
		this.internallinks;
		this.init();
	}
	
	constructor.prototype.init = function(){
		for(var n=0;n<this.length;n++){
			var navItem = document.getElementById(this.items[n]+"-tour").firstChild;
			navItem.num = n;
			navItem.Tour = this;
			navItem.onclick = function(){
				this.Tour.change(this.num);
				return false;
			}
			if(document.documentElement.lastChild.className.toString().indexOf(this.items[n]) != -1){
				this.current = n;
			}
		}
		this.btns = this.controller.getElementsByTagName('li');
		this.setControllerBtns();
		for(var t=0;t<this.btns.length;t++){
			if(this.btns[t].firstChild && /a/i.test(this.btns[t].firstChild.nodeName)){
				this.btns[t].firstChild.num = (t-(t>this.length?0:1))%(this.length-(t>this.length?0:1))+(t>=this.length?1:0);
				this.btns[t].firstChild.controller = this;
				this.btns[t].firstChild.onclick = function(){
					this.controller.change(this.num);
					return false;	
				}
			}
		}
		/* slide functionality for every link with classname tour-link*/
		this.internallinks = this.tour.getElementsByTagName('a');
		for(var a=0;a<this.internallinks.length;a++){
			if(/tour-link/.test(this.internallinks[a].className)){
				this.internallinks[a].controller = this;
				this.internallinks[a].onclick = function(){
					for(var x=0;x<this.controller.items.length;x++){
						if(this.className.indexOf(this.controller.items[x]) != -1){
							this.num = x;	
						}
					}
					if(this.num){
						this.controller.change(this.num);
						return false;
					}
				}
			}
		}
		//
	}
	
	constructor.prototype.change = function(num){
		if(!this.positionController.playing && num != this.current){
			this.previous = this.current;
			this.current = num;
			this.positionController.play("animate");
			this.setControllerBtns();
		}
	}
	
	constructor.prototype.setControllerBtns = function(){
		for(var k=0;k<this.btns.length;k++){
			if(this.btns[k].className==this.items[this.current]){
				this.btns[k].style.display = "block";
				if(k==0){
					this.btns[k].parentNode.style.display = "none";
				} else if(k==this.btns.length-1) {
					this.btns[k].parentNode.style.display = "none";
				} else {
					this.btns[k].parentNode.style.display = "block";
				}
			} else {
				this.btns[k].style.display = "none";	
			}
			
		}
	}
	
	constructor.prototype.animate = function(){
		var c = this.current;
		var p = this.previous;
		var t = this.pos;
		var e = this.length;
		var w = -980;//tour width
		var o = -124;//top nav offset
		var b = -188;//nav width/length (940/5)
		this.tour.style.left = t*(c*w-p*w)+p*w+"px";
		this.nav.style.backgroundPosition = t*(b*p-b*c)-b*p+b*(e-1)+o+"px 17px";
		
	}
	
	return constructor;
})();