var oImageCaptions = {
   	init: function() {
   		var aImages = YAHOO.util.Dom.getElementsByClassName('caption_img_left', 'img');
   		aImages = aImages.concat(YAHOO.util.Dom.getElementsByClassName('caption_img_right', 'img'));
   		for (var i = 0, j = aImages.length; i < j; i++) {
   		   	if (aImages[i].title != '') {
   		   		oAncestorA=YAHOO.util.Dom.getAncestorByTagName(aImages[i],'a');   		   		   		   		
   		   		var oParent = aImages[i].parentNode;
				var sTitle = aImages[i].title;
				var sClasses = aImages[i].className;
				aImages[i].className = '';				
   		   		if (oAncestorA !=null) {
   		   			oParent=oAncestorA.parentNode;
					var oContainer = document.createElement('div');
					oContainer.className = sClasses;
					oContainer.style.width = aImages[i].getAttribute('width') + 'px';					
					oParent.replaceChild(oContainer,oAncestorA);					
					oContainer.appendChild(oAncestorA);					
					var oCaption = document.createElement('p');
					var oSecondA = document.createElement('a');
					oSecondA.appendChild(document.createTextNode(sTitle));
					oCaption.appendChild(oSecondA);					
					oSecondA.href=oAncestorA.href;					
					oContainer.appendChild(oCaption);					
				} else {				
					var oContainer = document.createElement('div');
					oContainer.className = sClasses;
					oContainer.style.width = aImages[i].getAttribute('width') + 'px';
					//oContainer.style.height = aImages[i].getAttribute('height') + 'px';					
					oParent.replaceChild(oContainer, aImages[i]);					
					oContainer.appendChild(aImages[i]);					
					var oCaption = document.createElement('p');
					oCaption.appendChild(document.createTextNode(sTitle));					
					oContainer.appendChild(oCaption);					
					var iImageWidth = aImages[i].getAttribute('width');		
					oCaption.style.width = iImageWidth + 'px';
					if (oCaption.offsetWidth > iImageWidth) {
						oCaption.style.width = iImageWidth - (oCaption.offsetWidth - iImageWidth) + 'px';
					}
				}
			}
		}
   	}
}

YAHOO.util.Event.addListener(window, 'load', oImageCaptions.init);

