

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.gallery');
		var _el = _hold.find('ul.gallery > 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 _active = 0;
		var _a = _active;
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
			changeEl(_active);
		}
		_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 = 1500; // in ms
	var change_speed = 800; // in ms
	if(_hold.length){
		var _t;
		var _f = true;
		var _list = _hold.find('ul.fader > li');
		if($.browser.msie) _list.removeClass('active').hide().eq(_a).addClass('active').show();
		else{
			_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
			_list.eq(_a).css('opacity', 'auto');
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				if($.browser.msie){
					_list.eq(_a).removeClass('active').hide();
					_list.eq(_ind).addClass('active').show();
				}
				else{
					_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});
				}
				_a = _ind;
			}
		}
	}
		
		
	});
}

$(document).ready(function(){
	$('div.container').gallSlide({
		duration: 700,
		autoSlide: 4000
	});
});