// JavaScript Document

function hideCover(){
	$('cover').hide();
	if(ievers==6) {
		var selects = $A(document.getElementsByTagName('select'));
		for (var c=0; c<selects.size(); c++) {
			$(selects[c]).setStyle({visibility:'visible'});
		}
	}
	
	var objects = $A(document.getElementsByTagName('object'));
	for (var c=0; c<objects.size(); c++) {
		$(objects[c]).setStyle({visibility:'visible'});
	}
	
}

function set_height (element){
	var y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight;
	var test3 = getWindowHeight();
	if (test1 > test2) // all but Explorer Mac
	{
		y = test1;
	}
	else // Explorer Mac;
	     // would also work in Explorer 6 Strict, Mozilla and Safari
	{
		y = test2;
	}
	if (test3 > y) {	// this will fire if the screen is NOT completely filled by the content (no scrollbar visible)
		y = test3+'px';
	}
	$(element).setStyle({height : y});
}

function showCover(options){
	var options = options || {};
	var opacity = options.opacity || 45;
	var opaque = (opacity / 100);
	if(ievers==6) {
		var selects = $A(document.getElementsByTagName('select'));
		for (var c=0; c<selects.size(); c++) {
			$(selects[c]).setStyle({visibility:'hidden'});
		}
	}
	
	var objects = $A(document.getElementsByTagName('object'));
	for (var c=0; c<objects.size(); c++) {
		$(objects[c]).setStyle({visibility:'hidden'});
	}
	
	if(cover=$('cover')) {
		$('cover').show();
	}
	else {
		var dimensions=$('body').getDimensions();
		new Insertion.Top($('body'),'<div id="cover" onclick="hidePopup();"></div>');
		$('cover').style.width=dimensions.width+'px';
		$('cover').style.height=dimensions.height+'px';
		$('cover').style.opacity=opaque;                      
		$('cover').style.MozOpacity=opaque;                   
		$('cover').style.filter='alpha(opacity='+opacity+')'; 
		set_height('cover');
		$('cover').show();
	}
}
/* Functions used to show an d hide the popup-div */
function getWindowHeight(win){
    if (win == undefined)
        win = window;
    if (win.innerHeight) {
        return win.innerHeight;}
    else {
        if (win.document.documentElement && win.document.documentElement.clientHeight) {
            return win.document.documentElement.clientHeight;
        }
        return win.document.body.offsetHeight;
    }
}

function getSize() {   
    var myWidth = 0, myHeight = 0;   
  
    if( typeof( window.innerWidth ) == 'number' ) {   
        //Non-IE   
        myWidth = window.innerWidth;   
        myHeight = window.innerHeight;   
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {   
        //IE 6+ in 'standards compliant mode'   
        myWidth = document.documentElement.clientWidth;   
        myHeight = document.documentElement.clientHeight;   
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {   
        //IE 4 compatible   
        myWidth = document.body.clientWidth;   
        myHeight = document.body.clientHeight;   
    }   
    return [ myWidth, myHeight ];   
}  


function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;

	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

function showPopup(width, height, url, special) {
	showCover();
	if(popup=$('popup')){
	}
	else {
		new Insertion.After($('cover'),'<div id="popup"><div class="pu_header"><a href="javascript:hidePopup();">Fenster schlie&szlig;en</a></div><div id="pu_wrapper"><div id="pu_inner"></div></div>');
		var popup=$('popup');
		popup.hide();
	}
	popup.style.width=width+'px';
	popup.style.height=height+'px';
	$('pu_wrapper').style.height=height-17+'px';
	
	var wwidth=$('body').getWidth();								// width of the body
										// move popup to center of page
	
	// now we need the height of the window
	wwidth = getSize()[0];
	wheight = getSize()[1];
	
	// yOffset will give us the part of the screen currently seen - the position the user scrolled to
	var yOffset = getScrollXY()[1];
	
	var left=(wwidth - width)/2;
	var middle=((wheight - height)/2);
	if (middle<0) middle = wheight/2;
	var top = middle+yOffset;
	popup.style.left=left+'px';		// calculate position for popup
	popup.style.top=top+'px';										// move to center of screen
	Effect.Appear('popup');											// show the popup
	
	if(special == 'image') {
		$('pu_inner').update('<div id="pui_holder" style="height:480px;width:480px;position:relative;"><img id="pu_image" src="'+url+'" style="position:absolute;" onload="center_image(\'pu_image\',\'pui_holder\');">');
		$('pu_inner').setStyle({backgroundColor: '#FFF'});
		setTimeout("center_image('pu_image','pui_holder');",250);
	}
	else {
		new Ajax.Updater('pu_inner', url,{								// now we fill our popup with content
			evalScripts: true,											// try to evaluate any JS in the response
			onCreate: function(){
				if (tmp = $('ajaxwait')) {
					$('pu_inner').update($('ajaxwait').innerHTML);		// add the ajax-wait
				}
			},
			onComplete: function(){								// if everything is ok
				if(color = $('popup_bgr').value) {						// try to find a hidden input with the background-color for the popup
					$('pu_inner').setStyle({backgroundColor: color});	// set background-color
				}
				else {
					$('pu_inner').setStyle({backgroundColor: '#FFF'});
				}
			}
		});
	}
}

function hidePopup(){
	hideCover();												// hide cover
	Effect.Fade('popup');											// fade out popup
}

function ajax_GetXMLHttpRequest(){
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
	   http_request = new XMLHttpRequest();
	   if (http_request.overrideMimeType) {
		  http_request.overrideMimeType('text/html');
	   }
	}
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				//give up
			}
		 }
	  }
	  if (!http_request) {
		 alert('Cannot create XMLHTTP instance');
		 return false;
	  }
	  return http_request;
}

function ajax_sendGetRequest(url, elementId){
	var http_request = ajax_GetXMLHttpRequest();
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				ajax_updateResponseContent(elementId,http_request.responseText);
			} else {
				alert('Bei dem Request ist ein Problem aufgetreten.');
			}
		}
	}//end function
	http_request.open("GET", url);
	http_request.send(null);
}
function ajax_updateResponseContent(elementId, response){
	var elem = document.getElementById(elementId);
	elem.innerHTML = response;
}