jQuery.fn.gallery = function(_options){
	
	var _options = jQuery.extend({
		galleryList: 'ul li a',
		slideEl: 'div.img-hold img',
		btNext: 'a.next-photo',
		btPrev:'a.prev-photo',
		play:'a.play',
		loadIMG:'div.img-hold img.load',
		holderEl:'div',
		moveEl:'ul',
		simpleEl:'li',
		duration: 400,
		slideTime : 4000,
		autoscroll: 1000 
	},_options);
	
	return this.each(function(){
		var _THIS = jQuery(this);
		var _galleryList = jQuery(_options.galleryList, _THIS);
		var _btNext = jQuery(_options.btNext, _THIS);
		var _btPrev = jQuery(_options.btPrev, _THIS);
		var _btFirst = _options.btFirst ? jQuery(_options.btFirst, _THIS) : false;
		var _btLast = _options.btLast ? jQuery(_options.btLast, _THIS) : false;
		var _btPlay = _options.play ? jQuery(_options.play, _THIS) : false;
		var _slideEl = jQuery(_options.slideEl, _THIS);
		var _loadIMG = jQuery(_options.loadIMG, _THIS);
		var _duration = _options.duration;
		var _img = new Image();
		var _holderEl = jQuery(_options.holderEl, _THIS);
		var _moveEl = jQuery(_options.moveEl, _holderEl);
		var _simpleEl = jQuery(_options.simpleEl, _moveEl);
		var _holderWidth = _holderEl.outerHeight();
		var _simpleElWidth = _simpleEl.outerHeight(true);
		var _moveElwidth = _simpleElWidth*_simpleEl.length;
		var _margin = 0, _current, _next = 0;
		
		var _step = _simpleElWidth;
		
		if (!_slideEl.filter('.active').length) {
			_slideEl.eq(0).addClass('active');
			_current = 0;
		} else {
			_current = _slideEl.index(_slideEl.filter('.active'));
			_next = _current;
		}
		_slideEl.not(".active").hide();
		
		_galleryList.eq(0).parent().addClass('active');
		// click gallery
		_galleryList.click(function(){
			if (!jQuery(this).parent().is('.active')) {
				_current = _galleryList.index(this);
				showImg();
				addActiveClass();
			}
			return false;
		});
		function showImg(href) {
			if (_current != _next) {
				_slideEl.removeClass('active');
				_hide = _slideEl.filter(':visible');
				_slideEl.eq(_current).fadeIn(_duration, function(){
					_hide.hide().removeClass('hide');
				}).addClass('active');
				_next = _current;
			}
		}
		var _timerPlay = false;
		if (_btPlay) {
			_btPlay.click(function(){
				if (jQuery(this).is('.pause')) {
					jQuery(this).removeClass('pause');
					if (_timerPlay) clearTimeout(_timerPlay);
				} else {
					jQuery(this).addClass('pause');
					_timerPlay = setTimeout(function(){nextSlide()}, _options.slideTime);
				}
				return false;
			})
		}
		_timerPlay = setTimeout(function(){nextSlide()}, _options.slideTime);
		// Next/Prev
		if (_btNext) {
			_btNext.click(function(){
				if (!_slideEl.is(':animated'))
					nextSlide();
				return false;	
			});
		}
		function nextSlide(){
			_current++;
			if (_current > _galleryList.length-1) 
				_current = 0;
			showImg();
			moveNext();
			addActiveClass();
		}
		if (_btPrev) {
			_btPrev.click(function(){
				if (!_slideEl.is(':animated')) {
					_current--;
					if (_current < 0) 
						_current = _galleryList.length-1;
					
					showImg();
					movePrev();
					addActiveClass();
				}
				return false;
			});
		}
		function addActiveClass() {
			_galleryList.parent().removeClass('active');
			_galleryList.eq(_current).parent().addClass('active');
		}
				
		function moveNext() {
			if(_timerPlay) clearTimeout(_timerPlay);
			if (_margin+_step > _moveElwidth-_holderWidth) {
				_margin = _moveElwidth-_holderWidth;
			}
			else _margin += _step;
			if (_current == 0) {
				_margin = 0;
			}
			_moveEl.animate({marginTop:-_margin},{queue:false,duration:_duration,complete: function(){
				_timerPlay = setTimeout(function(){nextSlide()}, _options.slideTime);
			}});
		}
		function movePrev(){
			if(_timerPlay) clearTimeout(_timerPlay);
			if (_margin-_step < 0) {
				_margin = 0;
			}
			else _margin -= _step;
			if (_current == _galleryList.length - 1) _margin = _moveElwidth-_holderWidth;
			_moveEl.animate({marginTop:-_margin},{queue:false,duration:_duration,complete:function(){
				_timerPlay = setTimeout(function(){nextSlide()}, _options.slideTime);
			}});
		}
	});
}
//jQuery.noConflict();
jQuery(document).ready(function(){
	jQuery('div.gallery').gallery({
		galleryList:'.slider div ul a',
		slideEl: 'div.visual > div.slide',
		loadIMG:'.load',
		btNext: 'a.link-next',
		btPrev:'a.link-prev',
		play: false,
		holderEl:'.slider > div',
		moveEl:'ul',
		simpleEl:'li',
		slideTime : 10000,
		duration: 400
	});
});

