
/**
* CMSlite-Modul um per AJAX die Inhalte einer Seite nachzuladen
*
* Features: 
*   - Ein-/Ausblend-Effekt im main-Bereich nutzbar
*
* Zugehoerige Dateien:
* - ajax.php
* - ajax.js
*
* Anpassungen in den Standarddateien
* - index.php
*   - Laden der js-Datei ajax.js
*/

/**
* Globale Varialbe fuer Messaging in der Statusleiste
**/
var msg = ""; 

/**
* Variable mit zu ladenen Link
**/
var mylinknew = '';

/**
* Zeigt string in der Statusleiste des Browsers an
*
**/
function show_status()
{
  window.status = msg;     
}

/**
* Setzt die Opacity eine HTMLObjektes
*
* @param object myo - Handle zum HTMLObjekt
* @param float level - Opacity-Grad (0 - 1)
**/
function setOpac(myo, level, recursive)
{
  if (! myo) return;
  if (! myo.style) return; 
  
  if (typeof recursive == 'undefined') recursive = true;

  if (myo.style)
  {
    if (level == 1)
    {
      myo.style['display'] = '';    
        
      myo.style.filter = 'none';
      myo.style['-moz-opacity'] = 1; 
      myo.style['opacity'] = 1; 
    }
    else
    if (level == 0)
    {
      myo.style['display'] = 'none';    
    }
    else
    {
      myo.style['display'] = '';    

      myo.style.filter = 'Alpha(opacity='+parseInt(100*level)+')';
      myo.style['-moz-opacity'] = level; 
      myo.style['opacity'] = level; 
    }
  }
    
  if (recursive) 
  {
    var cc = myo.childNodes.length; 
    while(cc--)
    {
      if (myo.childNodes[cc].className == 'submain')
        setOpac(myo.childNodes[cc],level);
    }
  }
}

/**
* Ein-/Ausblenden eines HTMLObjektes nach 
* angegebenen Parametern
*
* @param object myo   - Handle zum HTMLObjekt
* @param float  from  - Ausgangs-OpacLevel
* @param float  to    - End-Opaclevel
* @param float  s     - Dauer in Sekunden
* @param string htmlt - HTML-Text des Elements
**/
function fade(myo,from,to,s)
{
  if (! myo) return; 
  
  var step = 0.2; 
  var act = 0; 
  var loops = 0;
  var ivid = -1;
  
  if (from > to)
  {
    iv = s /((from-to) / step) * 1000;
    iv = parseInt(iv); 
    act = from; 
    if (ivid)
      clearInterval(ivid); 
    
    ivid = window.setInterval(fadeout,iv);       
  }
  else
  if (to > from)
  {
    iv = s /((to-from) / step) * 100;
    iv = parseInt(iv); 

    act = from; 
    
    if (ivid)
      clearInterval(ivid); 
    
    ivid = window.setInterval(fadein,iv);       
  }  
  
 
  function fadeout()
  {
    setOpac(myo, act);
    act = act-step;
    loops++;
    
    if (act <= to)
    { 
      setOpac(myo,0);
      
      clearInterval(ivid);   
      sendRequest(mylinknew);  
    }
  } 

  function fadein()
  {
    setOpac(myo, act);
    act = act+step;
    
    if (act >= to)
    { clearInterval(ivid);   
      setOpac(myo, 1); 
    }
  }
}

/**
* Funktion wird einem Klick auf einen Link 
* ausgefuehrt
* 
* @param object e - Event-Objekt
**/
function linkbyajax(e)
{
  var dump = '';
  
  if (!e) e = window.event;
  
  if (e['srcElement'])
  {
    var mylink = e['srcElement']+'';
    mylinknew = mylink.replace(/index\.php/g,"ajax.php");
    
    //mylinknew = "http://192.168.2.223:7790/ajax.php?l=DE&p=3";  
  }

  if (e['target'])
  {
    var mylink = e['target']+'';
    mylinknew = mylink.replace(/index\.php/g,"ajax.php");

    //mylinknew = "http://192.168.2.223:7790/ajax.php?l=DE&p=3";  
  }
  
  // Main-Bereich ausblenden
  fade(document.getElementById('innermain'),1,0,0.2); 
  
  show_status('Lade Inhalt. Bitte warten ...')
  
  // Um Verlinkung zu verhindern
  return false;   
}

//document.onmouseover = show_status;

/**
* Registriert in allen Links die onclick-Funktion linkbyajax 
* Diese wird dann ausgefuehrt, wenn der Benutzer auf
* den Link klickt. 
**/
function registerLinks()
{
  for (var i=0; i < document.links.length; i++)
  {
    if (document.links[i].target == "")
    {
      if ((document.links[i].parentNode.className == 'mainnavpart') || 
          (document.links[i].parentNode.className == 'subnavpart'))
      {
        document.links[i].onclick = linkbyajax; 
      }
    }
  }   
  
  xmlHttp = getXMLRequester(); 
}

/**
* Nach dem Laden der Seite werden alle Links 
* registiert. 
**/ 
window.onload = registerLinks; 


/*
*-------------------------------------
*
* A J A X - Part
*
*--------------------------------------
*/

/**
 * instantiates a new xmlhttprequest object
 *
 * @return xmlhttprequest object or false
 */
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 ;
}

// constants
var REQUEST_GET        = 0;
var REQUEST_POST        = 2;
var REQUEST_HEAD    = 1;
var REQUEST_XML        = 3;

/**
 * sends a http request to server
 *
 * @param strSource, String, datasource on server, e.g. data.php
 *
 * @param strData, String, data to send to server, optionally
 *
 * @param intType, Integer,request type, possible values: REQUEST_GET, REQUEST_POST, REQUEST_XML, REQUEST_HEAD default REQUEST_GET
 *
 * @param intID, Integer, ID of this request, will be given to registered event handler onreadystatechange', optionally
 *
 * @return String, request data or data source
 */
function sendRequest( strSource, strData, intType, 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;
        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 );
            break;
        case 3: // HEAD
            // open the connection 
            xmlHttp.open( "HEAD", strSource, true );
            strData = null;
            break;
        default: // GET
            // open the connection 
            var strDataFile = strSource + (strData ? '?' + strData : '' );
            xmlHttp.open( "GET", strDataFile, true );
            strData = null;
    }
    
    // set onload data event-handler
    xmlHttp.onreadystatechange = new Function( "", "processResponse(" + intID + ")" ); ;

    // 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 )
{
    // 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.
    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, intID );
            }
            // loading not successfull, e.g. page not available
            else
            {
                if( window.handleAJAXError )
                    handleAJAXError( xmlHttp, intID );
                else
                    alert( "ERROR\n HTTP status = " + xmlHttp.status + "\n" + xmlHttp.statusText ) ;
            }
    }
}

// handle response errors
function handleAJAXError( xmlHttp, intID )
{
  // alert('Fehler beim Laden der Seite
}

/*
* Funktion wird ausgerufen, wenn der REQUEST vom Server beantwortet wird
*
* @param object xmlHttp - Handle zum aktuelle xmlHttp-Objekt
* @param integer intID  - Aktuelle ID des REQUESTs
*/
function processData( xmlHttp, intID )
{
    // process text data
    //updateMenu( xmlHttp.responseText );
    //alert(xmlHttp.responseText); 

  // OpacLevel des Mainbereichs runtersetzen 
  setOpac(document.getElementById('innermain'), 0);

  // Main-Bereich ausblenden
  document.getElementById('innermain').style['display'] = 'none'; 
  
  // Main mit neuen Daten fuellen

  document.getElementById('innermain').innerHTML = xmlHttp.responseText;

 /* document.getElementById('innermain').style['display'] = ''; */

  // Main wieder einblenden
  
  fade(document.getElementById('innermain'),0,1,0.2);    

  // Menu mit neuen Daten fuellen
  if (document.getElementById('onedivajax'))
  {
    if (document.getElementById('onediv').firstChild && document.getElementById('onedivajax').firstChild)
    {
      if (document.getElementById('onediv').firstChild.className=="mainnav" && document.getElementById('onedivajax').firstChild.className=="mainnav")
      {
        document.getElementById('onediv').firstChild.innerHTML= document.getElementById('onedivajax').firstChild.innerHTML;
      }
      else
      if (document.getElementById('onediv').firstChild.nextSibling && document.getElementById('onedivajax').firstChild.nextSibling)
      {
        if (document.getElementById('onediv').firstChild.nextSibling.className=="mainnav" && 
            document.getElementById('onedivajax').firstChild.nextSibling.className=="mainnav")
        {
          document.getElementById('onediv').firstChild.nextSibling.innerHTML= document.getElementById('onedivajax').firstChild.nextSibling.innerHTML;
        }
      }
    }

    registerLinks(); 
  }

  // Head mit neuen Daten fuellen
  if (document.getElementById('pageheaderajax'))
    document.getElementById('pageheader').innerHTML = document.getElementById('pageheaderajax').innerHTML;

  // Breadcrumbs ??? 
}

