/*
 *  Imageviewer jQuery
 * 
 *	Method to perform certain actions for the imageview object.
*/

$( function()
{
	// The current image that is being displayed from the array.
	var currentImageIndex = new Number(0);
	
	// Maximum number of images
	var maximumImages = new Number(5);
	
	// Method to initialize the image viewer.
	function initImageViewer(el)
	{
		alert(el);
	}
	
	function loadImageInImageViewer(nr)
	{
		imageUrl = '';
		index = '';
		image = '';
		
		// Get access to the element.
		linkElement = $("#image_viewer_link_" + parseInt(nr));
		if(linkElement.attr('id') == null)
		{
			return;
		}
		
		linkElement.parent().find('a').removeClass('selected');
		linkElement.addClass('selected');
		linkElement.parent().parent().find('.imageviewer_image').hide();
		
		imageElement = $("#image_viewer_image_" + nr);
		imageElement.fadeIn('slow');
	}
	
	// Get the image url from the array of images.
	function getImageViewerImage(index)
	{
		currentImageIndex = index;
		return imageViewerImages[currentImageIndex];
	}
	
	// Reset the array of images.
	function resetImageViewerArray()
	{
		imageViewerImages = new Array();
		currentImageIndex = new Number(0);
	}
	
	// Store the image in the image viewer array
	function storeImageInImageViewer(nr)
	{
		imageViewerImages[nr] = nr;
	}
	
	$(document).ready( function()
	{
		$('.btn_next_image a').click(function()
		{
			// Access the next image in the image viewer.
			nextImageInImageViewer($(this).parent().parent());
		});
		
		$('.btn_prev_image a').click(function()
	    {
	    	// Access to previous image in the image viewer.
	        previousImageInImageViewer($(this).parent().parent());
	    });
	    
	});
	
	// Goto the next image in the array
	function nextImageInImageViewer(el)
	{
		var itemToDisplay = false;
		var itemToHide = false;
		
		// Get the placeholder for the image viewer.
		placeHolder = el.parent().find('#image_viewer_thumbs');
		
		// Get the total amount of images from the image viewer.
		total_images = parseInt(placeHolder.attr('total_images'));
		object_id = parseInt(placeHolder.attr('object_id'));
		
		el.parent().find('#image_viewer_thumbs a').each(function(i)
		{
			thumb = $("#image_viewer_thumb_" + object_id + "" +i);
			if(thumb.parent().attr('class') == "selected")
			{
				itemToHide = i;
			
				if(i >= total_images-1)
				{
					itemToHide = false;
					return;
				}
				else
				{
					itemToDisplay = i+1;
				}
			}
		});
		
		loadImageInImageViewer(object_id + "" + itemToDisplay)
		
		if(itemToHide > 4)
		{
			itemToHide = itemToHide-5;
			itemToHide = $("#image_viewer_thumb_" + object_id + "" +itemToHide);
			itemToHide.parent().hide("fast");
			
			itemToDisplay = $("#image_viewer_thumb_" + object_id + "" +itemToDisplay);
			itemToDisplay.parent().show("fast");
		}
		
		return false;
	}
	
	// Goto the previous image in the array.
	function previousImageInImageViewer(el)
	{
		var itemToDisplay = false;
		var itemToHide = false;
		
		// Get the placeholder for the image viewer.
		placeHolder = el.parent().find('#image_viewer_thumbs');
		
		// Get the totl amunt of images from the image viewer.
		total_images = parseInt(placeHolder.attr('total_images'));
		object_id = parseInt(placeHolder.attr('object_id'));
		
		el.parent().find('#image_viewer_thumbs a').each(function(i)
		{
			thumb = $("#image_viewer_thumb_" + object_id + "" +i);
			if(thumb.parent().attr('class') == "selected")
			{
				itemToHide = i;
			
				if(i <= 0)
				{
					itemToHide = false;
					return;
				}
				else
				{
					itemToDisplay = i-1;
				}
			}
		});
		
		loadImageInImageViewer(object_id + "" + itemToDisplay)
		
		if(itemToDisplay >= 0)
		{
			itemToHide = itemToDisplay+6;
			itemToHide = $("#image_viewer_thumb_" + object_id + "" +itemToHide);
			itemToHide.parent().hide("fast");
					
			itemToDisplay = $("#image_viewer_thumb_" + object_id + "" +itemToDisplay);
			itemToDisplay.parent().show("fast");
		}
		
		return false;
	}
});