//-------------------------------------------------------------------------
//	WHGallery - by Wes Plybon
//		Inspired by and code modified from Lighbox 2.0 - http://www.huddletogether.com/projects/lightbox2/ - by Lokesh Dhakar 
//
//

//
//	Global Variables
//
var imageArray = new Array;
var activeImage;

//if(resizeSpeed > 10){ resizeSpeed = 10;}
//if(resizeSpeed < 1){ resizeSpeed = 1;}
//resizeDuration = (11 - resizeSpeed) * 0.15;

// -----------------------------------------------------------------------------------

//
//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

// -----------------------------------------------------------------------------------

//
//	Extending built-in Array object
//	- array.removeDuplicates()
//	- array.empty()
//
Array.prototype.removeDuplicates = function () {
	for(i = 1; i < this.length; i++){
		if(this[i][0] == this[i-1][0]){
			this.splice(i,1);
		}
	}
}

// -----------------------------------------------------------------------------------

Array.prototype.empty = function () {
	//debug(this.length);
	for(i = 0 ; this.length > 0 ; i++){
		this.shift();
	}
}

// -----------------------------------------------------------------------------------
var counter = 0;

var WHGalController = Class.create();

var WHGallery = Class.create();

WHGallery.prototype = {

	// initialize()
	// Constructor runs on completion of the DOM loading. 
	initialize: function(galid) {
		this.imageArray = new Array;
		this.galleryID = galid;
	//	this.firstPageLoad = true;
		this.resetGallery();
	},
	
	resetGallery: function(){
		this.imageArray.empty();
		this.currentImage = 0;
		this.maxWidth = 0;
		
		if (!document.getElementsByTagName){ return; }
		var anchors = $('galimages'+this.galleryID).getElementsByTagName('a');

		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			
			var anchor = anchors[i];
			
			var relAttribute = String(anchor.getAttribute('rel'));
			
			//debug(anchor.getAttribute('href'));
			// loop through anchors, find other images in set, and add them to imageArray
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match("gal"+this.galleryID))){
				//new Array(anchor.getAttribute('href'), anchor.getAttribute('title'), anchor.getAttribute('zoom'))
				this.imageArray.push( {
					largePath: anchor.getAttribute('href'),
					largeWidth: anchor.getAttribute('width'),
					caption: anchor.getAttribute('title'), 
					zoomPath: anchor.getAttribute('zoom'),
					zoomHeight: anchor.getAttribute('zheight'),
					zoomWidth: anchor.getAttribute('zwidth')
				} );
				//debug("size: " + this.imageArray.length);
			}
			//this.imageArray.removeDuplicates();
		//	while(imageArray[imageNum][0] != imageLink.getAttribute('href')) { imageNum++;}
		}
		
		this.imageTotal = this.imageArray.length;
		this.showImage(this.currentImage);
		
		//debug(this.imageTotal);
		
		//this.firstPageLoad = false;
	},
	
	next: function(){
		this.currentImage++; 
		if(this.currentImage == this.imageTotal){
			this.currentImage = 0;
		}
		this.showImage(this.currentImage);
	},
	
	prev: function(){
		this.currentImage--;
		if(this.currentImage < 0){
			this.currentImage = this.imageTotal - 1;
		}
		this.showImage(this.currentImage);
	},
	
	showImage: function(imgNum){
		if(this.imageTotal > 0){
			//debug("showing " + this.imageArray[imgNum].largePath);
			//alert("galimg" + this.galleryID);
			Element.setSrc("galimg" + this.galleryID, this.imageArray[imgNum].largePath);	
			
			$('galbar'+this.galleryID).style.display = this.imageTotal < 2 ? "none" : "";
			
			if(this.imageArray[imgNum].largeWidth){
				if(this.imageArray[imgNum].largeWidth > this.maxWidth){
					Element.setWidth("galimg" + this.galleryID, this.maxWidth);
				}else{
					Element.setWidth("galimg" + this.galleryID, this.imageArray[imgNum].largeWidth);
				}
			}else{
				Element.setWidth("galimg" + this.galleryID, this.maxWidth);
			}
			
			if(this.imageArray[imgNum].zoomPath){
				temp = $("galimg" + this.galleryID);
				temp.style.cursor = "pointer";
				temp.setAttribute("zimgf", this.imageArray[imgNum].zoomPath);
				temp.setAttribute("zimgh", this.imageArray[imgNum].zoomHeight);
				temp.setAttribute("zimgw", this.imageArray[imgNum].zoomWidth);
				//temp.onclick = eval("function(){ window.open('" + this.imageArray[imgNum].zoomPath + "') }");
				temp.onclick = function(){
					window.open("/common/zoom_popup.asp?type=photo&zoom_image="+this.getAttribute("zimgf"), "_blank", "location=no, menubar=no, status=no, scrollbars=no, resizable=yes,alwaysraised=yes, height="+this.getAttribute("zimgh")+", width="+this.getAttribute("zimgw")); 
				}
			}else{
				temp = $("galimg" + this.galleryID);
				temp.style.cursor = "default";
				temp.setAttribute("zimgf", "");
				temp.setAttribute("zimgh", "");
				temp.setAttribute("zimgw", "");
				temp.onclick = function(){};
			}
			
			Element.setInnerHTML("galcur" + this.galleryID, this.currentImage+1);
			Element.setInnerHTML("galtot" + this.galleryID, this.imageTotal);
			
			if($("galcap" + this.galleryID)){
				if(this.imageArray[imgNum].caption){
					$("galcap" + this.galleryID).style.display = "";
					Element.setInnerHTML("galcap" + this.galleryID, this.imageArray[imgNum].caption);
				}else{
					$("galcap" + this.galleryID).style.display = "none";
				}
			}
			$("galleryContainer" + this.galleryID).style.display = "";
		}else{
			$("galleryContainer" + this.galleryID).style.display = "none";
		}
	},
	
	setWidth: function(newW){
		this.maxWidth = newW;
		this.showImage(this.currentImage);
	}
	
}



