var content_breite = 0;
var content_hoehe = 0;
var hoehe_center = 0;

var preloads = new Array();

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 (new String(a[i]).indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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_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,nSrc=null,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.runtimeStyle && x.runtimeStyle.filter && x.runtimeStyle.filter.match(/src='(.*?)'/)) {
          var tmp = x.runtimeStyle.filter.match(/src='(.*?)'/);
            if (tmp != null && tmp.length == 2) {
                nSrc = tmp[1];
            }
        }
        if (nSrc != null && x.oSrc != nSrc) {
            x.oSrc = nSrc;
        } else if(!x.oSrc) {
            x.oSrc=x.src;
        }
        x.src=a[i+2];
    }
}

function zentrieren() {
	var i, obj, breite, hoehe;
	
 	if (window.innerWidth) {
		breite = window.innerWidth;
  	} else if (document.body && document.body.offsetWidth) {
    	breite = document.body.offsetWidth;
	} else {
    	breite = 0;
	}

	if (window.innerHeight) {
		hoehe = window.innerHeight;
  	} else if (document.body && document.body.offsetHeight) {
    	hoehe = document.body.offsetHeight;
	} else {
    	hoehe = 0;
	}
	obj = document.getElementById("center_div");

	if (obj) {
		i = breite - content_breite;
		i = i / 2;
		if (i < 0) i = 0;

		j = hoehe - content_hoehe;
		j = j / 2;
		if (j < 0) j = 0;
	
		obj.style.left = i+"px";
		if (hoehe_center) {
			obj.style.top = j+"px";
		}
	}
}

function zentrierenBreite(breite) {
	var i, obj, tmpBreite;
	
	content_breite = breite;
	
 	if (window.innerWidth) {
		tmpBreite = window.innerWidth;
  	} else if (document.body && document.body.offsetWidth) {
    	tmpBreite = document.body.offsetWidth;
	} else {
    	tmpBreite = 0;
	}	
	
	i = tmpBreite - breite;
	i = i / 2;
	if (i < 0) i = 0;
	
	/*return i;*/
	document.write('<style type="text/css">');
	document.write('#center_div { position: absolute; left: '+i+'px; }');
	document.write('</style>');
}

function zentrierenHoehe(hoehe, vertikal) {
	var i, obj, tmpHoehe;

	content_hoehe = hoehe;
	hoehe_center = vertikal;

	if (window.innerHeight) {
		hoehe = window.innerHeight;
  	} else if (document.body && document.body.offsetHeight) {
    	hoehe = document.body.offsetHeight;
	} else {
    	hoehe = 0;
	}

	j = hoehe - content_hoehe;
	j = j / 2;
	if (j < 0) j = 0;
	
	if (!vertikal) {
		j = 0;
	} 
	
	/*return j;*/
	document.write('<style type="text/css">');
	document.write('#center_div { position: absolute; top: '+j+'px; }');
	document.write('</style>');
}

function preload() {
  if (preloads.length)
    MM_preloadImages(preloads)
}

function push_preloads() {
    for(var i=0; i<push_preloads.arguments.length; i++) {
        preloads.push(push_preloads.arguments[i]);
    }
}

function getSel()
{
	var txt = '';
	var foundIn = '';
	if (window.getSelection)
	{
		txt = window.getSelection();
		foundIn = 'window.getSelection()';
	}
	else if (document.getSelection)
	{
		txt = document.getSelection();
		foundIn = 'document.getSelection()';
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;
		foundIn = 'document.selection.createRange()';
	}
	else return;
	document.forms[0].selectedtext.value = 'Found in: ' + foundIn + '\n' + txt;
}

function addResizeEvent(func) {
  var old = window.onresize;
  if (typeof window.onresize != 'function') {
    window.onresize = func;
  } else {
    window.onresize = function(e) {
      old(e);
      func(e);
    }
  }
}

function addScrollEvent(func) {
  var old = window.onscroll;
  if (typeof window.onscroll != 'function') {
    window.onscroll = func;
  } else {
    window.onscroll = function(e) {
      old(e);
      func(e);
    }
  }
}

function toggle_expand(id) {
    $('div.'+id).toggle();
}

function submit_form(name) {
    $('#'+name+'message').removeClass('fehler').html('').hide();
    jQuery.ajax({
        type:       'POST',
        url:        '/cgi/'+name,
        data:       $('#'+name+'form').serialize(),
        success:    function (response) {
            $('#'+name+'message').html(response).show();
            $('#'+name+'form')[0].reset();
            window.scrollTo(0,300);
        },
        error:      function (request, status, error) {
            var msg = 'An error occured while your request was submitted; please retry later'; 
            $('#'+name+'message').addClass('fehler').html(msg).show();
            window.scrollTo(0,300);
        }
    });
}

function getScrollerWidth() {
	var scr = null;
	var inn = null;
	var wNoScroll = 0;
	var wScroll = 0;

	// Outer scrolling div
	scr = document.createElement('div');
	scr.style.position = 'absolute';
	scr.style.top = '-1000px';
	scr.style.left = '-1000px';
	scr.style.width = '100px';
	scr.style.height = '50px';
	// Start with no scrollbar
	scr.style.overflow = 'hidden';

	// Inner content div
	inn = document.createElement('div');
	inn.style.width = '100%';
	inn.style.height = '200px';

	// Put the inner div in the scrolling div
	scr.appendChild(inn);
	// Append the scrolling div to the doc
	document.body.appendChild(scr);

	// Width of the inner div without scrollbar
	wNoScroll = inn.offsetWidth;
	// Add the scrollbar
	scr.style.overflow = 'auto';
	// Width of the inner div with scrollbar
	wScroll = inn.offsetWidth;

	// Remove the scrolling div from the doc
	document.body.removeChild(document.body.lastChild);

	// Pixel width of the scroller
	return (wNoScroll - wScroll);
}

// Get the height/width of the entire document
function getDocumentHeight() {
	var a=0,b=0;
	if (!isNaN(document.body.parentNode.scrollHeight)) a=document.body.parentNode.scrollHeight;
	if (!isNaN(window.innerHeight)) b=window.innerHeight;
	return Math.max(a,b);
}
function getDocumentWidth() {
	var a=0,b=0;
	if (!isNaN(document.body.parentNode.scrollWidth)) a=document.body.parentNode.scrollWidth;
	if (!isNaN(window.innerWidth)) b=window.innerWidth;
	var w = Math.max(a,b);
	// keine horizontalen Scrollbalken erzeugen, nur weil schon ein vertikaler da ist
	if (window.innerHeight < document.body.parentNode.scrollHeight) {
		w -= getScrollerWidth();
	}
	return w;
}

function do_onload_stuff() {
	preload();
	if ($('div#shadowlayer')) {
		var hgt = getDocumentHeight();
		var wdt = getDocumentWidth();
		$('div#shadowlayer').css({ 'height': hgt+'px', 'width': wdt+'px' });
	}
}

function zoom_image(id,type,w,h,alt) {
	var img='<img src="/images/'+id+'/source.'+type+'" alt="'+alt+'" width="'+w+'" height="'+h+'" />';
	document.getElementById('the_zoomimage').innerHTML=img;
	$('div#shadowlayer').show().fadeTo('slow',.5);
	if (navigator.userAgent.match(/MSIE/)) {
		window.setTimeout("document.getElementById('zoomimage').style.display='block';",500);
	} else {
		var div=$('div#zoomimage');
		var dw=(getDocumentWidth() - w) / 2;
		if (dw<10) dw=10;
		var scroll = window.scrollY;
		if (document.body.scrollTop) scroll = document.body.scrollTop;
		if (document.compatMode == 'CSS1Compat') scroll = document.body.parentNode.scrollTop;
		var dh=(window.innerHeight - h) / 2 + scroll;
		if (dh<10) dh=10;
		div.css({ 'top':dh+'px', 'left':dw+'px' }).show('slow');
	}
}
function hide_image() {
	$('div#shadowlayer').fadeTo('slow',0.01,hideShadow);
	if (navigator.userAgent.match(/MSIE/)) {
		document.getElementById('zoomimage').style.display='none';
	} else {
		$('div#zoomimage').hide('slow');
	}
}
function hideShadow() {
	$('div#shadowlayer').hide();
}