
function Gallery() {}
Gallery.prototype = {

	stack: new Array(),
	thumbs: new Array(),
	interval: null,
	
	timeout: 2000,
	scrollInterval: null,
	scrollToInterval: null,
	
	selectedImage: null,
	
	start: function()
	{
		if(!this.stack.length) return;
		var th = this;
		this.interval = setInterval(function(){th.changeImage()},this.timeout);
	},
	
	initImage: function(src,href)
	{
		var img = new Image();
		img.src = src;
		this.stack.push([src,href]);
	},
	
	initThumb: function(id)
	{
		this.thumbs.push(id);
	},
	
	changeImage: function()
	{
		var e = this.stack.shift();
		var thumbId = Math.round(Math.random() * (this.thumbs.length-1));
		this.stack.push([$('galleryThumb_'+this.thumbs[thumbId]).src,$('galleryHref_'+this.thumbs[thumbId]).href]);
		$('galleryThumb_'+this.thumbs[thumbId]).src = e[0];
		$('galleryHref_'+this.thumbs[thumbId]).href = e[1];
	},
	
	selectGalleryImg: function(id)
	{
		if(this.selectedImage) {
			$('img_'+this.selectedImage).style.border = 'none';
		}
		
		var content = $('galleryContent_'+id);
		var img = $('img_'+id);
		
		img.style.border = '#aaa 5px solid';
		$('galleryImage').src = img.getAttribute('fullsrc');
		
		this.scrollTo(id);
		
		$('galleryTitle').innerHTML = content.getAttribute('title');
		$('galleryDescription').innerHTML = content.innerHTML;
		
		this.selectedImage = id;
	},
	
	scrollTo: function(id)
	{
		var th = this;
		var offset = $('img_'+id).getAttribute('offset');
		var galleryScroll = $('gallery_scroll');
		var full = galleryScroll.clientWidth;
		var fullScroll = galleryScroll.scrollWidth;
		var pos = ((offset * 66) - 33) - Math.round(full/2,0);
		
		if(pos > full) pos = fullScroll - full;
		else if(pos < 0) pos = 0;
		
		galleryScroll.scrollLeft = pos;
		//var direction = (pos > galleryScroll.scrollLeft);
		//this.scrollToInterval = setInterval(function(){th._scrollTo(pos,direction)},5);
		
	},
	
	startScroll: function(direction)
	{
		if(this.scrollToInterval) return;
		
		this.scrollInterval = setInterval(function(){
			var galleryScroll = $('gallery_scroll');
			var value = ( direction > 0 ? galleryScroll.scrollWidth - galleryScroll.clientWidth : 0 );
			var step = 5;
			$('gallery_scroll').scrollLeft += step * direction;
		},10);
	},
	
	stopScroll: function()
	{
		clearInterval(this.scrollInterval);
		this.scrollInterval = null;
	}
	
}

Gallery = new Gallery();