window.onload = function(){
	addLinksToImages();
}

function addLinksToImages(){

	//search for textpics with image and link and wrap the link around the image
	var divs = document.getElementsByTagName('div');
	//alert('here');
	for(var index = 0; index < divs.length; index ++){
		
		//make sure it's a textpic
		if(
		//msie6 compatible class detection with .className	
		((divs[index].getAttribute('class') && divs[index].getAttribute('class').indexOf('textpic') != -1) ||  (divs[index].className && divs[index].className.indexOf('textpic') != -1))
		
		//make sure there's an anchor
		&& divs[index].getElementsByTagName('a')
		&& divs[index].getElementsByTagName('a').length > 0
		&& divs[index].getElementsByTagName('a')[0].getAttribute('href')
		
		//make sure there's an image
		&& divs[index].getElementsByTagName('img')
		&& divs[index].getElementsByTagName('img').length > 0
		
		//make sure the image doesn't already have a link
		&& divs[index].getElementsByTagName('img')[0].parentNode.nodeName.toLowerCase() != 'a'
		){
			wrapLinkAroundImage(divs[index].getElementsByTagName('img')[0],divs[index].getElementsByTagName('a')[0].getAttribute('href'))
		}
	}
}

function wrapLinkAroundImage(imageNode,href){
     
	//create replacement
	var wrapper = document.createElement('a');
	var attribute = document.createAttribute('href');
	attribute.nodeValue = href;
	wrapper.setAttributeNode(attribute);
	
	//copy image
	var picture = document.createElement('img');
	var attribute = document.createAttribute('src');
	attribute.nodeValue = imageNode.getAttribute('src');
	picture.setAttributeNode(attribute);
	wrapper.appendChild(picture);

	//insert replacement and delete original
	var parent = imageNode.parentNode;
	parent.insertBefore(wrapper,imageNode);
	parent.removeChild(imageNode);
	
}