var text2img = Class.create({
txt2imgClass: 'text2img',

initialize: function(e) {
		if(typeof e != 'object' && typeof e != 'undefined')	return false;
		if(e==undefined)
		{
			this.makeImg($$('.' + this.txt2imgClass));
		}
		else if(e.length > 1)
		{
			e.each(function(e){
				if(e.hasClassName(this.txt2imgClass))
				{
					this.makeImg([e]);
				}else{
					this.makeImg(e.select('.' + this.txt2imgClass));
				}
			}.bind(this));
		}
		else if(e.length == 1)
		{
			if(e[0].hasClassName(this.txt2imgClass))
			{
				this.makeImg(e);
			}else{
				this.makeImg(e[0].select('.' + this.txt2imgClass));
			}
		}
		else if(typeof e == 'object')
		{
			if(!e.hasClassName(this.txt2imgClass))
			{
				try{
					e = e.select('.' + this.txt2imgClass);
				}catch(err){}
				try{
					if(e.length == 0 || e.length == undefined)
					{
						e = $A(document.getElementsByClassName(this.txt2imgClass, e));
					}
				}catch(er){}
			}
			this.makeImg(e);
		}
	},
makeImg: function(e) {
		e.each(function(s){
			try{
			var parentFont = $(s).parentNode.getStyle('font-family');
			}catch(e){};
			var e = $(s).childElements();
			if(!e.length) e = [s];
			
			e.each(function(es){
			
				var c = $(es).getStyle('color');
				if(c.indexOf('rgb') > -1)
				{
					var c1='';
					c = c.match(/rgb\((\d+), ?(\d+), ?(\d+)\)/);
					c.shift();
					c.each(function(s){c1 += parseInt(s).toColorPart();});
					c = c1;
				}
				else if(c.indexOf('#') == 0)
				{
					c = c.substr(1,6);
					if(c.length == 3){
						var c1 = '';
						c1 = c;
						c = c1.substr(0, 1) + c1.substr(0, 1) + c1.substr(1, 1) + c1.substr(1, 1) + c1.substr(2, 1) + c1.substr(2, 1);
					}
				} else {c = '000000';}
				
				var f = $(es).getStyle('font-family');
				// alert('this font: ' + f);
				// alert('parent font: ' + $(es).parentNode.getStyle('font-family'));
				// Wenn mehrere fonts definiert sind dann nimm den 1. font
				
				if(f.indexOf(',') > -1) {var f1 = f.split(',');f = f1[0];}
				// wenn der aktuelle Font gleich dem urlspruenglichen ist, dann breche ab // bug? im opera, da der opera wenn es den font nicht kennt auch nicht ausgelesen werden kann
				if(f == parentFont)	return;
				switch($(es).getStyle('vertical-align'))
				{
				case 'top':
					var v = 't';
					break;
				case 'middle':
				case 'center':
					var v = 'c';
					break;
				default:
					var v = 'b';
					break;
				}
				
				
				var w = $(es).getStyle('font-weight');
				var u = [];
				u[0] = this.eUC(f.toUpperCase());
				u[1] = this.eUC($(es).getStyle('font-size'));
				u[2] = this.eUC($(es).getStyle('height'));
				u[3] = (w == 'bold' || w == '700') ? 'bold' : 'normal';
				u[4] = this.eUC(c);
				u[5] = this.eUC(v);
				
				if (this.isIE6()) {
					u[6] = this.eUC(es.innerHTML.stripScripts().stripTags().replace(/\//g, '_slash_').replace(/\?/g, '_qmark_').replace(/\(/g, '_28_').replace(/\)/g, '_29_') + ".png");
				}else{
					u[6] = this.eUC(es.innerHTML.stripScripts().stripTags().replace(/\//g, '&#47;').replace(/\?/g, '&#63;').replace(/\:/g, '&#58;') + ".png");
				}
				var url = '/src/images/dynamic/' + u.join('/');
				if(parseInt($(es).getStyle('height')) == 0)
				{
					$(es).setStyle({height: 'auto'});
				}
				
				var t = es.innerHTML.replace(/\n/g, ' ');
				
				if(es.innerHTML != '&nbsp;')
				{
					var img = Builder.node('img', {src: url, alt: t, title: t}).setStyle({visibility: 'hidden'});
					try{
						es.innerHTML = '';
					}catch(e){}				
					try{
					es.insert(img);
					}catch(e){}
					this.imageLoad(img, function(){img.setStyle({visibility: 'visible'})});
					
				}
				
			}.bind(this));
			
		}.bind(this));
		if (this.isIE6())
		{
			var ie6png = new correctIE6png();
		}
	},
imageLoad: function(img, onReadyFunc)
	{
		if(img.complete)
		{
			onReadyFunc();
		}
		else
		{
			var self = this;
			window.setTimeout(function(){self.imageLoad(img, onReadyFunc)}, 20);
		}
	},
eUC: function(s)
	{
		return encodeURIComponent(s);
	},
isIE6: function()
	{
		return (Prototype.Browser.IE && IE.version < 7);
	}
});
var correctIE6png = Class.create({
initialize: function()
	{
		// Auf IE6 Testen
		if (Prototype.Browser.IE && IE.version < 7) {
			// Um das flackern bei links mit hintergrundbildern zu vermeiden beim IE6
			try {
				document.execCommand("BackgroundImageCache", false, true);
			} catch(err) {}
			
			var oImgLoad = [];
			var oImgWait = [];
			var oImg = [];
			var waittime = $$('img').length > 10 ? 100 : 10;
			$$('img[src$="png"]').each(function(n, s) {
				window.setTimeout(function(){imgFixPng(n)}, waittime*s);
			});
			
			function imgFixPng(img)
			{
				var imgName = img.src.toUpperCase();
				try{
					var index = this.myCharCode(toString(img.src));
				}catch(err){}
				if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
				{
					// get the original width / height of the image
					if(imgName.indexOf('DYNAMIC') < 0 && oImgLoad[index] == undefined)
					{
						oImgLoad[index] = false;
						oImg[index] = new Image();
						oImg[index].onload = function(){ oImgLoad[index] = true;};
						oImg[index].src = img.src;
						oImgWait[index] = 0;
					}
					var lnkImg = '';
					var imgID = (img.id) ? "id='" + img.id + "' " : "";
					var imgClass = (img.className) ? "class='" + img.className + "' " : "";
					var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
					var imgStyle = "display:inline-block;" + img.style.cssText;
					if (img.align == "left") imgStyle = "float:left;" + imgStyle;
					if (img.align == "right") imgStyle = "float:right;" + imgStyle;
					try{
						if (img.parentElement && img.parentElement.href)
						{
							imgStyle = "cursor:hand;" + imgStyle;
							img.parentElement.setStyle({position: 'relative'});
							var lnkDim = img.parentElement.getDimensions();
							lnkImg = "<img src=\"/media_data/images/leer.gif\" width=\"" + lnkDim.width + "\" height=\"" + lnkDim.height + "\" style=\"position:absolute;top:0;left:0;\"/>";
						}
					}catch(err){}
					
					var height = img.height ? img.height : parseInt($(img).getStyle('height'));
					var width = img.width ? img.width : parseInt($(img).getStyle('width'));
					if(imgName.indexOf('DYNAMIC') < 0 && oImgLoad[index] == false)
					{
						oImgWait[index]++;
						if(oImgWait[index]<10)
						window.setTimeout(function(){imgFixPng(img)}, 100);
					}
					else
					{
						var sM = 'no-scale';
						try{
							if(imgName.indexOf('DYNAMIC') < 0 && (oImg[index].width != width || oImg[index].height != height))
							{sM = 'scale';}
						}catch(err){}
						delete(oImg[index]);
						delete(oImgLoad[index]);
						delete(oImgWait[index]);
						var src = img.src;
						var t = src.split("/");
						var file = decodeURIComponent(t.pop());
						var sonderzeichen = /\W/g;
						var Ergebnis;
						var newFile = file;
						while(sonderzeichen.test(file))
						{
							Ergebnis = /(\W)/.exec(file);
							var convert = Ergebnis[1].charCodeAt(0);
							newFile = newFile.replace(Ergebnis[1], "&#"+convert+";");
							file = file.replace(Ergebnis[1], convert);
							
						}
						file = newFile.replace('&#46;', '.');;
						src = t.join("/") + "/" + file;
						var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + width + "px; height:" + height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod="+sM+" src='"+src+"');\"></span>";
						if(lnkImg != '') strNewHTML += lnkImg;
						img.outerHTML = strNewHTML;
					}
				}
			}
		}
		else
		{
			return true;
		}
	},
myCharCode: function(s)
	{
		var r = 0;
		for(var i=0; i<s.length; i++)
		{
			r += s.charCodeAt(i);
		}
		return r;
	}
});

function initSroller()
{
	$$('.scroll.up').each(function(s){
		s.onclick = function()
		{
			var elem = $(s).up().previous('.scroll').childElements()[0];	// <ol>
			var wrapper = $(s).up().previous('.scroll'); //<div class="scroll">
			var steps = parseInt(wrapper.getHeight()*1);
			if(elem.positionedOffset().top <= 0 - steps)
			{
				scroll(elem, -1, steps)
			}
			else
			{
				scrollTo(elem, 0);
			}
			
			return false;
		}
	});
	$$('.scroll.down').each(function(s){
		s.onclick = function()
		{
			var elem = $(s).up().previous('.scroll').childElements()[0];	// <ol>
			var wrapper = $(s).up().previous('.scroll'); //<div class="scroll">
			var steps = parseInt(wrapper.getHeight()*1);
			if(elem.positionedOffset().top.abs() <= elem.getHeight() - wrapper.getHeight() - steps)// - steps)
			{
				scroll(elem, 1, steps)
			}
			else
			{
				scrollTo(elem, 0-(elem.getHeight() - wrapper.getHeight()));
			}
			return false;
		}
	});
}
var doneSteps = 0;
function scroll(elem, dir, steps)
{
	doneSteps += dir;
	new Effect.Move($(elem), {
x: elem.positionedOffset().left, y: -1 * steps * doneSteps, mode: 'absolute',
transition: Effect.Transitions.linear,
duration: .6
	});
}
function scrollTo(elem, pos)
{
	var dir = 0;
	if(elem.positionedOffset().top > pos)
		dir = 1;
	if(elem.positionedOffset().top < pos)
		dir = -1;
	
	doneSteps += dir;
	new Effect.Move($(elem), {
x: 0, y: pos, mode: 'absolute',
transition: Effect.Transitions.linear,
duration: .3
	});
}


