var ScrollWindow = (function(){
	function constructor(obj,nav,message,description,links,width,count){
		this.obj = obj;
		this.nav = nav;
		this.message = message;
		this.description = description;
		this.links = links;
		this.gTween;
		this.mTween;
		this.dTween;
		this.lTween;
		this.width = width;
		this.count = count;
		this.currentStep = 1;
		this.animateTime = 250;
		this.autoScrollTime = 12000;
		this.interval;
		this.currentImg = document.getElementById("img1");
		this.previousImg;
		this.cache = true;
	}
	
	constructor.prototype.init = function(){
	//	h=document.documentElement.firstChild;
	//	h.appendChild(c=document.createElement('link')).setAttribute('href','/css/scroll-content.css');
	//	c.setAttribute('rel','stylesheet');
		var testCache = new Image();
		
		//detect if browser is caching images
		testCache.controller = this;
		testCache.onload = function(){
			var testCache2 = new Image();
			testCache2.src = this.src;
			this.controller.cache = (testCache2.naturalWidth||(testCache2.complete && /MSIE/.test(navigator.userAgent)))?true:false;
		}
		testCache.src = "/images/ui/selector-pause.gif";
		
		this.description.style.left = "-944px";
		this.links.style.left = "-1600px";
		var topNavElements = this.nav.getElementsByTagName('a');
		topNavElements[0].className = "current";
		for(var q=0;q<topNavElements.length;q++){
			topNavElements[q].controller = this;
			topNavElements[q].num = q+1;
			topNavElements[q].onclick = function(){
				this.controller.scroll(this.num-this.controller.currentStep);
				document.getElementById("selector-pause").style.backgroundImage = "url(/images/ui/selector-pause.gif)";
				return false;
			}
		}
		document.getElementById("selector-pause").gallery = this;
		document.getElementById("selector-pause").onclick = function(){
			if(this.gallery.interval){
				clearInterval(this.gallery.interval);
				this.gallery.interval = null;
				this.style.backgroundImage = "url(/images/ui/selector-play.gif)";
			} else {
				this.gallery.scroll(this.gallery.currentStep==this.gallery.count?-this.gallery.count+1:1);
				this.style.backgroundImage = "url(/images/ui/selector-pause.gif)";
			}
		}
		if(/MSIE/.test(navigator.userAgent) && document.all){
			if(/MSIE/.test(navigator.userAgent) && this.currentImg.firstChild){
				this.currentImg.firstChild.src = "/images/ie-sucks.gif";
			}
			this.currentImg.gTween = new Tween(this.currentImg,"filter",100,0,250,")","alpha(opacity=","easeSin");
		} else {
			this.currentImg.gTween = new Tween(this.currentImg,"opacity",1,0,250,'','',"easeSin");
		}
		startInterval(this,this.autoScrollTime,null);
	}
	
	constructor.prototype.isImageLoaded = function(d){
		var i = new Image();
		if(!this.cache){return true;}//if browser is not caching, we cannot detect loaded images.
		if(d.firstChild&&d.firstChild.nodeName.toLowerCase() == 'img'){
			i.src = d.firstChild.src;
			return (i.naturalWidth||(i.complete && /MSIE/.test(navigator.userAgent)))?true:false;
		} else if(d.currentStyle){
				i.src = d.currentStyle["backgroundImage"].replace(/(url\()|\)|"/g,"");
				return i.complete;
		} else if(window.getComputedStyle){
				i.src = document.defaultView.getComputedStyle(d,null).getPropertyValue("background-image").replace(/(url\()|\)/g,"");
				if(!i.naturalWidth)
				return i.naturalWidth;
		}
		return true;
	}
	
	function removeClassName(ele){
		if(document.all){
			for(var i = 0; i < ele.attributes.length; i++){
				if(ele.attributes[i].name.toLowerCase() == 'class'){
					ele.attributes[i].value = "";
				}
			}
		} else {
			ele.removeAttribute("class");	
		}
	}
	
	function startInterval(instance,time,d){
		if(d){
			instance.interval = setInterval(function(){instance.scroll(d)},time);
		} else {
			instance.interval = setInterval(function(){instance.scroll((instance.currentStep<instance.count?1:1-instance.count))},time);
		}
		
	}
	
	constructor.prototype.scroll = function(d){
		if(this.interval){
			clearInterval(this.interval);
			this.interval = null;
		}
		

	//	var currentPos = Number(this.obj.style.right.match(/\d+/));
		var messagePos = Number(this.message.style.right.match(/\d+/));
		var descriptionPos = Number(this.description.style.left.match(/\-?\d+/));
		var linksPos = Number(this.links.style.left.match(/\-?\d+/));
		if((messagePos == 0 && d == -1) || (messagePos == this.width*(this.count-1) && d == 1) || (this.mTween && this.mTween.playing) || d == 0){
			return false;
		}
		if(!this.isImageLoaded( document.getElementById("img"+(this.currentStep+d)) )){
			startInterval(this,200,d);
			return false;
		}
		
		removeClassName(this.nav.getElementsByTagName('a')[this.currentStep-1]);
		
		this.previousImg = document.getElementById("img"+this.currentStep);
		
		this.currentStep += d;
		this.currentImg = document.getElementById("img"+this.currentStep);
		
		if(/MSIE 6/.test(navigator.userAgent) && this.currentImg.firstChild){this.currentImg.firstChild.src = "/images/ie-sucks.gif";}
		
	//	var pos = d*this.width+currentPos;
		var mpos = d*640+messagePos;
		var dpos = d*236+descriptionPos;
		var lpos = d*400+linksPos;
	//	this.pTween = new Tween(this.obj,"right",currentPos,pos,this.animateTime,'px','',"easeSin");

			if(/MSIE/.test(navigator.userAgent) && document.all){
				this.currentImg.gTween = new Tween(this.currentImg,"filter",0,100,250,")","alpha(opacity=","easeSin");
			} else {
				this.currentImg.gTween = new Tween(this.currentImg,"opacity",0,1,250,'','',"easeSin");
			}
		if(this.previousImg.gTween){this.previousImg.gTween.play();}
		
		this.mTween = new Tween(this.message,"right",messagePos,mpos,this.animateTime,'px','',"easeSin");
		this.lTween = new Tween(this.links,"left",linksPos,lpos,this.animateTime,'px','',"easeSin");
		this.dTween = new Tween(this.description,"left",descriptionPos,dpos,this.animateTime,'px','',"easeSin");
		
		
		this.nav.getElementsByTagName('a')[this.currentStep-1].className = "current";
		
		this.currentImg.gTween.play();
		this.mTween.play();
		this.lTween.play();
		this.dTween.play();
		
		
		startInterval(this,this.autoScrollTime,null);
	}
	return constructor;
})();