/******************************************
* komunikation mit den php scripten (AJAX)

******************************************/
window_array = new Array();
	window_array[1] = new Array(1,0,0,0);
	window_array[2] = new Array(2,0,0,0);
	window_array[3] = new Array(3,0,0,0);
					// ID - Status - hoehe (0=display:off) - kastenID
					
					/* Status	0 = normal
								1 = loading
								2 = resize
								4 = mit Inhalt beladen.
								
					*/
var old_kastenID=0;
var old_targetID=0;
var size_up_speed = 10;
var size_down_speed = 20;
var c_height = 0;
/****************** PRGRAMM **************/
var xmlHttp = false;



/** AJAX functions **/

// constants
var REQUEST_GET        = 0;
var REQEST_POST        = 2;
var REQUEST_HEAD    = 1;
var REQUEST_XML        = 3;

function getXMLRequester( )
{
    var xmlHttp = false;
            
    // try to create a new instance of the xmlhttprequest object        
    try
    {
        // Internet Explorer
        if( window.ActiveXObject )
        {
            for( var i = 5; i; i-- )
            {
                try
                {
                    // loading of a newer version of msxml dll (msxml3 - msxml5) failed
                    // use fallback solution
                    // old style msxml version independent, deprecated
                    if( i == 2 )
                    {
                        xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );    
                    }
                    // try to use the latest msxml dll
                    else
                    {
                        
                        xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
                    }
                    break;
                }
                catch( excNotLoadable )
                {                        
                    xmlHttp = false;
                }
            }
        }
        // Mozilla, Opera und Safari
        else if( window.XMLHttpRequest )
        {
            xmlHttp = new XMLHttpRequest();
        }
    }
    // loading of xmlhttp object failed
    catch( excNotLoadable )
    {
        xmlHttp = false;
    }
    return xmlHttp ;
}


function sendRequest( strSource, strData, intType, intID ){

	var targetID=intID;

    if( !strData )
        strData = '';

    // default type (0 = GET, 1 = xml, 2 = POST )
    if( isNaN( intType ) )
        intType = 0; // GET

    // previous request not finished yet, abort it before sending a new request
    if( xmlHttp && xmlHttp.readyState )
    {
        xmlHttp.abort( );
        xmlHttp = false;
    }
  
    // create a new instance of xmlhttprequest object
    // if it fails, return
    if( !xmlHttp )
    {
        xmlHttp = getXMLRequester( );
        if( !xmlHttp )
            return;
    }
    
    // parse query string
    if( intType != 1 && ( strData && strData.substr( 0, 1 ) == '&' || strData.substr( 0, 1 ) == '?' ) )
        strData = strData.substring( 1, strData.length );

    // data to send using POST
    var dataReturn = strData ? strData : strSource;
      
    switch( intType )
    {
        case 1:    // xml
            strData = "xml=" + strData;
			alert("xml daten");
        case 2: // POST
            // open the connection 
            xmlHttp.open( "POST", strSource, true );
            xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
            xmlHttp.setRequestHeader( 'Content-length', strData.length );
			alert("post daten");
            break;
        case 3: // HEAD
            // open the connection 
            xmlHttp.open( "HEAD", strSource, true );
            strData = null;
			alert("haed daten");
            break;
        default: // GET
            // open the connection 
            var strDataFile = strSource + (strData ? '?' + strData : '' );
            xmlHttp.open( "GET", strDataFile, true );
            strData = null;
			//alert("get daten");
    }
	
    // set onload data event-handler
	
	xmlHttp.onreadystatechange = new Function ( "processResponse( '"+intID+"', '"+targetID+"' )" );
	
	
	// send request to server
    xmlHttp.send( strData );    // param = POST data
    
    return dataReturn;
}
    
/**
 * process the response data from server
 *
 * @param intID, Integer, ID of this response
 */
function processResponse(intID,targetID){
    
	// status 0 UNINITIALIZED open() has not been called yet.
    // status 1 LOADING send() has not been called yet.
    // status 2 LOADED send() has been called, headers and status are available.
    // status 3 INTERACTIVE Downloading, responseText holds the partial data.
    // status 4 COMPLETED Finished with all operations.
	
	
	if(window_array[targetID][1]!=1){
		updateLoading(targetID);
		}

	
	switch( xmlHttp.readyState )
    {
        // uninitialized
        case 0:
        // loading
        case 1:
        // loaded
        case 2:
        // interactive
        case 3:
            break;
        // complete
        case 4:
            // check http status
            if( xmlHttp.status == 200 )    // success
            {
                processData( xmlHttp, targetID);
            }
            // loading not successfull, e.g. page not available
            else
            {
                if( window.handleAJAXError )
                    handleAJAXError( xmlHttp, intID );
                else
                   // var strData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\" ?><daten><error></error><ID>2</ID><title>404 Fehler</title><MSG><![CDATA[ Die Seite kann nicht weiter ausgeführt werden, es trat ein 404 Fehler auf. Bitte starten Sie das Spiel neu. ]]></MSG></daten>";
					
					updateData( strData , targetID);
					alert( "ERROR\n HTTP status = " + xmlHttp.status + "\n" + xmlHttp.statusText ) ;
            }
    }
}

// process data from server
function processData( xmlHttp,request_array){
    updateData( xmlHttp.responseXML , request_array);
}



// process data from server, updates second select menu
// data from server comes with this format: value=data&value=data...
function updateData( strData ,targetID){
		
	if(strData){
		
		// Löscht die Höhenangabe
		/*document.getElementById("box"+targetID).style.height="";*/
		
		// Ändern den Content
		
		
		document.getElementById("box"+targetID).innerHTML=""+strData.getElementsByTagName("content")[0].firstChild.data;
		window_array[targetID][1]=0;
	}
}


// Sendet den "Loading" Screen an das Fenster (dabei wird die höhe nicht verändert)
function updateLoading(targetID){

	/*var new_height=document.getElementById("box"+targetID).offsetHeight;
	document.getElementById("box"+targetID).style.height=new_height+"px";*/
	
	// Inhalt wird geändert
	document.getElementById("box"+targetID).innerHTML="<div class=\"inhalt\"><div id=\"loading\"><div id=\"l\"><img src=\"img/loading.gif\" width=\"18\" height=\"18\" alt=\"\" border=\"0\" align=\"absmiddle\" />&nbsp;Loading</div></div></div>";
	window_array[targetID][1]=1; // Wird als Loading markiert.
}

			 //js=intern,anfrage,target,0=new loading - 1 refresh content
function com (aktion,targetID,kastenID,w_height){
	
	//alert("0: "+window_array[targetID][0]+"\n1: "+window_array[targetID][1]+"\n2: "+window_array[targetID][2]+"\n3: "+window_array[targetID][3]);
	
	if(old_targetID != targetID && old_targetID!=0){
		size_down (old_targetID,window_array[old_targetID][2],0);
		}
	
	// kastenfarbe ändern
	if(old_kastenID != 0){
		display_kasten_option (old_kastenID,'off');
		}
	
	if(window_array[targetID][2]<w_height && window_array[3][1]!=1){
		
		display_kasten_option (kastenID,'on');
		window_array[3][1]=2;
		size_up(targetID,window_array[targetID][2],w_height);
		
		sendRequest("xml_lila/index.php","?t="+aktion+"&d=xml",0,targetID);
		}
	else if (window_array[targetID][2]>w_height && window_array[3][1]!=1){
		
		display_kasten_option (kastenID,'on');
		window_array[3][1]=2;
		size_down(targetID,window_array[targetID][2],w_height);
		
		sendRequest("xml_lila/index.php","?t="+aktion+"&d=xml",0,targetID);
		}
	else if (window_array[targetID][2] == w_height && window_array[3][1]!=1 && kastenID == old_kastenID){
		
		size_down (targetID,window_array[targetID][2],0);
		targetID=0; // aktion: Fenster einfahren (ohne weitern inhalt) old_targetID muss wieder 0 sein.
		}
	
	
	old_kastenID = kastenID;
	old_targetID = targetID;
	
	}

	
function size_up(targetID,start,ziel){
	
	// Wenn Fenster ausgeblendet wird es angezeigt und auf 1px gesetzt
	if(start == 0){
		display_option(targetID,'on');
		start = 1;
		}
	
	c_height = start + size_up_speed;
	
	document.getElementById('box'+targetID).style.height = c_height + "px";
	tmp_next_step = c_height + size_up_speed;
	
	if(tmp_next_step < ziel){
		window_array[targetID][2] = c_height;
		setTimeout("size_up("+targetID+","+c_height+","+ziel+")",0);
		}
	else{
		document.getElementById('box'+targetID).style.height = ziel + "px";
		window_array[targetID][2] = ziel;
		window_array[3][1]=0;

		
		}
	}

function size_down(targetID,start,ziel){
	c_height = start - size_down_speed;
	
	document.getElementById('box'+targetID).style.height = c_height + "px";
	var tmp_next_step = c_height - size_down_speed;
		
	if(tmp_next_step > ziel){
		//alert("c_height:"+c_height+" next:"+tmp_next_step+"  >"+ziel);
		window_array[targetID][2] = c_height;
		setTimeout("size_down("+targetID+","+c_height+","+ziel+")",0);
		}
	else{
		document.getElementById('box'+targetID).style.height = ziel + "px";
		window_array[targetID][2] = ziel;
		window_array[3][1]=0; // stellt das Fenster wieder auf 'normal'
		
		if(ziel == 0){ 
			display_option (targetID,'off');
			}
		
		}
	}
	
function display_option (targetID,aktion){
	if(aktion == "on"){
		document.getElementById('box'+targetID).style.visibility = "visible";
		}
	else{
		document.getElementById('box'+targetID).style.visibility = "hidden";
		}
	}

function display_kasten_option (kastenID,aktion){
	if(aktion == "on"){
		document.getElementById('content'+kastenID).className = "kasten active";
		}
	else{
		document.getElementById('content'+kastenID).className = "kasten";
		}
	}