/* Gallery (slide-on-click, auto-slide-left, stop-on-hover) */
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('.gallery-review ul');
		var _el = _hold.find('.gallery-review ul > li');
		var _next = _hold.find('a.link-next');
		var _prev = _hold.find('a.link-prev');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth();
		var _wrapHolderW = Math.ceil(_wrap.parent().width()/_w);
		var _t;
		var _active = 0;
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
		}
		function runTimer(){
			_t = setInterval(function(){
				_active++;
				if (_active > (_count - _wrapHolderW + 1)) _active = 0;
				scrollEl();
			}, _timer);
		}
		_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;
		});
			var wait_time = 5000; // in ms
			var change_speed = 500; // in ms
			var _holder = _hold.find('div.gallery-holder');
			if(_holder.length){
				var _f = true;
				var _list = _holder.find('.slide');
				var _btn = $('<ul class="paging"></ul>');
				_list.each(function(_i){
					_btn.append('<li><a href="#">'+(_i+1)+'</a></li>');
				});
				_btn = _hold.find('.gallery-review ul').find('a');
				var _a = _list.index(_list.filter('.active:eq(0)'));
				if(_a == -1) _a = 0;
				
				_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
				_btn.eq(_a).parent('li').addClass('active');
				_btn.click(function(){
					changeEl(_btn.index(this));
					return false;
				});		
				function changeEl(_ind){
					if(_ind != _a){
						_list.eq(_a).removeClass('active').animate({opacity: 0}, {queue:false, duration:change_speed});
						_list.eq(_ind).addClass('active').animate({opacity: 1}, {queue:false, duration:change_speed});
						_btn.eq(_a).parent('li').removeClass('active');
						_btn.eq(_ind).parent('li').addClass('active');
						_a = _ind;
					}
				}
			}
	});
	

}
$(document).ready(function(){
	$('#gallery').gallSlide({
		duration: 700,
		autoSlide: 4000
	});
});
