/*
    Disclaimer

    While we make every effort to ensure that this code is fit for its intended
    purpose, we make no guarantees as to its functionality. CoreTrek AS will
    accept no responsibility for the loss of data or any other damage or
    financial loss caused by use of this code.


    Copyright

    This programming code is copyright of CoreTrek AS. Permission to run this
    code is given to approved users of CoreTrek's publishing system CorePublish.

    This source code may not be copied, modified or otherwise repurposed for use
    by a third party without the written permission of CoreTrek AS.

    Contact webmaster@coretrek.com for information.

    ============================================================================
    IMPORTANT! This javascript is dependent on Prototype.
    ============================================================================
    

 
	<div class="caption" id="53527" style="width: px; float: right">
		<img src="/naringspolitikk/aktuelt/?53527.jpg&langid=1" height="128" width="100" vspace="4" hspace="4" border="" alt="Torny Pedersen.jpg"/>
		<div class="captioncell" style="text-align: left;">
			Torny Pedersen (Ap)
		</div>
	</div>

*/

var ImgCaption = Class.create({

	initialize: function() {
		objBody = document.getElementsByTagName('body').item(0);
		var i = 0;
		 $$(".full-articledisplay .tile-content img").each(function(element, i) {
			if(element.alt != "") {
		 	if((element.readAttribute('usemap') == null) || (element.readAttribute('usemap') == "")) {
		 	
		 	var captionObj = document.createElement('div');
		 	var captionObjText = document.createElement('div');
		 	
		 	var newImg = document.createElement('img');
		 	var link = document.createElement('a');
		 	var myparent = element.parentNode || element.parent;
		 	this.element = element;
		 	if(myparent.nodeName == "A") {
		 		this.element = myparent;
		 		
		 		link.href = myparent.href;
		 		link.title = myparent.nodeName;
                                link.target = myparent.target;
		 		/*link.setAttribute('onclick', myparent.readAttribute('onclick'));*/
		 		link.onclick = myparent.onclick;
		 		link.appendChild(newImg);
		 		captionObj.appendChild(link);
		 		myparent = myparent.parentNode || myparent.parent;
		 		
		 	} else { 
		 		captionObj.appendChild(newImg);
		 	};
		 	newImg.src = element.src;
		 	newImg.height = element.height;
		 	newImg.width = element.width;
		 	newImg.alt = element.alt;
		    newImg.title = element.alt;
		    newImg.id = element.id;
		    
		    var marginV = '0';
		    if (element.vspace != -1) {
		    marginV = element.vspace;	
		    }
		    var marginH = '0';
		    if (element.hspace != -1) {
		    marginH = element.hspace;
		    }
		    if ((marginH == '0') && (marginV == '0')) {
				$(captionObj).setStyle( {'float': element.align, width: element.width+'px'});
		    } else {
		    	$(captionObj).setStyle( {'float': element.align, width: element.width+'px',margin: marginV+'px '+marginH+'px' });
		    }
		    
		 	captionObj.setAttribute('id','caption'+i);
		 	captionObjText.setAttribute('id','captionText'+i);
			$(captionObj).toggleClassName('caption'+element.align);
			$(captionObjText).toggleClassName('captionText');
		 	
			captionObjText.innerHTML = element.getAttribute('alt');
			//captionObjText.innerHTML = 
			
			captionObj.appendChild(captionObjText);
			
			Element.replace(this.element,$(captionObj));
			i= i+1;
		 }}
		});	
	}
});

var caption;
document.observe('dom:loaded', function(event) {
	caption = new ImgCaption();
});


