/* ----------------------------------------------------------------------
 * js/ui.php : javascript-based user interface controls
 * ----------------------------------------------------------------------
 * OpenCollectionAccess
 * Open-source collections management software
 * ----------------------------------------------------------------------
 *
 * Software by Whirl-i-Gig (http://www.whirl-i-gig.com)
 * Copyright 2007 Whirl-i-Gig
 *
 * For more information visit http://www.opencollection.org
 *
 * This program is free software; you may redistribute it and/or modify it under
 * the terms of the provided license as published by Whirl-i-Gig
 *
 * OpenCollection is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTIES whatsoever, including any implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 *
 * This source code is free and modifiable under the terms of 
 * GNU General Public License. (http://www.gnu.org/copyleft/gpl.html). See
 * the "license.txt" file for details, or visit the OpenCollection web site at
 * http://www.opencollection.org
 *
 * ----------------------------------------------------------------------
 */
// ------------------------------------------------------------------------------
//
// Image scroller class definition
//
// ------------------------------------------------------------------------------
// Constructor
function imageScroller(scrollingImageList, imageContainerID, options) {
	if (!options) { options = new Object(); }
	
	this.scrollingImageList = scrollingImageList;
	this.curScrollImageIndex = options.startImage;					// initial image to display
	if (!this.curScrollImageIndex) { this.curScrollImageIndex = 0; }
	
	this.scrollingImageLookAhead = options.lookAhead;				// number of images to preload following current image
	if (!this.scrollingImageLookAhead) { this.scrollingImageLookAhead = 3; }
	
	this.scrollingImageScrollSpeed = options.scrollSpeed;			// time (in seconds) each scroll takes
	if (!this.scrollingImageScrollSpeed) { this.scrollingImageScrollSpeed = 0.25; }
	
	this.containerWidth	= options.containerWidth;					// height of DIV containing images
	if (!this.containerWidth) { this.containerWidth = 400; }
	
	this.containerHeight = options.containerHeight;					// width of DIV containing images
	if (!this.containerHeight) { this.containerHeight = 400; }
	
	this.imageContainerID = imageContainerID;
	if (!this.imageContainerID ) { this.imageContainerID = 'scrollingImages'; }
	
	this.imageCounterID = options.imageCounterID;
	this.imageTitleID = options.imageTitleID;
	
	this.noVertCentering = options.noVertCentering;
	this.noHorizCentering = options.noHorizCentering;
	
	this.scrollToImage(0, true);
}
// ------------------------------------------------------------------------------
imageScroller.prototype.getCurrentIndex = function() {
	return this.curScrollImageIndex;
}
// ------------------------------------------------------------------------------
imageScroller.prototype.scrollToNextImage = function() {
	this.scrollToImage(1);
}
// ------------------------------------------------------------------------------
imageScroller.prototype.scrollToPreviousImage = function() {
	this.scrollToImage(-1);
}
// ------------------------------------------------------------------------------
imageScroller.prototype.scrollToImage = function(offset, dontUseEffects) {
	var targetImageIndex = this.curScrollImageIndex + offset;
	if ((targetImageIndex < 0) || (targetImageIndex >= this.scrollingImageList.length)) { return false; }
	
	// create new image container divs if needed
	var i;
	var maxImageIndex = targetImageIndex + this.scrollingImageLookAhead;
	if (maxImageIndex >= this.scrollingImageList.length) { maxImageIndex = this.scrollingImageList.length - 1; }
	var minImageImage = targetImageIndex - this.scrollingImageLookAhead;
	if (minImageImage < 0) { minImageImage = 0; }
	
	for(i=minImageImage; i <= maxImageIndex; i++) {
		if (!$("scrollingImage" + i)) {
			var horizCentering, vertCentering;
			if (this.noHorizCentering) { horizCentering = ''; } else { horizCentering = 'margin-left: ' + ((this.containerWidth - this.scrollingImageList[i].width)/2) + 'px;'; }
			if (this.noVertCentering) { vertCentering = ''; } else { vertCentering = 'margin-top: ' + ((this.containerHeight - this.scrollingImageList[i].height)/2) + 'px;'; }
			Element.insert($(this.imageContainerID), '<div class="imageScrollerImage" id="scrollingImage' + i + '" style="'+horizCentering + ' ' + vertCentering +'"><img src="' + this.scrollingImageList[i].url+ '" width="' + this.scrollingImageList[i].width + '" height ="' + this.scrollingImageList[i].height + '" border=\'0\'"></div>');
			$('scrollingImage' + i).style.left = (this.containerWidth * i)  + "px";
		}
	}
	
	// do scroll
	if (dontUseEffects) {
		$(this.imageContainerID).style.left = (targetImageIndex * -1 * this.containerWidth) + "px";
	} else {
		new Effect.Move ($(this.imageContainerID),{ x: targetImageIndex * -1 * this.containerWidth, y: 0, mode: 'absolute', duration: this.scrollingImageScrollSpeed, queue: "end"});
	}
	if (this.imageTitleID) {
		$(this.imageTitleID).innerHTML = this.scrollingImageList[targetImageIndex].title;
	}
	if (this.imageCounterID) {
		$(this.imageCounterID).innerHTML = "(" + (targetImageIndex + 1) + "/" + this.scrollingImageList.length + ")";
	}
	this.curScrollImageIndex = targetImageIndex;
}
// ------------------------------------------------------------------------------
//
// DIV scroller utility (used by GoogleMaps plugin);
//
// ------------------------------------------------------------------------------
// Constructor
var _gMapScrollPositions = new Object();
function gMapScrollToNextItem(divID, c, counterDivID) {
	if (!_gMapScrollPositions[divID]) { _gMapScrollPositions[divID] = 0; }
	var p = _gMapScrollPositions[divID];
	
	if (p >= (c-1)) { return; }
	new Effect.Move($(divID), {x:0,y:-400,mode:"relative",queue:"end",duration:0.2})
	_gMapScrollPositions[divID]++;
	
	if ($(counterDivID)) {
		$(counterDivID).innerHTML = (p+2) + '/' + c;
	}
}
// ------------------------------------------------------------------------------
function gMapScrollToPreviousItem(divID, c,counterDivID) {
	if (!_gMapScrollPositions[divID]) { _gMapScrollPositions[divID] = 0; }
	var p = _gMapScrollPositions[divID];
	
	if (p <= 0) { return; }
	new Effect.Move($(divID), {x:0,y:400,mode:"relative",queue:"end",duration:0.2})
	_gMapScrollPositions[divID]--;
	
	if ($(counterDivID)) {
		$(counterDivID).innerHTML = p + '/' + c;
	}
}
// ------------------------------------------------------------------------------
//
// Utilities
//
// ------------------------------------------------------------------------------
// Simple Javascript pop-up window function
// ------------------------------------------------------------------------------
function OpenWindow(url,name,width,height,style) {
	browsername=navigator.appName;
	
	var styledesc;
	switch(style) {
		case 'minimal':
			styledesc = ',directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no';
			break;
		default:
			styledesc = ',directories=no,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=no,toolbar=no';
			break;
	}
	
	popupWin = window.open(url, name, 'width=' + width + ',height=' + height + styledesc);
	
	if (popupWin.opener==null) popupWin.opener = window;
	
	popupWin.moveTo(0,0);
	popupWin.focus();
}
// ------------------------------------------------------------------------------
