/*
	GalleryPreview v0.1 - rough jQuery plugin for the James Boags Pure gallery, depends on ColorBox
	(c) 2009 Holler Sydney - www.hollersydney.com.au - matt.stone@hollersydney.com.au
	Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
*/

(function($) {
	
	var galleryPublic, 
	pt,
	t;
	
	function setPreviewTimeout()
	{
		pt = setTimeout(function() {
			clearPreviewTimeout();
			$('#preview').fadeOut('fast', function() {
				$(this).remove();
			});
		}, 250);
	}
	
	function clearPreviewTimeout()
	{
		clearTimeout(pt);
	}
	
	galleryPublic = $.fn.galleryPreview = function()
	{
		return this.each(function() {
			var $$ = $(this);
			
			$$.bind('mouseover', function(e) {
				var thumbnailImage = $$.find('img');
				var mediumSize = $$.attr('class').split('x');
				
				if ($('#preview').length > 0) {
					galleryPublic.removePreviewContainer();
				}
				
				t = $$.attr('title');
				$$.attr('title', '');
				
				var caption = (t != "") ? t.substr(0, t.indexOf(' | ')) : "";
				var dateTime = t.substr(t.indexOf(' | ')+3, t.length);
				var largeImage = '/gallery/large/'+ $$.attr('id') +'.jpg';
				
				// lovely div soup for dropshadows
				if (!isUnderIE8()) {
					var previewHTML = "<div id='preview'>\n";
					previewHTML += "<div class='top-shadow'><span></span></div>\n";
					previewHTML += "<div class='left-shadow'>\n";
					previewHTML += "<div class='left-top-shadow'></div>\n";
					previewHTML += "<div class='right-shadow'>\n";
					previewHTML += "<div class='main'>\n";
					previewHTML += "<span class='image'><img src='" + $$.attr('href') + "' alt='" + caption + "' /></span>\n";
					previewHTML += "<div class='meta-data'>\n";
					previewHTML += "<span class='caption'>" + caption + "</span>\n";
					previewHTML += "<span class='date'>" + dateTime + "</span>\n";
					previewHTML += "<a href='" + largeImage + "' class='view-large' rel='modal'>View Large</a>\n";
					previewHTML += "</div>\n";
					previewHTML += "</div>\n";
					previewHTML += "<div class='right-top-shadow'></div>\n";
					previewHTML += "</div>\n";
					previewHTML += "</div>\n";
					previewHTML += "<div class='bottom-shadow'><span></span></div>\n";
					previewHTML += "</div>\n";
				} else {
					var previewHTML = "<div id='preview'>\n";
					previewHTML += "<div class='ieMain'>\n";
					previewHTML += "<span class='image'><img src='" + $$.attr('href') + "' alt='" + caption + "' /></span>\n";
					previewHTML += "<div class='meta-data'>\n";
					previewHTML += "<div class='caption'>" + caption + "</div>\n";
					previewHTML += "<span class='date'>" + dateTime + "</span>\n";
					previewHTML += "<a href='" + largeImage + "' class='view-large' rel='modal'>View Large</a>\n";
					previewHTML += "</div>\n";
					previewHTML += "</div>\n";
					previewHTML += "</div>\n";
				}
				
				var xOffset = thumbnailImage.offset().left - (mediumSize[0] / 4) + 10; // x offset from container - (width of the image / 4) + 10px for padding
				var yOffset = thumbnailImage.offset().top - ((mediumSize[1] / 4) + 10) - 35; // y offset from container - ((height of the image / 4) + 10px for padding) - 35px for caption
				
				$(previewHTML)
					.appendTo("body")
					.css({
						"z-index":	'2000',
						"top":		yOffset + "px",
						"left":		xOffset + "px"
					})
					.fadeIn("fast")
					.hover(function() {
						clearPreviewTimeout();
					}, function() {
						setPreviewTimeout();
					});
				
				// TODO: detect if colorbox is available
				$("a[rel='modal']").colorbox({transition:"fade"}); // attach the colorbox modal for the large photo
			}).bind('mouseout', function(e) {
				$$.attr('title', t);
				setPreviewTimeout();
			}).click(function() {
				return false;
			});
		});
	};
	
	$.fn.disableGalleryPreview = function()
	{
		if ($('#preview').length > 0) {
			galleryPublic.removePreviewContainer();
		}
		
		return this.each(function() {
			var $$ = $(this);
			
			$$.unbind('mouseover').unbind('mouseout');
		});		
	};
	
	galleryPublic.removePreviewContainer = function ()
	{
		clearPreviewTimeout();
		$('#preview').remove();
	}
})(jQuery);