/***************************************************************************
 * gallery object
 ***************************************************************************/  
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();
		}		
			// show the arrows
			this.$(this.next).style.display = "block";
			this.$(this.previous).style.display = "block";
		}else{
			// hide the arrows
			this.$(this.next).style.display = "";
			this.$(this.previous).style.display = "";
		}
	    */
	},
	
	itemNext: function() {
		this.gItems.push(this.gItems.shift())
		this.refreshThumbs();
		this.refreshContent();
	}, 
	
	itemPrevious: function() {
		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.imagePath + this.gItems[i].image;			
		}	
	},
	
	linkToContent: function() {
		var id = this.gCurrent;
		var link = this.gItems[id].link;
//		alert('linking to ' + link);
		if (link.indexOf("popup:") == 0)
			document.location = link.substring(6);
		else if (link.indexOf("external:") == 0)
			window.open(link.substring(9), "SLExternal", "");
		else if (link.indexOf("link:") == 0)
			window.open(link.substring(5), "SLExternal", "");
//			document.location = link.substring(5);
	},
	
	refreshThumbs: function() {
	
		//clear out current contents
		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 = "";
	
		//display new set of thumbs
		var galleryLimit = (this.displayLimit > 0) ? this.displayLimit : this.gItems.length;
		for (var i=0; i < galleryLimit; 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.onmouseover = function() {
					this.self.refreshContent(this.id);
				}
				item.onclick = function() {
					this.self.linkToContent();
				}
			
				this.$(this.chooser).appendChild(item);			
			}
		}
	},
	
	refreshContent: function(id) {
	
	    var hero = document.getElementById("galleryImage");
	    if (hero) hero.style.display = "inline";
	
		//determine passed id or use current
		var activeID = (id) ? id.substring(5) : this.gCurrent;		
		
//		//update nav state
//        var row = Math.floor(activeID/6);
//        var col = (activeID%6);
//		var thumbWidthAndMargin = this.thumbWidth + 8;
//        var colOffset = (thumbWidthAndMargin * col) + this.$(this.chooser).offsetLeft;
//        var thumbHeightAndMargin = 90;
//        var rowOffset = (thumbHeightAndMargin * row) - 2;
//		this.$(this.state).style.display = "inline";
//		this.$(this.state).style.left = colOffset + "px";
//		this.$(this.state).style.top = rowOffset + "px";
		
		
		//update content
		this.clearContent();
		this.$(this.gItems[activeID].id).style.display = "block";
		this.$(this.contentImage).src = this.imagePath + this.gItems[activeID].image;
		
		//update category
		//if (this.category != undefined) {
		//	this.$(this.category).src = this.imagePath + "cat_" + this.gItems[activeID].type + ".gif";
		//}
		
		//save state
		this.gCurrent = activeID;
	},
	
	clearContent: function() {
		for (var i=0; i < this.gItems.length; i++) {
			this.$(this.gItems[i].id).style.display = "";
		}
	},
		
	$: function(n) {
		return document.getElementById(n);
	}
	

}
 
 
 
