var firstRun = 0;
var flashTextCounter = 0;

function getStyle(el,styleProp){
  var x = document.getElementById(el);
	if (x.currentStyle){
		var y = x.currentStyle[styleProp];
	}else if (window.getComputedStyle){
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	}
	return y;
}
function Dec2Hex(Dec){
  var hexChars = "0123456789ABCDEF";
  var a = Dec % 16;
  var b = (Dec - a)/16;
  hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
  return hex;
}
function createFlashText(color,object,align){
  if(typeof sIFR == "function"){
    sIFR.replaceElement(object, named({sFlashSrc: "swf/josschrift-center.swf", sColor: color, sLinkColor:"#8e1d1d", sHoverColor:"#909f80", sFlashVars:"textalign="+align, sWmode: "transparent"}));
  };
}
function replaceHeaderWithFlash(tag){
	if(tag == "dt"){
		var h = document.getElementById("nav").getElementsByTagName(tag);
		var align = "center";
	}else{
		var h = document.getElementsByTagName(tag);
		var align = "left";
	}
	for(var i=h.length-1;i>=0;i--){
		// Get css-parameters
		var headerText = h[i].innerHTML;
		h[i].setAttribute("id","flashText"+flashTextCounter);
		var cssColor = getStyle("flashText"+flashTextCounter,"color");

		if(cssColor.substring(0,1) != "#"){
			var pattern = /\)| |rgb\(/gi;
			var hextemp = cssColor.replace(pattern, "").split(",");
			cssColor = Dec2Hex(hextemp[0]) + Dec2Hex(hextemp[1]) + Dec2Hex(hextemp[2]);
		}else{
			cssColor = cssColor.replace("#","");
		}
		if(cssColor.length != 6){
			cssColor = cssColor.substring(0,1) + cssColor.substring(0,1) +
			cssColor.substring(1,2) + cssColor.substring(1,2) +
			cssColor.substring(2,3) + cssColor.substring(2,3);
		}

		createFlashText(cssColor,tag+"#flashText"+flashTextCounter, align);
		flashTextCounter++;
	}
}

function initUnobtrusiveJs(){
    if(firstRun != 0)return;
    firstRun++;
    //replaceHeaderWithFlash("dt");
    replaceHeaderWithFlash("h1");
    replaceHeaderWithFlash("h2");
    //sIFR.replaceElement("p#test", named({sFlashSrc: "swf/josschrift-center.swf", sColor: "#8e1d1d", sWmode: "transparent"}));
    //sIFR.replaceElement("p#test2", named({sFlashSrc: "swf/josschrift-center.swf", sColor: "#8e1d1d", sWmode: "transparent"}));
}


if(document.addEventListener){
    document.addEventListener("DOMContentLoaded", initUnobtrusiveJs, false);
}
window.onload = initUnobtrusiveJs;

