jQuery.fn.svidleoslider = function(options){
	// default options
	var pos;
	var slideFlag = true;
	var options = jQuery.extend({
			sliderWidth: jQuery(window).width(),
			itemLength: '1',
			lineWidth: jQuery(window).width(),
			mainLine: jQuery('.slideLine'),
			leftArrow: jQuery('a.left'),
			rightArrow: jQuery('a.right'),
			scroller: false,
			slideStep: '0',
			debug: false
		},options);
	
	//var difference = parseInt(options.lineWidth) - parseInt(options.sliderWidth);
	var scrollerStep = '1';
	var imageIndex = '1';
	
	if (options.itemLength > 1){
		
		options.lineWidth = parseInt(options.sliderWidth) * parseInt(options.itemLength);
		
	}
	
	options.mainLine.css('width', options.lineWidth);
	
	options.leftArrow.click(function(){
		
		change_slide('left');
		
	});
	
	options.rightArrow.click(function(){
		
		change_slide('right');
		
	});
	
	if (options.scroller){
		
		options.scroller.slider({
			min: 0,
			max: parseInt(options.itemLength) - 1,
			slide: function(event, ui){
			
				scrollerStep = (parseInt(options.lineWidth) - parseInt(options.sliderWidth)) / (parseInt(options.itemLength) - 1);
		
				if (ui.value*scrollerStep < parseInt(options.lineWidth) - parseInt(options.sliderWidth)){
					options.mainLine.css('left','-'+ui.value*scrollerStep+'px');
				}else{
				
					//console.log('Dim: ' + (parseInt(options.lineWidth) - parseInt(options.sliderWidth)));
					options.mainLine.css('left',"-" + (parseInt(options.lineWidth) - parseInt(options.sliderWidth)) + "px");
					
				}
			}
		});
		
	}
	
	function slide(side){
		
		switch(side){
			case 'right':
					
				pos = parseInt(options.mainLine.css('left')) + parseInt(options.lineWidth);
				
				if (pos > options.sliderWidth){
						
					options.mainLine.animate({ left: '-='+ options.slideStep }, 0 );
									
				}
			break;
			case 'left':
				
				pos = parseInt(options.mainLine.css('left'));
				
				if (pos < 0){					
					
					options.mainLine.animate({ left: '+='+ options.slideStep }, 0 );
					
				}
			break;
			
		}
		if (slideFlag){
			
			setTimeout(function(){slide(side);}, 0)
			
		}
			
	}
	function start(side){
		
		slideFlag = true;
		
		slide(side);
		
	}
	function stop(){
	
		slideFlag = false;
		
	}
	function change_slide (side) {
		
		//scrollerStep = 100 / parseInt(options.itemLength);
		
		imageIndex = parseInt((parseInt(options.mainLine.css('left')) / parseInt(options.sliderWidth)) * -1);
		
		if (options.debug){
			
			console.log('Position: ' +  parseInt(options.mainLine.css('left')));
			
			console.log('Position index: ' + (imageIndex + 1));
			
		}
				
		switch (side) {

		
			case 'right':
				
				pos = parseInt(options.mainLine.css('left')) + parseInt(options.lineWidth);	
				
				
				if (pos > options.sliderWidth) {
					
					
					
					options.mainLine.animate({left: "-=" + options.sliderWidth}, 500);
					
					
					options.scroller.slider("value", imageIndex + 1);

					
					//jQuery(options.scroller).find('a').css('left', (parseInt(jQuery(options.scroller).find('a').css('left')) + scrollerStep) + "%");
				}
				
			break;
			
			case 'left':
			
				pos = parseInt(options.mainLine.css('left')) + parseInt(options.lineWidth);
				
				if (pos < options.lineWidth) {
					
					options.mainLine.animate({left: "+=" + options.sliderWidth}, 500);
					
					options.scroller.slider("value", imageIndex - 1);
					
					//jQuery(options.scroller).find('a').css('left', (parseInt(jQuery(options.scroller).find('a').css('left')) - scrollerStep) + "%");
				}
			
			break;
		
		
		}
	}
	/*
	options.leftArrow.mouseup(function(){
		
		//jQuery('.text').html('Mouse Up');
		if (options.debug)
			console.log();
		stop();
		
	});
	options.leftArrow.mouseout(function(){
		
		//jQuery('.text').html('Mouse Up');
		stop();
				
	});
	options.rightArrow.mousedown(function(){
		
		//jQuery('.text').html('Mouse Down');
		start('right');
		
		
	});
	options.rightArrow.mouseup(function(){
		
		//jQuery('.text').html('Mouse Up');
		stop();
		
	});
	options.rightArrow.mouseout(function(){
		
		stop();
				
	});
	
	options.leftArrow.mousedown(function(){
		
		//jQuery('.text').html('Mouse Down');
		if (options.debug)
			console.log("Left button event is mouse down");
		start('left');
		
		
	});
	*/
}
