var browser;
var ElusiveAjax;
var ElusiveTemplate;
var CssClasses=new Object();
var lastUpdate=0;
var alertType="firstTime";
var contextmenuBool=0;
var SelectName="";
var SelectValue="";
var DynamicJsCmd="";
var ScriptsArray= new Array();
var ElusiveStatus="loading";
var DragActivated=new Object();
var DropActivated=new Object();
var GoogleMap = new Object();
var NewspaperEditor="";
var OnExit="";
var ifrm=new Object();
var iterator=0;
var IframeActivated=0;
var Debugger;
var system_type_test="no";
var RecursionCount=0;
var publicTemp;
window.spy = new Object();
window.spy.object = '';
window.spy.last = '';
window.spy.list_class = '';
window.cms_history = new Object();
window.pre_history = new Object();
window.IframesEx=new Object();
window.IframeTaskManager = new Array();
window.sysMsg = new Object();

// WYSIWYG Editor
var formu, ifrm;
var uagent=navigator.userAgent.toLowerCase();
var isGecko = ( uagent.match('gecko') && navigator.productSub>=20030210 );
var isIE = uagent.match('msie') && !uagent.match('opera') && (!uagent.match('msie 5.5')||!uagent.match('msie 6.')); 
window.photocases = new Object();
window.dataselectors = new Object();
window.Modules=new Object();
window.EML=new Object();
document.onclick = clickLeft;
document.onkeypress = keyhandler;


window.onload = function()
{
	
	
	CleanNonJs();

	BrowserDetection();

	ElusiveTemplate = new ElusiveTemplate();

	ElusiveAjax = new ElusiveAjax();
	
	Debugger = new Debugger();

	Debugger.StartJS();

	CMS = new CMS();
	
	DynamicJS = new DynamicJS();

	//SetMsg("Seite wird geladen ...");

	LoadFirstUrl();
	
	SetBehaviour();

}


function LoadAndExecuteScript(url, cmd)
{
	treffer=0;
	scripts=ScriptsArray.length;
	for (n=0; n < scripts; n++)
	{
		if (ScriptsArray[n] == url) 
		{
			treffer=1;
			n=1000;
		}
		
	}
	
	if (treffer == 0)
	{
		LoadScript(url);
		ScriptsArray.push(url);
	}
	
	
	if (cmd != "")
	{
		//document.body.innerHTML="";
		ElusiveExecuteScript(cmd);
	}

}


function LoadScript(url)
{
	
	script=document.createElement('script');
	script.setAttribute("type", "text/javascript");
	script.setAttribute("language", "JavaScript");
	script.setAttribute("charset", "utf-8");
	
	
	script.src=url;
	
	document.getElementsByTagName('head')[0].appendChild(script);

}

function ElusiveExecuteScript(cmd)
{
	RecursionCount++;

	if (RecursionCount > 100)
	{
		//document.body.innerHTML+="versuche es jetzt!<br>";
		try 
		{
			eval(cmd);
			//document.body.innerHTML+="erfolgreich!<br>";
			RecursionCount=0;
			//alert("erfolgreich");
		}
		catch(e)
		{
			//document.body.innerHTML+="fehlgeschlagen!("+e+")";
			alert('e'+cmd);
			throw e;
		}
	}
	else if (ElusiveStatus == "ready")
	{
		//document.body.innerHTML+="versuche es jetzt!<br>";
		try 
		{
			eval(cmd);
			//document.body.innerHTML+="erfolgreich!<br>";
			RecursionCount=0;
			//alert("erfolgreich");
		}
		catch(e)
		{
			//document.body.innerHTML+="fehlgeschlagen!("+e+")";
			window.setTimeout("ElusiveExecuteScript('"+cmd+"')", 500); 
		}
	}
	else
	{
		//document.body.innerHTML="status noch nicht soweit...<br>";
		window.setTimeout("ElusiveExecuteScript('"+cmd+"')", 100);
		
	}
}



function SetBehaviour()
{
	var regeln = {
		'a' : function(el) {
			el.onclick = function()
			{
				checkLink(el);
				return false;
			}
		}
	
	}
	Behaviour.register(regeln);
}


function checkLink(el)
{
	
	href=el.href;
	link_class=el.className;
	
	if (link_class == "js_link")
	{
		onclick=el.getAttribute("elusivelink");

		eval(onclick);
	}

	
	hostname= window.location.hostname;
	
	RegExpression = new RegExp;
	RegExpression = "^http:\/\/"+hostname+"/";

	RegExpressionSSL = new RegExp;
	RegExpressionSSL = "^https:\/\/"+hostname+"/";
	
	RegExpressionBackendSSL = new RegExp;
	RegExpressionBackendSSL = "^https:\/\/"+hostname+"/backend/";

	RegExpressionBackend = new RegExp;
	RegExpressionBackend = "^http:\/\/"+hostname+"/backend/";


	if(  (href.match(RegExpression) || href.match(RegExpressionSSL))
		 &&	(!href.match(RegExpressionBackend)) 
		 && (!href.match(RegExpressionBackendSSL)) 
		)
	{
		if (link_class == "js_link")
		{
			// JS
			href= href.replace(/\?/, "#");
		
			link = href.split("#");
		
			link=unescape(link[1]);		
				
			eval(link);
		}
		if (link_class == "iframe")
		{
			href= href.replace(/\?/, "#");
		
			link = href.split("#");
		
			link=unescape(link[1]);		
						
			OpenIframe(link);
		}
		else if (link_class == 'image')
		{
			OpenIframe('iframe=image&url='+href);
		}
		else
		{
			// intern
			href= href.replace(/\?/, "#");
		
			link = href.split("#");
		
			link=unescape(link[1]);
		
			if (link.match("^http://(.*)$"))
				OpenExtern(link);
			else if (link != "") CmsOpen(link);
			
		}
		
		
	
	}
	else
	{
		OpenExtern(href);
	}

	
}

function loadEditor(editor_name, value)
{
		try
		{
			
			realloadEditor(editor_name);
			
			
			if (ifrm[editor_name].styleSheets.length == 0)
			{
				setTimeout("loadEditor('"+editor_name+"', '"+value+"');", 100);
				//alert(ifrm[editor_name].styleSheets.length);
			}
			else
			{
				loadEditorCss(editor_name);
				//alert("load css");
			}	
			
				
			if (typeof(value) != "undefined") 
			ifrm[editor_name].body.innerHTML=Base64.decode(value);
			
			EditorWatch(editor_name);
		
			
		
		}
		catch (e)
		{
			setTimeout("loadEditor('"+editor_name+"', '"+value+"');", 100);
			//alert("error!");
			//throw e;
			
		}
		
		
}


function startSpy(spyObject, list_class)
{
	window.spy.object = spyObject;
	window.spy.list_class = list_class;
	spyNow();
}

function stopSpy()
{
	window.spy.object = '';
	window.spy.last = '';
}


function spyNow()
{
	if (window.spy.object != '')
	{
		object = document.getElementById(window.spy.object);
		
		value = object.value;
		if (value != '' && value != window.spy.last)
		{
			query = 'list_class='+window.spy.list_class;
			query+='&input='+window.spy.object;
			query+='&value'+urlencode();
			ElusiveAjax.Update(query);	
			window.spy.last = value;
		}
		window.setTimeout("spyNow();", 1000);	
	}
}


function EditorWatch(editor_name)
{
	try
	{
		if (typeof(ifrm[editor_name].body != "undefined"))
		{
				content=ifrm[editor_name].body.innerHTML;
				
				document.getElementById("dummy_"+editor_name).value=content;
				
				window.setTimeout("EditorWatch('"+editor_name+"');", 250);
				
		}
	}
	catch(e)
	{
		//throw e;
	}	
}


function loadEditorCss(editor_name)
{	
	//alert("Css wird in Editor eingefügt");
	
	document.getElementById("wysiwyg_editor_"+editor_name).contentWindow.focus(); 	
	
	anz = (document.styleSheets[0].cssRules) ? document.styleSheets[0].cssRules.length : document.styleSheets[0].rules.length;
	
	for (n = 0; n < anz; n++)
	{
		name = (document.styleSheets[0].cssRules) ? document.styleSheets[0].cssRules[n].selectorText : document.styleSheets[0].rules[n].selectorText;
		
		css =cssRule("0/"+n);
		
		fullCss = (document.styleSheets[0].cssRules) ? document.styleSheets[0].cssRules[n].cssText : document.styleSheets[0].rules[n].cssText;
		
		
		(document.styleSheets[0].deleteRule)
		?
		ifrm[editor_name].styleSheets[0].insertRule(fullCss, ifrm[editor_name].styleSheets[0].cssRules.length)
		:
		ifrm[editor_name].styleSheets[0].addRule(name, css);
		
		
		
	} 
	

	
}


function realloadEditor(editor_name)
{
	
	if (isIE) 
		ifrm[editor_name] = window.frames["wysiwyg_editor_"+editor_name].document; 
	if (isGecko) 
		ifrm[editor_name] = document.getElementById("wysiwyg_editor_"+editor_name).contentWindow.document; 
						
	ifrm[editor_name].designMode='on';
}


function UpdateCMS(query)
{
	
        d = new Date();
        year= d.getYear();
        month= d.getMonth();
        day=d.getDay();

        hour = d.getHours();
        min = d.getMinutes();
        sec = d.getSeconds();

        timestamp=Date.UTC(year, month, day, hour, min, sec)/1000;

				Debugger.StartJS();

        nextUpdate=lastUpdate+9;

        if ((timestamp > nextUpdate) || (lastUpdate == 0) || query != "")
        {
        	if (IframeActivated == 0)
						ElusiveAjax.Update(query);
         }
}

function UpdateCMSsoon(url)
{
	window.setTimeout("UpdateCMS('"+url+"');", 1500);
}


function DoUpdate()
{
		last_query=ElusiveAjax.last_query;
		if (last_query != "form")
		{
			ElusiveAjax.Update(last_query);
		}
}

function php_info(objekt)
{
   php_function=objekt.firstChild.firstChild.nodeValue;
   CmsOpen('module=php_manual&function='+php_function);
}


function clickIt(event, object)
{
     if (browser == "MSIE")
     {
         mausX=window.event.clientX;
         mausY=window.event.clientY;
     } else if (browser == "Gecko")
     {
        mausX=event.clientX;
        mausY=event.clientY;
     }

     if (contextmenuBool == 0)
     {
        contextmenuBool=1;
        document.body.innerHTML+="<div class=Contextmenu id=contextmenu style=\"position:absolute; top: "+mausY+"px; left: "+mausX+"px; z-index:900; \">Das ist ein Contextmenu</div>";
     }
     else
     {
         document.getElementById("contextmenu").style.visibility="visible";
         document.getElementById("contextmenu").style.top=mausY+"px";
         document.getElementById("contextmenu").style.left=mausX+"px";


     }
     //alert("Maus: "+mausX+", "+mausY);
}


function clickLeft()
{
   //alert("ClickLeft(); "+contextmenuBool);
   if (contextmenuBool == 1)
    {
        document.getElementById("contextmenu").style.visibility="hidden";
        //alert("schließen");
    }

}


function waitFor(script)
{
	var success=false;
	document.body.innerHTML="";
	for (var n=0; n < 100; n++)
	{
		
		document.body.innerHTML+="Versuch "+n+" <br>";
		try
		{
			eval(script);
			document.body.innerHTML+="erfolgreich!";
			success=true;
			exit();
		}
		catch(e)
		{
			document.body.innerHTML+="fehlgeschlagen! ("+e+")";
		}
	}
	
	alert("OK!");
}

function CleanNonJs()
{
	node=document.getElementsByTagName("NOSCRIPT")[0];
	document.body.removeChild(node);

}

function CheckKeyDown(FormObject)
{
    Ereignis = window.event;
    if (Ereignis.keyCode == 13) SendForm(FormObject);

}

function onFocus(FormObject)
{
	

	if (FormObject.getAttributeNode("class").nodeValue == "form_deactivated")
	{
		alert("Dieses Feld ist derzeit nicht aktiviert!");

	}
	else
	{
				
		if (SelectName != "")
		{
	
			try
			{
				//if (SelectValue != "")
				//	document.getElementById(SelectName).value=SelectValue;
				att_class =document.createAttribute("class");
				att_class.nodeValue="form_standard";
				document.getElementById(SelectName).setAttributeNode(att_class);
				
				try
				{
					if (typeof(document.getElementById(SelectName).attributes['object_search']) != 'undefined')
					{
						document.getElementById(SelectName+'_search').style.display='none';	
						
						if ($(SelectName+'_new') != null)
							$(SelectName+'_new').style.display='none';
			
						//$('Kopf').removeChild($(SelectName+'_history'));
						
						document.getElementById(SelectName+'_history').style.display='none';	
					
					}
					
					
					
				}
				catch(e){
					throw 'SN:'+SelectName+' / '+e;
				}
			}
			catch (e) {
				//throw e;
			}
			
		}
		att_class =document.createAttribute("class");
		att_class.nodeValue="form_selected";
		FormObject.setAttributeNode(att_class);
		SelectName=FormObject.id;
		SelectValue=FormObject.value;
		

		list_class = document.getElementById(SelectName).getAttribute('list_class');
			
						
		if (typeof(list_class) != 'undefined')
			//startSpy(SelectName, list_class);
						
		
		if (typeof(FormObject.attributes['object_search']) != 'undefined')
		{
			document.getElementById(SelectName+'_search').style.display='block';
			
			if ($(SelectName+'_new') != null)
				$(SelectName+'_new').style.display='block';
			
			
			var posKopf = Element.cumulativeOffset( $('Kopf') );
			
			var posSelect = Element.cumulativeOffset( $(SelectName) );
			
			
			var scrollSelect = Element.cumulativeScrollOffset( $(SelectName) );
			
			var left = posSelect[0] - posKopf[0] + scrollSelect[0];
			var top = posSelect[1] - posKopf[1] + $(SelectName).getHeight() +2 + scrollSelect[1];

			
			
			
			$(SelectName+'_history').style.left = left+'px';
			$(SelectName+'_history').style.top = top+'px';
			
			document.getElementById(SelectName+'_history').style.display='block';
			
		}
	}
}


function object_search(FormObjectId, url2)
{
	FormObject=document.getElementById(FormObjectId);
	
	if (typeof(FormObject.attributes['object_search']) != 'undefined')
	{
		search_object=FormObject.attributes['object_search'].nodeValue;
		
		input_id=FormObject.attributes['id'].nodeValue;
			
		url='divframe=objectsearch&object='+search_object+'&input_id='+input_id+'&width=800&height=600';
		
		elements_tmp = FormObject.form.elements;
		
		tmp = new Object();
		
		for (i = 0; i < elements_tmp.length; i++)
		{
			tmp[elements_tmp[i].name] = elements_tmp[i].value;
		}

		publicTemp = tmp;
		
		OpenIframeEx(url+'&'+url2, "Suche", 950);
	
		window.setTimeout("revert_form('"+FormObject.form.name+"')", 2000);
		
	}	
}

function new_object(FormObjectId, FormName, close_frame, value_string)
{
	url='divframe=dataobjects_new&form='+FormName+'&input_id='+FormObjectId+'&'+value_string+'&height=400&width=800';

	OpenIframeEx(url, 'Neu Anlegen', 950);

	window.setTimeout("revert_form('"+FormObject.form.name+"')", 2000);

	if (close_frame)
		CloseIframeEx(close_frame);
}


function revert_form(form_name)
{
	form = document.forms[form_name];
	
	for (i = 0; i < elements_tmp.length; i++)
	{
		form.elements[i].value = publicTemp[form.elements[i].name];
		
	}
}

function object_search_result(input_id, iframe_id, value)
{
	CloseIframeEx(iframe_id);
	document.getElementById(input_id).value = value;
	
	SelectName = input_id;
	SelectValue = value;
	
	$(input_id+'_history').display='none';
	
	try
	{
		document.getElementById(input_id).onchange();
	}
	catch(e)
	{
		// nicht wichtig
	}
}


function close_histories()
{
	var histories = $$('.dataobject_history');
	
	for (n = 0; n < histories.length; n++)
	{
		histories[n].style.display = 'none';
	}
}

function CheckForm(FormObject)
{
	try
	{
		name=FormObject.getAttribute("name");
	    //value=FormObject.getAttribute("value");
	    //SelectName="";
	    value=FormObject.value;
	    SelectValue = value;
	    query="cms_mode=checkForm&form="+FormObject.form.name+"&checkName="+name+"&checkValue="+value;
		objectclass=FormObject.attributes['objectclass'].nodeValue;
		query+="&objectclass="+objectclass;

	    ElusiveAjax.Update(query);
	    
	}
	catch(e)
	{
		
	}

}

function FCKHack() {
	
	try 
	{
	
		for (i = 0; i < parent.frames.length; ++i) {
			if (typeof(parent.frames[i]) != 'undefined') {
				if (parent.frames[i].FCK) 
					parent.frames[i].FCK.UpdateLinkedField();
			}
		}
	}
	catch (e)
	{
		
	}
}


function SendForm(FormObject)
{
    FCKHack();
			
	
	form_name=FormObject.getAttribute("name");
    query="cms_mode=sendForm&sendForm="+form_name;
    //alert(FormObject.elements);
    anz=FormObject.elements.length;
    for (n=0; n < anz; n++)
    {
        element=FormObject.elements[n];

        if (element.type == "checkbox" || element.type == "radio")
        {
        	
        
        	if (element.checked)
        	{
        		//SetMsgEffect("is checked!");
        	 	value=element.value;
        	}
    	 	else
    	 		value="";
        }
        else
        	value=element.value;
        	
        	
 		try
 		{
	 		if (element.id != '' && typeof(element.id) != 'undefined' && typeof($(element.id)) != 'undefined' && typeof($(element.id).attributes) != 'undefined')
	 		{
	 			if(typeof($(element.id).attributes['object_search']) != 'undefined')
	 			{
	 				$(element.id+'_history').style.display='none';
	 			}
	 		}
	 		
 		}
 		catch (e)
 		{
 			
 			
 		}
 		
 		if (value != '')
 		{
	 		value=escape(value);
	 
	        value=value.replace(/\+/g, "%2B");
	        query+="&"+escape(element.name)+"="+value;
 		}

    }

	//alert(query);



	SelectName = '';
  	SetMsgEffect(FormObject.getAttribute('actioninfo'));    
    //alert(query+" SEND BY POST");
    ElusiveAjax.Update(query, "POST");


}

function OpenIframe(url)
{

	OpenIframeEx("iframe.php5?"+url);
	
	
	/*	DEPRECATED!
	 
		IframeActivated=1;			
					
		var Querys = url.split("&");
		var anz=Querys.length;
		var iframe="";
		var new_url="";
		var width=700;
		var height=200;
		for(n=0; n < anz; n++)
		{
			tmp=Querys[n].split("=");
			name=tmp[0];
			value=tmp[1];
			if(name == "width") width=value;
			else if(name == "height") height=value;
			else new_url+=name+'='+value+'&';
		} 	
		var top=50;
		var left=(screen.availWidth/2)-(width/2);		
		document.getElementById("iframe_box").style.top=top+'px';
		document.getElementById("iframe_box").style.left=left+'px';
		document.getElementById("iframe_box").style.width=width+'px';
		document.getElementById("iframe_box").style.height=height+'px';
		document.getElementById("iframe_frame").style.visibility='visible';
		
		document.getElementById("iframe_element").src="iframe.php5?"+url;

		document.getElementById("iframe_element").height=(height-23)+'px';
	 
	*/	
}


function ResizeIframe(iframe_id)
{
	
	//alert(frames['iframe_element_'+iframe_id].document.firstChild.offsetHeight);
}


function OpenIframeEx(url, iframe_name, zIndex)
{
	
	var query_url = url.split("?");
	query_url = query_url[query_url.length-1]; 


	var Querys = query_url.split("&");
	var anz=Querys.length;
	var iframe="";
	var new_url="";
	var width='auto';
	var height='auto';
	var divframe ="";
	var module = "";
	var title;
	for(n=0; n < anz; n++)
	{
		tmp=Querys[n].split("=");
		name=tmp[0];
		value=tmp[1];
		if(name == "width") width=value;
		else if(name == "height") height=value;
		else if (name == "divframe") divframe=value;
		else if (name == 'module') module=value;
		else if (name == "title") title = value;
		else if (name == 'iframe_image') image = value;
		else if (name == 'closeframe') closeframe = value;
		else new_url+=name+'='+value+'&';
	} 	
	if (typeof(closeframe) != 'undefined')
		CloseIframeEx(closeframe);
	
	var top=50;
	
	if (typeof(iframe_name) == 'undefined')
	{
		if (typeof(title) != 'undefined')
			iframe_name = title;
		else	
			iframe_name='Iframe';
	}
	
	if (typeof(zIndex) == 'undefined' || zIndex == '')
		zIndex = 800;
	
	
	iframe_id=PasswordGenerator(5);
	
	
	if (height == 'auto' && width == 'auto')
	{
		height = 400;
		width = 500;
		
		setTimeout('ResizeIframe("'+iframe_id+'");', 1000);
	}
	
	var left=(screen.availWidth/2)-(width/2);	
	
	
	iframe_html = 	'<div id="iframe_box_'+iframe_id+'" style="position:fixed; border-top: 1px black solid; border-left: 1px black solid; border-right: 2px black solid; border-bottom: 2px black solid; top:'+top+'px; left:'+left+'px; background-color: white; width:'+width+'px; height:'+height+'px; z-index: '+zIndex+';">\n' +
							'<div class=iframe_header id="iframe_header_'+iframe_id+'" style="height:26px; text-align: center; vertical-align: middle; overflow: hidden;">'+iframe_name + 					
								'<img id="iframe_minimize_'+iframe_id+'" onclick="MinimizeIframeEx(\''+iframe_id+'\');" border=0 style="position:absolute; right:50px; top: 2px;" src="template_image.php5?img=icons/minimize.gif">\n' +							
								'<img id="iframe_maximize_'+iframe_id+'" onclick="MaximizeIframeEx(\''+iframe_id+'\');" border=0 style="position:absolute; right:26px; top: 2px;" src="template_image.php5?img=icons/maximize.gif">\n' +							
								'<img id="iframe_close_'+iframe_id+'" onclick="CloseIframeEx(\''+iframe_id+'\');" border=0 style="position:absolute; right:0px; top: 0px;" src="template_image.php5?img=icons/toolbar/png/24x24/Close.png">\n' +
							'</div>\n';
	
	
	if (divframe == "" && module == "")
	{
		iframe_html +=			'<iframe id="iframe_element_'+iframe_id+'" name="iframe_element_'+iframe_id+'" frameborder=0 style="width:100%; height: '+(height-26)+'px;" src="'+url+'&iframe_id='+iframe_id+'"></iframe>\n';
	}
	else
	{
		iframe_html +=			'<div id="iframe_element_'+iframe_id+'" style="width:100%; height: '+(height-26)+'px;" class="Divframe"><div style="height: '+(height-26)+'px; width: 100%; vertical-align: middle; text-align: center;"><br><br><img src=template_image.php5?img=icons/waiting.gif><br>bitte warten...</div></div>';
	}
	iframe_html +=		'</div>\n';
		
		
		
	if (uagent.match('msie 6.'))
		document.getElementById('IframeContainer').innerHTML+=iframe_html;
	else				
		document.getElementById('Kopf').innerHTML+=iframe_html;
	
	window.IframesEx[iframe_id] = new Object();
	window.IframesEx[iframe_id]['id'] = iframe_id;
	window.IframesEx[iframe_id]['width'] = width;
	window.IframesEx[iframe_id]['height'] = height;
	window.IframesEx[iframe_id]['status'] = 'open';
	window.IframesEx[iframe_id]['name'] = iframe_name;
	
	//window.setTimeout("PerformIframe('"+iframe_id+"');", 200);


	window.setTimeout("PerformIframes();", 200);

	if (divframe != "" || module != "")
	{
		query_url+="&iframe_id="+iframe_id;
		cmd="UpdateCMS('"+query_url+"');"
		//document.getElementById('Body').innerHTML=url;
		window.setTimeout(cmd, 300);
	}	
}

function PerformIframe(iframe_id)
{
		d = new Draggable(
			'iframe_box_'+iframe_id,
			{
				handle:'iframe_header_'+iframe_id,
				starteffect:false,
				endeffect:false	
			}
			);
}



function PerformIframes()
{

	for (var iframe_id in window.IframesEx)
	{
		if (window.IframesEx[iframe_id]['status'] == 'open')
		{
			document.getElementById('iframe_header_'+iframe_id).onclick=new Function('');
			PerformIframe(iframe_id);
			
		}
		else if (window.IframesEx[iframe_id]['status'] == 'minimized')
			document.getElementById('iframe_header_'+iframe_id).onclick=new Function('RestoreIframeEx("'+iframe_id+'");');
								
	}

	for (id = 0; id < window.IframeTaskManager.length; id++)
	{
		var iframe_id = window.IframeTaskManager[id];
		
		if (window.IframesEx[iframe_id]['status'] != 'minimized')
		{
			RemoveTask(id);
			id--;
		}
	}



	Behaviour.apply();
}

function CloseIframeEx(iframe_id)
{
	if (typeof(iframe_id) != 'undefined' && iframe_id != '')
	{
		try
		{
			$("iframe_box_"+iframe_id).remove();
			
			
			delete(window.IframesEx[iframe_id]);
			
			close_histories();
			
			PerformIframes();
		}
		catch(e)
		{}
	}
}

function MaximizeIframeEx(iframe_id)
{
	var url = document.getElementById('iframe_element_'+iframe_id).src;
	
	window.open(url);
	
	CloseIframeEx(iframe_id);
}

function MinimizeIframeEx(iframe_id)
{
	
	document.getElementById('iframe_element_'+iframe_id).style.height='0px';
	
	
	var top_px = document.getElementById('iframe_box_'+iframe_id).style.top;
	var left_px = document.getElementById('iframe_box_'+iframe_id).style.left;
	
	var top = top_px.split("px");
	var left = left_px.split("px");
	
	top = top[0];
	left = left[0];
	
	window.IframesEx[iframe_id]['top']=top;
	window.IframesEx[iframe_id]['left']=left;
	
	var height= window.IframesEx[iframe_id]['height'];
	var width= window.IframesEx[iframe_id]['width'];
	
	var dest_height=26;
	var dest_width=100;
	
	var height_perc = Math.round((dest_height/height)*100);
	var width_perc = Math.round((dest_width/width)*100);
	
	
	document.getElementById('iframe_minimize_'+iframe_id).style.display='none';
	document.getElementById('iframe_maximize_'+iframe_id).style.display='none';
	document.getElementById('iframe_close_'+iframe_id).style.display='none';
	document.getElementById('iframe_element_'+iframe_id).style.display='none';

		
	//document.body.innerHTML='Height: '+height+" / "+height_perc+"<br>Width: "+width+" / "+width_perc;
	
	AddIframe2TaskManager(iframe_id);

	var innerHeight;
	if (self.innerHeight) // all except Explorer
	{
		innerHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		innerHeight = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		innerHeight = document.body.clientHeight;
	}

	var dest_top=innerHeight-50;
	var dest_left=50+(window.IframesEx[iframe_id]['task_id']*120);
	
	var top_move = dest_top-top;
	var left_move = dest_left-left;
	
	

	new Effect.Parallel(
				[
				new Effect.Scale('iframe_box_'+iframe_id, 
						height_perc,
						{
							scaleX:false
						}),
				new Effect.Scale('iframe_box_'+iframe_id, 
						width_perc,
						{
							scaleY:false
						}),
				new Effect.MoveBy('iframe_box_'+iframe_id, 
						top_move,
						left_move)
				],
				{
					afterFinish: function(effect)
					{
						
						window.IframesEx[iframe_id]['status']='minimized';
						document.getElementById('iframe_box_'+iframe_id).style.height='26 px';
						window.setTimeout("PerformIframes();", 200);
					}
				}
				);
	


	
	
}

function RestoreIframeEx(iframe_id)
{
	var top_px = document.getElementById('iframe_box_'+iframe_id).style.top;
	var left_px = document.getElementById('iframe_box_'+iframe_id).style.left;
	var height_px = document.getElementById('iframe_box_'+iframe_id).style.height;
	var width_px = document.getElementById('iframe_box_'+iframe_id).style.width;
	
	
	
	var top = top_px.split("px");
	var left = left_px.split("px");
	var height = height_px.split("px");
	var width = width_px.split("px");	
	
	top = top[0];
	left = left[0];
	height=height[0];
	width=width[0];
	
	height = Math.round(height)+3;
	width = Math.round(width)+3;
	
	top_move = window.IframesEx[iframe_id]['top'] - top;
	left_move = window.IframesEx[iframe_id]['left'] - left;
	
	
	var dest_height= window.IframesEx[iframe_id]['height'];
	var dest_width= window.IframesEx[iframe_id]['width'];
	
	var height_perc = Math.round((dest_height/height)*100);
	var width_perc = Math.round((dest_width/width)*100);
	
	new Effect.Parallel(
				[
				new Effect.Scale('iframe_box_'+iframe_id, 
						height_perc,
						{
							scaleX:false
						}),
				new Effect.Scale('iframe_box_'+iframe_id, 
						width_perc,
						{
							scaleY:false
						}),
				new Effect.MoveBy('iframe_box_'+iframe_id, 
						top_move,
						left_move)
				],
				{
					afterFinish: function(effect)
					{
						document.getElementById('iframe_minimize_'+iframe_id).style.display='block';
						document.getElementById('iframe_maximize_'+iframe_id).style.display='block';
						document.getElementById('iframe_close_'+iframe_id).style.display='block';
						document.getElementById('iframe_element_'+iframe_id).style.display='block';
						document.getElementById('iframe_element_'+iframe_id).style.height=(dest_height-27)+'px';
						
						window.IframesEx[iframe_id]['status']='open';
						RemoveTask(window.IframesEx[iframe_id]['task_id']);
						window.setTimeout("PerformIframes();", 200);
					}
				}
				);
}


function RemoveTask(task_id)
{
	window.IframeTaskManager.splice(task_id, 1);
	
	for (id = task_id; id < window.IframeTaskManager.length; id++)
	{
		new Effect.MoveBy('iframe_box_'+window.IframeTaskManager[id], 0, -120);	
		
		window.IframesEx[window.IframeTaskManager[id]]['task_id']--;
	}
	
	
}

function AddIframe2TaskManager(iframe_id)
{
	var task_id = window.IframeTaskManager.length;
	
	window.IframeTaskManager[task_id] = iframe_id;
	
	window.IframesEx[iframe_id]['task_id']=task_id;
}

function OpenExtern(url)
{
	//OpenIframeEx(url+"?width=900&height=600", url);	
	
	/* 	DEPRECATED
			
		IframeActivated=1;
		
		var width=900;
		var height=600;
		var top=50;
		var left=(screen.availWidth/2)-(width/2);		
		document.getElementById("iframe_box").style.top=top+'px';
		document.getElementById("iframe_box").style.left=left+'px';
		document.getElementById("iframe_box").style.width=width+'px';
		document.getElementById("iframe_box").style.height=height+'px';
		document.getElementById("iframe_frame").style.visibility='visible';
		
		document.getElementById("iframe_element").src=url;
		document.getElementById("iframe_element").height=(height-23)+'px';
		
	*/
	
	
	window.open(url, 'EXTERN');
	
}


function CloseIframe()
{
		IframeActivated=0;
	
		document.getElementById("iframe_frame").style.visibility='hidden';	
		document.getElementById("iframe_element").src="blank.html";	
}


function CmsOpenSoon(url)
{
	window.setTimeout("CmsOpen('"+url+"');", 1500);

}

function CmsOpen(url)
{
	
	location.hash = url;
	
	showLoadingIcon();
	
	Debugger.StartIframe();
	
	url=Base64.encode(url);
	href="control_frame.php?url="+url;

	window.frames['control_frame'].location.href=href;

	
	//RealCmsOpen(url);
}

function CmsHistory(code)
{
	hist = PasswordGenerator(5);
	
	window.pre_history[hist] = code;
	
	href="control_frame.php?history="+hist;

	window.frames['control_frame'].location.href=href;

}


function RealCmsHistory(hist)
{
	if (typeof(window.cms_history[hist]) == 'undefined')
	{
		window.cms_history[hist] = window.pre_history[hist];
	}
	else
	{
		eval(window.cms_history[hist]);
	}
}


function showLoadingIcon()
{
	$('loading_icon').style.visibility = 'visible';
	
}

function hideLoadingIcon()
{
	$('loading_icon').style.visibility = 'hidden';
	
	StopMsg();
}


function RealCmsOpen(url)
{
	url=Base64.decode(url);
			
	if (OnExit != "")
	{
		eval(OnExit);
		OnExit="";
	}
	
	try 
	{
		Debugger.StopIframe();
		Debugger.StartJS();
	}
	catch(e)
	{

	}
	
	
	var Querys = url.split("&");
	SelectName="";
	var anz=Querys.length;
	var iframe="";
	for(n=0; n < anz; n++)
	{
		tmp=Querys[n].split("=");
		name=tmp[0];
		value=tmp[1];
		if (name == "iframe") iframe=value;
	} 
	if (iframe != "")
	{
		OpenIframe(url);
	}
	else
	{
		iterator++;
		
		try
		{
			ElusiveAjax.Update(url);
		}
		catch(e)
		{}
	
	}
	
	
}




function alert(txt)
{
     
  	alertType="alert";      
	
	try
	{
			
		document.getElementById("MsgBox").style.display="block";
		document.getElementById("MsgBox").style.zIndex=1001;
		document.getElementById("MsgBox").style.visibility='visible';	
		
		document.getElementById("Body_object").style.zIndex=1001;
		document.getElementById("Body_object").style.position="absolute";
	
		document.getElementById("MsgTxt").firstChild.nodeValue=txt;
		document.getElementById("MsgTxt2").firstChild.nodeValue="bitte best&auml;tigen...";
		document.getElementById("MsgButton").innerHTML="<button onClick=\"StopAlert();\"> &nbsp; &nbsp; OK &nbsp; &nbsp; </button>";
		
		//document.getElementById("RealMsgBox").style.display="none";
	
		new Effect.BlindDown("RealMsgBox",
				{
					duration: 0.5,
					from: 0.0,
					to: 1.0
				});	
		
	}
	catch(e)
	{
		//document.body.innerHTML+="<b>Msg ("+e.description+")</b><br>"+txt+"<hr>";
		throw e;
	}
		
}


function StopAlert()
{
	if (alertType == "alert")
    {
        	new Effect.Fold("RealMsgBox",
        		{
					duration: 0.5,
        			afterFinish:function() {
        				//document.getElementById("MsgBox").style.display="none"; 
        				//document.getElementById("RealMsgBox").style.display="block";
        				alertType="info";
        				StopMsg();
        			}
        		}
        		);
        	
        }
	
}



function SetMsg(txt)
{
	try
	{	
		
		if (alertType != 'alert')
		{
			alertType="info";
		
		
			document.getElementById("RealMsgBox").style.display="block";
			document.getElementById("MsgBox").style.zIndex=1001;
			document.getElementById("MsgBox").style.visibility="visible";
			
			document.getElementById("Body_object").style.zIndex=1001;
			document.getElementById("Body_object").style.position="absolute";
			
			
			document.getElementById("MsgTxt").firstChild.nodeValue=txt;
			document.getElementById("MsgTxt2").firstChild.nodeValue="bitte warten!";
			
			document.getElementById("MsgButton").innerHTML="<img src=loading.gif alt=loading... \>";
		}	
		
		
	}
	catch(e)
	{
	}

}



function StopMsg()
{
 	try
 	{
		if (alertType == "info")
   		{
			document.getElementById("MsgBox").style.zIndex=-1001;
			document.getElementById("MsgBox").style.visibility="hidden";
			
			document.getElementById("Body_object").style.position="absolute";
			document.getElementById("Body_object").style.zIndex=-1001;
			
		}
		else if (alertType == "firstTime")
		{
			alertType="info";
		
			ElusiveStatus="ready";
	
			document.getElementById("MsgBox").style.zIndex=-1001;
			document.getElementById("MsgBox").style.visibility="hidden";
			
			document.getElementById("Body_object").style.zIndex=-1001;
			document.getElementById("Body_object").style.position="absolute";

		}

		ElusiveStatus="ready";

		Behaviour.apply();
	}
	catch(e)
	{
	}
		
}



function SetMsgEffect(txt)
{
	

	SetMsg(txt);
	
	
	
	
}


function BrowserDetection()
{
	SetMsg("Browser wird identifiziert");
	if (navigator.appName == "Opera") browser="Opera";
	else if (navigator.appName == "Microsoft Internet Explorer") browser="MSIE";
	else browser="Gecko";
	SetMsg("Browser: "+browser);
	
}

function AvatarReady(AvatarHash, AvatarId)
{
	
		document.getElementById("img_avatar_"+AvatarId).innerHTML="<img src='backend/data/avatars/"+AvatarHash+"'><input type=hidden name=hash_avatar_"+AvatarId+" value='"+AvatarHash+"'\>";
		CloseIframe();
		
		
}


function FileUploadReady(hash, file_name)
{
		CloseIframe();
    UpdateCMS("cms_mode=update&module=forum&action=AddFile&hash="+hash+"&name="+file_name);

	
}


function CmsPrint()
{
	window.PrintWindow = window.open('print.html', "Druckfenster", "width=950,height=600,scrollbars=yes");
	
	window.setTimeout('CmsPrintNow()', 1000);
	
	

}

function CmsPrintNow()
{
	PrintWindow=window.PrintWindow;
	
	if (document.styleSheets[0].cssRules)
	{
		rules=document.styleSheets[0].cssRules;
		for (n = 0; n < rules.length; n++)
		{
			text=rules[n].selectorText+" {"+rules[n].style.cssText+"}";
			//alert(text);
			PrintWindow.document.styleSheets[0].insertRule(text, 0);
		}
	}
	else
	{
		rules=document.styleSheets[0].rules;
		for (n = 0; n < rules.length; n++)
		{
			//alert(rules[n].selectorText+"\n"+rules[n].style.cssText);
			PrintWindow.document.styleSheets[0].addRule(rules[n].selectorText, rules[n].style.cssText);
		}
	}
	
	
	//document.styleSheets[0] = parent.document.styleSheets[0];
	//rules=document.styleSheets[0].cssRules;
	//alert(rules.length);
	
		
	module_html='<div class="Module" id="Module" style="margin-left: 10px; margin-right:10px;padding-top: 10px;">'+document.getElementById('ModuleContent').innerHTML+"</div>";
	//document.write(module_html);
	
	//alert(module_html);
	
	PrintWindow.document.body.innerHTML=module_html;
	
  	PrintWindow.focus();
	
	PrintWindow.print();
}

function onHistorySelect(object_id, data_id)
{
	$(object_id).value=data_id; 
	$(object_id+"_history").style.display="none";
	
	try
	{
		$(object_id).onchange();
	}
	catch (e)
	{
		
	}
}


function openObjectProtocol(class_name, id)
{
	url = "iframe.php5?iframe=dataobjects_protocol&class="+class_name+"&id="+id+"&width=800&height=600";
	
	OpenIframeEx(url, "Objekt-Protokoll");
	
}

function PasswordGenerator(len)
{
	conso = new Array("b","c","d","f","g","h","j","k","l","m","n","p",
	"r","s","t","v","w","x","y","z");
	vocal = new Array("a","e","i","o","u");
	var password = '';
	
	for(i=0; i < len; i++)
	{
		var c = Math.ceil(Math.random() * 1000) % 20;
		var v = Math.ceil(Math.random() * 1000) % 5;
		password += conso[c] + vocal[v];
	}
	return password;
}


function keyhandler(evt)
{
	evt = allEve(evt);
	if (evt) 
	{
		
		if (evt.key == 176) 
		{
			openConsole();
		}
		
	}
}









function allEve(e){
	var ev= (window.event)? window.event: e;
	if(!ev || !ev.type) return false;
	var ME= ev;
	
	if(ME.type.indexOf('key')!= -1){
		if(isIE || ME.type.indexOf('keypress')!= -1){
			ME.key= (ev.keyCode)? ev.keyCode: ((ev.charCode)? ev.charCode: ev.which);
		}
		else ME.key= ev.charCode;
		if(ME.key) ME.letter= String.fromCharCode(ME.key);
	}
	return ME;
}

function openConsole()
{
	
	title = 'Konsole';
	
	query = 'divframe=console&title='+title+'&height=450&width=900';
		
	OpenIframe(query);
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
  }
  
function encode_utf8(rohtext) {
             // dient der Normalisierung des Zeilenumbruchs
             rohtext = rohtext.replace(/\r\n/g,"\n");
             var utftext = "";
             for(var n=0; n<rohtext.length; n++)
                 {
                // ermitteln des Unicodes des  aktuellen Zeichens
                 var c=rohtext.charCodeAt(n);
                 // alle Zeichen von 0-127 => 1byte
                 if (c<128)
                     utftext += String.fromCharCode(c);
                 // alle Zeichen von 127 bis 2047 => 2byte
                 else if((c>127) && (c<2048)) {
                     utftext += String.fromCharCode((c>>6)|192);
                     utftext += String.fromCharCode((c&63)|128);}
                 // alle Zeichen von 2048 bis 66536 => 3byte
                 else {
                     utftext += String.fromCharCode((c>>12)|224);
                     utftext += String.fromCharCode(((c>>6)&63)|128);
                     utftext += String.fromCharCode((c&63)|128);}
                 }
             return utftext;
}

function decode_utf8(utftext) {
             var plaintext = ""; var i=0; var c=c1=c2=0;
             // while-Schleife, weil einige Zeichen uebersprungen werden
             while(i<utftext.length)
                 {
                 c = utftext.charCodeAt(i);
                 if (c<128) {
                     plaintext += String.fromCharCode(c);
                     i++;}
                 else if((c>191) && (c<224)) {
                     c2 = utftext.charCodeAt(i+1);
                     plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
                     i+=2;}
                 else {
                     c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
                     plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
                     i+=3;}
                 }
             return plaintext;
}


function LoadFirstUrl()
{
	anker = window.location.hash;
	
	anker=anker.replace(/^#/, "");

	path = window.location.pathname;
	
	if (anker)
	{
		UpdateCMS(anker);
	}
	else if (path.substr(0, 4) == '/go/')
	{
		var parts = new Array();
		
		parts = path.split("/");
		
		plength = parts.length;
		
		plength -= 1;

		url = "";
	
		for (i = 2; i < plength; i++)
		{
			url += parts[i] + '=' ;
			
			i++;
			
			if (i < plength)
				url += parts[i] + '&';
		}
		
		UpdateCMS(url);
	
	}
	else if (path.substr(0, 6) == '/page/')
	{
		
		//document.body.innerHTML = 'page';
		
		var parts = new Array();
		
		parts = path.split("/");
		
		plength = parts.length;
		
		plength -= 1;

		var page = parts[plength];
		
		parts = page.split("\.");

		url = "module=pages&page="+parts[0];
		
		//document.body.innerHTML = url;
		
		UpdateCMS(url);
	}
	else
	{
		//document.body.innerHTML = 'std: '+path.substr(0, 7);
		
		UpdateCMS(anker);
	
	}
		
	
	//$('loading_msg').innerHTML = 'Path: '+path+' / Url: '+url + ' / Length: '+length;
}

