var SlideShow = (function(){
	function constructor(data,slideTime){
		this.frame = document.getElementById('slideshow_frame');
		this.playing;
		this.imgs = [];
		this.intervalRate = slideTime;
		this.data = data;
		this.setBtns();
		this.parseData();
	}
	
	function startInterval(instance){
		instance.playing = setInterval(function(){instance.show()}, instance.intervalRate);
	}
	
	constructor.prototype.parseData = function(){
		for(var node = this.data.firstChild.firstChild;node != null;node = node.nextSibling){
			if(node.nodeType != 3){
				for(var n=node.firstChild;n != null;n = n.nextSibling){
					if(n.nodeName.toLowerCase() == 'h2'){
						var title = n.firstChild.nodeValue;
					} else if(n.nodeName.toLowerCase() == 'p'){
						var msg = n;
					}
				}
				this.imgs.push({"img":node.getAttribute('id'),"title":title,"msg":msg})
			}
		}
	}
	
	constructor.prototype.start = function(){
		this.next();
		startInterval(this);
	};
	
	constructor.prototype.stop = function(){
		clearInterval(this.playing);
	};
	
	function XMLtoDOM(p,node){
		for(var n = node.firstChild;n != null;n = n.nextSibling){
			if(n.nodeType == 3){//text
				p.appendChild(document.createTextNode(n.nodeValue));
			} else if(n.nodeType == 1) {//element
				var newNode = p.appendChild(document.createElement(n.nodeName))
				for(var k=0;k<n.attributes.length;k++){newNode[n.attributes[k].name] = n.attributes[k].value;}
				XMLtoDOM(newNode,n);
			}
		}
	}
	
	constructor.prototype.showText = function(){
		removeChildren(document.getElementById('slideshow_text'));
		document.getElementById('slideshow_text').appendChild(document.createElement('h2')).appendChild(document.createTextNode(this.imgs[this.imgs.length-1].title));
		//document.getElementById('slideshow_text').appendChild(document.createElement('p')).appendChild(document.createTextNode(this.imgs[this.imgs.length-1].msg));
		var p = document.getElementById('slideshow_text').appendChild(document.createElement('p'))
		
		XMLtoDOM(p,this.imgs[this.imgs.length-1].msg);
		
		for(var node = document.getElementById('slideshow_btns').firstChild;node != null;node = node.nextSibling){
			if(node.nodeName.toLowerCase() == 'li'){
				node.firstChild.style.backgroundPosition = "0 100%";
			}
		}
		document.getElementById('slideshow_btn'+this.imgs[this.imgs.length-1].img).style.backgroundPosition = "0 0";
	}
	
	constructor.prototype.next = function(){
		var i = new Image();
		var newNum = this.imgs.shift();
		i.onload = function(){
			var newImg = document.createElement('img');
			newImg.src = this.src;
			newImg.className = "slideshowimage transparent";
			document.getElementById('slideshow_frame').appendChild(newImg);
			if(document.all){
				newImg.t = new Tween(newImg,"filter",0,100,1500,'alpha(opacity=',')','easeSin')
			} else {
				newImg.t = new Tween(newImg,"opacity",0,1,1500,'','','easeSin');
			}
		}
		i.src = "/images/slideshow/"+newNum.img+".jpg";
		this.imgs.push(newNum);	
	}
	
	constructor.prototype.nextDisplay = function(){
		var i = new Image();
		var newNum = this.imgs.shift();
		i.onload = function(){
			var newImg = document.createElement('img');
			newImg.src = this.src;
			newImg.className = "slideshowimage transparent";
			document.getElementById('slideshow_frame').appendChild(newImg);
			if(document.all){
				newImg.t = new Tween(newImg,"filter",0,100,1500,'alpha(opacity=',')',"easeSin")
			} else {
				newImg.t = new Tween(newImg,"opacity",0,1,1500,'','',"easeSin");
			}
			newImg.t.play();
		}
		i.src = "/images/slideshow/"+newNum.img+".jpg";
		this.imgs.push(newNum);	
	}
	
	constructor.prototype.show = function(){
		var imgArray = this.frame.getElementsByTagName('img');
		if(imgArray.length>1){
			if(!imgArray[imgArray.length-1].t.playing){
				this.showText();
				imgArray[imgArray.length-1].t.play();
			} else {
				while(this.frame.lastChild.previousSibling){
					this.frame.removeChild(this.frame.firstChild);
				}
			}
			this.next();
		}
	}
	
	constructor.prototype.setBtns = function(){
		for(var node = document.getElementById('slideshow_btns').firstChild;node != null;node = node.nextSibling){
			if(node.nodeName.toLowerCase() == 'li'){
				node.firstChild.slideshow = this;
				node.firstChild.onclick = function(){
					this.num = Number(this.id.substring(this.id.length-1));
					while(this.slideshow.imgs[0].img != this.num){
						this.slideshow.imgs.push(this.slideshow.imgs.shift());
					}
					this.slideshow.stop();
					this.slideshow.nextDisplay();
					this.slideshow.showText();
					this.slideshow.start();
					//this.slideshow.showText();
					//this.slideshow.show();
					return false;
				}
			}
		}
	}
	
	function removeChildren(element){
		while(element.hasChildNodes()){
			element.removeChild(element.lastChild);	
		}
	}
	
	return constructor;
})();