var Portfolio;
if (!Portfolio) Portfolio = {};
if (!Portfolio.galleryViewer) Portfolio.galleryViewer = {};

Portfolio.galleryViewer = function (element) {

	this.LeftButton; 
	this.RightButton;
	this.element = element;
	this.photos;
	this.selectedIndex = 0;

	this.attachHandlers (element);
};


Portfolio.galleryViewer.prototype.attachHandlers = function (element) {
	
	//recieves a box with multiple photos inside and a particular setup for the buttons.
	var self = this;
	var thebutton;
	var divsToCheck = element.getElementsByTagName("div");	
	for (y in divsToCheck){
		if (isNaN(y)) { continue; }
		if (divsToCheck[y].tagName == "div" || divsToCheck[y].tagName == "DIV"){
			//only check divs, not any other sort of objects.
			if ( document.hasClassName(divsToCheck[y], "galleryViewerButtonLeft")) 	{
				thebutton = divsToCheck[y];
				thebutton.addEventListener('click',function(e) { return self.changeViewer(e, "left"); },false);
				this.LeftBtn = thebutton;
			} else 	if ( document.hasClassName(divsToCheck[y], "galleryViewerButtonRight")) 	{
				thebutton = divsToCheck[y];
				Portfolio.galleryViewer.addEventListener(thebutton,'click',function(e) { return self.changeViewer(e, "right"); },false);
				this.RightBtn = thebutton;
			} 
		} 
	}

	var turnOnPhoto = true;
	photos = new Array();

	var imgsToCheck = element.getElementsByTagName("img");	
	for (x in imgsToCheck){
		if (isNaN(x)) { continue; }
		if (imgsToCheck[x].tagName.toUpperCase() == "IMG") {
			if (document.hasClassName(imgsToCheck[x], "viewer_photo")) 	{
				if (turnOnPhoto) {
					turnOnPhoto = false;
					//this.setDisplay(imgsToCheck[x], "inline");
					Portfolio.galleryViewer.setDisplay(imgsToCheck[x], "inline");
				}
				photos.push(imgsToCheck[x]); //will use this later to scroll through photos
			}
		}
	}
	this.photos = photos;

};

Portfolio.galleryViewer.addEventListener = function(element, eventType, handler, capture)
{
	try
	{
		if (element.addEventListener)
			element.addEventListener(eventType, handler, capture);
		else if (element.attachEvent)
			element.attachEvent("on" + eventType, handler);
	}
	catch (e) {}
};

Portfolio.galleryViewer.setDisplay = function(ele, display)
{
	if( ele )
		ele.style.display = display;
};

Portfolio.galleryViewer.prototype.changeViewer = function (e, direction) {
	//var sender = (e && e.target) || (window.event && window.event.srcElement); //sender is the DOM element which was clicked
	var newImage, lastImageIndex;
	
	this.photos[this.selectedIndex].style.display = "none";

	if (direction == "right") { 
		if (this.selectedIndex >= this.photos.length-1) {
			//if this is the end of the list, start over
			//this.selectedIndex = 0;
		} else {
			this.selectedIndex++;
		}
	} else if (direction == "left") { 
		if (this.selectedIndex <= 0) {
			//if this is the beginning of the list, go the other direction
			//this.selectedIndex = this.photos.length-1;
		} else {
			this.selectedIndex--;
		}	
	}
	this.photos[this.selectedIndex].style.display = "inline";				
};

