jQuery.fn.gallSlide = function(_options){
	// defaults options
	var _options = jQuery.extend({
		duration: 900,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('ul.gallery');
		var _el = _hold.find('ul.gallery > li');
		var _next = _hold.find('a.link-next');
		var _prev = _hold.find('a.link-prev');
		var _autoBtn = _hold.find('a.auto-btn');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth();
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _t;

		var _btn = $('<ul class="indication"></ul>');
		_el.each(function(_i){
			_btn.append('<li><a href="#">'+(_i+1)+'</a></li>');
		});
		$('div.slidenav').prepend(_btn);

		_btn = _btn.find('li');

		var _active = 0;
		_btn.removeClass('active');
		_btn.eq(_active).addClass('active');

		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
			_btn.removeClass('active');
			_btn.eq(_active).addClass('active');
		}
		function runTimer(){
			_t = setInterval(function(){
				_active++;
				if (_active > (_count - _wrapHolderW + 1)) _active = 0;
				scrollEl();
			}, _timer);
		}
		runTimer();

		_autoBtn.click(function(){
			if($(this).hasClass('paused')){
				$(this).removeClass('paused');
				runTimer();
				$(this).html('PAUSE');
			}
			else{
				if(_t) clearTimeout(_t);
				$(this).addClass('paused');
				$(this).html('PLAY');
			}
			return false;
		});

		_next.click(function(){
			_active++;
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			return false;
		});
		_prev.click(function(){
			_active--;
			if (_active < 0) _active = _count - _wrapHolderW + 1;
			scrollEl();
			return false;
		});

		_btn.click(function(){
			_active = _btn.index($(this));
			if (_active < 0) _active = _count - _wrapHolderW + 1;
			scrollEl();
			return false;
		});

	});
}
$(document).ready(function(){
	$('div.slider').gallSlide({
		duration: 900,
		autoSlide: 5000
	});
});

