﻿gallery = function(oConfig) 
{
	for (var c in oConfig) 
		this[c] = oConfig[c];
	
	this.gCurrent = 0;
}

gallery.prototype = 
{
	init: function() 
	{
		this.initControls();
		this.refreshThumbs();
		this.refreshContent();
		this.preloadThumbs();
	},
	
	initControls: function() 
	{
		var self = this;
		if (this.showArrows) 
		{
		    this.$(this.next).onclick = function() 
		    {
			    self.itemNext();
			}
			    
		    this.$(this.previous).onclick = function() 
		    {
			    self.itemPrevious();
			}
			    
			this.$(this.next).style.display = "block";
			this.$(this.previous).style.display = "block";
		}
		else
		{
			this.$(this.next).style.display = "none";
			this.$(this.previous).style.display = "none";
		}	
	},
	
	itemNext: function() 
	{
	    this.gCurrent -=1;
	    if(this.gCurrent < 0)
	        this.gCurrent =0;
	        
		this.gItems.push(this.gItems.shift())
		this.refreshThumbs();
		this.refreshContent();
	}, 
	
	itemPrevious: function() 
	{
		this.gCurrent +=1;
		var len = this.gItems.length;
		if(len>3) len=3;
		
		if(this.gCurrent > len)
	        this.gCurrent = len;

		this.gItems.unshift(this.gItems.pop())
		this.refreshThumbs();
		this.refreshContent();
	},
	
	preloadThumbs: function() 
	{	
		for (var i=0; i < this.gItems.length; i++) 
		{
			window["thumb" + i] = new Image()
			window["thumb" + i].src = this.imagePath + this.gItems[i].src;
			window["img" + i] = new Image()
			window["img" + i].src = this.imagePathZoom + this.gItems[i].image;			
		}	
	},
		
	refreshThumbs: function() 
	{
		for (var i=0; i < this.displayLimit; i++) 
		{
			if (this.$("gItem"+i)) 
			{
				this.$("gItem"+i).self = null;
				this.$("gItem"+i).onmouseover = null;				
			}
		}
		this.$(this.chooser).innerHTML = "";
	
		for (var i=0; i < this.displayLimit; i++) 
		{
			if (this.gItems[i]) 
			{
				var item = document.createElement("img");			
				item.src = this.imagePath + this.gItems[i].src; 
				item.id = "gItem" + i;
				item.self = this;
				item.width = this.thumbWidth;
				item.height = 95;
				//item.onmouseover = function() 
				item.onclick = function() 
				{
					this.self.refreshContent(this.id);
				}
							
				this.$(this.chooser).appendChild(item);			
			}
		}
	},
	
	refreshContent: function(id) 
	{
		var activeID = (id) ? id.substring(5) : this.gCurrent;		
		
		this.$(this.state).style.left = (((this.thumbWidth * activeID) + this.$(this.chooser).offsetLeft)) + "px";
	
		this.clearContent();
		this.$(this.gItems[activeID].id).style.display = "block";	
		this.$(this.contentImage).src = this.imagePathZoom + this.gItems[activeID].image;	
		this.$("prixProduct").innerHTML = this.gItems[activeID].price + " &euro;";
		this.$("minProduct").innerHTML = this.gItems[activeID].min;
		this.$("titleProduct").innerHTML = this.gItems[activeID].id;
		this.$("longProduct").innerHTML = this.gItems[activeID].length + " ";
		this.$("largProduct").innerHTML = this.gItems[activeID].width + " ";
		this.$("hautProduct").innerHTML = this.gItems[activeID].height;
		this.$("zoom").href = this.link.replace("$PRODUCT_ID$",this.gItems[activeID].product_id);
			
		this.gCurrent = activeID;		
	},
	
	clearContent: function() {
		for (var i=0; i < this.gItems.length; i++) 
			this.$(this.gItems[i].id).style.display = "none";
	},
		
	$: function(n) {
		return document.getElementById(n);
	}
}

