function getHTTPObject()
{
  var xmlhttp = false;

  /* Compilation conditionnelle d'IE */
  /*@cc_on
  @if (@_jscript_version >= 5)
     try
     {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e)
     {
        try
        {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
           xmlhttp = false;
        }
     }
  @else
     xmlhttp = false;
  @end @*/

  /* on essaie de créer l'objet si ce n'est pas déjà fait */
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
     try
     {
        xmlhttp = new XMLHttpRequest();
     }
     catch (e)
     {
        xmlhttp = false;
     }
  }

  if (xmlhttp)
  {
     /* on définit ce qui doit se passer quand la page répondra */
     xmlhttp.onreadystatechange=function()
     {
        if (xmlhttp.readyState == 4) /* 4 : état "complete" */
        {
           if (xmlhttp.status == 200) /* 200 : code HTTP pour OK */
           {
              /*
              Traitement de la réponse.
              Ici on affiche la réponse dans une boîte de dialogue.
              */
              doneGetPage(xmlhttp.responseText);
           }
        }
     }
  }
  return xmlhttp;
}


/* CROSS-BROWSER EVENT HANDLER */
function addEvent(obj, evType, fn){
     if (obj.addEventListener){
         obj.addEventListener(evType, fn, true);
         return true;
     } else if (obj.attachEvent){
         var r = obj.attachEvent("on"+evType, fn);
        return r;
     } else {
        return false;
     }
}
/* END EVENT HANDLER */

/* PAGELOCATOR */
function PageLocator(propertyToUse, dividingCharacter) {
    this.propertyToUse = propertyToUse;
    this.defaultQS = 1;
    this.dividingCharacter = dividingCharacter;
}
PageLocator.prototype.getLocation = function() {
    return eval(this.propertyToUse);
}
PageLocator.prototype.getHash = function() {
    var url = this.getLocation();
    if(url.indexOf(this.dividingCharacter)>-1) {
        var url_elements = url.split(this.dividingCharacter);
        return url_elements[url_elements.length-1];
    } else {
        return this.defaultQS;
    }
}
PageLocator.prototype.getHref = function() {
    var url = this.getLocation();
    var url_elements = url.split(this.dividingCharacter)
    return url_elements[0];
}
PageLocator.prototype.makeNewLocation = function(new_qs) {
    return this.getHref() + this.dividingCharacter + new_qs;
}
/* END PAGELOCATOR */


/* AjaxIframesFixer */
function AjaxIframesFixer(iframeid) {
    this.iframeid = iframeid;
    if (document.getElementById('ajaxnav')) {
        this.fixLinks();
        
        this.locator = new PageLocator("document.frames['"+this.iframeid+"'].getLocation()", "?hash=");
        this.windowlocator = new PageLocator("window.location.href", "#");
        this.timer = new Timer(this);
        
        this.delayInit(); // required or IE doesn't fire
    }
}
AjaxIframesFixer.prototype.fixLinks = function (iframeid) {
    var links = document.getElementsByTagName("A");
    for(var i=0; i<links.length; i++) {
        var href = links[i].getAttribute("href");
        if (href.indexOf("http://www.phila.fr/"+site+"/")==-1) continue; 
        href=href.substr(20+site_len);
        //if (href.indexOf("http://phila-kimsufi.ariberti.net/"+site+"/")==-1) continue; 
        //href=href.substr(34+site_len);
        links[i].setAttribute("href","javascript:document.getElementById('"+this.iframeid+"').setAttribute('src', '/mock-page.php?hash="+href+"');");
    }
}
AjaxIframesFixer.prototype.delayInit = function(){
    this.timer.setTimeout("checkBookmark", 100, "");
}
AjaxIframesFixer.prototype.checkBookmark = function(){
    window.location = this.windowlocator.makeNewLocation(this.locator.getHash());
    this.checkWhetherChanged(0);
}
AjaxIframesFixer.prototype.checkWhetherChanged = function(location){
    if(this.locator.getHash() != location) {
        doGetPage(this.locator.getHash());
        window.location = this.windowlocator.makeNewLocation(this.locator.getHash());
    }
    this.timer.setTimeout("checkWhetherChanged", 200, this.locator.getHash());
}
/* END AjaxIframesFixer */


/* AjaxUrlFixer */
function AjaxUrlFixer() {
    this.fixLinks();
    
    this.locator = new PageLocator("window.location.href", "#");
    this.timer = new Timer(this);
    this.checkWhetherChanged(0);
}
AjaxUrlFixer.prototype.fixLinks = function () {
    var links = document.getElementsByTagName("A");
    for(var i=0; i<links.length; i++) {
        var href = links[i].getAttribute("href");
        if (href.indexOf("/"+site+"/")==0) { 
            links[i].setAttribute("href","#"+href.substr(site_len+1));
        }
    }
}
AjaxUrlFixer.prototype.checkWhetherChanged = function(location){
    if(this.locator.getHash() != location) {
        doGetPage(this.locator.getHash());
    }
    this.timer.setTimeout("checkWhetherChanged", 200, this.locator.getHash());
}
/* END AjaxUrlFixer */

var _init = false;

function FixBackAndBookmarking() {
    if (_init) return false;
    _init = true;
    if(!document.getElementById || !document.getElementsByTagName) return;
    if(document.iframesfix) {
        fix = new AjaxIframesFixer('ajaxnav');
    } else {
        fix = new AjaxUrlFixer();
    }
}

var detect = navigator.userAgent.toLowerCase();
if(detect.indexOf("msie")>-1)  document.iframesfix = true;
addEvent(window, "load", FixBackAndBookmarking);


