/*
	galleryList
	largeIMG
*/

jQuery.fn.gallery = function(_options){

	var _options = jQuery.extend({
		galleryList: 'ul li a',
		largeIMG: 'div.img-hold img',
		btNext: 'a.next-photo',
		btPrev:'a.prev-photo',
		btFirst:'a.first-photo',
		btLast:'a.last-photo',
		play:'a.play',
		loadIMG:'div.img-hold img.load',
		holderEl:'div',
		moveEl:'ul',
		simpleEl:'li',
		duration: 400,
		slideTime : 4000
	},_options);

	return this.each(function(){
		var _THIS = $(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 _largeIMG = jQuery(_options.largeIMG, _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.outerWidth();
		var _simpleElWidth = _simpleEl.outerWidth();
		var _moveElwidth = _simpleElWidth*_simpleEl.length;
		var _margin = 0;

		var _step = _simpleElWidth;

		// preload
		_loadIMG.hide();
		_largeIMG.filter('.next').hide();
		_galleryList.eq(0).addClass('active');

		// click gallery
		_galleryList.find('a').click(function(){
			if (jQuery(this).attr('href') != '' && !jQuery(this).is('.active')) {
				var _href = jQuery(this).attr('href');
				var _this = this;
				_loadIMG.show();
				_img.onload = function(){
					showImg(_href);
					_galleryList.removeClass('active');
					jQuery(_this).parent().addClass('active');
					_img.onload = function(){};
				};
				_img.src = _href;

			}
			return false;
		});
		function showImg(href) {
			_loadIMG.hide();
			_largeIMG.filter('.active').removeClass('active').addClass('temp').fadeOut(_duration);
			_largeIMG.filter('.next').fadeIn(_duration).removeClass('next').addClass('active').attr('src',href);
			_largeIMG.filter('.temp').removeClass('temp').addClass('next');
		}
		var _timerPlay = false;
		if (_btPlay) {
			_btPlay.click(function(){
				if ($(this).is('.pause')) {
					$(this).removeClass('pause');
					if (_timerPlay) clearTimeout(_timerPlay);
				} else {
					$(this).addClass('pause');
					_timerPlay = setTimeout(function(){nextSlide()}, _options.slideTime/2);
				}
				return false;
			})
		}
		// Next/Prev
		if (_btNext) {
			_btNext.click(function(){
				nextSlide.apply(this);
				return false;
			});
		}
		function nextSlide(){
			if (!_loadIMG.is(':visible')) {
				var _activeItem = _galleryList.filter('.active');
				var _indexActive = _galleryList.index(_activeItem) + 1;
				if (_indexActive > _galleryList.length-1) 
					_indexActive = 0;
				var _href = _galleryList.eq(_indexActive).find('a').attr('href');

				_loadIMG.show();
				var _img = new Image();
				_img.onload = function(){
					showImg(_href);
					addActiveClass(_indexActive);
					moveNext();
					_img.onload = function(){};
				};
				_img.src = _href;
				if ($(_btPlay).is('.pause')) _timerPlay = setTimeout(function(){nextSlide()}, _options.slideTime);
			}
		}
		if (_btPrev) {
			_btPrev.click(function(){
				if (!_loadIMG.is(':visible')) {
					var _activeItem = _galleryList.filter('.active');
					var _indexActive = _galleryList.index(_activeItem) - 1;
					if (_indexActive < 0) 
						_indexActive = _galleryList.length-1;
					var _href = _galleryList.eq(_indexActive).find('a').attr('href');

					_loadIMG.show();
					var _img = new Image();
					_img.onload = function(){
						showImg(_href);
						addActiveClass(_indexActive);
						movePrev();
						_img.onload = function(){};
					};
					_img.src = _href;
				}
				return false;
			});
		}
		if (_btFirst) {
			_btFirst.click(function(){
				if (!_loadIMG.is(':visible')) {
					var _indexActive = 0;

					var _href = _galleryList.filter('.active').find('a').attr('href');
					_img.src = _href;
					_loadIMG.show();
					_img.onload = function(){
						showImg(_href);
						addActiveClass(_indexActive);
						_img.onload = function(){};
					};
					_margin = 0;
					_moveEl.animate({marginLeft:-_margin},{queue:false,duration:_duration});
				}
				return false;
			});
		}
		function addActiveClass(_indexActive) {
			_galleryList.removeClass('active');
			_galleryList.eq(_indexActive).addClass('active');
		}
		if (_btLast) {
			_btLast.click(function(){
				if (!_loadIMG.is(':visible')) {
					var _indexActive = _galleryList.length - 1;
					var _href = _galleryList.filter('.active').find('a').attr('href');

					_img.src = _href;
					_loadIMG.show();
					_img.onload = function(){
						showImg(_href);
						addActiveClass(_indexActive);
						_img.onload = function(){};
					};
					_margin = _moveElwidth-_holderWidth;
					_moveEl.animate({marginLeft:-_margin},{queue:false,duration:_duration});
				}
				return false;
			});
		}

		function moveNext() {
			if (_margin+_step > _moveElwidth-_holderWidth) {
				_margin = _moveElwidth-_holderWidth;
			}
			else _margin += _step;
			if (_galleryList.index(_galleryList.filter('.active')) == 0) {
				_margin = 0;
			}
			_moveEl.animate({marginLeft:-_margin},{queue:false,duration:_duration});
		}
		function movePrev() {
			if (_margin-_step < 0) {
				_margin = 0;
			}
			else _margin -= _step;
			if (_galleryList.index(_galleryList.filter('.active')) == _galleryList.length - 1) _margin = _moveElwidth-_holderWidth;
			_moveEl.animate({marginLeft:-_margin},{queue:false,duration:_duration});
		}
	});
}

$(document).ready(function(){
	$('div.gallery-block').gallery({
		galleryList:'div.gallery-move-holder li',
		largeIMG: 'div.image-holder img',
		loadIMG:'.load',
		btNext: 'a.next',
		btPrev:'a.prev',
		btFirst: false,
		btLast: false,
		play: false,
		holderEl:'div.gallery-move-holder',
		moveEl:'ul',
		simpleEl:'li'
	})
});

