/* ----------------------------------------------------------------------
 * js/OpenCollectionAccessUI.js : javascript-based user interface controls
 * ----------------------------------------------------------------------
 * OpenCollectionAccess
 * Open-source collections management software
 * ----------------------------------------------------------------------
 *
 * Software by Whirl-i-Gig (http://www.whirl-i-gig.com)
 * Copyright 2007-2008 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
 *
 * ----------------------------------------------------------------------
 */

var OpenCollectionAccessUI = OpenCollectionAccessUI || {};

(function ($) {
	
	//
	// Image scroller - displays a list of images with nice horizontal scrolling movement
	//
	OpenCollectionAccessUI.newImageScroller = function(scrollingImageList, imageContainerID, options) {
		// init
		var that = {
			scrollingImageList: scrollingImageList,
			curScrollImageIndex: options.startImage	? options.startImage : 0,				// initial image to display
			scrollingImageLookAhead: options.lookAhead ? options.lookAhead : 3,				// number of images to preload following current image
			scrollingImageScrollSpeed: options.scrollSpeed ? options.scrollSpeed : 0.25,	// time (in seconds) each scroll takes
			containerWidth: options.containerWidth ? options.containerWidth : 400,			// height of DIV containing images
			containerHeight: options.containerHeight ? options.containerHeight : 400,		// width of DIV containing images
			imageContainerID: imageContainerID ? imageContainerID : 'scrollingImages',
			imageCounterID: options.imageCounterID,
			imageTitleID: options.imageTitleID,
			noVertCentering: options.noVertCentering,
			noHorizCentering: options.noHorizCentering
		};
		
		// methods
		that.getCurrentIndex = function() {
			return this.curScrollImageIndex;
		}
		that.scrollToNextImage = function() {
			this.scrollToImage(1);
		}
		that.scrollToPreviousImage = function() {
			that.scrollToImage(-1);
		}
		that.scrollToImage = function(offset, dontUseEffects) {
			var targetImageIndex = that.curScrollImageIndex + offset;
			if ((targetImageIndex < 0) || (targetImageIndex >= that.scrollingImageList.length)) { return false; }
			
			// create new image container divs if needed
			var i;
			var maxImageIndex = targetImageIndex + that.scrollingImageLookAhead;
			if (maxImageIndex >= that.scrollingImageList.length) { maxImageIndex = that.scrollingImageList.length - 1; }
			var minImageImage = targetImageIndex - that.scrollingImageLookAhead;
			if (minImageImage < 0) { minImageImage = 0; }
			
			for(i=minImageImage; i <= maxImageIndex; i++) {
				if ($("#scrollingImage" + i).length == 0) {
					var horizCentering, vertCentering;
					if (that.noHorizCentering) { horizCentering = ''; } else { horizCentering = 'margin-left: ' + ((that.containerWidth - that.scrollingImageList[i].width)/2) + 'px;'; }
					if (that.noVertCentering) { vertCentering = ''; } else { vertCentering = 'margin-top: ' + ((that.containerHeight - that.scrollingImageList[i].height)/2) + 'px;'; }
					
					$('#' + that.imageContainerID).append('<div class="imageScrollerImage" id="scrollingImage' + i + '" style="'+horizCentering + ' ' + vertCentering +'"><a href="' + that.scrollingImageList[i].link + '" '+(that.scrollingImageList[i].onclick ? 'onclick="' + that.scrollingImageList[i].onclick + '"' : '') + '><img src="' + that.scrollingImageList[i].url+ '" width="' + that.scrollingImageList[i].width + '" height ="' + that.scrollingImageList[i].height + '" border=\'0\'"></a></div>');
					$('#scrollingImage' + i).css('left', (that.containerWidth * i)  + "px");
				}
			}
			
			// do scroll
			if (dontUseEffects) {
				$('#' + that.imageContainerID).css('left', (targetImageIndex * -1 * that.containerWidth) + "px");
			} else {
				$('#' + that.imageContainerID).animate(
					{
						left: (targetImageIndex * -1 * that.containerWidth) + "px"
					},
					that.scrollingImageScrollSpeed * 1000
				);
			}
			if (that.imageTitleID) {
				$('#' + that.imageTitleID).html(that.scrollingImageList[targetImageIndex].title);
			}
			if (that.imageCounterID) {
				$('#' + that.imageCounterID).html("Image " + (targetImageIndex + 1) + " of " + that.scrollingImageList.length);
			}
			that.curScrollImageIndex = targetImageIndex;
		}
		
		
		that.scrollToImage(0, true);
		
		return that;
	}
	
	//
	// OpenWindow (TODO: replace this code with nice new shiny code)
	//
	OpenCollectionAccessUI.OpenWindow = function(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();
	}
	
	//
	// GoogleMaps balloon scrollers
	//
	OpenCollectionAccessUI.mapScroller = function() {
		var that = {};
		var _gMapScrollPositions = new Object();
		
		that.gMapScrollToNextItem = function (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;
			}
		}
		
		that.gMapScrollToPreviousItem = function (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;
			}
		}
		
		return that;
	}
	
	//
	// Search form
	//
	/*OpenCollectionAccessUI.toggleSearchFormPresence = function(dontUseEffects) {
		var searchFormInitialHeight = 1;
		var i = 1;
		searchFormPresent = !searchFormPresent;
		if (searchFormPresent) {
			jQuery('#toggleSearchFormPresenceControl').attr('src', "<?php print $tmpl_parameters["templates_url_root"]; ?>/graphics/arrow_open.gif");
			if (dontUseEffects) {
				jQuery('#searchResults').attr('top', jQuery('#hideableHeader').attr('offsetHeight') * -1);
			} else {
				jQuery('#searchResults').animate({ top: jQuery('#hideableHeader').attr('offsetHeight') * -1}, 300);
			}
			searchFormSettings.formNotPresent = true;
		} else {
			jQuery('#toggleSearchFormPresenceControl').attr('src', "<?php print $tmpl_parameters["templates_url_root"]; ?>/graphics/arrow_close.gif");
			
			if (dontUseEffects) {
				jQuery('#searchResults').attr('top', 0);
			} else {
				jQuery('#searchResults').animate({ top: 0 }, 300);
			}
			
			searchFormSettings.formNotPresent = false;
			//toggleSearchFormSize(true, true);
		}
		//cookieJar.put('ocaSearchFormSettings', searchFormSettings);
	}*/
	
	//
	// Search options form
	//
	OpenCollectionAccessUI.showSearchOptions = function() {
		jQuery('#search_options').css('zIndex', '10000');
		jQuery('#search_options').slideDown(150);
	}
	
	OpenCollectionAccessUI.hideSearchOptions = function() {
		jQuery('#search_options').slideUp(150);
	}
	
	OpenCollectionAccessUI.showAdvancedSearch = function() {
		jQuery('#advanced_search').slideDown(150);
	}
	
	OpenCollectionAccessUI.hideAdvancedSearch = function() {
		jQuery('#advanced_search').slideUp(150);
	}
	
})(jQuery);