
function JC_Gallery(_thumbs)
{
	var that = this;
	var Left = "JC_Gallery_Left";
	var Right = "JC_Gallery_Right";
	var ThumbImageName = "JC_Gallery_Thumb_";

	this.totalThumbs = _thumbs;	
	this.ThumbPath;
	this.Viewer = "JC_Gallery_Viewer";
	this.Switcher = "JC_Gallery_Switch";
	this.ThumbImages = Array();
	this.Offset = 0;
	this.AddedImages = 0;
	this.DefaultPosition = 582;
	this.Position = this.DefaultPosition;
	this.Speed = 20;
	this.Refresh = 10;
	
	this.AddImage = function(_image)
	{
		this.imagePaths.push(_image);
	}
	
	this.SetThumbPath = function(_path)
	{
		this.ThumbPath = _path;
	}
	
	this.Load = function()
	{
		//bind to gallery elements
		document.getElementById(Left).onclick = this.LeftClick;
		document.getElementById(Right).onclick = this.RightClick;
	}
	
	
	this.UpdateThumbs = function(_offset)
	{
		var oldOffset = that.Offset;
		//alter offset
		if(_offset < 0)
		{
			if(that.Offset > 0)
			{
				that.Offset += _offset;
			}
			else
			{
				that.Offset = that.ThumbImages.length - 1;
			}
		}
		else
		{
			if(that.Offset + _offset < that.ThumbImages.length)
			{
				that.Offset += _offset;
			}
			else
			{
				that.Offset = 0;
			}
		}
		
		//if change update thumbs
		if(that.Offset != oldOffset)
		{
			that.LoadNewImage(document.getElementById(that.ThumbImages[that.Offset]).src);
		}
	}
	
	this.RightClick = function()
	{
		that.UpdateThumbs(1);
		return false;
	}
	
	this.LeftClick = function()
	{
		that.UpdateThumbs(-1);
		return false;
	}
	
	this.LoadNewImage = function(path)
	{
		var srcBits = path.split("?img=");
		
		//add sliding animation system
		document.getElementById(that.Switcher).src = srcBits[1];
		document.getElementById(that.Switcher).onload = that.ImageLoaded;
	}
	
	this.ThumbClick = function()
	{
		//get large image url
		that.LoadNewImage(this.src);
		
		//find thumb and set offset
		for(var x=0;x < that.ThumbImages.length;x++)
		{
			if(this.id == that.ThumbImages[x])
			{
				that.Offset = x;
				break;
			}
		}
	}
	
	this.ImageLoaded = function()
	{
		that.Animation();
	}
	
	this.Animation = function()
	{
		if(that.Position != 0)
		{
			that.Position -= that.Speed;
			
			if(that.Position < 0)
			{
				that.Position = 0;
			}
			
			document.getElementById(that.Switcher).style.left = "+" + that.Position + "px";
			
			setTimeout(that.Animation,that.Refresh);
		}
		else
		{
			document.getElementById(that.Viewer).src = document.getElementById(that.Switcher).src;
			that.Position = that.DefaultPosition;
			document.getElementById(that.Switcher).style.left = "+" + that.Position + "px";
			
		}
	}
	
	//setup thumb names
	for(var x=1;x <= this.totalThumbs;x++)
	{
		this.ThumbImages.push(ThumbImageName + x);
		document.getElementById(this.ThumbImages[x -1]).onclick = this.ThumbClick;
	}
}
