;(function($){
$.fn.moescroller_scroller = 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(),
	laststop	= null,
	cur_pos		= 0,
	timer		= null,

	settings = jQuery.extend({
		restart_beginning:	true,
		btn_previous:		null,
		btn_next:		null,
		btn_pause:		null,
		container_height:	200,
		container_width:	500,
		duration:		3000,
		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:		'easeOutBack',
		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);

	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': 'auto',
				'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; goNext(); });
	if (($(btn_previous).length != 0) && ($(btn_previous) != null))	$(btn_previous).bind('click', function(event) { paused2 = false; paused = false; goPrevious(); });

	stops[stops.length] = 0;

	$(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.scrollaxis == 'y') {
			if (i < $(el_items).length-1) stops[stops.length] = -total_height;
		} else {
			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);

	var starting_pos = 0;
	if (settings.direction  == -1) starting_pos = stops.length-1;

	if (settings.scrollaxis == 'x') {
		$(el_ul).animate({ left: stops[starting_pos]+'px' }, { duration: 0 });
		$(el_ul).css('width', total_width);
	} else {
		$(el_ul).animate({ top: stops[starting_pos]+'px' }, { duration: 0 });
	}

	timer = setTimeout( function() { repeater(); }, settings.pausetime);

	function goPrevious() {
		clearTimeout(timer);
		cur_pos -= 1;
		if (cur_pos < 0) cur_pos = stops.length-1;
		moveItem();
		timer = setTimeout( function() { repeater(); }, settings.pausetime+settings.duration);
	};

	function goNext() {
		clearTimeout(timer);
		cur_pos += 1;
		if (cur_pos >= stops.length) cur_pos = 0;
		moveItem();
		timer = setTimeout( function() { repeater(); }, settings.pausetime+settings.duration);
	};

	function moveItem() {
		if (settings.scrollaxis == 'y') {
			$(el_ul).animate({ top: stops[cur_pos]+'px' }, {
				duration: settings.duration,
				easing: settings.transition
			});
		} else {
			$(el_ul).animate({ left: stops[cur_pos]+'px' }, {
				duration: settings.duration,
				easing: settings.transition
			});
		}
	};

	function repeater() {

		if (!paused && !paused2) {

			cur_pos = cur_pos + settings.direction;

			if (settings.direction == 1) {
				if (cur_pos >= stops.length) {
					if (settings.restart_beginning) {
						cur_pos = 0;
					} else {
						cur_pos -= 2;
						settings.direction = -1;
					}
				}
			} else if (cur_pos < 0) {
				if (settings.restart_beginning) {
					 cur_pos = stops.length - 1;
				} else {
					cur_pos = 1;
					settings.direction = 1;
				}
			}

			moveItem();
		}

		debug_msg('<br />Direction: '+settings.direction+
				'<br />Cur Pos: '+cur_pos+
				'<br />Stops ['+stops.length+']: '+stops
		);

		timer = setTimeout( function() { repeater(); }, settings.pausetime+settings.duration);

	};

	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();

