/*!
* @file
* @brief    sigplus Image Gallery Plus image captions overlay engine with jQuery
* @author   Levente Hunyadi
* @version  1.2
* @remarks  Copyright (C) 2009-2010 Levente Hunyadi
* @remarks  Licensed under GNU/GPLv3, see http://www.gnu.org/licenses/gpl-3.0.html
* @see      http://hunyadi.info.hu/projects/sigplus
*/

/*
* sigplus Image Gallery Plus plug-in for Joomla
* Copyright 2009-2010 Levente Hunyadi
*
* sigplus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sigplus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

(function ($) {
	function asinteger(value) {
		value = value.toInt();
		if (isNaN(value)) {
			return 0;
		} else {
			return value;
		}
	}

	$.fn.imageCaptions = function(settings) {
		// default configuration properties
		var defaults = {
			showDownload: false,
			showMetadata: false
		};
		settings = $.extend(defaults, settings);
	
		var images = $("ul>li img:visible", this);
		images.each( function() {
			var image = $(this);
			var imgtext = image.attr('alt');  // image caption if any
			var imageoptions = $('div#'+image.attr('id')+'_options');
			
			if (!imgtext && !(imageoptions && (settings.showDownload || settings.showMetadata))) {
				return;
			}
			var containerhtml = '<div class="imageCaptionContainer"></div>';
			var parent = image.parent('a');
			if (parent) {
				parent.wrap(containerhtml);
			} else {
				image.wrap(containerhtml);
			}
			var container = image.parents('div.imageCaptionContainer:first');
			container.css({
				width: image.outerWidth(true),
				height: image.outerHeight(true)
			});
			var caption = $('<div class="imageCaption"></div>');
			caption.css({
				display: 'none',
				width: image.css('width').toInt(),  // image.width() returns invalid value with IE
				marginLeft: image.css('margin-left').toInt() + asinteger(image.css('border-left-width')) + image.css('padding-left').toInt(),
				marginRight: image.css('margin-right').toInt() + asinteger(image.css('border-right-width')) + image.css('padding-right').toInt(),
				marginTop: image.css('margin-top').toInt() + asinteger(image.css('border-top-width')) + image.css('padding-top').toInt(),
				marginBottom: image.css('margin-bottom').toInt() + asinteger(image.css('border-bottom-width')) + image.css('padding-bottom').toInt()
			});
			caption.html(imgtext);

			if (imageoptions && (settings.showDownload || settings.showMetadata)) {
				var buttons = $('<div class="buttons"></div>');
				if (settings.showDownload) {
					var downloadoption = $('a.download', imageoptions);
					if (downloadoption) {
						buttons.append(downloadoption);
					}
				}
				if (settings.showMetadata) {
					var metadataoption = $('a.metadata', imageoptions);
					if (metadataoption) {
						sigplusMetadataDialog(metadataoption);
						buttons.append(metadataoption);
					}
				}
				caption.append(buttons);
			}

			container.append(caption);
			container.mouseenter(function() {
				caption.css('display','block');
			});
			container.mouseleave(function() {
				caption.css('display','none');
			});
		});
	}
})(jQuery);