function initGallery(){
	if($('#main-gallery').length){
		// options
		_fadeDur = 500; // fade animation speed
		// variables
		var _holder = $('#main-gallery');
		var _img = _holder.find('.imageblock .image .item');
		var __animated = false;
		var _textHold = $('.imageblock .imagenav p');
		var _prevLink = $('.imageblock a.prev');
		var _nextLink = $('.imageblock a.next');
		
		if(_holder.find('.rightside .imagelist a').length==_img.length){
			// reset
			var _currEl = _img.index(_img.filter('.active'));
			if(_currEl==-1) _currEl=0;
			var _nextEl = _currEl;
			_img.eq(_currEl).removeClass('active').css('z-index',2);
			_holder.find('.rightside .imagelist a').eq(_currEl).addClass('active');
			
			// click 'next' button
			_nextLink.click(function(){
				if(!__animated){
					__animated = true;
					_currEl = _nextEl;
					if(_nextEl<$('.rightside .imagelist a').length-1) _nextEl++
					else _nextEl=0;
					fadeAnimate();
				}
				return false;
			});
			
			// click 'prev' button
			_prevLink.click(function(){
				if(!__animated){
					__animated = true;
					_currEl = _nextEl;
					if(_nextEl>0) _nextEl--
					else _nextEl=$('.rightside .imagelist a').length-1;
					fadeAnimate();
				}
				return false;
			});
			
			// click thumbnail
			_holder.find('.rightside .imagelist a').live('click',function(){
				if(!$(this).hasClass('active') && !__animated){
					__animated = true;
					_currEl = _nextEl;
					_nextEl = $('.rightside .imagelist a').index(this);
					fadeAnimate();
				}
				return false;
			});
			
			// animation
			function fadeAnimate(){
				_img.eq(_currEl).css('z-index',3);
				_img.eq(_nextEl).css('z-index',2);
				_img.eq(_currEl).animate({
					opacity:0
				},_fadeDur,function(){
					_img.eq(_currEl).css({
						opacity:1,
						zIndex:1
					});
					$('.rightside .imagelist a').removeClass('active');
					$('.rightside .imagelist a').eq(_nextEl).addClass('active');
					var _text = $('.rightside .imagelist a').eq(_nextEl).attr('rel');
					if(_text) _textHold.text(_text);
					__animated = false;
				});
			}
		}
	}
}
$(function(){
	initGallery();
});