/**
 * Fixes standard Joomla 1.5 captions if image is inside an a tag (in IE the link won't work anymore
 * We check if the parent of the caption element is an a tag, if so we remove the a tag and set it around the image inside the caption div
 * We override JCaption class with our fixed implementation
 */

var JCaption = new Class({
	initialize: function(selector)
	{
		this.selector = selector;

		var images = $$(selector);
		images.each(function(image){ this.createCaption(image); }, this);
	},

	createCaption: function(element)
	{
		var container = new Element('div');
        var caption   = element.getProperty('title');
		var align     = element.getProperty('align');
        var width     = element.getStyle('width');

        element.setStyle('display', 'block');
		container.injectBefore(element);
		element.injectInside(container);

        container.className = this.selector.replace('.', '_');
        container.className = container.className + " " + align;
		
        container.setStyle('width', width);
		if(align == 'left' || align == 'right') {
			container.setStyle('float', align);
	        container.setStyle('display', 'inline');
        } else {
	       container.setStyle('display', 'block');
	    }

		if ( caption != '' ) {
            var text = new Element('p');
            text.setStyle('clear', 'both');
            text.setText(caption);
			text.injectInside(container);
		}



		
/*
		if(container.parentNode.tagName =='A') {
			var a = container.parentNode;
			var img = element;
			container.injectAfter(a);
			a.injectBefore(img);
			a.adopt(img);
		}
*/
	}
});
