//-------------------------------------------------------------------------
//	WHGallery - by Wes Plybon
//		Inspired by and code modified from Lighbox 2.0 - http://www.huddletogether.com/projects/lightbox2/ - by Lokesh Dhakar 
//
//

//A simple debug function
//Throws an error to the js console
function debug(aMsg) {
	setTimeout(function() { throw new Error(aMsg); }, 0);
}


//
//	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 () {
	for(i = 0; i <= this.length; 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. Loops through anchor tags looking for 
	// 'lightbox' references and applies onclick events to appropriate links. The 2nd section of
	// the function inserts html at the bottom of the page which is used to display the shadow 
	// overlay and the image container.
	//
	initialize: function(galid) {
		this.imageArray = new Array;
		this.galleryID = galid;
		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'));
			
			//alert(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')
				} );
				//alert("");
			}
			//this.imageArray.removeDuplicates();
		//	while(imageArray[imageNum][0] != imageLink.getAttribute('href')) { imageNum++;}
		}

		this.imageTotal = this.imageArray.length;		

		this.showImage(this.currentImage);
	},
	
	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){
		//alert("galimg" + this.galleryID);
		Element.setSrc("galimg" + this.galleryID, this.imageArray[imgNum].largePath);	
		
		if(this.imageArray[imgNum].largeWidth > this.maxWidth){
			Element.setWidth("galimg" + this.galleryID, this.maxWidth);
		}else{
			Element.setWidth("galimg" + this.galleryID, this.imageArray[imgNum].largeWidth);
		}
		
		if(this.imageArray[imgNum].zoomPath){
			temp = $("galzoom" + this.galleryID);
			if(!temp){ 
				temp = $("galimage" + this.galleryID);
			}else{
				temp.style.display = "";
			}
			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("/commoninc/pushpage/5/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 = $("galzoom" + this.galleryID);
			if(!temp){ 
				temp = $("galimage" + this.galleryID);
			}else{
				temp.style.display = "none";
			}
			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);
				Element.setWidth("galcap" + this.galleryID, this.imageArray[imgNum].largeWidth - 10);
			}else{
				$("galcap" + this.galleryID).style.display = "none";
			}
		}
		$('galbar'+this.galleryID).style.display = this.imageTotal < 2 ? "none" : "";
	},
	
	setWidth: function(newW){
		this.maxWidth = newW;
		this.showImage(this.currentImage);
	}
	
}



