;(function($){
        $.fn.moescroller_smooth = function(settings){

	var btn_next	= null,
	btn_pause	= null,
	btn_previous	= null,
	el_container	= null,
	el_debug	= null,
	el_items	= null,
	el_ul		= null,
	paused		= false,
	paused2		= false,
	total_width	= 0,
	total_height	= 0,
	stops		= new Array(),
	userstops	= new Array(),
	userstoptimes	= new Array(),
	laststop	= null,

	settings = jQuery.extend({
		restart_beginning:	true,
		btn_previous:		null,
		btn_next:		null,
		btn_pause:		null,
		container_height:	200,
		container_width:	500,
		heightunit:		'px',
		widthunit:		'px',
		debug:			false,
		direction:		1,
		el_ul:			null,
		li_fixedwidth:		true,
		li_fixedheight:		true,
		play_phrase:		null,
		pause_phrase:		null,
		pauseable:		false,
		pausetime:		2000,
		shownav:		false,
		scrollaxis:		'x',
		scroll_jump:		2,
		scroll_speed:		60,
		start_offscreen:	true,
		transition:		'Fx.Transitions.Bounce.easeOut',
		wheelstop:		true,
		classsfx:		'',
		usestops:		false
	}, settings);

	btn_next	= $('#'+settings.btn_next);
	btn_pause	= $('#'+settings.btn_pause);
	btn_previous	= $('#'+settings.btn_previous);
	el_container	= $(this);
	el_ul		= $('#'+settings.el_ul);
	el_items	= $('#'+settings.el_ul+' li.mditem'+settings.classsfx);
	el_stops	= $('#'+settings.el_ul+' div.mdstop');

	if (settings.debug) {
		el_debug = $(document.createElement('div'));
		var css_settings = {
				'background': '#fff',
				'border': '1px solid pink',
				'display': 'block',
				'clear': 'both',
				'position': 'fixed',
				'top': '10px',
				'right': '10px',
				'width': '200px',
				'height': '500px',
				'z-index': '100',
				'opacity': '1'
			}
		$(el_debug).css(css_settings);
		$(el_debug).insertBefore($(el_container));
	}

	$(el_container).css('height', settings.container_height+settings.heightunit);
	$(el_container).css('width', settings.container_width+settings.widthunit);

	var container_width     = $(el_container).width();
	var container_height    = $(el_container).height();

	if (settings.scroll_speed < 10) settings.scroll_speed = 10;

	if (($(btn_pause).length != 0) && ($(btn_pause) != null)) {
		$(btn_pause).bind('click', (function() { pauseScroller2(); }));
	}

	if (($(btn_next).length != 0) && ($(btn_next) != null)) {
		$(btn_next).bind('click', function(event) { paused2 = false; paused = false; goRight(); });
	}

	if (($(btn_previous).length != 0) && ($(btn_previous) != null)) {
		$(btn_previous).bind('click', function(event) { paused2 = false; paused = false; goLeft(); });
	}

//	stops[stops.length] = 0;

	$(el_stops).each( function(h) {

		if (settings.scrollaxis == 'y') {
			userstops[userstops.length] = $($(el_stops)[h]).position().top;
		} else {
			userstops[userstops.length] = $($(el_stops)[h]).position().left;
		}

		userstoptimes[userstoptimes.length] = $($(el_stops)[h]).attr('rel');

	});

	$(el_items).each( function(i) {

		if (settings.li_fixedheight) $(el_items[i]).css('height', parseFloat(container_height)+'px');
		else $(el_items[i]).css('height', 'auto');
		if (settings.li_fixedwidth)  $(el_items[i]).css('width', parseFloat(container_width)+'px');
		else $(el_items[i]).css('width', 'auto');
		if ((settings.li_fixedheight) && (settings.li_fixedwidth)) $(el_items[i]).css('overflow', 'hidden');

		$(el_items[i]).text(el_items[i].text);

		total_height += $(el_items[i]).height();
		total_width  += $(el_items[i]).width();

/*
		if (settings.usestops) {
				if (settings.scrollaxis == 'y') {
					if (!jQuery.inArray(-total_height, stops))
						if (i < $(el_items).length-1) stops[stops.length] = -total_height;
				} else {
					if (!jQuery.inArray(-total_width, stops))
						if (i < $(el_items).length-1) stops[stops.length] = -total_width;
				}
		}
*/

		if (settings.pauseable) {
			$(el_items[i]).bind('mouseover', 
				(function(event) {
					if (!paused2) stopScroller();
				})
			);
			$(el_items[i]).bind('mouseout', (function(event) { if (!paused2) startScroller(); }));
		}

	});

	total_height	= Math.floor(total_height);
	total_width	= Math.floor(total_width);

	if (settings.scrollaxis == 'x') {

		$(el_ul).css('width', total_width);

		if (!settings.usestops) stops[stops.length] = (total_width - $($(el_items)[$(el_items).length-1]).width())*settings.direction;

		if (settings.direction == 1)

			if (settings.start_offscreen)
				$(el_ul).css('left', -total_width+'px');
			else
				$(el_ul).css('left', -total_width+container_width-1+'px');
		else
			if (settings.start_offscreen)
				$(el_ul).css('left', container_width+'px');
			else
				$(el_ul).css('left', '-1px');

		repeater_x();

	} else {

		if (!settings.usestops) stops[stops.length] = (total_height - $($(el_items)[$(el_items).length-1]).height())*settings.direction;

		if (settings.direction == 1)

			if (settings.start_offscreen)
				$(el_ul).css('top', -total_height);
			else
				$(el_ul).css('top', -total_height+container_height);
		else
			if (settings.start_offscreen)
				$(el_ul).css('top', container_height+'px');
			else
				$(el_ul).css('top', '0px');

		repeater_y();
	}

	function goLeft() {
		settings.direction = -1;
		startScroller();
	};

	function goRight() {
		settings.direction = 1;
		startScroller();
	};


	function repeater_y() {

		if (!paused && !paused2) {

			if (settings.direction == 1) {

				if ((settings.start_offscreen) && ($(el_ul).css('top').toInt() > $(el_container).height())) {
					setTimeout( function() {
						if (settings.restart_beginning) $(el_ul).css('top', -total_height+"px");
						else settings.direction = -1;
						repeater_y();
					}, settings.pausetime);
					return;
				} else if ((!settings.start_offscreen) && ($(el_ul).css('top').toInt() > 0)) {
					setTimeout( function() {
						if (settings.restart_beginning) $(el_ul).css('top', -$(el_ul).height()+$(el_container).height()+"px");
						else settings.direction = -1;
						repeater_y();
					}, settings.pausetime);
					return;
				}

			} else if (settings.direction == -1) {

				if ((settings.start_offscreen) && ($(el_ul).css('top').toInt() <= -total_height)) {
					setTimeout( function() {
						if (settings.restart_beginning) $(el_ul).css('top', $(el_container).height()+"px");
						else settings.direction = 1;
						repeater_y();
					}, settings.pausetime);
					return;
				} else if ((!settings.start_offscreen) && ($(el_ul).css('top').toInt() <= -(total_height-$($(el_items)[$(el_items).length-1]).height()))) {
					setTimeout( function() {
						if (settings.restart_beginning) $(el_ul).css('top', '0px');
						else settings.direction = 1;
						repeater_y();
					}, settings.pausetime);
					return;
				}

			}

			new_pos = $(el_ul).css('top').toInt() + (settings.direction * settings.scroll_jump);
			$(el_ul).css('top', new_pos+'px');

			userstop = jQuery.inArray(-$(el_ul).css('top').toInt(), userstops);

			if ((userstop > -1)
				&& ($(el_ul).css('top').toInt() != total_height)
				&& (laststop != $(el_ul).css('top').toInt())) {
					setTimeout( function() { repeater_y(); }, $($(el_stops)[userstop]).attr('rel'));
					laststop = $(el_ul).css('top').toInt();
					return;
			}
/*
else if ((jQuery.inArray($(el_ul).css('top').toInt(), stops) > -1)
				&& ($(el_ul).css('top').toInt() != total_height)
				&& (laststop != $(el_ul).css('top').toInt())) {
					setTimeout( function() { repeater_y(); }, settings.pausetime);
					laststop = $(el_ul).css('top').toInt();
					return;
			}
*/

		}

		debug_msg('<br />Direction: '+settings.direction+
				'<br />OL Bottom (CSS): '+$(el_ul).css('bottom')+
				'<br />OL Top (CSS): '+$(el_ul).css('top')+
				'<br />OL Top (OFF): '+$(el_ul).offset().top+
				'<br />OL Size (X): '+$(el_ul).width()+
				'<br />OL Size (Y): '+$(el_ul).height()+
				'<br />Cont. Top (CSS): '+$(el_container).css('top')+
				'<br />Cont. Top (OFF): '+$(el_container).offset().top+
				'<br />Cont. Bottom (CSS): '+$(el_container).css('bottom')+
				'<br />Total Height: '+total_height+
				'<br />Total Width: '+total_width+
				'<br />Stops ['+stops.length+']: '+stops+
				'<br />User Stops ['+userstops.length+']: '+userstops
		);

		setTimeout( function() { repeater_y(); }, settings.scroll_speed);

	};


	function repeater_x() {

		if (!paused && !paused2) {

			if (settings.direction == 1) {

				if ((settings.start_offscreen) && ($(el_ul).css('left').toInt() > $(el_container).width())) {
					setTimeout( function() {
						if (settings.restart_beginning) $(el_ul).css('left', -total_width+"px");
						else settings.direction = -1;
						repeater_x();
					}, settings.pausetime);
					return;
				} else if ((!settings.start_offscreen) && ($(el_ul).css('left').toInt() > 0)) {
					setTimeout( function() {
						if (settings.restart_beginning) $(el_ul).css('left', -$(el_ul).width()+$(el_container).width()+"px");
						else settings.direction = -1;
						repeater_x();
					}, settings.pausetime);
					return;
				}

			} else if (settings.direction == -1) {

				if ((settings.start_offscreen) && ($(el_ul).css('left').toInt() <= -total_width)) {
					setTimeout( function() {
						if (settings.restart_beginning) $(el_ul).css('left', $(el_container).width()+"px");
						else settings.direction = 1;
						repeater_x();
					}, settings.pausetime);
					return;
				} else if ((!settings.start_offscreen) && ($(el_ul).css('left').toInt() <= -(total_width-$($(el_items)[$(el_items).length-1]).width()))) {
					setTimeout( function() {
						if (settings.restart_beginning) $(el_ul).css('left', '0px');
						else settings.direction = 1;
						repeater_x();
					}, settings.pausetime);
					return;
				}

			}

			new_pos = $(el_ul).css('left').toInt() + (settings.direction * settings.scroll_jump);
			$(el_ul).css('left', new_pos+'px');

			userstop = jQuery.inArray(-$(el_ul).css('left').toInt(), userstops);

			if ((userstop > -1)
				&& ($(el_ul).css('left').toInt() != total_width)
				&& (laststop != $(el_ul).css('left').toInt())) {
					setTimeout( function() { repeater_x(); }, $($(userstops)[userstop]).attr('rel'));
					laststop = $(el_ul).css('left').toInt();
					return;
			} else if ((jQuery.inArray($(el_ul).css('left').toInt(), stops) > -1)
				&& ($(el_ul).css('left').toInt() != total_width)
				&& (laststop != $(el_ul).css('left').toInt())) {
					setTimeout( function() { repeater_x(); }, settings.pausetime);
					laststop = $(el_ul).css('left').toInt();
					return;
			}


		}

		setTimeout( function() { repeater_x(); }, settings.scroll_speed);

		debug_msg('<br />Direction: '+settings.direction+
				'<br />OL Right (CSS): '+$(el_ul).css('right')+
				'<br />OL Left (CSS): '+$(el_ul).css('left')+
				'<br />OL Left (OFF): '+$(el_ul).offset().left+
				'<br />OL Size (X): '+$(el_ul).width()+
				'<br />OL Size (Y): '+$(el_ul).height()+
				'<br />Cont. Left (CSS): '+$(el_container).css('left')+
				'<br />Cont. Left (OFF): '+$(el_container).offset().left+
				'<br />Cont. Right (CSS): '+$(el_container).css('right')+
				'<br />Total Height: '+total_height+
				'<br />Total Width: '+total_width+
				'<br />Stops ['+stops.length+']: '+stops
		);

	};

	function pauseScroller2() { if (paused2) startScroller2(); else stopScroller2(); };
	function startScroller2() { paused2 = false; if ($(btn_pause).length > 0) $(btn_pause).html(settings.pause_phrase); };
	function stopScroller2()  { paused2 = true; if ($(btn_pause).length > 0) $(btn_pause).html(settings.play_phrase); };
	function pauseScroller()  { if (!paused2) if (paused) startScroller(); else stopScroller(); };
	function startScroller()  { paused = false; if ($(btn_pause).length > 0) $(btn_pause).html(settings.pause_phrase); };
	function stopScroller()   { paused = true; if ($(btn_pause).length > 0) $(btn_pause).html(settings.play_phrase); };
	function debug_msg(msg)   { if ((settings.debug) && ($(el_debug).length > 0)) $(el_debug).html(msg+'<br style="clear: both" />'); };

	};
}(jQuery));
jQuery.noConflict();

