/**
 * Gives groups of images gallery functionality.
 *
 * @author	Ben Rogalski
 */

var galleryMgr = new SimpleGalleryMgr();


/**
 * Searches the document for elements with the class name 'gallery'
 * and creates Gallery objects for each one of them.
 */
function SimpleGalleryMgr()
{
	var galleryObjects = new Array();
  
	
	$(window).load(loadHandler);
	
  
	function loadHandler()
	{
        var
            gallerySections,
            forLimit,
            i
        ;
        
		gallerySections = $('.gallery');
		forLimit = gallerySections.length;
		for(i = 0; i < forLimit; ++i)
		{
			galleryObjects.push(new SimpleGallery(gallerySections[i]));
		}
	}
}


/**
 * The actual object that gives gallery functionality to the HTML element.
 *
 * @param container The HTML container with the class name 'gallery'.
 */
function SimpleGallery(container)
{
	var 
        images,
        currIndex = 0,
        numImages
    ;
	
  
    init();

	
    /**
     * Prepare the gallery.
     */
	function init()
	{
        var prevLink,
            nextLink;
        
        // Set the zIndexes of the images.
		images = $(container).find('img');
        numImages = images.length;
		for(i = 0; i < numImages; ++i)
		{
			$(images[i]).css('zIndex', numImages - i);
		}
		
        // Set the handlers for the previous and next buttons.
        prevLink = $(container).find('.prev');
        nextLink = $(container).find('.next');
        $(prevLink).show();
        $(nextLink).show();
        $(prevLink).click(prevHandler);
        $(nextLink).click(nextHandler);
	}
	
	
    /**
     * Show the previous image.
     */
	function prevHandler()
	{
        $(images[currIndex]).fadeOut('slow');
        
        --currIndex;
        
		if(currIndex < 0)
		{
			currIndex = numImages - 1;
		}
		
		swapImage(currIndex);
        
        return false;
	}
    
    
    /**
     * Show the next image.
     */
	function nextHandler()
	{
        $(images[currIndex]).fadeOut('slow');
        
        ++currIndex;
        
		if(currIndex > numImages - 1)
		{
			currIndex = 0
		}
		
		swapImage(currIndex);
        
        return false;
	}
  
	
  /**
   * Replaces the current image with the image at the index specified.
   *
   * @param reqIndex The index of the image that should now be shown.
   */
	function swapImage(reqIndex)
	{	
		var
            // The zIndex of the requested image.
            reqImgZIndex,
            // Used to store zIndexes of images that were 
            // not requested, one at a time.
            nonReqImgZIndex
        ;
		
        reqImgZIndex = parseInt($(images[reqIndex]).css('zIndex'));
		
        // The image is not on top of the stack.
		if(reqImgZIndex < numImages)
		{
			if($(images[reqIndex]).is(':visible'))
			{
				$(images[reqIndex]).hide();
			}
			
            // Move the image to the top of the stack and fade it in.
            $(images[reqIndex]).css('zIndex', numImages);
			$(images[reqIndex]).fadeIn('slow');
			
            // Move all images above displacedIndex down by 1 level
            // to close up the gap.
			for(j = 0; j < numImages; j++)
			{
				if( j !== reqIndex)
				{
					nonReqImgZIndex = parseInt($(images[j]).css('zIndex'));
					if(nonReqImgZIndex > reqImgZIndex)
					{
						$(images[j]).css('zIndex', nonReqImgZIndex - 1);
					}
				}
			}
		}		
		
		return false;
	}
	
	return true;
}
