function Flext(tags){
	this.tags = tags;
}
Flext.titelCounter = 0;
Flext.prototype.secure = false;
Flext.prototype.tags = null;
Flext.prototype.currentTag = null;
Flext.prototype.currentSWF = null;
Flext.prototype.setup = function() {
	for(var tag in this.tags){
		var els = document.getElementsByTagName(tag);
		var l = els.length;
		for(var i = 0; i < l; i++){
			var el = els[i];
			
			this.currentTag = tag;
			this.currentSWF = this.tags[tag];
			
			var l2 = el.childNodes.length;
			
			var t = '';
			for(var j = 0; j < l2; j++){
				var node = el.childNodes[j];
				if(node.nodeType == 3){
					t += node.nodeValue.replace(/\n$/, '<br>');
				} else if (node.nodeType == 1) {
					if(node.tagName.toLowerCase() == 'br'){
						t += '<br>';
					}
				}
			}

		//	alert(t);

			var c = getColor(el);
			title = this.transform(t);
				
			var span;
			var div;
			
			span = document.createElement('span');
			span.className = 'flextHide';
			span.appendChild(document.createTextNode(title));
				
			div = document.createElement('div');
			div.className = 'flextNoPrint';
			div.setAttribute('id', 'titel' + Flext.titelCounter);
			div.innerHTML = this.createText(title, Flext.titelCounter, c);
				
			Flext.titelCounter++;
			
			el.innerHTML = '';
			el.appendChild(span);
			el.appendChild(div);
		}
	}
}
Flext.prototype.transform = function(s){
	s = s.replace(/^\s+|\s+$/gm, "");	// trim
	s = s.replace(/[\r\n]/, '');
	s = s.replace('<br>', '\n');
	
	return s;
}
Flext.prototype.createText = function(s, id, color) {
	var protocol = this.secure ? 'https' : 'http';
	var color = color || '';
	s = escape(s);
	return AC_FL_RunContentStr('codebase', protocol + '://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0', 'id', 'myTitle' + id, 'name', 'myTitle' + id, 'width','100%','height','48','src','titel'+ id,'quality','high','pluginspage', protocol + '://www.macromedia.com/go/getflashplayer','bgcolor', '#ffffff', 'wmode', 'transparent', 'movie','/swf/' + this.currentSWF + '?&id='+ id + '&titel=' + s + '&color=' + color);
}

function setTitleSize(w, h, nr){
	var div = document.getElementById('titel' + nr);
	
	if(div){
		var objs = div.getElementsByTagName('object');
		var embs = div.getElementsByTagName('embed');
		var o = objs[0];
		var e = embs[0];
	
		if(o){
			o.setAttribute('width', w);
			o.setAttribute('height', h+5);
		}
	
		if(e) {
			e.setAttribute('width', w);
			e.setAttribute('height', h+5);
		}
	}
}

function getStyle(el,styleProp)
{
	if (el.currentStyle)
		var y = el.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
	return y;
}

function toHex(n, l, prefix) {
	prefix = prefix || '';
	var s = n.toString(16);
	if(s.length < l){
		s = ((l - s.length) * '0') + s;
	}
	return prefix + s;
}

function getColor(el){
	var c = String(getStyle(el, 'color'));
	var re1 = new RegExp(/rgb\((\s*\d+,\s*\d+,\s*\d+)\)/i); // rgb(255, 255, 255)
	var re2 = new RegExp(/\#([0-9A-F]+)/i); // #ff00ff
	var m1 = re1.exec(c);
	var m2 = re2.exec(c);
	if(m1){
		var a = m1[1].split(',');
		var s = '0x';
		
		s += toHex(Number(a[0].trim()), 2);
		s += toHex(Number(a[1].trim()), 2);
		s += toHex(Number(a[2].trim()), 2);
		
		return s;
	} else if (m2) {
		var s = '';
		if(m2[1].length != 6){
			var a = m2[1].split("");
			for(var i = 0; i < a.length; i++){
				s += String(a[i]) + String(a[i]);
			}
		} else {
			s = m2[1];
		}
		
		return '0x' + s;
	}
	
	return '';
}

String.prototype.trim = function(){
	return( this.replace(/^\s+|\s+$/gm, "") );
}
