(function($) {

$.bgcar = {
	active : []
};
$.fn.bgcar = function(settings, moves)
{
	settings 	= $.extend({}, $.fn.bgcar.defaults, settings);
	moves	= $.extend({}, $.fn.bgcar.movements, moves);
	animating = 'false';
	
	
	
	
	var rf = function() { return false; };
	
	return this.each(
		function()
		{
			
			
			// Fire init
			init($(this));
			


			function move(slider, direction)
			{
			
			
				if (direction == 'forward')
				{			
					window.setTimeout(function(){
					    go_forward(slider);
					}, 2000);	
					
				} else if (direction == 'backwards') {
					window.setTimeout(function(){
					    go_backward(slider);
					}, 2000);	
				}		
				
				
				
			}




			function go_forward(slider)
			{
			
				if (animating == 'false')
				{
				
					animating = 'true';
					
					// Fade Out Front image
					$('.sub_1.left').fadeOut('slow');
					
					// Assign to moves
					do_animation($('.sub_2.left'), moves.slide1, 'forward');		
					do_animation($('.active'), moves.slide2, 'forward');				
					do_animation($('.sub_2.right'), moves.slide3, 'forward');				
					do_animation($('.sub_1.right'), moves.slide4, 'forward');	
					
					// Show first qued item
					$('.qued').first().fadeIn(100, function(){
					
						// First, take the first li and move to the end of the line
						var shuffle = $('.sub_1.left').html();
						
						slider.children('li').first().remove();
						slider.append('<li class="qued">' + shuffle + '</li>');
						
						window.setTimeout(function(){
						
							// Reassign class names
							assign(slider);		
							slider.children('li').removeAttr('style');	
							slider.children('li').find('img').removeAttr('style');	
							slider.children('li').find('h2').removeAttr('style');	
							animating = 'false';
						}, 3000);
					
						
					
						window.setTimeout(function(){
							go_forward(slider);	
						}, 8000);
					});	
				}		
						
			}


			function go_backward(slider)
			{
				
				
				if (animating == 'false')
				{
				
					animating = 'true';
					
					// Fade Out back image
					$('.sub_1.right').fadeOut('slow');
					
					// Grab last li and append to front quickly!
					var shuffle = slider.children('li').last().html();
					
					slider.children('li').last().remove();
					slider.prepend('<li class="qued" style="left: 0">' + shuffle + '</li>');
					
					
					slider.children('li').first().fadeIn(1000);
					
					
					// Assign to moves
					do_animation($('.sub_1.left'), moves.slide2, 'back');						
					do_animation($('.sub_2.left'), moves.slide3, 'back');				
					do_animation($('.active'), moves.slide4, 'back');				
					do_animation($('.sub_2.right'), moves.slide5, 'back');	
					
					
					
					
						window.setTimeout(function(){
						
							// Reassign class names
							assign(slider);		
							slider.children('li').removeAttr('style');	
							slider.children('li').find('img').removeAttr('style');	
							slider.children('li').find('h2').removeAttr('style');	
							animating = 'false';
						}, 3000);
			
				
				}
				
			}


			function do_animation(el, params, direction)
			{
			
			
				// Container
				el.stop().animate({ 
				    marginTop			: params.container.marginTop, 
				    width				: params.container.width, 
				    zIndex				: params.container.zIndex, 
				    left				: params.container.left 
				}, 2000, do_zindex(el.width(), direction));
				
				
				// Img
				el.find('img').stop().animate({
				    height				: params.img.height,
				    width				: params.img.width
				}, 2000)
				
				
				// H2
				el.find('h2').stop().animate({
				    bottom				: params.h2.bottom,
				    width				: params.h2.width,
				    fontSize			: params.h2.fontSize,
				    opacity				: params.h2.opacity
				}, 2000);
			
			}
			
			
			
			function do_zindex(width, direction)
			{



				window.setTimeout(function(){

				    if (direction == 'forward')
				    {
				    	var target = '.active';
				    	var second = '.sub_2.right';
				    	
				    	
				    	$(target).css('zIndex', 1002);
				    	$(second).css('zIndex', 1003);
				    	
				    	// Show overlay
				    	$(target).find('img.overlay').fadeIn(500);
				    	$(second).find('img.overlay').fadeOut(500);
				    	
				    
				    } else if (direction == 'back') {
				    
				    	var target = '.sub_2.left';
				    	var second = '.active';
				    	
				    	$(target).css('zIndex', 1003);
				    	$(second).css('zIndex', 1002);

				    	var target2 = '.sub_2.right';
				    	
				    	$(target2).css('zIndex', 1001);
				    	
				    	// Show overlay
				    	$(target).find('img.overlay').fadeIn(500);
				    	$(second).find('img.overlay').fadeOut(500);
				    	
				    	
				    }
				
				}, 200);
					
					
			
			}




			function buttons(slider)
			{
				$('a.trigger').click(function(){
				
							
					slider.stop();
				
					if (animating == 'false')
					{
						
					
					
						// Get the buttons direction
						var direction = $(this).attr('rel');
						
						// Trigger movement
						if (direction == 'forward')
						{
							go_forward(slider);
						} else if (direction == 'backward') {
							go_backward(slider);
						
						}
					}
				
				});
			
			
			
			
			}

			
			
			
			
			
			
			function init(slider)
			{
				var slides = $(this).children('li').size();
				
				
				// Loop through all slides and add the id
				slider.children('li').each(function(i)
				{
					$(this).attr('id', 'slide_' + i);	
				});
			
				// Assign class names
				assign(slider);
				
				
				// Fire the auto sliding!!!
				move(slider, 'forward');
				
				buttons(slider);
			
			}
			
			
			




			function assign(slider)
			{
				// Assign initial classes
				var i = 1;
				slider.children('li').each(function()
				{
					
					$(this).removeClass();
					
					switch(i)
					{
						case i = 1 : 
							$(this).addClass('sub_1');
							$(this).addClass('left');
							break;
						case i = 2 :
							$(this).addClass('sub_2');
							$(this).addClass('left');
							break;
						case i = 3 :
							$(this).addClass('active');
							break;
						case i = 4 :
							$(this).addClass('sub_2');
							$(this).addClass('right');
							break;
						case i = 5 :
							$(this).addClass('sub_1');
							$(this).addClass('right');							
							break;
						default :
							$(this).addClass('qued');
					
					}
					
					i++;
				});								
				
			}			
								
			
			
			
			
		}
	);		
};




$.fn.bgcar.movements = {

	// Slide 1 doesn't have any specific place to go. it's fading out then removed then appended to the end of the line.
	
	
	slide1: {
		container: {
    		marginTop	: 100,
    		width		: 376,
    		zIndex		: 1001,
    		left		: 0
		},
		
		img: {
   			height		: 268,
    		width		: 376
		},
		
		h2: {
			bottom		: 50,
    		width		: 376,			
			fontSize	: 14,
			opacity		: .5
		},
		
		
		overlay: {
			opacity		: 1
			
		
		
		}
	},
	
	
	
	slide2: {
		container: {
    		marginTop	: 70,
    		width		: 485,
    		zIndex		: 1002,
    		left		: 130
		},
		
		img: {
   			height		: 345,
    		width		: 485	
		},
		
		h2: {
			bottom		: 15,
    		width		: 485,			
			fontSize	: 22,
			opacity		: .5
			
		},
		
		
		overlay: {
			opacity		: 1
			
		
		
		}
	},
	
	
	slide3: {
		container: {
    		marginTop	: 0,
    		width		: 777,
    		zIndex		: 1003,
    		left		: 275
		},
		
		img: {
   			height		: 553,
    		width		: 777	
		},
		
		h2: {
			bottom		: 50,
    		width		: 777,			
			fontSize	: 27,
			opacity		: 1
			
		},
		
		
		overlay: {
			opacity		: 0
		}
	},
	
	
	
	
	
	
	slide4: {
		container: {
    		marginTop	: 70,
    		width		: 485,
    		zIndex		: 1002,
    		left		: 710
		},
		
		img: {
   			height		: 345,
    		width		: 485	
		},
		
		h2: {
			bottom		: 15,
    		width		: 485,			
			fontSize	: 22,
			opacity		: .5
			
		},
		
		
		overlay: {
			opacity		: 1
			
		
		
		}
	},
		



	slide5: {
		container: {
    		marginTop	: 100,
    		width		: 376,
    		zIndex		: 1001,
    		left		: 968
		},
		
		img: {
   			height		: 268,
    		width		: 376	
		},
		
		h2: {
			bottom		: 10,
    		width		: 376,			
			fontSize	: 20,
			opacity		: .5
		},
		
		
		overlay: {
			opacity		: 1
		}
	},
		




		
	
	
	/// New slide to join the family	
	slide6: {
		container : {
			left: 710
		}
		
	}
	
	
	
	
	
	
	
	
	
};

})(jQuery);
