/* Gallery (slide-on-click, auto-slide-left) */
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.btn-next');
		var _prev = _hold.find('a.btn-prev');
		var _count = _el.index(_el.filter(':last'));
		var _w = _el.outerWidth();
		var _wrapHolderW = 1;
		var _t;
		var _active = _el.index(_el.filter('.active:eq(0)'));
		if (_active < 0) _active = 0;
		var _last = _active;
		
		
		_wrap.css({marginLeft: -(_w * _active) + "px"});
		_el.eq(_active).css({
			height: 258,
			width: 382,
			paddingTop: 0,
			left: -90,
			marginLeft: 0,
			marginRight: -180
		});
		if ($.browser.webkit){
			_el.find('img').css({
				height: 153,
				width: 236
			});
			_el.eq(_active).find('img').css({
				height: 235,
				width: 362
			});
		}
		
		function scrollEl(){
			_wrap.eq(0).animate({
				marginLeft: -(_w * _active) + "px"
			}, {queue:false, duration: _speed});
			_el.removeClass('active').css({zIndex:1}).eq(_active).addClass('active');
			_el.eq(_last).animate({
				height: 176,
				width: 256,
				paddingTop: 48,
				left: 0,
				marginLeft:0,
				marginRight: 0
			}, {queue:false, duration: _speed}).css({zIndex:5});
			_el.eq(_active).animate({
				height: 258,
				width: 382,
				paddingTop: 0,
				left: -90,
				marginRight: -180
			}, {queue:false, duration: _speed}).css({zIndex:10});
			if ($.browser.webkit){
				_el.eq(_last).find('img').animate({
					height: 153,
					width: 236
				}, {queue:false, duration: _speed});
				_el.eq(_active).find('img').animate({
					height: 235,
					width: 362
				}, {queue:false, duration: _speed});
			}
			_last = _active;
		}
		function runTimer(){
			_t = setInterval(function(){
				_active++;
				if (_active > (_count - _wrapHolderW + 1)) _active = 0;
				scrollEl();
			}, _timer);
		}
		//runTimer();
		_next.click(function(){
			_active++;
			if(_t) clearTimeout(_t);
			if (_active > (_count - _wrapHolderW + 1)) _active = 0;
			scrollEl();
			//runTimer();
			return false;
		});
		_prev.click(function(){
			_active--;
			if(_t) clearTimeout(_t);
			if (_active < 0) _active = _count - _wrapHolderW + 1;
			scrollEl();
			//runTimer();
			return false;
		});
	});
}
$(document).ready(function(){
	$('div#gallery').gallSlide({
		duration: 700,
		autoSlide: 4000
	});
});

