/*
	Javascript 'Images to Figures'
	Written by Jurriaan van Hengel (February 2009)
	
	This script converts all 'img' to a '.figure > img + .caption'
*/

function images2figures(imgs){

	// for each image in imgs
	for (var i = 0; i < imgs.length; i++) {

		//only if img has no float
		//if((imgs[i].style 'styleFloat' : 'cssFloat') == ''){
		if(imgs[i].style.cssFloat != 'undefined' && imgs[i].style.cssFloat == ''
				|| imgs[i].style.styleFloat != 'undefined' && imgs[i].style.styleFloat == ''){

			//create a div.figure
			var fig = document.createElement('div');

			fig.setAttribute((document.all ? 'className' : 'class'),'figure');

			//replace image with div.figure, and add removed image to div.figure
			fig.appendChild(
				imgs[i].parentNode.replaceChild(fig,imgs[i])
			);
			
			//if alt text is not empty
			if(imgs[i].alt != ""){
			
				//create a p.caption
				var cap = document.createElement('p');
				cap.setAttribute((document.all ? 'className' : 'class'), 'caption');
				
				//create em.title (e.g. 'Figure 1')
				var ttl = document.createElement('em');
				ttl.setAttribute((document.all ? 'className' : 'class'), 'title');
				ttl.appendChild(
					document.createTextNode('Figure '+(i+1))	
				);
				cap.appendChild(ttl);
						
				//add the alt text as caption text
				cap.appendChild(
					document.createTextNode(imgs[i].alt)	
				);

				//add caption to figure
				fig.appendChild(cap);
			
			} //end if alt
		} //end if float
	} //end for
}

addLoadEvent(function(){
	//check if required functions work
  if( document.getElementById && 
  	document.getElementsByTagName ){
  	//convert all images to figures, within #content
		images2figures(
			document.getElementById('content').getElementsByTagName('img')
		);
	}
});
