/*!
* @file
* @brief    sigplus Image Gallery Plus image captions overlay engine with MooTools
* @author   Levente Hunyadi
* @version  1.2.6
* @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 imageCaptions(el, settings) {
	// default configuration properties
	var defaults = {
		showDownload: false,
		showMetadata: false
	};
	settings = $extend(defaults, settings);

	/**
	* Computed size of element without padding and border width.
	*/
	Element.extend({
		getInnerSize: function () {
			var size = this.getSize().size;
			var w = size.x
					- this.getStyle('padding-left').toInt() - this.getStyle('padding-right').toInt()
					- this.getStyle('border-left-width').toInt() - this.getStyle('border-right-width').toInt();
			var h = size.y
					- this.getStyle('padding-top').toInt() - this.getStyle('padding-bottom').toInt()
					- this.getStyle('border-top-width').toInt() - this.getStyle('border-bottom-width').toInt();
			return {
				width: w > 0 ? w : 0,
				height: h > 0 ? h : 0
			};
		},
		getOuterSize: function () {
			var size = this.getSize().size;
			return {
				width: size.x + this.getStyle('margin-left').toInt() + this.getStyle('margin-right').toInt(),  // width with padding and border
				height: size.y + this.getStyle('margin-top').toInt() + this.getStyle('margin-bottom').toInt()  // height with padding and border
			};
		}
	});

	var images = el.getElements("ul li img");
	images.each( function(image) {
		var imgtext = image.getProperty('alt');  // image caption if any
		var imageoptions = document.getElementById(image.getProperty('id')+'_options');

		if (!imgtext && !(imageoptions && (settings.showDownload || settings.showMetadata))) {
			return;
		}
		var imgsize = Element.getInnerSize(image);  // image size without margin, border or padding width
		var imgoutersize = Element.getOuterSize(image);
		var container = new Element('div', {
			'class': 'imageCaptionContainer',
			'styles': {
				width: imgoutersize.width,
				height: imgoutersize.height
			}
		});
		var parent = image.getParent();
		if (parent.getTag() == 'a') {
			container.injectBefore(parent);
			container.adopt(parent);
		} else {
			container.injectBefore(image);
			container.adopt(image);
		}
		var caption = new Element('div', {
			'class': 'imageCaption imageCaptionHidden',
			'styles': {
				width: imgsize.width,
				marginLeft: image.getStyle('margin-left').toInt() + image.getStyle('border-left-width').toInt() + image.getStyle('padding-left').toInt(),
				marginRight: image.getStyle('margin-right').toInt() + image.getStyle('border-right-width').toInt() + image.getStyle('padding-right').toInt(),
				//marginTop: image.getStyle('margin-top').toInt() + image.getStyle('border-top-width').toInt() + image.getStyle('padding-top').toInt(),
				marginBottom: image.getStyle('margin-bottom').toInt() + image.getStyle('border-bottom-width').toInt() + image.getStyle('padding-bottom').toInt()
			}
		});
		caption.setHTML(imgtext);
		
		if (imageoptions && (settings.showDownload || settings.showMetadata)) {
			imageoptions = new Element(imageoptions);
			var buttons = new Element('div', {
				'class': 'buttons'
			});
			if (settings.showDownload) {
				var downloadoption = imageoptions.getElement('a.download');
				if (downloadoption) {
					downloadoption.injectInside(buttons);
				}
			}
			if (settings.showMetadata) {
				var metadataoption = imageoptions.getElement('a.metadata');
				if (metadataoption) {
					sigplusMetadataDialog(metadataoption);
					metadataoption.injectInside(buttons);
				}
			}
			buttons.injectInside(caption);
		}
		
		caption.injectInside(container);
		container.addEvent('mouseenter', function(event) {
			caption.removeClass('imageCaptionHidden');
		});
		container.addEvent('mouseleave', function(event) {
			caption.addClass('imageCaptionHidden');
		});
	});
}

