$(function ($) {
	$.fn.imagescroller = function (options) {

		function scroll_imagescroller(img_div, imagewidth, options, fast) {
			curpos = Math.abs(parseInt($(img_div).css('margin-left')));

			if (options.scroll_direction == 'ltr'){
				var tmp_scroll_speed = fast == 'fast'?((options.scroll_speed_fast / imagewidth) * curpos):((options.scroll_speed / imagewidth) * curpos);
				$(img_div).animate({marginLeft: 0},  tmp_scroll_speed, 'linear', function (){if (options.scroll_continuous){$(img_div).css('margin-left', '-'+(parseInt(parseInt(imagewidth))+'px'));scroll_imagescroller(img_div, imagewidth, options, fast);}});
			}else{	
				var rightlimit = options.scroll_continuous?imagewidth:imagewidth-options.scroll_visible;
				var tmp_scroll_speed = fast == 'fast'?((options.scroll_speed_fast / rightlimit) * (rightlimit - curpos)):((options.scroll_speed / rightlimit) * (rightlimit - curpos));				
				$(img_div).animate({marginLeft: -rightlimit},tmp_scroll_speed,'linear',function (){if (options.scroll_continuous){$(img_div).css('margin-left', 0);scroll_imagescroller(img_div, imagewidth, options, fast);}});
			}
		}

		this.each(function (){ 

			options.scroll_speed_fast = parseInt(options.scroll_speed) / parseInt(3);
			
			var imageheight = $(this).attr('height');
			var imagewidth = $(this).attr('width');

			var current_margin, prevxDiff,mouseDown, clickX, imagescrollerwrap, imagescrollerinner;
			var div_imagescrollerwrap = "<div id='imagescrollerwrap' unselectable='on'></div>";
			var div_imagescrollerinner = "<div id='imagescrollerinner' unselectable='on'></div>";
			var div_imagescrollerbuttons = "<div id='imagescrollerbuttons' unselectable='on'><span unselectable='on' id='move_ltr_fast'></span> <span unselectable='on' id='move_ltr'></span> <span unselectable='on' id='move_stop'></span> <span unselectable='on' id='move_rtl'></span> <span unselectable='on' id='move_rtl_fast'></span></div>"
			
			$(this).wrap(div_imagescrollerinner);
			
			if (options.scroll_continuous)$(this).clone().removeAttr('id').insertAfter(this);

			imagescrollerinner = $(this).parent();
			imagescrollerinner.css('height', imageheight+'px').css('width', (parseInt(imagewidth)*3)+'px').wrap(div_imagescrollerwrap).parent().css('width',options.scroll_visible+'px');

			imagescrollerwrap = imagescrollerinner.parent();
			imagescrollerwrap.bind('contextmenu',function (){return false;}).css('height', (parseInt(imageheight)+parseInt(50))+'px').append(div_imagescrollerbuttons);

			$('#move_ltr').live('click', function () {options.scroll_direction = 'ltr';$(imagescrollerinner).stop();scroll_imagescroller(imagescrollerinner, imagewidth, options);});
			$('#move_ltr_fast').live('click', function () {options.scroll_direction = 'ltr';$(imagescrollerinner).stop();scroll_imagescroller(imagescrollerinner, imagewidth, options, 'fast');});
			$('#move_rtl').live('click', function () {options.scroll_direction = 'rtl';$(imagescrollerinner).stop();scroll_imagescroller(imagescrollerinner, imagewidth, options);});
			$('#move_rtl_fast').live('click', function () {options.scroll_direction = 'rtl';$(imagescrollerinner).stop();scroll_imagescroller(imagescrollerinner, imagewidth, options, 'fast');});
			$('#move_stop').live('click', function () {$(imagescrollerinner).stop();});

			if (options.scroll_controls === 'show'){
				$('#imagescrollerbuttons').show();
			}else if (options.scroll_controls == 'hover'){
				imagescrollerwrap.bind('mouseover', function (e){
					e.preventDefault();
					$('#imagescrollerbuttons').show();
				}).bind('mouseout', function (e){
					e.preventDefault();
					$('#imagescrollerbuttons').hide();
				});				
			}else{
				$('#imagescrollerbuttons').hide();
			}

			$(this).parent().css('margin-left', '-'+options.scroll_initial+'px');
			if (options.scroll_start)scroll_imagescroller(imagescrollerinner, imagewidth, options, 'fast');
		});
		
	};
});
