	function RGBtoHex(R,G,B) { return toHex(R)+toHex(G)+toHex(B) }
	function toHex(N) {
 		if (N==null) return "00";
 		N=parseInt(N); if (N==0 || isNaN(N)) return "00";
		N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 		return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
	}	
	var i = false;
	function resizeEmbed(el,width,height) {
		var element=document.getElementById(el);
		if (element != null) {
			if (width != null) { writeLog('setando largura: '+width); element.width = width;	}
			if (height != null) { writeLog('setando altura: '+height); element.height = height; }
		}
		writeLog(el+", "+width+", "+height);	
	}

	function changeTitlesByTagName(tag, cssClass){
		var arrTags = document.getElementsByTagName(tag);
		
		for (var i = arrTags.length-1; i>=0 ; i--) {
			if ((cssClass == null) || ((cssClass != null) && (arrTags[i].className == cssClass))) {
				/* Obtendo a formatação do background */
				var rule = getRuleBySelectorText(arrTags[i].className);
				var bg="";
				var hostName="";
				var font_size="12";
				var font_weight="";
				var font_style="";
				var color="0x000000";
				var stage_width="0";
				var stage_height="0";
				var padding="0,0,0,0";
				var textAlign="";
				var textAlign="";
				
				if (rule != null) {
					/* Tratando o Background */
					vet = rule.style.background.split("(");
					if (vet.length > 1) { bg = vet[1].split(")")[0]; }
					if (bg.indexOf("http") > -1) {
						id1 = bg.indexOf("img[");
						id2 = bg.indexOf("grd[");
						if (id1 == -1) { id1 = id2; }
						if ((id2 != -1) && (id2 < id1)) { id1 = id2; }
						hostName = bg.substr(0,id1).split("%20").join(" ");
						bg = bg.substr(id1,bg.length-(id1-1)).split("%20");
						bg = bg.join(" ");
					}
					
					/* Tratando a fonte */
					font_size = rule.style.fontSize.replace("px","") || "12";
					/* Tratando a cor da fonte */
					color = rule.style.color;
					vet = color.split("rgb(");
					if (vet.length > 1) { vet = vet[1].split(")"); }
					if (vet.length > 1) { vet = vet[0].split(","); }
					if (vet.length > 2) { color = "#"+RGBtoHex(vet[0],vet[1],vet[2]); }
					color = color.replace("#","0x") || "0x000000";
					/* Tratando tamanho do stage */
					stage_width = rule.style.width.replace("px","");// || "0";
					stage_height = rule.style.height.replace("px","");// || "0";
					/* Tratando negrito */
					font_weight = rule.style.fontWeight;
					/* Tratando negrito */
					font_style = rule.style.fontStyle;
					/* Tratando Padding */
					pl="0"; pt="0"; pr="0"; pb="0";
					if (rule.style.padding != "") {
						vetPadding = rule.style.padding.split("px");
						if (vetPadding.length >= 1) { pt = vetPadding[0].replace(" ",""); }
						if (vetPadding.length >= 2) { pr = vetPadding[1].replace(" ",""); }
						if (vetPadding.length >= 3) { pb = vetPadding[2].replace(" ",""); }
						if (vetPadding.length >= 4) { pl = vetPadding[3].replace(" ",""); }
					}
					pl = rule.style.paddingLeft.replace("px","") || pl;
					pt = rule.style.paddingTop.replace("px","") || pt;
					pr = rule.style.paddingRight.replace("px","") || pr;
					pb = rule.style.paddingBottom.replace("px","") || pb;
					padding = pt+","+pr+","+pb+","+pl;
					
					/* Tratando text-align */
					textAlign = rule.style.textAlign.replace("inherit","");
				}
				SWFTitle(arrTags[i], arrTags[i].innerHTML, bg, hostName, font_size,color,font_weight,font_style,stage_width,stage_height,padding,textAlign);
			}
		}
	}

	function getRuleBySelectorText(selectorText) { 
	    var style = document.styleSheets[0];
	    var rules = style.cssRules || style.rules || null;
		selectorText = selectorText.toUpperCase().replace(" ","");
	    if(!rules) return;
	    for(var i = 0; i < rules.length; i++) {
			var selectorTextMatches = rules[i].selectorText.replace(".","").toUpperCase();
			selectorTextMatches = selectorTextMatches.replace(" ","");
			if(selectorTextMatches == selectorText) {
				return rules[i]; 
			}
	    }
    
	    return null;
	}


	var swfCounter = 0;

	function SWFTitle(obj,textContent,background,hostName,fontSize,fontColor,fontWeight,fontStyle,stageWidth,stageHeight,padding, textAlign) {
		// encode the Ampersand (&)
		textContent = encodeAmpersand(textContent);
		
		embedW = stageWidth;
		embedH = stageHeight;
		href = obj.getAttribute('href') || "";
		target = obj.getAttribute('target') || "";
		title = obj.title || "";
		if (embedW == 0) { embedW=100; }
		if (embedH == 0) { embedH=18; }
		swfCounter++;
		var so = new SWFTgObject("/titleGenerator.swf", "title_" + swfCounter, title, embedW, embedH, "3", "#FFFFFF", "", "", "");
		so.addParam("scale", "noscale");
		so.addParam("wmode", "transparent");
		so.addParam("allowScriptAccess", "always");
		so.addParam("swLiveConnect", "true");
		so.addVariable("id","title_" + swfCounter);
		if (background != "") { so.addVariable("stage_bg",background); }
		if (hostName != "") { so.addVariable("host_name",hostName); }
		if (textContent != "") { so.addVariable("text_content",textContent); }
		if (fontSize != "") { so.addVariable("text_size",fontSize); }
		if (fontColor != "") { so.addVariable("text_color",fontColor); }
		if (fontWeight != "") { so.addVariable("text_weight",fontWeight); }
		if (fontStyle != "") { so.addVariable("text_style",fontStyle); }
		if (padding != "") { so.addVariable("text_padding",padding); }
		if (textAlign != "") { so.addVariable("text_align",textAlign); }
		if (stageWidth != "") { so.addVariable("stage_width",stageWidth); }
		if (stageHeight != "") { so.addVariable("stage_height",stageHeight); }
		if (href != "") { so.addVariable("href",href); }
		if (target != "") { so.addVariable("target",target); }
		if (obj.getAttribute('track') != "") { so.addVariable("tracker",obj.getAttribute('track')); }
	//	so.write(obj);
		so.replaceObj(obj);
	//	so.inputObj(obj);
	}

	// function to "ENCODE" the Ampersand (&) to send to Flash
	function encodeAmpersand(text) {
		return text.replace("&","*[e]*");
	}

	function writeLog(logText) {
//		document.getElementById('logDiv').innerHTML += logText+"<br />";
	}
