/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


(function($) {
 
 $.fn.backanforthSlider = function(options) {
	 		
	var defaults = {			
	 container: '.container',
	 slidespeed: 15, // Speed of slide animation
	 fast:5,
	 slow:10,
	 direction:'right',
	 autoHeight: false // Set to positive number for auto height and animation speed
	};


	this.each(function() {
	 var o = $.extend(defaults, options);
	 var container=$(o.container,$(this));
	 var direction=o.direction;	

	 $(this).append('<div class="partitions"></div>');
	 $('.partitions').append('<div class="transparent partition_left_fast"></div>')
	 $('.partitions').append('<div class="transparent partition_left_slow"></div>')
	 $('.partitions').append('<div class="transparent partition_hold"></div>')
	 $('.partitions').append('<div class="transparent partition_right_slow"></div>')
	 $('.partitions').append('<div class="transparent partition_right_fast"></div>')
	 
	 $('.partition_left_fast').mouseenter(function(){
		Stop();
		SlideLeft(o.fast,function(){
		 Stop();
		})
	 }).mouseleave(function(){
		Stop();
		SlideLeft(o.slidespeed);
	 });
	 
	 $('.partition_left_slow').mouseenter(function(){
		Stop();
		SlideLeft(o.slow,function(){
		 Stop();
		})
	 }).mouseleave(function(){
		Stop();
		SlideLeft(o.slidespeed);
	 });
	 
	 $('.partition_hold').mouseenter(function(){
		Stop();
	 }).mouseleave(function(){
		if(direction=='left')
		 SlideLeft(o.slidespeed);
		else
		 SlideRight(o.slidespeed);
	 });
	 
	 $('.partition_right_slow').mouseenter(function(){
		Stop();
		SlideRight(o.slow,function(){
		 Stop();
		})
	 }).mouseleave(function(){
		Stop();
		SlideRight(o.slidespeed);
	 });

	 $('.partition_right_fast').mouseenter(function(){
		Stop();
		SlideRight(o.fast,function(){
		 Stop();
		})
	 }).mouseleave(function(){
		Stop();
		SlideRight(o.slidespeed);
	 });
	 
	 
	 
	 function SlideRight(speed,next) {
		direction='right';
//		alert('right: '+' scroll width: '+container[0].scrollWidth+' scrollLeft: '+container.scrollLeft()+' Doing '+(container[0].scrollWidth-container.width()-container.scrollLeft())+'px in '+(container[0].scrollWidth-container.scrollLeft())*speed+' milisec');
		if(next==undefined)
		{
		 next=function(){
			SlideLeft(speed);
		 }
		}
		container.animate({
		 scrollLeft:container[0].scrollWidth-container.width()
		},(container[0].scrollWidth-container.scrollLeft())*speed,next);
	
	 };
	
	 function SlideLeft(speed,next) {
//		alert('left: '+' scroll width: '+container[0].scrollWidth+' scrollLeft: '+container.scrollLeft()+' Doing '+(container.scrollLeft())+'px in '+(container.scrollLeft())*speed+' milisec');
		direction='left';
		if(next==undefined)
		{
		 next=function(){
			SlideRight(speed);
		 }
		}
		container.animate({
		 scrollLeft:0
		},(container.scrollLeft())*speed,next);
	 };

	 function Stop() {
//	 alert("Stop!");
		container.stop(true);
	 }
	 SlideRight(o.slidespeed);
	});
	
 };
 	
})(jQuery);
