// BEGIN PHOTOBOX2 SUBCLASS
PhotoBox2 = function(el, userConfig) {
	if (arguments.length > 0) {
		PhotoBox2.superclass.constructor.call(this, el, userConfig);
	}
}

// Inherit from YAHOO.widget.Module
YAHOO.extend(PhotoBox2, YAHOO.widget.Module);

// Initialize the PhotoBox2
PhotoBox2.prototype.init = function(el, userConfig) {
	PhotoBox2.superclass.init.call(this, el);
	
	this.beforeInitEvent.fire(PhotoBox2);
	
	if (userConfig) {
		this.cfg.applyConfig(userConfig, true);
	}
	
	YAHOO.util.Event.addListener(this.element, "mousedown", this.thumbClicked, this, true);
	this.thumbListDiv = YAHOO.util.Dom.get("gall_thumbwrap");
	this.captionDiv = YAHOO.util.Dom.get("gall_caption");
	this.previewDiv = YAHOO.util.Dom.getElementsByClassName("main_gallpic", "div", this.element)[0];
	
	this.playTimerID = 0;
	
	var navControls = YAHOO.util.Dom.get("gall_controls");
	var prevDiv = YAHOO.util.Dom.getElementsByClassName("gall_prev", "div", navControls)[0]; 
	var playDiv = YAHOO.util.Dom.getElementsByClassName("gall_playstop", "div", navControls)[0]; 
	var nextDiv = YAHOO.util.Dom.getElementsByClassName("gall_next", "div", navControls)[0];
	var expandDiv = YAHOO.util.Dom.get("expand_butt");
	
	this.onExpand = new YAHOO.util.CustomEvent("expand", this);
	
	YAHOO.util.Event.addListener(prevDiv, "mousedown", this.prevClicked, this, true);
	YAHOO.util.Event.addListener(nextDiv, "mousedown", this.nextClicked, this, true);
	YAHOO.util.Event.addListener(playDiv, "mousedown", this.playClicked, this, true);
	
	YAHOO.util.Event.addListener(expandDiv, "mousedown", this.expandClicked, this, true);
	
	this.initEvent.fire(PhotoBox2);
}

// Set up the PhotoBox2's "photos" property for setting up the list of photos
PhotoBox2.prototype.initDefaultConfig = function() {
	PhotoBox2.superclass.initDefaultConfig.call(this);
	this.cfg.addProperty("photos", {handler: this.configPhotos, suppressEvent:true });
	this.cfg.addProperty("preview_width", {value: 278});
	this.cfg.addProperty("preview_height", {value: 186});
	this.cfg.addProperty("thumbnail_width", {value: 84});
	this.cfg.addProperty("thumbnail_height", {value: 56});
	this.cfg.addProperty("hspacing", {value: 2});
	this.cfg.addProperty("vspacing", {value: 2});
	this.cfg.addProperty("thumbnail_count", {value: 16}),
	this.cfg.addProperty("timer", {value: 3000});
}

// Handler executed when the "photos" property is modified
PhotoBox2.prototype.configPhotos = function(type, args, obj) {
	var photos = args[0];
	if (photos) {
		this.images = [];
		
		if (! (photos instanceof Array)) {
			photos = [photos];
		}
		
		this._removeChildrenFromNode(this.thumbListDiv);
		
		var thumbnailCount = this.cfg.getProperty("thumbnail_count");
		
		for (var p = 0; p < photos.length; p++) {
			var photo = photos[p];
			
			// create thumbnail
			var thumbDiv = document.createElement("DIV");
			YAHOO.util.Dom.addClass(thumbDiv, "gall_thumb");
			this.thumbListDiv.appendChild(thumbDiv);
			
			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("width", this.cfg.getProperty("thumbnail_width"));
			thumbImg.setAttribute("height", this.cfg.getProperty("thumbnail_height"));
			thumbImg.setAttribute("id", this.id + "_img_" + p);
			thumbDiv.appendChild(thumbImg);
			if (p >= thumbnailCount) {
				YAHOO.util.Dom.setStyle(thumbImg.parentNode, "display", "none");
			} else {
				YAHOO.util.Dom.setStyle(thumbImg.parentNode, "display", "block");
			} 
			YAHOO.util.Dom.setStyle(thumbImg.parentNode, "opacity", 0.5);
			this.images[this.images.length] = thumbImg;
		}
		
		this.firstVisibleThumb = 0;
		this.setImage(0);
	}
}

PhotoBox2.prototype.thumbClicked = function(e) {
	try {
		var id = YAHOO.util.Event.getTarget(e).id;
		var pos = id.substring(id.lastIndexOf("_") + 1);
		var newIndex = parseInt(pos);
		if (!isNaN(newIndex)) {
			this.setImage(newIndex);
		}
	} catch (e) {}
}


PhotoBox2.prototype.prevClicked = function(e) {
	try {
		if (this.firstVisibleThumb == 0) {
			return;
		}
		
		var thumbnailCount = this.cfg.getProperty("thumbnail_count");
		
		for (var i = this.firstVisibleThumb; i < (this.firstVisibleThumb + thumbnailCount) && i < this.images.length; i++) {
			YAHOO.util.Dom.setStyle(this.images[i].parentNode, "display", "none");
		}
		
		this.firstVisibleThumb = Math.max(0, this.firstVisibleThumb - thumbnailCount);
		
		for (var i = this.firstVisibleThumb; i < (this.firstVisibleThumb + thumbnailCount) && i < this.images.length; i++) {
			YAHOO.util.Dom.setStyle(this.images[i].parentNode, "display", "block");
		} 

		if (this.firstVisibleThumb > 0) {
			YAHOO.util.Dom.setStyle(this.prevDiv, "display", "block");
		} else {
			YAHOO.util.Dom.setStyle(this.prevDiv, "display", "none");
		}
		
		if (i < this.images.length) {
			YAHOO.util.Dom.setStyle(this.nextDiv, "display", "block");
		} else {
			YAHOO.util.Dom.setStyle(this.nextDiv, "display", "none");
		}
	} catch (e) {}
}

PhotoBox2.prototype.nextClicked = function(e) {
	try {
		if (this.firstVisibleThumb + thumbnailCount >= this.images.lenth) {
			return;
		}
		
		var thumbnailCount = this.cfg.getProperty("thumbnail_count");
		
		for (var i = this.firstVisibleThumb; i < (this.firstVisibleThumb + thumbnailCount) && i < this.images.length; i++) {
			YAHOO.util.Dom.setStyle(this.images[i].parentNode, "display", "none");
		}
		
		this.firstVisibleThumb = i;
		
		for (var i = this.firstVisibleThumb; i < (this.firstVisibleThumb + thumbnailCount) && i < this.images.length; i++) {
			YAHOO.util.Dom.setStyle(this.images[i].parentNode, "display", "block");
		} 

		if (this.firstVisibleThumb > 0) {
			YAHOO.util.Dom.setStyle(this.prevDiv, "display", "block");
		} else {
			YAHOO.util.Dom.setStyle(this.prevDiv, "display", "none");
		}
		
		if (i < this.images.length) {
			YAHOO.util.Dom.setStyle(this.nextDiv, "display", "block");
		} else {
			YAHOO.util.Dom.setStyle(this.nextDiv, "display", "none");
		}
	} catch (e) {}
}

PhotoBox2.prototype.playClicked = function(e) {
	try {
		if (this.playTimerID == 0) {
			// play
			this.showNext();
			this.playTimerID = setTimeout("pagePhotoBoxView.showNext()", parseInt(this.cfg.getProperty("timer")));
		} else {
			// stop
			clearTimeout(this.playTimerID);
			this.playTimerID = 0;
		}
	} catch (e) {}
}

PhotoBox2.prototype.expandClicked = function(e) {
	try {
		//this.onExpand.fire(this.currentImage);
		displayObject('photo_holdlg');pagePhotoBox3View.doShow(0);
	} catch (e) { alert(e);}
}


// sets the current image displayed in the PhotoBox2
PhotoBox2.prototype.setImage = function(index) {
	if (this.currentImage) {
		YAHOO.util.Dom.setStyle(this.images[this.currentImage].parentNode, "opacity", 0.5);
	}
	YAHOO.util.Dom.setStyle(this.images[index].parentNode, "opacity", 1);
		
	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;
}

PhotoBox2.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);
	}
	
	if (this.playTimerID != 0) {
		clearTimeout(this.playTimerID);
		this.playTimerID = setTimeout("pagePhotoBoxView.showNext()", parseInt(this.cfg.getProperty("timer")));
	}
}

PhotoBox2.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 PhotoBox2
PhotoBox2.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 createPhotoBox2(divId, previewWidth, previewHeight, thumbnailWidth, thumbnailHeight, photos) {
	var pagePhotoBox2View = new PhotoBox2(divId, {preview_width: previewWidth, preview_height: previewHeight, 
		thumbnail_width:thumbnailWidth, thumbnail_height: thumbnailHeight });
	pagePhotoBox2View.render();
	
	pagePhotoBox2View.cfg.setProperty("photos", photos);
	return pagePhotoBox2View;
}

