// Handle multiple window onloads

window.addOnload = function (fn) {
    if (!window.OnloadCache) window.OnloadCache = [];
    var ol = window.OnloadCache;
    ol.push(fn);
}

window.onload = function () {
    var ol = window.OnloadCache;
    if (ol)
        for (var x = 0; x < ol.length; x++)
            ol[x]();
}

// path to floris cache folder
// var path = "http://bang.jungle-world.com/gfx/floris";
var path = "/gfx/floris";

var elements = new Array("span");

// SIIR - Element innerhtml

function SIIR_normalizeWhiteSpace(txt)
{
	var rE = /\s+/gi;
	return txt.replace(rE,' ');
}

function SIIR_get_href(txt)
{
	var text = txt;
	var thishref = text.replace(/<a(.*)href=\"/gi,'');
	thishref = thishref.replace(/\">(.*)/gi,'');
	return thishref;
}

function SIIR_get_id(txt)
{
	var text=txt;
	var thisid = text.replace(/<a(.*)id=\"/gi,'');
	thisid = thisid.replace(/\">(.*)/gi,'');
	return thisid;
}

function SIIR_get_text(txt)
{
	var text = txt;
	text = text.replace(/<\/.*?>/gi,'');
	text = text.replace(/<.*?>/gi,'');
	return text;
}

// SIIR - Replace the element with an iframe

function SIIR_replace()
{
	var d = document;

	for (var a = (elements.length-1); a >= 0; a--) {

		var current_element = elements[a];
		var element = d.getElementsByTagName(current_element);

		// Loop through each <element>
		for (var i = (element.length-1); i >= 0; i--)
		{
			contain = element[i];
			if ((contain.className)&&(contain.className.indexOf("floris_") == 0))
			{
				var floris_class = contain.className;
				var floris_parts = floris_class.split("_");
				
				var imageURL = path + "/" 
											 + floris_parts[1] + "/" 
											 + floris_parts[2] + "/" 
											 + floris_parts[3] + "/"
											 + floris_parts[4] + "." + floris_parts[5];
				
				// alert(imageURL);
				
				// Container to work with
				var c = d.createElement(current_element);
				// Class to work with
				var eClass = contain.className;
				// set class to floris + floris classname
				c.className = "floris_" + floris_parts[1];
				// Swap <element> tag with the container
				contain.parentNode.replaceChild(c,contain);
				// Set the "current_element.Text" variable
				var cText = SIIR_normalizeWhiteSpace(contain.innerHTML);

				var thisid = '';
				reg = /id=/i;
				if (reg.test(cText)){
					var thisid = SIIR_get_id(cText);
				}

				var href = '';
				reg = /href=/i;
				if (reg.test(cText)) {
					var href = SIIR_get_href(cText);
				}

				cText = SIIR_get_text(cText);
			
				alt = ''+(unescape(cText))+''
			
				idname = ''+current_element+'_'+i+'';

				if (href != '' || thisid != '') {
					var pastelink = document.createElement("a");
					if (href != '') {
						pastelink.setAttribute("href", href);
					}
					if (thisid != '') {
						pastelink.setAttribute("id", thisid);
					}
					c.appendChild(pastelink);
				}

				var pasteHTML = document.createElement("img");
				pasteHTML.setAttribute("class", "floris");
				pasteHTML.setAttribute("src", imageURL);
				pasteHTML.setAttribute("alt", alt);

				if (href != '' || thisid != '') {
					pastelink.appendChild(pasteHTML);
				} else {
					c.appendChild(pasteHTML);
				}
			}
		}
	}
}

// SIIR - Onload call

function SIIR_init() {
	SIIR_replace();
} 

window.addOnload(SIIR_init);
