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');
		var _el = _hold.find('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.eq(0).outerWidth();
		var _active = _el.index(_el.filter('.active'));
		if (_active < 1) _active = 1;
		var _last = _active;
		var _wAct = _el.eq(_active).outerWidth();
		var topEl = _wrap.outerHeight()/2-_w/2;
		var wEl = [];
		
		_el.each(function(){
			wEl.push($(this).find('img').outerWidth());
			$(this).find('img').css({width: _w});
		}).eq(_active).find('img').css({
			width: 'auto',
			top:_wrap.outerHeight()/2-wEl[_active]/2
		});
		_el.eq(_active).css({
			width: _wAct
		});
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * (_active-1)) + "px"
			}, {queue:false, duration: _speed});
			_el.eq(_last).find('img').animate({
				width: _w,
				top:topEl
			}, _speed);
			_el.eq(_last).animate({
				width: _w
			}, _speed);
			_el.eq(_active).find('img').animate({
				width: wEl[_active],
				top: _wrap.outerHeight()/2-wEl[_active]/2
			}, _speed);
			_el.eq(_active).animate({
				width: _wAct
			}, _speed);
			_el.removeClass('active').eq(_active).addClass('active');
			_last = _active;
		}
		_next.click(function(){
			_active++;
			if (_active > _count-1) _active = 1;
			scrollEl();
			return false;
		});
		_prev.click(function(){
			_active--;
			if (_active < 1) _active = _count-1;
			scrollEl();
			return false;
		});
	});
}

$(document).ready(function(){
	$('div.gallery').gallSlide({
		duration: 700,
		autoSlide: 4000
	});
});