// BEGIN PHOTOBOX2 SUBCLASS
PhotoBox3 = function(el, userConfig) {
	if (arguments.length > 0) {
		PhotoBox3.superclass.constructor.call(this, el, userConfig);
	}
}

// Inherit from YAHOO.widget.Module
YAHOO.extend(PhotoBox3, YAHOO.widget.Overlay);

// Initialize the PhotoBox3
PhotoBox3.prototype.init = function(el, userConfig) {
	PhotoBox3.superclass.init.call(this, el);
	
	this.beforeInitEvent.fire(PhotoBox3);
	
	if (userConfig) {
		this.cfg.applyConfig(userConfig, true);
	}
	
/*
	this.shadeDiv = document.createElement("DIV");
	YAHOO.util.Dom.setStyle(this.shadeDiv, "z-index", "1000");
	YAHOO.util.Dom.setStyle(this.shadeDiv, "width", "100%");
	YAHOO.util.Dom.setStyle(this.shadeDiv, "height", "100%");
	YAHOO.util.Dom.setStyle(this.shadeDiv, "background-color", "#cecece");
	YAHOO.util.Dom.setStyle(this.shadeDiv, "position", "absolute");
	YAHOO.util.Dom.setStyle(this.shadeDiv, "left", "0px");
	YAHOO.util.Dom.setStyle(this.shadeDiv, "top", "0px");
	YAHOO.util.Dom.setStyle(this.shadeDiv, "opacity", 0);
	YAHOO.util.Dom.setStyle(this.shadeDiv, "display", "none");
	
	document.body.appendChild(this.shadeDiv);
*/
	
	YAHOO.util.Dom.setStyle(this.element, "z-index", "1001");
	
	this.captionDiv = YAHOO.util.Dom.get("photo_caption");
	this.previewDiv = YAHOO.util.Dom.get("photo_lg");
	
	var prevDiv = YAHOO.util.Dom.get("photo_prev"); 
	var nextDiv = YAHOO.util.Dom.get("photo_next"); 
	var closeDiv = YAHOO.util.Dom.get("photo_close");
	
	YAHOO.util.Event.addListener(prevDiv, "mousedown", this.prevClicked, this, true);
	YAHOO.util.Event.addListener(nextDiv, "mousedown", this.nextClicked, this, true);
	YAHOO.util.Event.addListener(closeDiv, "mousedown", this.closeClicked, this, true);
	
	this.initEvent.fire(PhotoBox3);
}

// Set up the PhotoBox3's "photos" property for setting up the list of photos
PhotoBox3.prototype.initDefaultConfig = function() {
	PhotoBox3.superclass.initDefaultConfig.call(this);
	this.cfg.addProperty("photos", {handler: this.configPhotos, suppressEvent:true });
}

// Handler executed when the "photos" property is modified
PhotoBox3.prototype.configPhotos = function(type, args, obj) {
	var photos = args[0];
	if (photos) {
		this.images = [];
		if (! (photos instanceof Array)) {
			photos = [photos];
		}
		for (var p = 0; p < photos.length; p++) {
			var photo = photos[p];
			
			var thumbImg = document.createElement("IMG");
			if (photo.thumbsrc && photo.thumbsrc != "") {
				thumbImg.setAttribute("src", photo.thumbsrc);
			} else {
				thumbImg.setAttribute("src", photo.src);
			}
			thumbImg.setAttribute("title", photo.caption);
			thumbImg.setAttribute("id", this.id + "_img_" + p);
			this.images[this.images.length] = thumbImg;
		}
		this.setImage(0);
	}
}

PhotoBox3.prototype.prevClicked = function(e) {
	try {
		this.showPrev();
	} catch (e) {}
}

PhotoBox3.prototype.nextClicked = function(e) {
	try {
		this.showNext();
	} catch (e) {}
}

PhotoBox3.prototype.closeClicked = function(e) {
	try {
		this.doHide();
	} catch (e) {}
}

PhotoBox3.prototype.doShow = function(startImg) {
	try {
		// YAHOO.util.Dom.setStyle(this.shadeDiv, "display", "block");
		// YAHOO.util.Dom.setStyle(this.shadeDiv, "opacity", 0.6); 
		this.show();
		if (startImg) {
			this.setImage(startImg);
		} else {
			this.setImage(0);
		}
	} catch (e) {}
}

PhotoBox3.prototype.doHide = function() {
	try {
		this.hide();
		//YAHOO.util.Dom.setStyle(this.shadeDiv, "opacity", 0);
		//YAHOO.util.Dom.setStyle(this.shadeDiv, "display", "none");
	} catch (e) {}
}


// sets the current image displayed in the PhotoBox3
PhotoBox3.prototype.setImage = function(index) {
	var photos = this.cfg.getProperty("photos");
	if (photos) {
		if (! (photos instanceof Array)) {
			photos = [photos];
		}
	}
	
	var current = this.images[index];
	
	this.currentImage = index;
	
	var imgNode = document.createElement("IMG");
	imgNode.setAttribute("src", this.cfg.getProperty("photos")[index].src);
	imgNode.setAttribute("title", current.title);
//	imgNode.setAttribute("width", this.cfg.getProperty("preview_width"));
//	imgNode.setAttribute("height", this.cfg.getProperty("preview_height"));
	
	if(this.previewDiv.childNodes.length != 0) {
		this.previewDiv.replaceChild(imgNode, this.previewDiv.childNodes[0]);
	} else {
		this.previewDiv.appendChild(imgNode);
	}
	
	this.captionDiv.innerHTML = current.title;
}

PhotoBox3.prototype.showNext = function() {
	if (typeof this.currentImage == "undefined") {
		this.currentImage = 0;
	}
	if (this.currentImage + 1 < this.images.length) {
		this.setImage(this.currentImage + 1);
	} else {
		this.setImage(0);
	}
}

PhotoBox3.prototype.showPrev = function() {
	if (typeof this.currentImage == "undefined") {
		this.currentImage = 0;
	}
	if (this.currentImage - 1 >= 0) {
		this.setImage(this.currentImage - 1);
	} else {
		this.setImage(this.images.length - 1);
	}
}

PhotoBox3.prototype._removeChildrenFromNode = function(node) {
   if (node == undefined && node == null) {
      return;
   }
   var len = node.childNodes.length;
   for(var i = len -1 ; i >= 0; i--) {
      node.removeChild(node.childNodes[i]);
   }
}

// sets the current image displayed in the PhotoBox3
PhotoBox3.prototype.setImageByUrl = function(url) {
	
	var imgNode = document.createElement("IMG");
	imgNode.setAttribute("src", url);
	imgNode.setAttribute("title", "");
//	imgNode.setAttribute("width", this.cfg.getProperty("preview_width"));
//	imgNode.setAttribute("height", this.cfg.getProperty("preview_height"));
	
	if(this.previewDiv.childNodes.length != 0) {
		this.previewDiv.replaceChild(imgNode, this.previewDiv.childNodes[0]);
	} else {
		this.previewDiv.appendChild(imgNode);
	}
	
	this.captionDiv.innerHTML = "";
}

function createPhotoBox3(divId, photos) {
	var pagePhotoBox3View = new PhotoBox3(divId, {fixedcenter: true, visible: false} );
	pagePhotoBox3View.render();
	
	pagePhotoBox3View.cfg.setProperty("photos", photos);
	return pagePhotoBox3View;
}

