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

/*
  ////////////////////////////////////////////////////////////////
 // MAIN FUNKTIONEN											   //
////////////////////////////////////////////////////////////////
*/

var loadingMainContent = "<table border='0' cellspacing='0' celpadding='0' width='100%'><tr><td style='padding-top: 200px; text-align: center;'><img src='images/loading/maincontent.gif' /><br /></td></tr></table>";

var activeModule=false;
var activeFotoLeiste = false;

var activeTN; // aktive TopNav
var activeSN; // aktive SubNav

var newOverflow;

function loadModule(id){
	var jetzt = new Date();
	var tc=jetzt.getTime();
	var funcstring="modinclude(";
	for(var i=0; i < loadModule.arguments.length; i++)
  		{
			funcstring+='"'+loadModule.arguments[i]+'"';
			if((i+1) < loadModule.arguments.length) funcstring+=',';
		}
	funcstring+=')';

	$('historyframe').src='history.php?tc='+tc+'&callback='+escape(funcstring)+'#'+jetzt.getTime();
}

function content(id){
	if(id<1) id=1;
	if($('content')){
		$('content').update("<br><br><br><center><img src='images/loading_small2.gif'><img src='images/loading_font.gif'></center>");
		new Ajax.Request('content.php?kat='+id,
		{
			method: 'get',
			asynchronous: true,
			evalScript: true,
			onComplete: function(t) {
			   $('content').update(t.responseText);
			   //spanToFlash();
			   $('header_cell').style.height=$('header').style.height;
			   window.setTimeout("callResizeFunctions();",100);
			   window.setTimeout("callResizeFunctions();",1000);
			   window.setTimeout("callResizeFunctions();",2000);
			},
			// Handle other errors
			onFailure: function(t) {
				alert('Error ' + t.status + ' -- ' + t.statusText);
			}
		}
		);
	}
}

function callResizeFunctions() {

	//alert($('maincontent').style.paddingTop + " - " + $('maincontent').style.paddingBottom + " - " + $('maincontent').style.height);
	var arrayPageSize = getPageSize();
	//alert(arrayPageSize);
	if(ie) abzug = 280;
	else abzug = 280;
	/*
	if(ie) abzug2 = 173;
	else abzug2 = 187;
	*/


	var screenBreite = arrayPageSize[0];
	var screenHoehe = (arrayPageSize[3]-abzug);
	//if(screenHoehe<320){
	//	screenHoehe+=186;
	//	$('header_swf').hide();
	//	$('header_jpg').show();
	//}else{
		$('header_jpg').hide();
		$('header_swf').show();
	//}
return;
	sizeToSession(screenBreite,screenHoehe);
	$('mainTable').style.height=screenHoehe+'px';
	if($('content_scroll')) {
		$('content_scroll').style.height=screenHoehe+'px';
		$('content').style.height=screenHoehe+'px';
	}

}

function modinclude(id) {
	//var tc=
	if(modinclude.arguments.length > 1) sid = modinclude.arguments[1];
	else sid = "";

	$('maincontent').show();
	$('maincontent').innerHTML = loadingMainContent;
	new Ajax.Request('modinclude.php?kat='+id+'&sid='+sid,
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		   $('maincontent').update(t.responseText);
		   spanToFlash();
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	);
}

var lastrequest;

function history(func) {
  var jetzt = new Date();
  var tc=jetzt.getTime();
  var funcstring=func+"(";
  for(var i=1; i < history.arguments.length; i++)
	{
		funcstring+='"'+history.arguments[i]+'"';
		if((i+1) < history.arguments.length) funcstring+=',';
	}
  funcstring+=')';
  $('historyframe').src='history.php?tc='+tc+'&callback='+escape(funcstring)+'#'+jetzt.getTime();
}

function setHash(hash){
	window.location.hash=hash;
}

function spanToFlash() {
	if(sIFR != null && sIFR.replaceElement != null){
		sIFR.replaceElement("span.inhalt_header", "images/inhalt/arial.swf", "#727271", null, null, null, 0, 0, 0, 0, null, null, "transparent");
	}
}

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
		}
	);
}

/*
  ////////////////////////////////////////////////////////////////
 // GRAYBOX FUNKTIONEN										   //
////////////////////////////////////////////////////////////////
*/

function showGrayBox() {
	center('gray_box');
	$('gray_box').show();
	//$('gray_box').className = 'op';
	new Effect.Fade('gray_box',
			{ duration: 1.0,
			  from: 0.0, to: 0.99 });
	$('gray_overlay').show();
	hideSelect();
}

function hideGrayBox(){
   new Effect.Opacity('gray_box',
			{ duration: 0.5,
			  transition: Effect.Transitions.linear,
			  from: 0.99, to: 0.0,
			  afterFinish: hidegb});
    setTimeout("$('gray_overlay').hide();",500);
	setTimeout("$('gray_overlay').style.height = '1px';",500);
	/*
	if($('fotogalerie_frame').$('objectleiste') || $('fotogalerie_frame').$('embedleiste')) {
		if(ie) $('fotogalerie_frame').$('objectleiste').SetVariable("signal",true);
		else $('fotogalerie_frame').$('embedleiste').SetVariable("signal",true);
	}*/
}

function galerie(num) {
	$('g_loading').show();
	if(num) $('dspl_num').value=num;
	else $('dspl_num').value='0';
	new Ajax.Request('ajax/galerie.php',
	{
		method: 'post',
		postBody: $('galleryf').serialize(),
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		   $('g_loading').hide();
		   $('galerie').update(t.responseText);
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	);
}

function center(element){
    try{
        element = $(element);
    }catch(e){
        return;
    }

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	//alert(window.document.body.offsetHeight + " - " + document.body.clientHeight + " - " + document.documentElement.clientHeight + " - " + window.innerHeight + " - " + document.documentElement.scrollTop);

	element.style.position = 'absolute';
    element.style.zIndex   = 999;

    var elementDimensions = Element.getDimensions(element);

	//alert(elementDimensions.width + " " + element +" " + elementDimensions.height);
	topsize = arrayPageScroll[1] + ((arrayPageSize[3] - elementDimensions.height) / 2);
	if(topsize < 5) topsize = 5;

	element.style.top = (topsize + 'px');
	element.style.left = (((arrayPageSize[0] - elementDimensions.width) / 2) + 'px');

	//Größe verändern
	if(ie) breite = arrayPageSize[0]+"px";
	else breite = "100%";

	$('gray_overlay').style.width = breite;
	$('gray_overlay').style.height = arrayPageSize[1]+"px";

	//alert(arrayPageSize[0] + " - " + arrayPageSize[1] + " - " + arrayPageSize[2] + " - " + arrayPageSize[3]);
	//alert(arrayPageScroll[0] + " - " + arrayPageScroll[1]);
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}


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 hideSelect() {
	selects = parent.document.getElementsByTagName( 'select' );
   	for ( i = 0; i != selects.length; i++ )
	{
		selects[i].style.visibility = 'hidden';
    }
}

function showSelect() {
	selects = parent.document.getElementsByTagName( 'select' );
    for ( i = 0; i != selects.length; i++ )
	{
		selects[i].style.visibility = 'visible';
	}
}

function hidegb() {
	$('gray_box').hide();
	showSelect();
	$('gray_box_content').update("<img src='images/loading/sw.gif' width='73' height='12' hspace='20' vspace='20' /><br />");
}

function showgb() {
	$('gray_box').show();
}

/*
  ////////////////////////////////////////////////////////////////
 // NAVIGATION FUNKTIONEN									   //
////////////////////////////////////////////////////////////////
*/

var actualTopNavi;
var actualSubNavi;
var navirt;
var navidt;

function naviDisplayTimeout(id) {
	window.clearTimeout(navirt);
	navidt = window.setTimeout("naviDisplayBottom('"+id+"');", 100);
}

function naviDisplaycTimeout() {
	navirt = window.setTimeout("restoreNavi();",1000);
	window.clearTimeout(navidt);
}

function naviDisplayBottom(id) {

	for(i = 0; i < document.getElementsByClassName("nav_top_aktiv").length; i++) {
		$(document.getElementsByClassName("nav_top_aktiv")[i]).className='nav_top_inaktiv';
	}

	for(i = 0; i < document.getElementsByClassName("nav_bottom_aktiv").length; i++) {
		$(document.getElementsByClassName("nav_bottom_aktiv")[i]).className='nav_bottom_inaktiv';
	}

	for(i = 0; i < $('navi_bottom').getElementsByTagName("div").length; i++) {
		$($('navi_bottom').getElementsByTagName("div")[i]).hide();
	}
	$("navi_bottom_"+id).show();
	$("nav_top_link_"+id).className = 'nav_top_aktiv';

}

function naviHoverBottom(id) {
	window.clearTimeout(navirt);
	for(i = 0; i < document.getElementsByClassName("nav_bottom_aktiv").length; i++) {
		$(document.getElementsByClassName("nav_bottom_aktiv")[i]).className='nav_bottom_inaktiv';
	}
	//actualSubNavi=id;
	$("nav_bottom_link_"+id).className = 'nav_bottom_aktiv';
	//navirt = window.setTimeout("restoreNavi();",1000);
}

function restoreNavi(){
	//alert(actualTopNavi+ " - "+actualSubNavi);
	if(actualTopNavi>0){
		naviDisplayBottom(actualTopNavi);
	}
	if(actualSubNavi>0){
		naviHoverBottom(actualSubNavi);
	}
}
/*
  ////////////////////////////////////////////////////////////////
 // MODUL-SPEZIFISCHE FUNKTIONEN							   //
////////////////////////////////////////////////////////////////
*/

function neueZeichnung() {
	$("weitere_zeichnungen").insert($('template').innerHTML);
}

function loadProjektStep2() {
	/*if(loadProjektStep2.arguments.length>0) var lps_pre=loadProjektStep2.arguments[0];
	else var lps_pre="";*/
	new Ajax.Request('/ajax/projekt_step2.php',
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		   $('loading').hide();
		   $('productlist').update(t.responseText);

		},
		// Handle other errors
		onFailure: function(t) {
			$('productlist').update('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	);

}

function displayTeam(id) {
	Effect.toggle('team_'+id,'blind');

	if($('team_'+id).style.display == 'none') {
		$('team_plus_'+id).hide();
		$('team_minus_'+id).show();
	} else {
		$('team_plus_'+id).show();
		$('team_minus_'+id).hide();
	}
}


function displayMesseTermin(id) {
	Effect.toggle('messetermin_'+id,'blind');
	Effect.toggle('messebild_'+id,'blind');

	if($('messetermin_'+id).style.display == 'none') {
		$('messe_plus_'+id).hide();
		$('messe_minus_'+id).show();
	} else {
		$('messe_plus_'+id).show();
		$('messe_minus_'+id).hide();
	}
}

function displayEvent() {
	for(var i=0; i < displayEvent.arguments.length; i++)
	{
		displayMesseTermin(displayEvent.arguments[i]);
	}
}

function loadMesseKalender(dir) {
	$('kalender_loading').show();
	new Ajax.Request('ajax/messeKalender.php',
	{
		method: 'get',
		parameters: 'dir='+dir,
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		   $('messekalender').update(t.responseText);
		   $('kalender_loading').hide();
		},
		// Handle other errors
		onFailure: function(t) {
			$('messekalender').update('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	);
}

function checkContact() {
	$('loading').show();
	new Ajax.Request('ajax/kontakt.php',
	{
		method: 'post',
		postBody: $('kontakt_form').serialize(),
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		   $('loading').hide();
		   $('kontakt_result').update(t.responseText);
		   $('kontakt_tr').show();
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	);
}

function checkProjekt() {
	$('loading').show();
	new Ajax.Request('ajax/projekt.php',
	{
		method: 'post',
		postBody: $('projekt_form').serialize(),
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		   $('loading').hide();
		   $('projekt_result').update(t.responseText);
		   $('projekt_tr').show();
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	);
}


function checkNewsletter() {
	$('loading').show();
	new Ajax.Request('ajax/newsletter.php',
	{
		method: 'post',
		postBody: $('newsletter_form').serialize(),
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		   $('loading').hide();
		   $('newsletter_result').update(t.responseText);
		   $('newsletter_tr').show();
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	);
}


function callReferenz(id) {
	displayReferenz(id, 'refimage');
	displayReferenz(id, 'productlist');
	displayReferenz(id, 'reftitel');
}

function displayReferenz(id,typ) {

	if(typ != "reftitel") $(typ).innerHTML = "<table border='0' cellspacing='0' cellpadding='0' width='100%' height='100'><tr><td align='center'><img src='images/loading/sw.gif' /></td></tr></table>";
	new Ajax.Request('ajax/referenzen.php?id='+id+'&type='+typ,
	{
		method: 'get',
		asynchronous: true,
		evalScript: true,
		onComplete: function(t) {
		   $(typ).update(t.responseText);
		},
		// Handle other errors
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	}
	);
}

function projektHighlight(id1, id2, direct) {

	if(direct == "over") {
		$('refoverlay').show();
	} else {
		$('refoverlay').hide();
	}


	$('refoverlay').clonePosition($('box_'+id1+'_'+ id2));

	posref = $('refoverlay').style.top.replace("px","");
	pospl = $('ploverlay').style.top.replace("px","");
	posref_w = $('refoverlay').style.width.replace("px","");

	posref = parseInt(posref);
	pospl = parseInt(pospl);
	pos2 = $('box_'+id1+'_'+ id2).cumulativeScrollOffset();

	if(ie) {
		posref+= pos2[1];
		posref_w-= 16;
	}

	posref2 = posref + $('refoverlay').getHeight();
	pospl2 = pospl + $('ploverlay').getHeight();

	$('refoverlay').style.top = posref+"px";
	$('refoverlay').style.width = posref_w+"px";

	if(posref < pospl) {
		diff = pospl - posref;
		toppos = $('refoverlay').style.top.replace("px","");
		if((toppos + $('refoverlay').getHeight()) < pospl) {
			$('refoverlay').hide();
		} else {
			$('refoverlay').style.top = $('ploverlay').style.top;
			$('refoverlay').style.height = ($('refoverlay').getHeight()) - diff + "px";
		}
	}

	if(pospl2 < posref2) {
		toppos = $('refoverlay').style.top.replace("px","");
		maxheight = pospl + $('ploverlay').getHeight();

		if(toppos > maxheight) {
			$('refoverlay').hide();
		} else {
			diff = 	posref2 - pospl2;
			$('refoverlay').style.height = ($('refoverlay').getHeight()) - diff + "px";
		}
	}

}

//********     PAGEFLIP      *********\\
function zoomPage(img,site){
	showGrayBox();
	new Ajax.Request('ajax/zoomPage.php?i='+img+'&s='+site,
	{
		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);
		}
	}
	);
}
//********		Bildergalerie		********\\

function zoomPic(img){
	showGrayBox();
	new Ajax.Request('ajax/zoomPic.php?i='+img,
	{
		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 gbZoom(element){
	if(element.src){
		var timg=element.src.split("?i=");

		if(timg.length<2) return false;
		else{

			uimg=timg[1].split("&");
			img=uimg[0];

			showGrayBox();

			new Ajax.Request('ajax/zoomPic.php?i='+img,
			{
				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 pgalerieZoom(img){
	if($('szenegalerie')){
		if($('szenegalerie').className!='imgborder') $('szenegalerie').className='imgborder';
		$('szenegalerie').src='images/php/rsz.php?i='+img+'&w=260&h=380&nofill=true';
	}
}


function sendForm(action,id) {
	$('form_'+id+'_loading').show();
	new Ajax.Request('ajax/form_'+action+'.php?kat='+id,
	{
		method: 'post',
		asynchronous: true,
		evalScript: true,
		postBody: $('form_'+id).serialize(),
		onComplete: function(t) {
		//alert("haha");
			//if(id!='profil') showSelect();
			//if(id!='profil') hideAllBoxes();
			$('form_'+id+'_response').update(t.responseText);
			$('form_'+id+'_loading').hide();
		},
		onFailure: function(t) {
			alert(t.status+' -- '+t.statusText);
		}
	}
	);
}



function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function download(file){
	if($('hiddenframe')){
		$('hiddenframe').src="download.php?"+file;
	}
}

function op(element,opacity){
	element.style.MozOpacity=opacity/100;
	element.style.filter="Alpha(opacity="+opacity+")";
}

function UnCryptMailto(s) { //
var n=0;
var r="";
for(var i=0; i < s.length; i++) {
n=s.charCodeAt(i);
if (n>=8364) {n = 128;}
r += String.fromCharCode(n-(1));
}
return r;
}
// JS function for uncrypting spam-protected emails:
function linkTo_UnCryptMailto(s) { //
location.href=UnCryptMailto(s);
}
  function findeFlash (flash) {
    if (document.all) {
      if (document.all[flash]) {
        return document.all[flash];
      }
      if (window.opera) {
        var movie = eval(window.document + flash);
        if (movie.SetVariable) {
          return movie;
        }
      }
      return;
    }
    if(document.layers) {
      if(document.embeds) {
        var movie = document.embeds[flash];
        if (movie.SetVariable) {
          return movie;
        }
      }
      return;
    }
    if (!document.getElementById) {
      return;
    }
    var movie = document.getElementById(flash);
    if (movie.SetVariable) {
      return movie;
    }
    var movies = movie.getElementsByTagName('embed');
    if (!movies || !movies.length) {
      return;
    }
    movie = movies[0];
    if (movie.SetVariable) {
      return movie;
    }
    return;
  }

