/*
 * Scroller plugin
 * (c)Eftimie Andrei 2008
 * k3liutZu@gmail.com
 * 
 * You can call it like
 * $('#scroll').listScroll(speed, handlerPrevious, handlerNext);
 * 
 * It should be chainable
 * 
 * It expects html the like
 *	<div id="scroll">
 * 		<ul>
 * 			<li>...</li>
 * 			...
 * 		</ul>
 * 	</div>
 * 
 * Layout should be addressed by CSS
 * list elements should float
 */
jQuery.fn.listScroll = function(settings){
    settings = jQuery.extend({
        speed: 10,
		handlePrev: null,		
		handleNext: null
    }, settings);
	
    return this.each(function(){
		
		var next = $(settings.handleNext);
		var prev = $(settings.handlePrev);
		var realSpeed = settings.speed*1000; //realspeed is in miliseconds
		
		var listWrapper = $(this);
		var animatedObject = listWrapper.find('ul');
		var listLength = 0;
		$('li',listWrapper).each(function(i){
			listLength += $(this).width()+20;
		});
		
		animatedObject.css({ 'position':'relative', 'z-index':100, 'width':listLength });
		
		if(listLength>listWrapper.width()) var scrollDistance = eval(listLength - listWrapper.width());
		else{ var scrollDistance=0; }
		
		
		//Setting the events on the handlers
		prev.hover(
			function(){
				turn(next,'on');
				animatedObject.animate({
					left: -scrollDistance+'px'
				},realSpeed, function(){
					turn(prev,'off');
				});
			},
			function(){
				animatedObject.stop();
			}
		);
		
		next.hover(
			function(){
				turn(prev,'on');
				animatedObject.animate({					
					left: '0px'
				},realSpeed, function(){
					turn(next, 'off');
				});
			},
			function(){
				animatedObject.stop();
			}
		);
		
		//For setting a on/off state for the handlers
		function turn(obj,state){
			if (state == 'on')
				obj.removeClass('off')
			else
				obj.addClass('off')
		}		
    });
};

$(function(){
	$('.garsoniere').listScroll({
		speed: 1,
		handleNext: '.garsoniere .prev',
		handlePrev: '.garsoniere .next'
	});	
	$('.camere2').listScroll({
		speed: 1,
		handleNext: '.camer2 .prev',
		handlePrev: '.camere2 .next'
	});	
	$('.camere3').listScroll({
		speed: 1,
		handleNext: '.camere3 .prev',
		handlePrev: '.camere3 .next'
	});	
})

