var Site =  {
  Version: "0.7",
  CustomVersion: "zhtk.de",
  Locked:1,
  Page:'index',
  
  Load: function() {
  $('s_loading').show();
    Site.Locked=0;
    Site.RewriteLinks();
 
    dhtmlHistory.initialize();
    dhtmlHistory.addListener(DhistoryChange);
    dhtmlHistory.add("index", "zhtk.de");
    $('s_loading').hide();
  },
  
  LoadContent: function(Template){
    if(Site.Locked == 0){
      Site.Locked = 1;
      if (Template == ''){Template ="index"}
      $('s_loading').show();
      Site.Page=Template;
  $('content').innerHTML= '<h1>Daten werden geladen...<\/h1>';
     new Ajax.Request('content/' + Template + '.htm', {method:'get', asynchronous:true, evalScripts:true, onSuccess: Site.ShowContent,
    onFailure:showFailureMessage});
      dhtmlHistory.add(Site.Page , "ZHTK.de - " + Site.Page);

      
    }
  },
  
  ShowContent: function(req){
    Site.Locked = 0;

   $('content').innerHTML= req.responseText;
   Site.ExternalLinks();
   Site.RewriteLinks();
   initLightbox();
    
    /* execute code javascript */
var sCodeJavascript = document.getElementsByTagName("script");
for (var i = 0; i < sCodeJavascript.length; i++)
{
        var contentScript = sCodeJavascript[i];
        if (contentScript.src && contentScript.src != "") z = 1;
        else
        {
                window.eval(contentScript.innerHTML);
                //console.info('window.eval : %d', contentScript.innerHTML);
        }

} 

    window.setTimeout('$(\'s_loading\').hide',1000);
    
  },
  RewriteLinks:function(){
    var LinkList = document.getElementsByTagName('a');    
    var Links = $A(LinkList);

    Links.each(function(Link){
       if (Link.getAttribute("href") && Link.getAttribute("rel") == "internal"){
         var Content = Link.href
         if (Content.indexOf('javascript')){
         Content = Content.substring(Content.lastIndexOf('/') + 1, Content.lastIndexOf('.')) 
         Link.href="javascript:Site.LoadContent(\'" + Content + "\');";
         }
       }
    });
    //Site.LightboxLinks();
  },
  ExternalLinks: function(){
    var LinkList = document.getElementsByTagName('a');    
    var Links = $A(LinkList);

    Links.each(function(Link){
       if (Link.getAttribute("href") && Link.getAttribute("rel") == "external"){
         Link.target = "_blank";
	 Link.title = "(Neues Fenster) " +Link.title;
       }
    });
  },
  LightboxLinks:function(){
    var LinkList = document.getElementsByTagName('a');    
    var Links = $A(LinkList);

    Links.each(function(Link){
       if (Link.getAttribute("href") && Link.getAttribute("rel") == "lightbox"){
         Link.target = "_blank";
	 Link.title = "(Neues Fenster) " +Link.title;
       }
    });
  },
  doSearch: function(q){
    if(Site.Locked == 0){
      Site.Locked = 1;
      if (Template == ''){Template ="srch"}
      $('s_loading').show();
      Site.Page='srch';
  $('content').innerHTML= '<h1>Daten werden geladen...<\/h1>';
     new Ajax.Request('search.cgi?q=' + q , {method:'get', asynchronous:true, evalScripts:true, onSuccess: Site.ShowContent,
    onFailure:showFailureMessage});
      //dhtmlHistory.add(Site.Page , "ZHTK.de - " + Site.Page);
      
    }
  }
}

function DhistoryChange(newLocation, historyData) {
          if(!newLocation){return;}
           Site.LoadContent(newLocation);
         }
         
  addEvent(window, "load", Site.ExternalLinks);
  addEvent(window, "load", Site.Load);
  
function addEvent(obj, evType, fn){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}



function callInProgress (xmlhttp) {
    switch (xmlhttp.readyState) {
        case 1: case 2: case 3:
        return true;
        break;
        // Case 4 and 0
        default:
        return false;
        break;
    }
}

function showFailureMessage() {
    $('content').innerHTML='<h1>Fehler<\/h1><p>Die angeforderte Seite ist momentan nicht verfügbar.<\/p><p>Bitte versuchen Sie es erneut.<\/p>';

                window.setTimeout('$(\'s_loading\').hide()',1000);
        Site.Locked = 0;
}
// Register global responders that will occur on all AJAX requests
   Ajax.Responders.register({
        onCreate: function(request) {
        request['timeoutId'] = window.setTimeout(
         function() {
            // If we have hit the timeout and the AJAX request is active, abort it and let the user know
            if (callInProgress(request.transport)) {
                request.transport.abort();
                showFailureMessage();
                // Run the onFailure method if we set one up when creating the AJAX object
                if (request.options['onFailure']) {
                    request.options['onFailure'](request.transport, request.json);
                }
            }
            
        },
        5000 // Five seconds
        );
    },
    onComplete: function(request) {
        // Clear the timeout, the request completed ok
        window.clearTimeout(request['timeoutId']);
        window.setTimeout('$(\'s_loading\').hide()',1000);
        Site.Locked = 0;
    }
});


function submitsearch(q){
Site.doSearch(q);
return false;

}

