/**
 * General Gallery Javascript file  
 * Note that due to a limitation of how the regex parsing works, the image
 * names should only contain letters, numbers, _ and -.
 * If you need this behavior to change, edit the size function.
 */

Gallery = {
	mini: function(fullImageId, thumbListClass) {
		$(thumbListClass).css({visibility:'visible'});
		
		$(thumbListClass+' li img').bind('click', function(e){
			var imgOld = $('#'+fullImageId+' img');
			var imgNew = '<img src="'+$(this).attr('src').replace('_thumb', '')+'" class="full" / >';
			$(this).parent().parent().find('li').removeClass('active');
			$(this).parent().addClass('active');
			//GALLERY LOADER
			//$('#gallery-loader').show();
			$(imgNew).load(function(){
				//GALLERY LOADER
				//$('#gallery-loader').hide();
			 	$(imgNew).fadeIn('slow').insertAfter(imgOld);
				$(imgOld).fadeOut('slow', function(e){
					$(this).remove();
					$('#lightbox').show();
				});
			});			
		});
		$(thumbListClass+' li img').bind('mouseover', function(e){
			$(this).parent().addClass('gallery-hover');
		});
		$(thumbListClass+' li img').bind('mouseout', function(e){
			$(this).parent().removeClass('gallery-hover');
		});
	},
	size: function(imageList){
		var regExNo = new RegExp('([0-9]+)[a-zA-Z0-9_-]+\.jpg');
		var regexResult = regExNo.exec($(imageList+' li img:last').attr('src'));
		var numResult = parseFloat(regexResult);

		if (isNaN(numResult)) {
			throw "Regex result for Gallery.size ["+regexResult+"] is not a number.";
		} else {
			return parseFloat(numResult);
		}
	},
	position: function(){
		$('#lightbox-mask').css('width', $(document).width()+'px');
		$('#lightbox-mask').css('height', $(document).height()+'px');
		$('#lightbox').css('top', ($(window).height()/2)-($('#lightbox').height()/2)+'px');
		$('#lightbox').css('left', ($(window).width()/2)-($('#lightbox').width()/2)+'px');
	},
	swapImage: function(nextImg){
		var currentImg = $('#lightbox img.full');
		//GALLERY LOADER
		$('#gallery-loader').show();
		$(nextImg).load(function(){
			//GALLERY LOADER
			$('#gallery-loader').hide();
		 	$(nextImg).fadeIn('slow').insertAfter(currentImg);
			$(currentImg).fadeOut('slow', function(e){
				$(this).remove();
				$('#lightbox').show();

				if (($.browser.msie) && ($.browser.version <= 6)) {
					IEFix.runFixes();
				}
			});
		});
	},
	toggleImage: function(imageList, direction, element){
		var imgNew;
		var imgNo;
		var regExNo = new RegExp('([0-9]+)\.jpg');
		var regExPath = new RegExp('(.*/)[0-9]+\.jpg$');
		var imgPath = regExPath.exec($('#lightbox img.full').attr('src'));
		var imgSize = Gallery.size(imageList);
		if(direction==''){
			$('#lightbox-mask').show();
			imgNo = parseFloat(regExNo.exec($(element).attr('src')));
			imgNew = '<img src="'+$(element).attr('src').replace('_thumb_lg', '')+'" class="full" / >';
		}
		else {
			imgNo = parseFloat(regExNo.exec($('#lightbox img.full').attr('src')));
			if(direction=="next"){
				if(imgNo == imgSize){
					imgNo = "01";
				}
				else{
					(imgNo+1 < 10) ? imgNo = '0'+(imgNo+1) : imgNo = imgNo+1;
				}
			}
			else if(direction=="prev"){
				if(imgNo == 1){
					imgNo = imgSize;
				}
				else{
					(imgNo-1 < 10) ? imgNo = '0'+(imgNo-1) : imgNo = imgNo-1;
				}	
			}
			imgNew = '<img src="'+imgPath[1]+imgNo+'.jpg" class="full" / >';
			
		}
		Gallery.swapImage(imgNew);
	},
	close: function(){
		$('#lightbox').fadeOut('fast', function(){
			$('#lightbox-mask').hide();
		})
	},
	init: function(imageList){
		Gallery.position();
		$(window).bind('resize', function(e){
			Gallery.position();
		});
		$(imageList+' li a').each(function(){
			$(this).parent().html($(this).html());
		});
		$(imageList+' li').addClass('inactive');
		$(imageList+' li').bind('mouseover',function(e){
			$(this).addClass('active');
			$(this).removeClass('inactive');
		});
		$(imageList+' li').bind('mouseout',function(e){
			$(this).addClass('inactive');
			$(this).removeClass('active');
		});


		$(imageList+' li img').bind('click',function(e){
			Gallery.toggleImage(imageList, '', $(this));
		});
		$('#lightbox .next').bind('click',function(e){
			Gallery.toggleImage(imageList, 'next', $(this));
		});
		$('#lightbox .prev').bind('click',function(e){
			Gallery.toggleImage(imageList, 'prev', $(this));
		});
		
		$('#lightbox img.close').bind('click',Gallery.close);
		$('#lightbox-mask').bind('click',Gallery.close);
	}
}

