/*
 * jQuery SmartBox Plugin v7.9.3
 *
 * Copyright (c) 2010 Ben Plum <http://www.benjaminplum.com>
 * 
 * Changes: 
 * - (11/08/10) Fixed pagination display bug 
 */
 
 
(function($){
	var fadeSpeed = 300;
	var resizeTimer;
	var originalWidth;
	var originalHeight;
	var imageLoaded = false;
	var isAnimating = false;
	
	var galleryName;
	var galleryActive = false;
	var galleryArray = [];
	var galleryCount = 0;
	var galleryIndex;

	$.fn.smartbox = function() {
		return this.each(function () {
			$(this).click(drawSmartbox);
		});
	};
	
// MAIN FUNCTIONS --------------------
	
	function drawSmartbox(event)
	{
		if(isAnimating)
		{
			return false;
		}
		isAnimating = true;
	
		var imgSrc = $(this).attr('href');
		var caption = $(this).attr('title');
		
		if($(this).attr('rel') != '')
		{
			galleryName = $(this).attr('rel');
			galleryActive = true;
			galleryIndex = $('a[rel= ' + galleryName + ']').index($(this));
			$('a[rel= ' + galleryName + ']').each(function(i) {
				galleryArray[i] = $(this);
				galleryCount = i;
			});
		}
	
		$('body').append('<div id="smartbox_cover" style="opacity: 0"></div>');
		$('#smartbox_cover').animate({opacity: 0.7}, fadeSpeed).click(closeSmartbox);
		
		var html = '<div id="smartbox" style="opacity: 0; height: 200px; width: 200px;">';
		html += '<div class="inner">';
		html += '<div class="info">';
		if(galleryActive)
		{
			html += '<p class="pagination">';
			html += '<span class="current">' + (galleryIndex + 1) + '</span> of <span class="total">' + (galleryCount + 1) + '</span>';
			html += '</p>';
		}
		html += '<p class="caption">';
		html += caption;
		html += '</p>';
		html += '</div>';
		html += '</div>';
		if(galleryActive)
		{
			html += '<a href="#" class="previous">Previous</a>';
			html += '<a href="#" class="next">Next</a>';
		}
		html += '<a href="#" class="close" style="opacity: 0;">Close</a>';
		html += '</div>';
		$('body').append(html);
		$('#smartbox .close').click(closeSmartbox);
		
		$('#smartbox .info').css({opacity: 1});
		var infoHeight = $('#smartbox .info').outerHeight();
		$('#smartbox .info').css({bottom: -infoHeight + 'px'});
		$('#smartbox').hover(showInfo, hideInfo);
		
		$('#smartbox .next, #smartbox .previous').css({opacity: 0})
			.mouseenter(showPagination).mouseleave(hidePagination)
			.mouseenter(function() { $(this).addClass("over"); }).mouseleave(function() { $(this).removeClass("over"); });

		if(galleryActive)
		{
			$('#smartbox a').not('.close').click(galleryClick);
		}
		
		centerSmartbox();
		if(imgSrc.substr(0, 1) == '#')
		{
			loadElement(imgSrc);
		}
		else
		{
			loadImage(imgSrc);
		}
		$('#smartbox').animate({opacity: 1}, fadeSpeed);
		
		$(window).bind('resize', resizeWindow);
		
		return false;
	}
	
	function loadElement(src)
	{
		var clone = $(src).clone().show();
		$('#smartbox .inner').append(clone);
		
		var padding = parseInt($('#smartbox').css('paddingLeft'), 10) * 2;
		var newWidth = clone.outerWidth();
		var newHeight = clone.outerHeight();
		var scrollOffset = $(window).scrollTop();
		var offsetLeft = ($(window).width() - newWidth - padding) / 2;
		var offsetTop = ($(window).height() - newHeight - padding) / 2 + scrollOffset;
		
		$('#smartbox').css({height: newHeight + 'px', left: offsetLeft + 'px', top: offsetTop + 'px', width: newWidth + 'px'});
		$('#smartbox .close').animate({opacity: 1}, fadeSpeed, function() {
			isAnimating = false;
		});
	}
	
	function loadImage(imageSrc)
	{
		var newImage = new Image();
		$(newImage).load(function() {
			var padding = parseInt($('#smartbox').css('paddingLeft'), 10) * 2;
			
			imageLoaded = true;
			originalWidth = newImage.width;
			originalHeight = newImage.height;
			var size = resizeImage();
						
			var newWidth = size.width;
			var newHeight = size.height;
			var scrollOffset = $(window).scrollTop();
			var offsetLeft = ($(window).width() - newWidth - padding) / 2;
			var offsetTop = ($(window).height() - newHeight - padding) / 2 + scrollOffset;
			
			$('#smartbox').animate({height: newHeight + 'px', left: offsetLeft + 'px', top: offsetTop + 'px', width: newWidth + 'px'}, fadeSpeed, function(event) {
				$('#smartbox .inner').append(newImage);
				$('#smartbox .smartbox_image').css({height: '100%', width: '100%'}).animate({opacity: 1}, fadeSpeed);
				$('#smartbox .close').animate({opacity: 1}, fadeSpeed);
				isAnimating = false;
				
				$('#smartbox .next, #smartbox .previous').css({width: (newWidth/5)*2 + 'px'});
				$('#smartbox .over').animate({opacity: 1});
			});
		}).attr('src', imageSrc).attr('class', 'smartbox_image').css({opacity: 0});
	}
	
	
// GALLERY FUNCTIONS --------------------

	function loadGallery()
	{
		$("#smartbox .over").animate({opacity: 0});
		$('#smartbox .close').animate({opacity: 0}, fadeSpeed);
		$('#smartbox .inner img').animate({opacity: 0}, fadeSpeed, function() {
			$(this).remove();
			loadImage(galleryArray[galleryIndex].attr('href'));
			$('#smartbox .caption').html(galleryArray[galleryIndex].attr('title'));
			$('#smartbox .pagination .current').html((galleryIndex+1));
		});
	}
	
	function galleryNext()
	{
		if(galleryIndex < galleryCount)
		{
			galleryIndex++;
			loadGallery();
		}
		else
		{
			isAnimating = false;
		}
	}
	
	function galleryPrevious()
	{
		if(galleryIndex > 0)
		{
			galleryIndex--;
			loadGallery();
		}
		else
		{
			isAnimating = false;
		}
	}
	
	function galleryClick(event)
	{
		if(isAnimating)
		{
			return false;
		}
		isAnimating = true;
						
		if($(this).hasClass('next'))
		{
			galleryNext();
		}
		else if($(this).hasClass('previous'))
		{
			galleryPrevious();
		}
		else
		{
			isAnimating = false;
		}
		
		return false;
	}

// HELPER FUNCTIONS --------------------

	function resizeImage()
	{
		var windowPadding = 100;
		var windowWidth = $(window).width() - windowPadding;
		var windowHeight = $(window).height() - windowPadding;
				
		var newWidth = originalWidth;
		var newHeight = originalHeight;
		var ratio;

		if(originalWidth > originalHeight)
		{
			ratio = originalHeight / originalWidth;
			newWidth = windowWidth;
			newHeight = newWidth * ratio;
			
			if(newHeight > windowHeight)
			{
				ratio = originalWidth / originalHeight;
				newHeight = windowHeight;
				newWidth = newHeight * ratio;
			}
		}
		else
		{
			ratio = originalWidth / originalHeight;
			newHeight = windowHeight;
			newWidth = newHeight * ratio;
			
			if(newWidth > windowWidth)
			{
				ratio = originalHeight / originalWidth;
				newWidth = windowWidth;
				newHeight = newWidth * ratio;
			}
		}
		
		//max width / height
		if(newWidth > originalWidth || newHeight > originalHeight)
		{
			newWidth = originalWidth;
			newHeight = originalHeight;
		}
		
		//min width / height
		if(newWidth < 200 || newHeight < 200)
		{
			if(originalWidth < originalHeight)
			{
				ratio = originalHeight / originalWidth;
				newWidth = 200;
				newHeight = newWidth * ratio;
			}
			else
			{
				ratio = originalWidth / originalHeight;
				newHeight = 200;
				newWidth = newHeight * ratio;
			}
		}
		
		return { height: Math.round(newHeight), width: Math.round(newWidth) };
	}
	
	function showInfo(event)
	{
		$('#smartbox .info').animate({bottom: '-1px'}, fadeSpeed);
	}
	function hideInfo(event)
	{
		var infoHeight = $('#smartbox .info').outerHeight();
		$('#smartbox .info').animate({bottom: -infoHeight + 'px'}, fadeSpeed);
	}
	
	function showPagination()
	{
		if(!isAnimating)
		{
			if($(this).hasClass('next'))
			{
				if(galleryIndex < galleryCount)
				{
					$(this).stop().animate({opacity: 1});
				}
			}
			else if($(this).hasClass('previous'))
			{
				if(galleryIndex > 0)
				{
					$(this).stop().animate({opacity: 1});
				}
			}
		}
	}	
	function hidePagination()
	{
		$(this).stop().animate({opacity: 0});
	}
	
	function keyboardListener(event)
	{
		if (event.which == 27)
		{
			closeSmartbox(event);
		}
	}
	
	function closeSmartbox(event)
	{	
		$('#smartbox').stop().animate({opacity: 0}, fadeSpeed, function() {
			$(this).remove();
		});
		$('#smartbox_cover').stop().animate({opacity: 0}, fadeSpeed, function() {
			$(this).remove();
			isAnimating = false;
		});
		imageLoaded = false;
		
		galleryName = null;
		galleryActive = false;
		galleryArray = [];
		galleryCount = 0;
		galleryIndex = null;
		
		$(window).unbind('resize', resizeWindow);
		
		return false;
	}
	
	function centerSmartbox()
	{
		var size;
		var padding = parseInt($('#smartbox').css('paddingLeft'), 10) * 2;
		
		if(imageLoaded)
		{
			size = resizeImage();
		}
		else
		{
			size = {height: 200, width: 200 };
		}
		
		var scrollOffset = $(window).scrollTop();
		var offsetLeft = Math.round(($(window).width() - size.width - padding) / 2);
		var offsetTop = Math.round(($(window).height() - size.height - padding) / 2) + scrollOffset;
		
		$('#smartbox').css({left: offsetLeft + 'px', height: size.height + 'px', top: offsetTop + 'px', width: size.width + 'px'});
	}
	
	function resizeWindow()
	{
		centerSmartbox();
	}
	
})(jQuery);
