newsChanger = function(id1, id2, bAsPreview)
{
	this.unique		= Math.round((Math.random() / Math.random() * 1000000000000000), 0);

	this.div1		= document.getElementById(id1);
	this.div2		= document.getElementById(id2);
	
	this.data_div1	= null;
	this.data_div2	= null;

	this.active		= null;
	this.loaded		= false;
	this.loading	= false;
	
	this.timerID	= null;
	this.reminder	= null;;
	
	this.DivHeight	= newsChanger.DivHeight;
	
	this.ImgHeight	= newsChangerImgDiv.HEIGHT;
	this.ImgWidth	= newsChangerImgDiv.WIDTH;

	this.reentrant	= false;

	if (this.div1 && this.div2 && !bAsPreview)
	{
		this._loadContent(true);
		
		this._setOnMouseOver();
	}
}

newsChanger.prototype.run			= function()
{
	if (this.loaded && this.data_div1 != null)
	{
		this.active	= this.div1;

		this._blende(1, this.DivHeight);
	}
}

newsChanger.prototype.preview		= function(text, link_url, link_text, img_info, img_link, redrawLogo)
{
	var img_url						= "";

	this.active						= this.div1;
	
	this.div1.style.display			= 'block';
	this.div1.style.height			= this.DivHeight+"px";
	this.div1.style.top				= "1px";
	
	var linkData					= new Object();
		linkData["url"]				= link_url;
		linkData["text"]			= link_text;
		
	if (isNumeric(img_info))
	{
		var now						= new Date()
	
		img_url						= "../ext/showImage.php?id="+img_info+"&rnd="+now.getTime();
	}
	else
		img_url						= img_info;
	
	var imgData						= new Object();
		imgData["logo"]				= img_url;
		imgData["link"]				= img_link;
		
	this._setTextComplete(this.div1.id, text);
	this._setLink(linkData, this.div1.id);
	this._setImg(imgData, this.div1.id, redrawLogo);
}

newsChanger.prototype.setDivHeight	= function(DivHeight)
{
	this.DivHeight	= DivHeight;
}

newsChanger.prototype.setImgHeight	= function(ImgHeight)
{
	this.ImgHeight	= ImgHeight;
}

newsChanger.DivHeight	= "100";
newsChanger.DivWidth	= "249";

newsChanger.spacerHeight= "6";

newsChanger.prototype._loadContent	= function(b_loadSync)
{
	if (!this.loading)
	{
		var ajax	= new ajax_handler(true);
		var res		= null;
		var info	= new Object();

		this.loading= true;

		if (!isNull(ajax))
		{
			ajax.setClass		("class_newshandler");

			ajax.setParameter	("object_id",		this.unique);

			ajax.setFunction	("getNews");

			if (b_loadSync == true)
				ajax.opensync(ajax_handler.get, this._handleContent.bind(this));
			else
				ajax.open(ajax_handler.get, this._handleContent.bind(this));
		}
		else
			this.loaded	= false;
	}
}

newsChanger.prototype._handleContent	= function(result)
{
	result	= xmlString2Array(result);

	if (result[0] == 1)
	{
		id	= "";

		if (this.active == null || this.active == this.div2)
		{
			this.data_div1	= result[2];
			id				= this.div1.id;
		}
		else
		{
			this.data_div2	= result[2];
			id				= this.div2.id;
		}

		this.loaded	= true;
		
		var imgData	= new Object();
			imgData["logo"]	= result[2]["logo"];
			imgData["link"]	= result[2]["logo_link"];
			
		this._setImg(imgData, id);
		this._setTextComplete(id, result[2]["text"]);
		this._setLink(result[2]["link"], id);
	}
	else
		this.loaded	= false;

	this.loading= false;
}

newsChanger.prototype._blende		= function(direction, hoehe)
{
	if (!this.reentrant)
	{
		this.reentrant	= true;

		var elm			= this.active;
		var move		= 0;

		var b			= parseFloat(elm.style.width);
		var h			= parseFloat(elm.style.height);

		var top			= parseFloat(elm.style.top);

		if		(direction == 1)
		{
			elm.style.display	= 'block';

			if (h < hoehe)
			{
				h += 4;
				move++;
			}

			top	-= 2;

			this._resizeImg(elm.id, this.ImgWidth, (this.ImgHeight*(h/hoehe) - newsChanger.spacerHeight));
		}
		else if (direction == 0)
		{
			if (h <= hoehe)
				elm.style.display = 'none';

			if (h > hoehe)
			{
				h	-= 4;
				move++;
			}

			top	+= 2;

			this._resizeImg(elm.id, this.ImgWidth, (this.ImgHeight*(h/this.DivHeight) - newsChanger.spacerHeight));
		}

		if (move)
		{
			elm.style.height	= h+'px';
			elm.style.width		= b+'px';

			elm.style.top		= top+'px';

			/*this.timerID		= */window.setTimeout(function(){ this._blende(direction, hoehe) }.bind(this), 20);
//			this.reminder		= [direction, hoehe];
		}
		else
		{
			var timeout	= 20;
			var text	= ""

			if (direction == 1)//Info ist komplett sichtbar
			{
				this._loadContent(false);

				hoehe		= 0;
				direction	= 0;

				timeout		= (Math.random()+1)*10000;

				if (elm == this.div1)	text	= this.data_div1["text"];
				else					text	= this.data_div2["text"];
			}
			else//keine Info zu sehen
			{
				if (this.loaded && !this.loading)
				{
					if (this.active == this.div1)	this.active = this.div2
					else							this.active = this.div1
				}

				hoehe		= this.DivHeight;
				direction	= 1;
			}

//			this._setText(elm.id, text);
//			this._setTextComplete(elm.id, text);

			this.timerID	= window.setTimeout(function(){ this._blende(direction, hoehe) }.bind(this), timeout);
			this.reminder	= [direction, hoehe];
		}

		this.reentrant	= false;
	}
}

newsChanger.prototype._setImg		= function(data, id, redrawLogo)
{
	if (typeof redrawLogo == "undefined" || redrawLogo == null)
		redrawLogo = true;
	
	var elm					= document.getElementById(id+"_IMG");
	
	if (elm && redrawLogo)
	{
		var img			= document.createElement("IMG");
			img.src		= data["logo"];
			img.height	= this.ImgHeight;
			img.width	= this.ImgWidth;
			img.border	= 0;

		var a			= null;

		if (!isEmpty(data["link"]))
		{
			var a		= document.createElement("A");
				a.href	= data["link"];
				a.target= "_blank";
				a.appendChild(img);
		}

		if (elm && data["logo"])
		{
			removeAllChilds(elm);

			if (a != null)	elm.appendChild(a);
			else
			{
				elm.appendChild(img);
				
			}
		}
	}
	else if (elm)
	{
		var img			= elm.getElementsByTagName("IMG")[0];
		var a			= elm.getElementsByTagName("A")[0];

		if(!isEmpty(data["link"]))
		{
			if(typeof a != "undefined")
				a.href = data["link"];
			else
			{
				if(typeof img != "undefined")
				{
					var a			= document.createElement("A");
						a.href		= data["link"];
						a.target	= "_blank";
						a.appendChild(img);

					removeAllChilds(elm);
					
					if (a != null)
						elm.appendChild(a);
				}
			}
		}
		else
		{
			if(typeof img != "undefined")
			{
				var c_img	= img.cloneNode(true);

				removeAllChilds(elm);

				elm.appendChild(c_img);
			}
		}
	}
}

newsChanger.prototype._resizeImg	= function(id, width, height)
{
	var elm	= document.getElementById(id+"_IMG");

	if (elm && elm.hasChildNodes())
	{
		var img	= elm.childNodes[0];

		if (img.tagName.toUpperCase() == "IMG")
		{
			img.height	= height;
			img.width	= width;
		}
	}
}

newsChanger.prototype._setTextComplete	= function(id, text)
{
	var elm	= document.getElementById(id+"_INFO");
	
	if (elm && text)
		elm.innerHTML	= text;
}


newsChanger.prototype._setText		= function(id, text)
{
	var elm	= document.getElementById(id+"_INFO");
	
	if (elm)
	{
		elm.innerHTML	= "";

		this._setChar(elm, text, 0);
	}
}

newsChanger.prototype._setChar		= function(elm, str, index)
{
	elm.innerHTML	+= str.charAt(index);

	if (++index < str.length)
		setTimeout(function(){this._setChar(elm, str, index)}.bind(this), 50);
}

newsChanger.prototype._setLink		= function(data, id)
{
	var elm			= document.getElementById(id+"_LINK");
	
	if (elm && data)
	{
		removeAllChilds(elm);
		
		if (data["url"])
		{
			var a			= document.createElement("A");
				a.href		= data["url"];
				a.target	= "_blank";
				a.appendChild(document.createTextNode(data["text"] ? data["text"] : data["url"]));

			elm.appendChild(a);
		}
		else if(data["text"])
			elm.appendChild(document.createTextNode(data["text"]));
	}
}

newsChanger.prototype._setOnMouseOver	= function()
{
	this.div1.onmousemove	= this._onMouseOver.bind(this);
	this.div2.onmousemove	= this._onMouseOver.bind(this);
	
	this.div1.onmouseout	= this._onMouseOut.bind(this);
	this.div2.onmouseout	= this._onMouseOut.bind(this);
}

newsChanger.prototype._onMouseOver	= function()
{
	window.clearTimeout(this.timerID);
}

newsChanger.prototype._onMouseOut	= function()
{
	if (this.reminder != null)
		this.timerID	= window.setTimeout(function(){ this._blende(this.reminder[0], this.reminder[1]) }.bind(this), (Math.random()+1)*10000);
}

/* Globale Funktionen */

function unloadNewsChanger()
{
	var ajax	= new ajax_handler(true);

	if (!isNull(ajax))
	{
		ajax.setClass		("class_newshandler");

		ajax.setFunction	("cleanQueue");

		ajax.open(ajax_handler.get);
	}
}

function createNewsDiv(baseId, baseTop,bAsPreview)
{
	var div_main	= createDiv(baseId,					"border:1px solid lightgrey; position:relative; top:"+baseTop+"px; left:6px; height:"+((newsChanger.DivHeight * 1) + 4)+"px; width:"+((newsChanger.DivWidth * 1) + 4)+"px;");

	var initalvars	= bAsPreview ? {display:'block',top:1,height:newsChanger.DivHeight} : {display:'none',top:(newsChanger.DivHeight/2),height:0};
	
	var div_1		= createDiv(baseId+"_1",			"position:absolute; top:"+initalvars.top+"px;  left:1px; width:"+newsChanger.DivWidth+"px; height:"+initalvars.height+"px; overflow:hidden; display:"+initalvars.display+";");
		
		div_1.appendChild(createDiv(baseId+"_1_INFO",	"position:absolute; top:"+newsChangerInfoDiv.TOP+"px; left:"+newsChangerInfoDiv.LEFT+"px; width:"+newsChangerInfoDiv.WIDTH+"px; height:"+newsChangerInfoDiv.HEIGHT+"px; overflow:hidden; fontSize:7pt; fontFamily:verdana; color:#4C6171;"));
		div_1.appendChild(createDiv(baseId+"_1_IMG",	"position:absolute; top:"+newsChangerImgDiv.TOP+"px;  left:"+newsChangerImgDiv.LEFT+"px;"));
		div_1.appendChild(createDiv(baseId+"_1_LINK",	"position:absolute; top:"+newsChangerLinkDiv.TOP+"px; left:"+newsChangerLinkDiv.LEFT+"px; width:"+newsChangerLinkDiv.WIDTH+"px; height:"+newsChangerLinkDiv.HEIGHT+"px; overflow:hidden; fontSize:7pt; fontFamily:verdana; fontWeight:bold; color:#4C6171;"));

	var div_2		= createDiv(baseId+"_2",			"position:absolute; top:51px;  left:1px; width:"+newsChanger.DivWidth+"px; height:0px; overflow:hidden; display:none;");
		
		div_2.appendChild(createDiv(baseId+"_2_INFO",	"position:absolute; top:"+newsChangerInfoDiv.TOP+"px; left:"+newsChangerInfoDiv.LEFT+"px; width:"+newsChangerInfoDiv.WIDTH+"px; height:"+newsChangerInfoDiv.HEIGHT+"px; overflow:hidden; fontSize:7pt; fontFamily:verdana; color:#4C6171;"));
		div_2.appendChild(createDiv(baseId+"_2_IMG",	"position:absolute; top:"+newsChangerImgDiv.TOP+"px;  left:"+newsChangerImgDiv.LEFT+"px;"));
		div_2.appendChild(createDiv(baseId+"_2_LINK",	"position:absolute; top:"+newsChangerLinkDiv.TOP+"px; left:"+newsChangerLinkDiv.LEFT+"px; width:"+newsChangerLinkDiv.WIDTH+"px; height:"+newsChangerLinkDiv.HEIGHT+"px; overflow:hidden; fontSize:7pt; fontFamily:verdana; fontWeight:bold; color:#4C6171;"));

	div_main.appendChild(div_1);
	div_main.appendChild(div_2);
	
	return div_main;
}

function createDiv(id, style)
{
	var styleElm	= null;

	var div			= document.createElement("DIV");
		div.id		= id;
		
		style		= style.split(";");
	var c_style		= style.length;

	for (var i = 0; i < c_style; i++)
	{
		styleElm	= trim(style[i], " ;");
		styleElm	= styleElm.split(":");
		
		if (styleElm.length == 2)
			div.style[styleElm[0]]	= styleElm[1];
	}
	
	return div;
}