
// Replace HTML element(s) text with FlashText
// ****** requires FlashObject.js ******

var FlashTEXTs = 0;

function FlashTextHeadlines() {
	FlashTextHeadline("H1");
	FlashTextHeadline("H2");
}

function FlashTextHeadline(tag) {
	var Harr = document.getElementsByTagName(tag);
	for(var i=0; i<Harr.length; i++) {
		var st = Harr[i].innerHTML.toString();
		st = st.replace('/',"%2F");
     	st = st.replace('?',"%3F");
     	st = st.replace('=',"%3D");
     	st = st.replace('&amp;',"%26");
     	st = st.replace('@',"%40");
     	
     	var objWidth = readStyle(Harr[i], 'width');
     	var objHeight = readStyle(Harr[i], 'height');
     	
     	if(objWidth == 'auto') {
     		objWidth = Harr[i].offsetWidth;
     	} else {
     		objWidth = objWidth.replace('px',"");
     	}
     	if(objHeight == 'auto') {
     		objHeight = Harr[i].offsetHeight;
     	} else {
     		objHeight = objHeight.replace('px',"");
     	}
     	objHeight = Number(objHeight) + 2;
     	
     	var TextColor = readStyle(Harr[i], 'color');
     	
     	try {
     		var TextSize = readStyle(Harr[i], 'font-size');
     		TextSize = TextSize.replace('px',"");
     	} catch(e) {
     		var TextSize = Harr[i].currentStyle.fontSize;
     		TextSize = TextSize.replace('px',"");
     	}

     	var color = new FText_RGBColor(TextColor);
		if (color.ok) {
    		drawFlashHeadline(Harr[i], st, objWidth, objHeight, color.toHex(), TextSize);
		}
	}
}

function drawFlashHeadline(HTMLobj, FlashText, FlashWidth, FlashHeight, TextColor, TextSize) {
	var fo = new FlashObject("images/assets/FlashTitle.swf?FlashText=" + FlashText + "&TextColor=" + TextColor + "&TextSize=" + TextSize + "&TextWidth=" + FlashWidth, "FlashTEXT" + ++FlashTEXTs, FlashWidth, FlashHeight, "6", "#FFFFFF");
	if(fo != null) {
		fo.addParam("quality", "high");
		fo.addParam("menu", "false");
		fo.addParam("scale", "noscale");
		fo.addParam("wmode", "transparent");
		
		HTMLobj.innerHTML = '<div id="TEXT_' + FlashTEXTs + '"></div>';
		
		fo.write("TEXT_" + FlashTEXTs);
		HTMLobj.style.visibility = 'visible';
	}
}

function readStyle(element, property) {
	if (element.style[property]) {
		return element.style[property]
	} else if (element.currentStyle) {
		return element.currentStyle[property]
	} else if (document.defaultView && document.defaultView.getComputedStyle) {
		var style = document.defaultView.getComputedStyle(element, null)
		return style.getPropertyValue(property)
	} else {
		return null
	}
}

/* @author Stoyan Stefanov <sstoo@gmail.com> */
function FText_RGBColor(_c) {this.ok=false;if(_c.charAt(0)=='#') {_c=_c.substr(1,6);}_c=_c.replace(/ /g,'');_c=_c.toLowerCase();var _d=[{re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,_pr:function(bt){return[parseInt(bt[1]),parseInt(bt[2]),parseInt(bt[3])];}},{re: /^(\w{2})(\w{2})(\w{2})$/,_pr: function(bt){return [parseInt(bt[1], 16),parseInt(bt[2], 16),parseInt(bt[3], 16)];}},{re: /^(\w{1})(\w{1})(\w{1})$/,_pr: function(bt){return [parseInt(bt[1] + bt[1], 16),parseInt(bt[2] + bt[2], 16),parseInt(bt[3] + bt[3], 16)];}}];for (var i=0;i<_d.length;i++){var re=_d[i].re;var _p=_d[i]._pr;var bt=re.exec(_c);if(bt){_ch=_p(bt);this.r=_ch[0];this.g=_ch[1];this.b=_ch[2];this.ok=true;}}this.r=(this.r < 0 || isNaN(this.r)) ? 0 : ((this.r > 255) ? 255 : this.r);this.g=(this.g < 0 || isNaN(this.g)) ? 0 : ((this.g > 255) ? 255 : this.g);this.b=(this.b < 0 || isNaN(this.b)) ? 0 : ((this.b > 255) ? 255 : this.b);this.toRGB=function () {return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')';};this.toHex=function() {var r=this.r.toString(16);var g=this.g.toString(16);var b=this.b.toString(16);if (r.length==1) {r='0' + r;}if (g.length==1) {g='0' + g;}if(b.length==1) {b='0' + b;}return '#' + r + g + b;};}
