var ns = document.layers ? true : false;
var ie = document.all && !window.opera ? true : false;;    // IEs
var dom = document.getElementById && !ie ? true : false;  // DOM-Browser

var gbloading = "<img src='images/loading.gif' hspace='20' vspace='20' /><br />";

function warenkorbPos() {
/*	$('warenkorb_overlay').clonePosition($('warenkorb_position'));
	posy = parseInt($('warenkorb_overlay').style.top.replace("px",""));
	posx = parseInt($('warenkorb_overlay').style.left.replace("px",""));
	posx+=84;
	posy+=111;
	$('warenkorb_overlay').style.top = posy+"px";
	$('warenkorb_overlay').style.left = posx+"px";
	$('warenkorb_overlay').show();
	*/
}

function changeFilter(eid,wert,pk){
	new Ajax.Request("ajax/changefilter.php?eid="+eid+"&wert="+escape(wert)+"&pk="+pk+"&search="+escape($('filter_search').value), {
		method: "get",
		onSuccess: function(t) {
			if($('hiddenDiv')) $('hiddenDiv').update(t.responseText);
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		},
		evalScript: true,
		asynchronous: true
		}
	);
}

function openWK(step){
	window.location.href="?kat=14&step="+step;	
}

function loginPos() {
	if($('image_right')) {
		var pos = $('image_right').positionedOffset();
		$('login_layer').style.top = pos[1]+84+"px";
		$('login_layer').style.left = pos[0]+100+"px";
	}
}

function languagePos() {
	if($('image_right')) {
	var pos = $('image_right').positionedOffset();
	var lnpos = $('language_td').positionedOffset();
	$('language_div').style.top = lnpos[1]+pos[1]+106+"px";
	$('language_div').style.left = lnpos[0]+pos[0]+100+"px";
	$('language_div').style.width = $('language_td').getWidth()+15+"px";
	}
}

function sucheNavi() {
	if($('suche_navi')){
	horpos = $('horbar').positionedOffset();
	$('suche_navi').style.top = horpos[1]+"px";
	$('suche_navi').style.left = horpos[0]+$('horbar').getWidth()+"px";
	}
}

function callOnLoad() {
	callOnResize();
}

function to_session(k,v){
	request('get','ajax/to_session.php?'+k+'='+v,'hiddenDiv');
}

function switchFH(id){
	if($("fhhead"+id)){
		ns=($("fhhead"+id).className.indexOf('off')>0?"on":"off");
		$("fhhead"+id).className="fhclass_"+ns;
		// hier könnte man noch was mit slide-effekten machen...
		if(ns=="on") $("fhdiv"+id).show();
		else $("fhdiv"+id).hide();
		to_session("fh_classes_"+id,ns);
	}
}

function callOnResize() {
	if($('suche_layer')){
	$('suche_layer').clonePosition($('suche_link'), true, true, false, false);
	var or_pos=$('suche_layer').cumulativeOffset();
	$('suche_layer').style.top=(or_pos[1]-51)+"px";
	}
	//$('suche_layer').hide();
	warenkorbPos();
	scrollContent();
	loginPos();
	languagePos();
	sucheNavi()
	if($('userdata')){
		var user_pos=$('user_pos').cumulativeOffset();
	//	alert(user_pos);
		$('userdata').style.left=Math.floor(user_pos[0]+586)+"px";
		$('userdata').style.top =Math.floor(user_pos[1]-10)+"px";
		//alert($('userdata').cumulativeOffset().join(","));
		//$('userdata').update(user_pos.join(",") + " offset: "+$('userdata').cumulativeOffset().join(","))
	}
}

function sizeToSession(breite,hoehe) {
	new Ajax.Request('ajax/sizeToSession.php?breite='+breite+'&hoehe='+hoehe, {
		method: 'get',	
		onSuccess: function(t) {
			//$('modulecontent').update(t.responseText);
		},
		onFailure: function(t) {
			//$('modulecontent').update('Error ' + t.status + ' -- ' + t.statusText);
		},
		evalScript: true,
		asynchronous: true
		}
	);
}

function reposNavi(){
	var size = getPageSize();
	mx = (size[2] -1000) / 2;
	if(mx<0) mx = 0;
	$('navi').style.left = mx+"px";
	posSubnavi();
}
	
function scrollContent(){
		
	var arrayPageSize = getPageSize();
	if(ie) abzug = 322;
	else abzug = 346;
	
	abzug-=196;
	
	var screenBreite = arrayPageSize[0];
	
	if($('scenebild')){
		var sch=$('scenebild').getHeight();
		abzug+=sch;
	}
	var screenHoehe = (arrayPageSize[3]-abzug);
		
	sizeToSession(screenBreite,screenHoehe);

//alert(abzug);
	if($('inhalt_scroll')){
		//var pgSz=getPageSize();
		//var inh_scr=$('inhalt').getHeight();
		/*if(inh_scr>400) inh_scr=400;
		fspacer=abzug;
		
		if((pgSz[1]-fspacer) > inh_scr) pgs = inh_scr;
		else 
		*/
		$('inhalt_scroll').style.height=screenHoehe+"px";	
	}
	if($('infobox')) $('infobox').style.height=Math.floor(screenHoehe+$('erste_ebene').getHeight())+"px";
	if($('navi')) $('navi').style.height=Math.floor(screenHoehe)+"px";
}

function loading(){
	if($('scroll_content') && $('loading_div')){
	Element.clonePosition($('loading_div'),$('scroll_content'));
	$('loading_div').show();
	//alert($('loading_div').getHeight());
	}
}
function unloading(){
	if($('loading_div')) $('loading_div').hide();	
}

function request(method,url,ziel,postform,loadingani,newserialize){
	if($(loadingani)) $(loadingani).show();
	else loading(); //$(ziel).update("<img src='images/loading_sitemap.gif' />");
	//alert(loadingani);
	if(method=='post' || method=='POST'){
		//alert(postform);
		if(newserialize) var pbody=$(postform).serialize(true);
		var pbody=$(postform).serialize();
	}else var pbody="";
	new Ajax.Request(url, {
		method: method,
		postBody: pbody,
		onSuccess: function(t) {
			$(ziel).update(t.responseText);
			if($(loadingani)) $(loadingani).hide();
			else unloading();
		},
		onFailure: function(t) {
			$(ziel).update('Error ' + t.status + ' -- ' + t.statusText);
			if($(loadingani)) $(loadingani).hide();
			else unloading();
		},
		evalScript: true,
		asynchronous: true
		}
	);
	window.setTimeout("callOnLoad();",500);
}

function doAjax(method,url,ziel,postform,loadingani,newserialize){
	

//alert("request('"+method+"','"+url+"','"+ziel+"','"+postform+"',"+newserialize+")");
	var ajaxString=escapeTxt("request('"+method+"','"+url+"','"+ziel+"','"+postform+"','"+loadingani+"',"+newserialize+")");
	
	if($('hiddenFrame')){
		$('hiddenFrame').src='history.php?request='+ajaxString;
		//alert(ajaxString);
		//alert("OK");
	}
/*
	if(method=='post' || method=='POST'){
		if(newserialize) var pbody=postform.serialize(true);
		var pbody=postform.serialize();
	}else var pbody="";
	new Ajax.Request(url, {
		method: method,
		postBody: pbody,
		onSuccess: function(t) {
			$(ziel).update(t.responseText);
		},
		onFailure: function(t) {
			$(ziel).update('Error ' + t.status + ' -- ' + t.statusText);
		},
		evalScript: true,
		asynchronous: true
		}
	);
	*/
}

function escapeTxt(os){
var ns='';
var t;
var chr='';
var cc='';
var tn='';
for(i=0;i<256;i++){
tn=i.toString(16);
if(tn.length<2)tn="0"+tn;
cc+=tn;
chr+=unescape('%'+tn);
}
cc=cc.toUpperCase();
os.replace(String.fromCharCode(13)+'',"%13");
for(q=0;q<os.length;q++){
t=os.substr(q,1);
for(i=0;i<chr.length;i++){
if(t==chr.substr(i,1)){
t=t.replace(chr.substr(i,1),"%"+cc.substr(i*2,2));
i=chr.length;
}}
ns+=t;
}
return ns;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight =  yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function getArt(gid){
	if(gid>0){
		var pBody=$('eigform'+gid).serialize();
		if($('eig2form'+gid)) pBody=pBody+"&"+$('eig2form'+gid).serialize();
		if($('eig3form'+gid)) pBody=pBody+"&"+$('eig3form'+gid).serialize();
		pBody=pBody+"&gid="+gid;
	}
	else var pBody="";
	new Ajax.Request('../ajax/eigenschaften2art.php',
	{
		method: 'post',
		postBody: pBody,
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		  // $('loading'+gid).hide();	
		   $('ajaxlayer').update(t.responseText);
		  // alert(t.responseText);
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);	
}

function updateImage(targetImage,fileRequest,w,h){
	if(targetImage){
		//
		// loadingLayer > cloneposition von targetImage
		// laden anzeigen
		// wenn bild da ist, ersetzen und laden ausblenden
		// wenn nicht, fehler?
		if($('loadingLayer')){
			with($('loadingLayer')){
				clonePosition(targetImage);
				update("<img src='images/loading.gif'><img src='images/loading_font.gif'>");
				show();
			}
		}
		new Ajax.Request('../ajax/makeThumb.php?f='+fileRequest+'&w='+w+'&h='+h,
		{
			method: 'get',
			asynchronous: true,
			evalScript: true,
			onComplete: function(t) {
			  // $('loading'+gid).hide();	
			   targetImage.src=t.responseText;
			   $('loadingLayer').hide();
			  // alert(t.responseText);
			},
			// Handle other errors
			onFailure: function(t) {
				alert('Error ' + t.status + ' -- ' + t.statusText);
				$('loadingLayer').hide();
			}		
		}
		);	
	}
}
function add2wk(form){
	new Ajax.Request('ajax/warenkorb.php',
		{
			method: 'post',
			postBody: 'action=add&'+form.serialize(),
			asynchronous: true,
			evalScript: true,
			onComplete: function(t) {
			//alert(t.responseText);
			   userdata(t.responseText);
			   //alert(t.responseText);
			},
			// Handle other errors
			onFailure: function(t) {
				alert('Error ' + t.status + ' -- ' + t.statusText);
			}		
		}
	);
}
function number_format (zahl,decimal_num,dec_point,mill_sep) {
  var sign = '';
  if(zahl < 0) {sign = '-';}
  zahl = Math.abs(zahl);
  var divisor = 1;
  for(var i = 0; i < decimal_num; i++) {zahl *= 10; divisor *= 10;}
  zahl = Math.floor(zahl);
  var decimals= zahl % divisor;
  decimals = '' + decimals;
  for(var i = 0; i < decimal_num; i++) {zahl /= 10;}
  zahl = Math.floor(zahl);
  zahl = '' + zahl;
  zahl_grp = '';
  
  while(zahl.length > 3) {
    zahl_grp = mill_sep + zahl.substr(zahl.length-3,3) + zahl_grp;
    zahl = zahl.substr(0,zahl.length-3);
  }
  var str = zahl + zahl_grp;
  if(decimal_num > 0) {
    str = str + dec_point;
	//alert(decimals.length + " - " + decimal_num);
    //while(decimals.length != decimal_num) {decimals= decimals+ '0';}
	while(decimals.length < decimal_num) {decimals+='0';}
    str = str + decimals
  }
  return sign + str;
}
function openLogin() {
	showGrayBox();
	$('gray_box_content').update(gbloading);
	new Ajax.Request('modules_gb/login.php',
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {	
		   $('gray_box_content').update(t.responseText);
		   center('gray_box');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

function myaccount() {
	showGrayBox();
	$('gray_box_content').update(gbloading);
	new Ajax.Request('modules_gb/myaccount.php',
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {	
		   $('gray_box_content').update(t.responseText);
		   center('gray_box');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

function img_zoom(i,w,h){
 hideSelect();

 showGrayBox();
 $('gray_box_content').innerHTML = "<table border='0' cellspacing='0' width='"+(w+10)+"' height='"+(h+10)+"' cellpadding='5'><tr><td align='center'><img src='"+i+"?w="+w+"&h="+h+"' onClick='hideGrayBox();' title='Bild anklicken zum schliessen.' alt='Bild anklicken zum schliessen.'><br><br><a href='javascript:;' onClick='hideGrayBox()'>Fenster schliessen</a><br>&nbsp;</td></tr></table>";
 center('gray_box');
}

function logout(){
	userdata("&logout=true");	
}

function userdata(){
	var typus = "";
	if($('userdata')){
		if(userdata.arguments.length>0){
			var pBody="&message="+userdata.arguments[0];
		}
		else if($('loginform')){
			var pBody=$('loginform').serialize();
			typus = "login";
		}	else if(userdata.arguments[0]) {
			var pBody="highlight="+userdata.arguments[0];
		}
		
		else var pBody="";
		new Ajax.Request('ajax/userdata.php',
		{
			method: 'post',
			postBody: pBody,
			asynchronous: true,
			evalScript: true,
			onComplete: function(t) {
				if(typus == "login") $('login_result').update(t.responseText);
				else $('userdata').update(t.responseText);
			},
			onFailure: function(t) {
				$('userdata').update('Error '+t.status+' -- '+t.statusText);
			}
		}
		);	
	}
}
var udatatimeout;
function userdata_message(msg){
	//alert(msg);
	if(udatatimeout) window.clearTimeout(udatatimeout);
	var ud_pos=$('userdata').cumulativeOffset();
	//alert(ud_pos);
	$('userdata_message').style.top=Math.floor(ud_pos[1]+37)+"px";
	$('userdata_message').style.left=ud_pos[0]+"px";
	var oldBorder=$('userdata').style.borderBottom;
	//alert(oldBorder);
	$('userdata').style.borderBottomStyle='none';
	//$('userdata_message').style.width=$('userdata').getWidth()+"px";o
	//$('userdata_message').clonePosition($('userdata'), true, true, false, 21);
	//alert($('userdata_message').cumulativeOffset());
	
	//$('userdata_message').show();
	//var or_pos=$('wkimg'+art).cumulativeOffset();
	//$('wkbox'+art).style.left=(or_pos[0]-158)+"px";
	
	//
	// effect slide funzt nicht...position TOP ok...
	//
	
	//$('userdata_message').show();
	//alert($('userdata_message') + '\n' + $('userdata_message').style.display)
	$('userdata_message').update("<table border='0' cellspacing='0' cellpadding='0' width='100%'><tr><td align='left'><div>"+Base64.decode(msg)+"</div></td></tr></table>");
	//Effect.toggle('userdata_message');
	Effect.SlideDown('userdata_message',{
	 duration:1,
	 afterFinish: function(){ 
	 udatatimeout=window.setTimeout("userdata_messageUP();",5000);
	 udataborder=window.setTimeout("$('userdata').style.borderBottom='"+oldBorder+"';",6000);
	 
	 }
	 }
	 );
}
function userdata_messageUP(){
	//Effect.toggle('userdata_message');
	Effect.SlideUp('userdata_message');
}

function openLanguageLayer() {
	showLanguage();
}

var languageAktiv = false;
var languageTimeout;

function showLanguage() {	
	if(languageAktiv == false) {
		window.clearTimeout(languageTimeout);
		$('language_div').show();
		Effect.toggle('language_inner','slide', {duration: 0.5});
		languageTimeout=window.setTimeout("language_inner = true;",500);
	} else {
		window.clearTimeout(languageTimeout);
		Effect.toggle('language_inner','slide', {duration: 0.5, afterFinish: language_inner});
		languageTimeout=window.setTimeout("language_inner = false;",500);
	}
}

function hideLanguage() {
	$('language_div').hide();
}

function openSucheLayer() {
	showSuche();
}

function searchSite(offset) {
	new Ajax.Request('ajax/searchToSession.php?search='+Base64.encode($('search').value),
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {	
		   //loadModule(10, '&offset='+offset);
			if(offset == -1) {
				Effect.toggle('suche_inner','slide', {duration: 0.5, afterFinish: hideSuche});
				sucheAktiv = false;
			}
			location.href = "?kat=15&offset="+offset;
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);	
	
}

var sucheAktiv = false;
var sucheTimeout;

function showSuche() {	
	if(sucheAktiv == false) {
		window.clearTimeout(sucheTimeout);
		$('suche_layer').show();
		Effect.toggle('suche_inner','slide', {duration: 0.5});
		sucheTimeout=window.setTimeout("sucheAktiv = true;",500);
	} else {
		window.clearTimeout(sucheTimeout);
		Effect.toggle('suche_inner','slide', {duration: 0.5, afterFinish: hideSuche});
		sucheTimeout=window.setTimeout("sucheAktiv = false;",500);
	}
}

function hideSuche() {
	$('suche_layer').hide();
}

function pwVergessen() {
	$('gray_box_content').innerHTML = gbloading;
	new Ajax.Request('modules_gb/lostpw.php',
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {	
		   $('gray_box_content').update(t.responseText);
		   center('gray_box');
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

function changepw() {
	$('gray_box_content').innerHTML = gbloading;
	new Ajax.Request('modules_gb/changepw.php',
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {	
		   $('gray_box_content').update(t.responseText);
		   window.setTimeout("center('gray_box');", 100);
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}

function sitemapGetContent(top_id, level) {
	h = $('content_level_'+level).getHeight();
	$('content_level_'+level).innerHTML = "<table border='0' cellspacing='0' cellpadding='0' width='100%' height='"+h+"'><tr height='"+h+"'><td height='"+h+"' align='center' valign='middle' style='border-bottom: none;'><img src='images/loading_sitemap.gif' /></td></tr></table>";
	new Ajax.Request('ajax/getSitemap.php?top_id='+top_id+'&level='+level,
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {	
		   $('content_level_'+level).update(t.responseText);
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);	
}

  ////////////////////////////////////////////////////////////////
 // POPUP FUNKTION											   //
////////////////////////////////////////////////////////////////

Xoffset=10;   // modify these values to ...
Yoffset=10;    // change the popup position.
var x = 0;
var y = 0;
document.onmousemove=get_mouse;
	
function popup(text){
	if(text != '') {
		var content= "<table border='0' cellspacing='0' cellpadding='2'><tr><td>"+text+"</td></tr></table>";
		$('hoverdiv').innerHTML=content;
		$('hoverdiv').style.left=(x+Xoffset)+"px";
		$('hoverdiv').style.top=(y+Yoffset)+"px";
		$('hoverdiv').show();
	}
}

function sitemapPopup(url){
	$('hoverdiv').show();
	$('hoverdiv').innerHTML="<table border='0' cellspacing='0' cellpadding='2'><tr><td><img src='images/loading_small2.gif' /></td></tr></table>";
	
	$('hoverdiv').style.left=(x+Xoffset)+"px";
	$('hoverdiv').style.top=(y+Yoffset)+"px";
	
	new Ajax.Request('ajax/sitemap.php?url='+url,
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {	
		   $('ausgabe').update("<table border='0' cellspacing='0' cellpadding='2' width='300'><tr><td style='width: 300px;font-size: 11px;'>"+t.responseText+"</td></tr></table>");
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}		
	}
	);
}
	
function get_mouse(e){
	if(!e) e = window.event;
	x = e.clientX+document.documentElement.scrollLeft;
	//	if (document.documentElement.scrollTop!=0) alert(document.documentElement.scrollTop );
    y = e.clientY+document.documentElement.scrollTop ;
}
function kill(){
	$('hoverdiv').hide();	
}