/*
 * jQuery SmartBox Plugin v.3
 * http://benjaminplum.com/
 *
 * Copyright (c) 2009 Ben Plum under MIT License
 *
 * Date: 11-7-09
 * Revision: 6 (last: 12-08-09)
 *
 * USE:	$('# WRAPPER_DIV_ID ').smartBox();
 */
 
 
(function($){
	var fadeSpeed = 350;
	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(){
		$(this).live('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.8}, fadeSpeed).click(closeSmartbox);
		
		var html = '<div id="smartbox" style="opacity: 0; height: 200px; width: 200px;">';
		html += '<div class="inner">';
		html += '<div class="info">';
		html += '<p class="caption">';
		html += caption;
		html += '</p>';
		if(galleryActive)
		{
			html += '<div class="navigation">';
			html += '<a href="#" class="previous">Previous</a>';
			html += '<span class="current">' + (galleryIndex + 1) + '</span> of <span class="total">' + (galleryCount + 1) + '</span>';
			html += '<a href="#" class="next">Next</a>';
			html += '</div>';
		}
		html += '<a href="#" class="close">Close</a>';
		html += '</div>';
		html += '</div>';
		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);
		
		if(galleryActive)
		{
			$('#smartbox a').not('.close').click(galleryClick);
		}
		
		centerSmartbox();
		loadImage(imgSrc);
		$('#smartbox').animate({opacity: 1}, fadeSpeed);
		
		$(window).bind('resize', resizeWindow);
		
		return 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 offsetLeft = ($(window).width() - newWidth - padding) / 2;
			var offsetTop = ($(window).height() - newHeight - padding) / 2;
				
			$('#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);
				isAnimating = false;
			});
		});
		$(newImage).attr('src', imageSrc).attr('class', 'smartbox_image').css({opacity: 0});
	}
	
	
// GALLERY FUNCTIONS --------------------

	function loadGallery()
	{
		$('#smartbox .inner img').animate({opacity: 0}, fadeSpeed, function() {
			$(this).remove();
			loadImage(galleryArray[galleryIndex].attr('href'));
			$('#smartbox .caption').html(galleryArray[galleryIndex].attr('title'));
			$('#smartbox .navigation .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 keyboardListener(event)
	{
		if (event.which == 27)
		{
			closeSmartbox(event);
		}
	}
	
	function closeSmartbox(event)
	{
		$('#smartbox').animate({opacity: 0}, fadeSpeed, function() {
			$(this).remove();
		});
		$('#smartbox_cover').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 offsetLeft = Math.round(($(window).width() - size.width - padding) / 2);
		var offsetTop = Math.round(($(window).height() - size.height - padding) / 2);
		
		$('#smartbox').css({left: offsetLeft + 'px', height: size.height + 'px', top: offsetTop + 'px', width: size.width + 'px'});
	}
	
	function resizeWindow()
	{
		centerSmartbox();
		/*
if(resizeTimer)
		{
			clearTimeout(resizeTimer);
		}
		resizeTimer = setTimeout(centerSmartbox, 5);
*/
	}
	
})(jQuery);
