/**
 * @author Perplex
 */

 $(window).load(function() {
	var scrollInterval;
	var galleryWidth = 0;
	var enter = false;
			
	$("#scrollLeft").bind("mouseenter", onMouseEnterLeft );
	$("#scrollLeft").bind("mouseleave", onMouseLeave );
			
	$("#scrollRight").bind("mouseenter", onMouseEnterRight );
	$("#scrollRight").bind("mouseleave", onMouseLeave );
	
	if( $('#galleryPreview').css('display') == 'none' ) {$(".caption").css( "display", "block" );}

	//$(".galleryHolder").bind("mouseenter", openPreview );
	//$(".galleryHolder").bind("mouseleave", closePreview );
	
	$('#gotoIndex').click( switchIndex );
	
	function switchIndex()
	{
		if( $('#galleryPreview').css('display') == 'none' )
		{
			$('#galleryPreview').fadeIn();
			$('.gallery').fadeOut();
			$(".caption").css( "display", "none" );
		}
		else
		{
			$('#galleryPreview').fadeOut();
			$('.gallery').fadeIn();
			$(".caption").css( "display", "block" );
		}
	}
	
	//$("#fullImageHolder").click( function() {$("#fullImageHolder").fadeOut( function() {  $("#fullImageHolder img").attr({ 'src': 'lib/img/transparent.gif' }) } ) } );
	
	$("#galleryPreview img").each( function() 
	{
		var _this;
		_this = $(this); 
		
		_this.bind("mouseenter", onMouseEnterBlack );
		_this.bind("mouseleave", onMouseLeaveBlack );
		
		
	});
	
	function onMouseEnterBlack()
	{
		$(this).attr( 'src', 'files/generatet/prevBar_' + $(this).attr( 'alt' ) + '.jpg' );
	}
	
	function onMouseLeaveBlack()
	{
		$(this).attr( 'src', 'files/generatet/prevBar_black_' + $(this).attr( 'alt' ) + '.jpg' );
	}
	
	$(".gallery img").each( function() 
	{ 
		var _this;
		
		_this = $(this);
		
		galleryWidth += _this.width() + parseFloat(_this.css('margin-right'));
		
		/*_this.click( function() 
		{	
			$("#fullImageHolder img").attr({ src: _this.attr('rel'), alt: "Test Image" });
			var margin = -$("#fullImage").width() / 2;
			
			
			
			
			if( $("#fullImageHolder").css('display') == 'none' )
			{
				$("#fullImageHolder").css( { "top": "65px", "left": "0", 'margin-left': '40px', 'margin-top': '0' } );	
				$("#fullImageHolder").fadeIn();
			} 
			
				
		})*/
	} );

	
	$(".gallery").width( galleryWidth );
	
	function openPreview()
	{
		
		$("#galleryPreview").fadeIn();
		
	}
	
	function closePreview()
	{
		$("#galleryPreview").fadeOut();
	}
	
	function onMouseEnterLeft()
	{
		if( !enter )
		{
			scrollInterval = setInterval( scrollRight, 10 );
			enter = true;
		}
	}
			
	function onMouseEnterRight()
	{
		if( !enter )
		{
			scrollInterval = setInterval( scrollLeft, 10 );
			enter = true;
		}
	}
			
	function onMouseLeave()
	{
		clearInterval( scrollInterval );
		enter = false;
	}
			
	function scrollLeft()
	{
		scroll( 'left', 5 )
	}
			
	function scrollRight()
	{
		scroll( 'right', 5 )
	}
			
	function scroll( side, speed )
	{		
		switch( side )
		{
			case 'right':
				speed = -speed;
				
				if( $("#scrollRight").css( "display" ) == "none" )
				{
						$("#scrollRight").css( "display", "block" )
				}
				
				if ( $(".gallery").position().left < 0 ) {
					$(".gallery").css( "left", ( $(".gallery").position().left - speed ).toString() + 'px' );
				}
				else
				{
					$("#scrollLeft").css( "display", "none" )
				}
			break;
				
			case 'left':
				
				if( $("#scrollLeft").css( "display" ) == "none" )
				{
						$("#scrollLeft").css( "display", "block" )
				}
			
				if ( $(".gallery").position().left > -$(".gallery").width() + $(".galleryHolder").width() ) {					
					$(".gallery").css( "left", ( $(".gallery").position().left - speed ).toString() + 'px' );
				}
				else
				{
					$("#scrollRight").css( "display", "none" )
				}
				
				// $(".gallery").animate({ left: $(".gallery").position().left - speed }, 0);
						
			break;
		}
	}
});
