// 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");
	this.prevDiv = YAHOO.util.Dom.getElementsByClassName("gall_prev", "div", navControls)[0]; 
	this.playDiv = YAHOO.util.Dom.getElementsByClassName("gall_play", "div", navControls)[0]; 
	this.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(this.prevDiv, "mousedown", this.showPrev, this, true);
	YAHOO.util.Event.addListener(this.nextDiv, "mousedown", this.showNext, this, true);
	YAHOO.util.Event.addListener(this.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("thumbnail_padding", {value: 6});
	this.cfg.addProperty("thumbnail_count", {value: 14}),
	this.cfg.addProperty("timer", {value: 3500});
	this.cfg.addProperty("nowrap", {value: false});
	this.cfg.addProperty("use_tabs", {value: false});
	this.cfg.addProperty("photo_tabs", {value: null});
};

// 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;
		}
		
		if (this.cfg.getProperty("nowrap")) {
			YAHOO.util.Dom.setStyle("gall_thumbwrap", "width", 
					"" + (this.images.length * (this.cfg.getProperty("thumbnail_width") + this.cfg.getProperty("thumbnail_padding"))) + "px" );
		}
		
		this.setFirstVisibleThumb(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 {
		var thumbnailCount = this.cfg.getProperty("thumbnail_count");
		
		if (this.firstVisibleThumb == 0) {
			return;
		}
		
		this.setFirstVisibleThumb(Math.max(0, this.firstVisibleThumb - thumbnailCount));
	} catch (e) {}
};

PhotoBox2.prototype.nextClicked = function(e) {
	try {
		var thumbnailCount = this.cfg.getProperty("thumbnail_count");
		
		if (this.firstVisibleThumb + thumbnailCount >= this.images.length) {
			return;
		}
		
		this.setFirstVisibleThumb(this.firstVisibleThumb + thumbnailCount);
	} catch (e) {}
};

PhotoBox2.prototype.setFirstVisibleThumb = function(index) {
	try {
		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 = index;
		
		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, "opacity", 1);
//		} else {
//			YAHOO.util.Dom.setStyle(this.prevDiv, "opacity", 0.5);
//		}
		
//		if (i < this.images.length) {
//			YAHOO.util.Dom.setStyle(this.nextDiv, "opacity", 1);
//		} else {
//			YAHOO.util.Dom.setStyle(this.nextDiv, "opacity", 0.5);
//		}
	} catch (e) {}
};

PhotoBox2.prototype.playClicked = function(e) {
	this.play(true);
};

PhotoBox2.prototype.play = function(doShowNext) {
	try {
		if (this.playTimerID == 0) {
			// play
			if (doShowNext) {
				this.showNext();
			}
			YAHOO.util.Dom.replaceClass(this.playDiv, 'gall_play', 'gall_stop');
			this.playTimerID = setTimeout("pagePhotoBoxView.showNext()", parseInt(this.cfg.getProperty("timer")));
		} else {
			// stop
			YAHOO.util.Dom.replaceClass(this.playDiv, 'gall_stop', 'gall_play');
			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 (YAHOO.lang.isNumber(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");
	
	var me = this;
	YAHOO.util.Event.addListener(imgNode, "load", function() {
		me.imgLoaded(imgNode);
	});
	
	imgNode.setAttribute("src", this.cfg.getProperty("photos")[index].src);
	imgNode.setAttribute("title", current.title);
	this.captionDiv.innerHTML = current.title;
	
	if (this.cfg.getProperty("nowrap")) {
		var thumbWrapHolder = YAHOO.util.Dom.get("gall_thumbwrapholder");
		if (thumbWrapHolder.scrollLeft + thumbWrapHolder.clientWidth < this.images[index].offsetLeft + this.images[index].clientWidth) {
			thumbWrapHolder.scrollLeft = this.images[index].offsetLeft - thumbWrapHolder.clientWidth +  this.images[index].clientWidth;
		} else if (this.images[index].offsetLeft < thumbWrapHolder.scrollLeft) {
			thumbWrapHolder.scrollLeft = this.images[index].offsetLeft;
		}
	}
};

PhotoBox2.prototype.showNext = function() {
	if (typeof this.currentImage == "undefined") {
		this.currentImage = 0;
	}
	if (this.currentImage + 1 < this.images.length) {
		this.setImage(this.currentImage + 1);
		if (this.currentImage >= this.firstVisibleThumb + this.cfg.getProperty("thumbnail_count")) {
			this.setFirstVisibleThumb(this.currentImage);
		}
	} else {
		this.setImage(0);
		this.setFirstVisibleThumb(0);
	}
	
	if (this.playTimerID != 0) {
		clearTimeout(this.playTimerID);
		this.playTimerID = setTimeout("pagePhotoBoxView.showNext()", parseInt(this.cfg.getProperty("timer")));
	}
};

PhotoBox2.prototype.showPrev = function() {
	if (typeof this.currentImage == "undefined") {
		this.currentImage = 0;
	}
	if (this.currentImage > 0) {
		this.setImage(this.currentImage - 1);
		if (this.currentImage < this.firstVisibleThumb) {
			this.setFirstVisibleThumb(this.firstVisibleThumb - this.cfg.getProperty("thumbnail_count"));
		}
	} else {
		this.setImage(this.images.length - 1);
		this.setFirstVisibleThumb(this.images.length - 1 - ((this.images.length - 1) % this.cfg.getProperty("thumbnail_count")));
	}
	
	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");
	
	var me = this;
	YAHOO.util.Event.addListener(imgNode, "load", function() {
		me.imgLoaded(imgNode);
	});
	
	imgNode.setAttribute("src", url);
	imgNode.setAttribute("title", "");
	
	this.captionDiv.innerHTML = "";
};

PhotoBox2.prototype.imgLoaded = function(imgNode) {
	var origRatio = imgNode.width / imgNode.height;
	var fitRatio = parseFloat(this.cfg.getProperty("preview_width")) / parseFloat(this.cfg.getProperty("preview_height"));
	var finalWidth = 0;
	var finalHeight = 0;
	if (origRatio >= fitRatio) {
		finalWidth = parseFloat(this.cfg.getProperty("preview_width"));
		finalHeight = finalWidth / origRatio;
	} else {
		finalHeight = parseFloat(this.cfg.getProperty("preview_height"));
		finalWidth = finalHeight * origRatio;
	}
	imgNode.setAttribute("width", finalWidth);
	imgNode.setAttribute("height", finalHeight);
	
	if (this.previewDiv.childNodes.length != 0) {
		this.previewDiv.replaceChild(imgNode, this.previewDiv.childNodes[0]);
	} else {
		this.previewDiv.appendChild(imgNode);
	}
};

function createPhotoBox2(divId, previewWidth, previewHeight, thumbnailWidth, thumbnailHeight, thumbnailCount, photos) {
	var pagePhotoBox2View = new PhotoBox2(divId, {preview_width: previewWidth, preview_height: previewHeight, 
		thumbnail_width:thumbnailWidth, thumbnail_height: thumbnailHeight, thumbnail_count:  thumbnailCount});
	pagePhotoBox2View.render();
	
	pagePhotoBox2View.cfg.setProperty("photos", photos);
	//pagePhotoBox2View.play(false);
	return pagePhotoBox2View;
}

function createMultiMiniPhotoBox(divId, previewWidth, previewHeight, thumbnailWidth, thumbnailHeight, thumbnailPadding, thumbnailCount, photos) {
	var pagePhotoBox2View = new PhotoBox2(divId, {preview_width: previewWidth, preview_height: previewHeight, 
		thumbnail_width:thumbnailWidth, thumbnail_height: thumbnailHeight, thumbnail_padding: thumbnailPadding, thumbnail_count:  thumbnailCount, nowrap: true});
	pagePhotoBox2View.render();
	pagePhotoBox2View.cfg.setProperty("photos", photos);
	//pagePhotoBox2View.play(false);
	return pagePhotoBox2View;
}

function createMultiMiniTabsPhotoBox(divId, previewWidth, previewHeight, thumbnailWidth, thumbnailHeight, thumbnailCount, photoTabs) {
	var photoViewDiv = YAHOO.util.Dom.get(divId);
	var tabs = document.createElement("div");
	tabs.setAttribute("id", divId + "_tabs");
	photoViewDiv.parentNode.insertBefore(tabs, photoViewDiv);
	var tabView = new YAHOO.widget.TabView();
	for (var i = 0; i < photoTabs.length; i++) {
	    tabView.addTab( new YAHOO.widget.Tab({label: photoTabs[i].label}));
	}
	tabView.appendTo(divId + "_tabs");
	tabView.subscribe('activeIndexChange', function(event) {
		var tabs = pagePhotoBoxView.cfg.getProperty("photo_tabs");
		if (tabs) {
			pagePhotoBoxView.cfg.setProperty("photos", tabs[event.newValue].list);
		}
	});
	
	var pagePhotoBox2View = new PhotoBox2(divId, {preview_width: previewWidth, preview_height: previewHeight, 
		thumbnail_width:thumbnailWidth, thumbnail_height: thumbnailHeight, thumbnail_count:  thumbnailCount, nowrap: true,
		use_tabs: true});
	pagePhotoBox2View.render();
	pagePhotoBox2View.cfg.setProperty("photo_tabs", photoTabs);
	//pagePhotoBox2View.play(false);
	pagePhotoBox2View.tabView = tabView;
	pagePhotoBox2View.tabView.selectTab(0);
	return pagePhotoBox2View;
}


