jQuery(document).ready(function() { 
	// get all image elements with class "credit"
	jQuery('.credit').each( function() {

		// make sure they have caption text set
		if( this.title != '') {
			// save some stuff for later use
			var oParent = this.parentNode;
			var oSubParent = this;
			// if the parent is a link, IE doesn't get it, so we have to keep the a tag with the img
			if( oParent.tagName == "A" ) {
				oSubParent = oParent;
				oParent = oParent.parentNode;
			}
			var sTitle = this.title;
			var sClassname = this.className;
			
			// clear title and className properties
			this.title = '';
			this.className = '';
			
			// create new parent container for image
			var oContainer = document.createElement('div');
			
			// assign classes from img tag
			oContainer.className = sClassname;
			
			// constrain the width and height of DIV to image dimensions
			oContainer.style.width = this.width + 'px';
			oContainer.style.height = this.height + 'px';
			
			// swap existing image
			oParent.replaceChild(oContainer, oSubParent);
			oContainer.appendChild(oSubParent);
			
			// create credit
			var oCaptionContainer = document.createElement('div');
			var oCaption = document.createElement('p');
			oCaption.appendChild(document.createTextNode(sTitle));
			oCaptionContainer.appendChild(oCaption);
			
			// append strapline to parent container
			oContainer.appendChild(oCaptionContainer);
			
			oCaptionContainer.style.width = this.width + 'px';
			
			var iImageWidth = this.width;
			oCaptionContainer.style.width = iImageWidth + 'px';
			if (oCaptionContainer.offsetWidth > iImageWidth) {
				oCaptionContainer.style.width = iImageWidth - (oCaptionContainer.offsetWidth - iImageWidth) + 'px';
			} 
		}
	});
});
