/*
	galleryImage v 0.0.1
*/

jQuery.fn.galleryImage = function(_options){
	
	var _options = jQuery.extend({
		galleryList: 'ul li a',
		largeIMG: 'div.img-hold img',
		btNext: 'a.next',
		btPrev:'a.prev',
		currentNum:'span.cur',
		allNum:'span.all',
		loader:'.load',
		duration:250
	},_options);
	
	return this.each(function(){
		var _THIS = jQuery(this),
			_galleryList = jQuery(_options.galleryList, _THIS),
			_largeIMG = jQuery(_options.largeIMG, _THIS),
			_btNext = jQuery(_options.btNext, _THIS),
			_btPrev = jQuery(_options.btPrev, _THIS),
			_currentNum = jQuery(_options.currentNum, _THIS),
			_allNum = jQuery(_options.allNum, _THIS),
			_loader = jQuery(_options.loader, _THIS),
			_tempIMG = jQuery('<img>').hide(),
			_length = _galleryList.length;

		if (_loader.length) _loader.hide();
		_largeIMG.after(_tempIMG);
		
		var _galImages = _largeIMG.parent().children('img');
		_THIS.h = _largeIMG.parent().height();
		_THIS.w = _largeIMG.parent().width();
		// preload
		var _imagesG = [],
			_current = 0,
			_duration = _options.duration;
			
		var _activeItem = _galleryList.parent().filter('.active').children('a');
		
		if (_activeItem.length) {
			_current = _galleryList.index(_activeItem);
			_largeIMG.attr('src',_activeItem.attr('href'));
		} else {
			_largeIMG.attr('src',_galleryList.eq(0).parent().filter('.active').children('a').attr('href'));
		}
		
		if (_allNum.length) {
			var _allNumValue = _galleryList.length;
			if (_allNumValue < 10) _allNumValue = '0'+_allNumValue;
			_allNum.text(_allNumValue);
			if (_currentNum.length) {
				if (_current+1 < 10) _curNumValue = '0'+(_current+1);
				_currentNum.text(_curNumValue);
			}
		}
		
		_largeIMG.css({
			'marginTop':(_THIS.h-_largeIMG.height())/2,
			'marginLeft':(_THIS.w-_largeIMG.width())/2
		});
		
		_galleryList.click(function(){
			if (!_tempIMG.is(':animated')) {
				var _index = _galleryList.index(this);
				showImage(_index);
				setActiveThumb(_index);
			}
			return false;
		});
		_btNext.click(function(){
			if (!_tempIMG.is(':animated')) {	
				_current++;
				if (_current >= _length) _current = 0;
				showImage(_current);
				setActiveThumb(_current);
			}
			return false;
		});
		_btPrev.click(function(){
			if (!_tempIMG.is(':animated')) {	
				_current--;
				if (_current < 0) _current = _length-1;
				showImage(_current);
				setActiveThumb(_current);
			}
			return false;
		});
		
		function showImage(_index) {
			_current = _index;
			var _newImage = new Image();
			_newImage.src = _galleryList.eq(_index).attr('href');
			if (_newImage.complete) {
				_galImages.hide();
				_tempIMG.attr('src',_newImage.src).fadeIn(_duration, function(){
					switchImage();	
				});
				_galImages.css({
					'marginTop':(_THIS.h-_newImage.height)/2,
					'marginLeft':(_THIS.w-_newImage.width)/2
				});
			} else {
				if (_loader.length) _loader.show();
				_newImage.onload = function(){
					_galImages.hide();
					_tempIMG.attr('src',this.src);
					setTimeout(function(){
						_tempIMG.fadeIn(_duration, function(){
							switchImage();
						});
						_galImages.css({
							'marginTop':(_THIS.h-_tempIMG.height())/2,
							'marginLeft':(_THIS.w-_tempIMG.width())/2
						});
					}, 30);
					if (_loader.length) _loader.hide();
					this.onload = function(){};
				}
			}
		}
		function switchImage(){
			_largeIMG.attr('src', _tempIMG.attr('src')).show();
			_tempIMG.hide();
		}
		function setActiveThumb(_index) {
			_galleryList.parent().removeClass('active');
			_galleryList.eq(_index).parent().addClass('active');
			if (_currentNum.length) {
				var _curNumValue = _index+1;
				if (_curNumValue < 10) _curNumValue = '0'+_curNumValue;
				_currentNum.text(_curNumValue);
			}
		}
		
		
	});
}

jQuery(window).bind('load', function(){
	jQuery('div.gallery-holder').galleryImage({
		galleryList: 'div.gallery-list li a',
		largeIMG: 'div.image img'
	})
});

	
