// Funciones para poner las citas de los blockquotes en la parte inferior
// http://www.sitepoint.com/article/structural-markup-javascript

function addEvent(obj, evType, fn){
 if (obj.addEventListener){
   obj.addEventListener(evType, fn, false);
   return true;
 } else if (obj.attachEvent){
   var r = obj.attachEvent("on"+evType, fn);
   return r;
 } else {
   return false;
 }
}

function extractBlockquoteCitations() {
 var quotes = document.getElementsByTagName('blockquote');
 for (var i = 0; i < quotes.length; i++) {
   var cite = quotes[i].getAttribute('cite');
   if ((cite != '') && (cite != null)) {
     var a = document.createElement('a');
     a.setAttribute('href', cite);
     a.setAttribute('title', cite);
     a.appendChild(document.createTextNode(cite));
     var p = document.createElement('p');
     p.className = 'blockquotesource';
     p.appendChild(a);
     quotes[i].appendChild(p);
   }
 }
}

// Funciones para convertir los title de ciertas imágenes en pies de foto
// http://www.boagworld.com/archives/2006/07/styled_images_with_caption.html

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

function addCaption(xClass) {
	var allImages = getElementsByClassName(document, "img", xClass);
	for ( var i=0; i < allImages.length; i++) {
		var imageCaption = document.createTextNode(allImages[i].title);
		var imageContainer = document.createElement("div");
		var imagePara = document.createElement("p");
		var imageWidth = allImages[i].getAttribute("width");
		var spareSpan = document.createElement("span"); //This adds an extra span. Useful for curved corners
		imagePara.appendChild(imageCaption);
		allImages[i].parentNode.insertBefore(imageContainer, allImages[i]);
		imageContainer.appendChild(allImages[i]);
		if ( allImages[i].title != "" ) {
		imageContainer.appendChild(imagePara); 
		}
		imageContainer.appendChild(spareSpan);		
		imageContainer.className = xClass
		spareSpan.className = "spareSpan"
		allImages[i].className = "imgCaption"
		imageContainer.style.width = imageWidth + "px";
		
    }
}

function iniciar() {
	extractBlockquoteCitations();
	addCaption("conpie");
}

addEvent(window, 'load', iniciar);