/* ----------------------------------------------------- */
/*	Copyright (c) THREENET GbR (http://www.threenet.de)	 */
/* ----------------------------------------------------- */

/**
 * Javascript Gallery
 */
function Gallery()
{
	/**
	 * Instance of Gallery class
	 * @var
	 */
	var self = this;
	
	
	/**
	 * Width of all gallery elements summed up
	 * @var
	 */
	var galleryWidth = 0;
	
	
	/**
	 * Width of visible area
	 * @var
	 */
	var visibleWidth = 465;
	
	
	/**
	 * Mouse button currently pressed
	 * @var
	 */
	var mouseDown = false;
	
	
	/**
	 * Duration of move animation
	 * @var
	 */
	var moveDuration = 1;
	
	
	/**
	 * Movement in pixels
	 * @var
	 */
	var movePixels = 5;
	
	
	/**
	 * Initialize gallery
	 */
	this.init = function()
	{
		$(window).load
		(
			function()
			{
				galleryWidth = $("#gallery #images #wrapper").width();
				
				// Kommentar zum zurück-button.... hierin
				$("#gallery #prev").css("display", "none");
				
				$("#gallery #next").mousedown
	        	(
	                function()
	                {
	                	mouseDown = true;
	                	self.forwards();
	                }
	            );
				
				$("#gallery #prev").mousedown
	        	(
	                function()
	                {
	                	mouseDown = true;
	                	self.backwards();
	                }
	            );
				
				$(document).mouseup
	        	(
	                function()
	                {
	                	mouseDown = false;
	                }
	            );
			}
		);
	}
	
	
	/**
	 * Move gallery elements left
	 */
	this.forwards = function()
	{
		moveForward  = parseInt($("#gallery #images").css("left").replace(/px/, '')) + parseInt(galleryWidth) > visibleWidth;
		moveBackward = $("#gallery #images").css("left").replace(/px/, '') < 0;
		
    	if (mouseDown && moveForward)
		{
    		$("#gallery #images").animate({"left": "-="+movePixels+"px"}, moveDuration, "linear", self.forwards);
		}
    	
    	// Disable forwards button
    	if ( ! moveForward)
    	{
    		$("#gallery #next").css("display", "none");
    	}
    	
    	// Enable backwards button
    	if (moveBackward)
    	{
    		$("#gallery #prev").css("display", "block");
    	}
    }

	
	/**
	 * Move gallery elements right
	 */
	this.backwards = function()
	{
		moveForward = parseInt($("#gallery #images").css("left").replace(/px/, '')) + parseInt(galleryWidth) > visibleWidth;
		moveBackward = $("#gallery #images").css("left").replace(/px/, '') < 0;
		
    	if (mouseDown && $("#gallery #images").css("left").replace(/px/, '') < 0)
		{
    		$("#gallery #images").animate({"left": "+="+movePixels+"px"}, moveDuration, "linear", self.backwards);
		}
    	
    	// Enable forwards button
    	if (moveForward)
    	{
    		$("#gallery #next").css("display", "block");
    	}
    	
    	// Disable backwards button
    	if ( ! moveBackward)
    	{
    		$("#gallery #prev").css("display", "none");
    	}
    }
}

var gallery = new Gallery();
gallery.init();
