/**
 * This program activates automtically all scrollable galleries inside the HTML
 * content. A scrollable gallery is a HTML structure where you can present a
 * (horizontally) scrollable list of item (i.e. images).
 * A gallery's HTML structure is recognized by special CSS class names
 * (i.e. 'scrollGallery', see the following example for details).
 * Needs jQuery to work.
 *
 * The HTML structure looks for example like this (the used CSS class names
 * are mandatory, so you also have to use them in a similar way):
 *
 * <div class="scrollGallery">
 *   <img class="scrollGallery_movePrevious" src="scrollLeft.gif" alt="Previous"
 *      style="float: left" />
 *   <div style="overflow: hidden; width: 270px; position: relative; float: left">
 *     <div style="width: 100000px; position: relative"
 *         class="scrollGallery_content">
 *       <img class="scrollGallery_item" ... alt="item1" />
 *       <img class="scrollGallery_item" ... alt="item2" />
 *       <img class="scrollGallery_item" ... alt="item3" />
 *    </div>
 *    </div>
 *    <img class="scrollGallery_moveNext" src="scrollRight.gif" alt="Previous"
 *        style="float: right" />
 *    <div style="clear: both"></div>
 *  </div>
 *
 * @author		Ralf Obmann <ralf.obmann@dmc.de>
 */

 
jQuery(function() {
	var $ = jQuery;


	// Find and activate all scrolling galleries that have not yet
	// been activated
	$('.scrollGallery:not(.scrollGallery_activated)').each(function() {
		// The jQuery object of the scrollable gallery
		// (-> class 'scrollGallery')
		var galleryObj = $(this);

		// The jQuery object of the 'move previous' button
		// (-> class 'scrollGallery_movePrevious')
		var movePrevObj = galleryObj.find('.scrollGallery_movePrevious:first')

		// The jQuery object of the 'move next' button
		// (-> class 'scrollGallery_moveNext')
		var moveNextObj = galleryObj.find('.scrollGallery_moveNext:first');

		// The jQuery object of the content area of the gallery
		// (-> class 'scrollGallery_content')
		var contentObj = galleryObj.find('.scrollGallery_content:first');

		// The full numbertext
		var numberObj = galleryObj.find('.scrollGallery_Number:first');

		// The currentPage number
		var pageObj = galleryObj.find('.scrollGallery_Page:first');

		// The amount of Elements 
		var amountObj = galleryObj.find('.scrollGallery_Amount:first');

        // define the direction of scroll logic
        var scrollDirection = 'horizontal';

        // define the items to scroll
        var itemsToScroll = 1;

		// The heigth (in pixels) of the parent HTML container of
		// gallery's content area.
		var contentParentHeight = (contentObj.size() == 0)
			? null : contentObj.get(0).parentNode.offsetHeight;

		// The Width (in pixels) of the parent HTML container of
		// gallery's content area.
		var contentParentWidth = (contentObj.size() == 0)
			? null : contentObj.get(0).parentNode.offsetWidth;

		// jQuery objects of the gallery's scrolling items
		// (-> class 'scrollGallery_items')
		var items = contentObj.find('.scrollGallery_item');

		// The number of the items of the gallery.
		var itemCount = items.size();
		
		// additionally set value for global var
		numberOfGalleryItems = itemCount;

		// The HTML element of the last item of the gallery (will be set later).
		var lastItemElem = null;

		// The index of the currently first visible gallery item.
		var scrollIdx = 0;

		// The current vertical scroll offset of the gallery (<= 0)
		var offsetTop = 0;

		// The current horizontal scroll offset of the gallery (<= 0)
		var offsetLeft = 0;

		// Check whether the gallery has any items to show.
		// If yes, then activate the gallery by registering events etc.
		if (itemCount > 0) {
			lastItemElem = items.get(itemCount - 1);
			registerScrollerClickEvent(movePrevObj, -1);
			registerScrollerClickEvent(moveNextObj, 1);
			setTimeout(refreshScrollers, 100);
			galleryObj.addClass('scrollGallery_activated');
		} // end: if

		refreshScrollers();

		/**
		 * Activates the scrollers onClick event.
		 *
		 * @param	obeject		jQuery object of scroll button
		 * @param	integer		steps to scroll (-1 or 1)
		 * @return	void
		 * @access	private
		 */
		function registerScrollerClickEvent(obj, step) {
			obj.click(function() {
                
                horizontalCheck = lastItemElem.offsetLeft + lastItemElem.offsetWidth + offsetLeft > contentParentWidth;
                verticalCheck = lastItemElem.offsetTop + lastItemElem.offsetHeight + offsetTop > contentParentHeight;
                
                if(scrollDirection == "vertical") {
    				if (step < 0 && scrollIdx > 0 ||
    						(scrollIdx < itemCount - 1
    						&& verticalCheck)) {
    					scrollIdx = Math.max(0, Math.min(itemCount - 1,
    							scrollIdx + step));
    					offsetTop = -items.get(scrollIdx).offsetTop * itemsToScroll;
    					contentObj.animate({top: offsetTop}, 'slow');
    					refreshScrollers();
    					return false;
    				} // end: if
                }
                if(scrollDirection == "horizontal") {
    				if (step < 0 && scrollIdx > 0 ||
    						(scrollIdx < itemCount - 1
    						&& horizontalCheck)) {
    					scrollIdx = Math.max(0, Math.min(itemCount - 1,
    							scrollIdx + step));
    					offsetLeft = -items.get(scrollIdx).offsetLeft * itemsToScroll;
    					contentObj.animate({left: offsetLeft}, 'medium');
    					refreshScrollers();
    					return false;
    				} // end: if
                }
                
			});
		} // end: function registerScrollerClickEvent

		/**
		 * Refreshes the appearance an the activation status of both
		 * scroller buttons.
		 *
		 * @return	void
		 * @access	private
		 */
		function refreshScrollers() {
			var opacityLeft = 1;
			var cursorLeft = 'pointer';
			var opacityRight = 1;
			var cursorRight = 'pointer';
			var displayLeft = 'block';
			var displayRight = 'block';

			// Check/modify values for button 'move previous'
			if (itemCount == 0 || scrollIdx < 1) {
				opacityLeft = 0;
				cursorLeft = 'auto';
			} // end: if

            horizontalCheck = lastItemElem.offsetLeft + lastItemElem.offsetWidth + offsetLeft <= contentParentWidth;
            verticalCheck = lastItemElem.offsetTop + lastItemElem.offsetHeight + offsetTop <= contentParentHeight;
            check = 0;
            if(scrollDirection == "horizontal") {
                check = horizontalCheck;
            }
            if(scrollDirection == "vertical") {
                check = verticalCheck;
            }

			// Check/modify values for button 'move next'
			if (itemCount == 0 || check || itemCount == (scrollIdx +1)) {
				opacityRight = 0;
				cursorRight = 'auto';
			} // end: if

			// Check/modify values for number fields
			if (itemCount <= itemsToScroll) {
			     numberObj.hide();
			} // end: if

			// Set the style values for 'opacity' and 'cursor'
			movePrevObj.css('opacity', opacityLeft);
			movePrevObj.css('cursor', cursorLeft);
			moveNextObj.css('opacity', opacityRight);
			moveNextObj.css('cursor', cursorRight);
			
			pageObj.html(scrollIdx+1);
			amountObj.html(itemCount);
			
		} // end: function refresh
	});
});
