/********************************************************
Name: Caraselle
Author: Terence Jefferies
Business: Glow Creative
Version 1.1
Comments: This product was created by Terence Jefferies
for caraselle - style image viewing

Requires: Jquery & Jquery UI
********************************************************/

$(function() {
	
		var elem = parseInt($('#innerbox > a').length);
		var img_wid = 50;
		var wid = parseInt($('#innerbox').width());
		var direction = 0;
		var di = 0;
		var vis = 0;
		
		$('#innerbox > a').each(function() {
		
			$(this).fadeTo('slow',1.0);
		
		});
	
		function init()
		{
			$('#top_bar').hide();
			start();
		
		}
		
		function start()
		{
			
			var left = parseInt($('#innerbox').css('left'));
			if(direction == 0 && left == 0)
			{
			
				normal_start();
				
			}
		
		}
		
		function normal_start()
		{
		
			if(direction == 0)
			{
				var distance = ( img_wid * elem );
				var time = ( distance * 20 );
				di = distance;
				$('#innerbox').animate({ left:'-=' + distance + 'px' },time,'linear',function() { direction = 1; normal_start(); });
				
			}
			else if(direction == 1)
			{
			
				var distance = ( img_wid * elem );
				var time = ( distance * 20 );
				di = distance;
				$('#innerbox').animate({ left:'+=' + distance + 'px' },time,'linear',function() { direction = 0; normal_start(); });
			
			}
		
		}
		
		$('.car_img').hover(function() {
		
			$(this).fadeTo('slow',0.4);
		
		},function() {

			$(this).fadeTo('slow',1.0,function() { $(this).dequeue(); });
		
		});
		
		$('#innerbox').hover(function() {
		
			$('#innerbox').stop();
		
		},function() { 
		
			if(direction == 0)
			{
				var left = parseInt($('#innerbox').css('left'));
				var new_distance = ( di + left );
				var time = ( new_distance * 20 );
				$('#innerbox').animate({ left:"-=" + new_distance + "px" },time,'linear',function() { direction = 1; normal_start(); });
			}
			else if(direction == 1)
			{
				
				var left = convert_to_posotive(parseInt($('#innerbox').css('left')));
				var time = ( left * 20 );
				$('#innerbox').animate({ left:"+=" + left + "px" },time,'linear',function() { direction = 0; normal_start(); });
			
			}
			
		 });
		 
		 
		 function convert_to_posotive(item)
		 {
		 
		 	var tmp = ( item * 2 );
		 	var tmp2 = ( item - tmp );
		 	return tmp2;
		 
		 }
		
		function display_menu()
		{
			if(vis == 0)
			{
				$('#top_bar').show('blind',function() { vis = 1; });
			}
	
		}
		
		function hide_menu()
		{
		
			if(vis == 1)
			{
			
				$('#top_bar').hide('blind',function() { vis = 0; });
			
			}
		
		}
		
		$('#box').hover(function() {
		
			display_menu();
		
		},function() { hide_menu(); });
		
		
		$('#left').click(function(){
		
			$('#innerbox').stop();
			direction = 0;
			var left = parseInt($('#innerbox').css('left'));
			var new_distance = ( di + left );
			var time = ( new_distance * 20 );
			$('#innerbox').animate({ left:"-=" + new_distance + "px" },time,'linear',function() { direction = 1; normal_start(); });
		
		});
		
		
		$('#right').click(function(){
		
			$('#innerbox').stop();
			direction = 1;
			var left = convert_to_posotive(parseInt($('#innerbox').css('left')));
			var time = ( left * 20 );
			$('#innerbox').animate({ left:"+=" + left + "px" },time,'linear',function() { direction = 0; normal_start(); });
		
		});
		init();
	
	});