/*
	Création objet Ajax
*/
function createXHR() {
        var xhr;
        if ( window.XMLHttpRequest ) { xhr = new XMLHttpRequest(); }
        else if ( window.ActiveXObject ) {
                try {
                        xhr = new ActiveXObject("Msxml2.XMLHTTP");
		        } catch(e) {
			        xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
        } else xhr = false;
        return xhr;
}
/*
	Rafraichissement menu Flash
*/
var sendPageUrlToFlash = function(pageUrl) {
    var fl;
    
    if ( document.getElementById("flprincipal") ) {
        if (navigator.appName.indexOf("Microsoft") != -1) {
            fl = window["flprincipal"];
        } else {
            if (document["flprincipal"].length != undefined) {
                fl = document["flprincipal"][1];
            } else {
                fl = document["flprincipal"];
            }
        }
        fl.getPageUrl ( pageUrl );
    }
	return false;
}
/*
	Chargement de la page
*/
var loadPage = function(pageUrl) {
    if(/http:\/\/[^\/]*\/(.*)/.test(pageUrl)) pageUrl = pageUrl.split("\/").pop(); 
    
	/*
		Ajout historique
	*/
	if ( typeof(dhtmlHistory) != 'undefined' ) {
		if ( SCRIPT_NAME != pageUrl || (String(document.location).lastIndexOf("#") > -1) ) dhtmlHistory.add( pageUrl );
	}
    
	/*
		Chargement Ajax de la page
	*/
    AjaxLoad( pageUrl );

	/*
		Scroll de la page => retour en haut
	*/
	if ( document.getElementsByTagName ) {
		var t = document.getElementsByTagName("table");
		if ( t[0].scrollIntoView ) t[0].scrollIntoView(true);
	}

	return false;
};

/*
    Chargement Ajax d'une page
*/
var AjaxLoad = function( pageUrl ) {
    var div_content = document.getElementById("contenu");
    var xhr = createXHR();
    xhr.onreadystatechange = function() { 
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                // retour OK
                var content =  xhr.responseText;

				// affichage contenu chargé
                div_content.innerHTML = content;

                // mise à jour du titre
				refreshTitle( content );
            }
        }
    }
    xhr.open("GET", pageUrl+"?abstract=1" , true);
    xhr.send(null);
}

/*
	Rafraichissement du title de la page
*/
var refreshTitle = function(content) {
//    var div_content = document.getElementById("contenu");
//	var content = div_content.innerHTML;
    var from = content.indexOf("<!-- PAGE_TITLE=") + 16;
	if ( from > -1 ) {
	    var to   = content.indexOf("-->", from);
		if ( to > -1 ) {
			var t = content.substring(from, to);
			document.title = t;
		}
	}
}
/*
	Redirection pour refresh sur pages
*/
var url = String(document.location);
if ( url.length > 0 ) {
	var diese = url.lastIndexOf("#");
	if( diese > -1 ) {
		var page = url.substr(diese+1,url.length);
		document.location = '/'+page;
	}
}

/*
	Initialisation du système de gestion de l'historique
*/ 
window.dhtmlHistory.create({
    blankURL: '/Scripts/rsh/blank.html'
});

window.onload = function() {
        dhtmlHistory.initialize();
        dhtmlHistory.addListener( sendPageUrlToFlash );
};







