/* Gallery (slide-on-click) */
jQuery.fn.gallSlide = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		duration: 700,
		autoSlide: 5000
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _timer = _options.autoSlide;
		var _wrap = _hold.find('ul.slider');
		var _el = _hold.find('ul.slider > li.slider-item');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth();
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _active = 0;
		var _btn = $('<ul class="paging"></ul>');
		_hold.find('div.control-holder').append(_btn);
		_btn.append('<li><a class="prev" href="#">prev</a></li>');
		_el.each(function(_i){
			_btn.append('<li><a class="control" href="#">'+(_i+1)+'</a></li>');
		});
		_btn.append('<li><a class="next" href="#">next</a></li>');
		_btn = _btn.find('a.control');
		var _next = _hold.find('a.next');
		var _prev = _hold.find('a.prev');
		_btn.eq(_active).parent('li').addClass('active');
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
			_btn.parent('li').removeClass('active');
			_btn.eq(_active).parent('li').addClass('active');
			
		}
		_btn.click(function(){
			_active = _btn.index($(this));
			scrollEl();
			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;
		});
	});
}

$(document).ready(function(){
	$('div.slide-gallery').gallSlide({
		duration: 700,
		autoSlide: 4000
	});
});

