var Gallery = (function(){
	function constructor(timeout){
		this.images = [];
		this.timeout = timeout;
		this.slideshow;
		this.links = [];
		this.preload = new Image();
		if(document.getElementById('slideshow')){
			this.slideshow = document.getElementById('slideshow');
			this.picture = document.getElementById('picture');
			this.button = document.getElementById('slidebutton');
			this.buildGallery();
			startSlideShow(this);
		}
	}					
	
	constructor.prototype.setPicture = function(){
		this.picture.src = this.images[0].replace(/.jpg/,"_small.jpg");
		this.images.push(this.images.shift());
		removeClass(this.links[0]);
		this.links.push(this.links.shift());
		setClass(this.links[0],"current");
		this.preload.src = this.images[0].replace(/.jpg/,"_small.jpg");
	}
	
	function removeClass(e){
		if(document.all){
			for(var i = 0; i < e.attributes.length; i++){
				if(e.attributes[i].name.toLowerCase() == 'class'){
					e.attributes[i].value = "";
				}
			}
		} else {
			e.removeAttribute("class");	
		}
		
	}
	
	function setClass(e,c){
		e.className = c;	
	}
	
	function startSlideShow(instance){
		instance.slideshow = setInterval(function(){instance.setPicture()},instance.timeout);
	}
	
	constructor.prototype.buildGallery = function(){
		var a = this.slideshow.getElementsByTagName('a');
		var len = a.length;
		for(var k=0;k<len;k++){
			this.links.push(a[k]);
			this.images.push(this.links[k].href);
			//a[k].gallery = this;
			/*a[k].onclick = function(){
				this.gallery.setPicture();
				return false;
			}*/
		}
		this.images.push(this.images.shift());
	//	this.links.push(this.links.shift());
		
		this.button.gallery = this;
		this.button.onclick = function(){
			if(this.firstChild.nodeValue!='PLAY'){
				this.firstChild.nodeValue = 'PLAY';
				clearInterval(this.gallery.slideshow);
			}else{
				this.firstChild.nodeValue = "PAUSE";
				startSlideShow(this.gallery);
			}
		}
	
	
	}
	
	
	return constructor;
})();