/**************************
Dectection du navigateur :
**************************/
var navigateur = navigator.userAgent;
var isOpera = (navigateur.toLowerCase().indexOf('opera')>=0)?true:false;
var isFirefox = (navigateur.toLowerCase().indexOf('firefox')>=0)?true:false;
var isMSIE = (navigateur.toLowerCase().indexOf('msie')>=0)?true:false;
var navigatorVersion = navigator.appVersion.replace(/.*?MSIE(\d\.\d).*/g,'$1')/1;

/**
 * This derivative version of subModal can be downloaded from http://gabrito.com/files/subModal/
 * Original By Seth Banks (webmaster at subimage dot com)  http://www.subimage.com/
 * Contributions by Eric Angel (tab index code), Scott (hiding/showing selects for IE users), Todd Huss (submodal class on hrefs, moving div containers into javascript, phark method for putting close.gif into CSS), Thomas Risberg (safari fixes for scroll amount), Dave Campbell (improved parsing of submodal-width-height class)
 */

// Popup code
var activeCentreAuto = false;

/**
 * Initializes popup code on load.
 */
function initPopUp(BtFermer,titre) {
	// Add the HTML to the body
	var body = document.getElementsByTagName('body')[0];

    //on crée le fond (masque ) ...
    var popmask = document.createElement('div');
	popmask.id = 'popupMask';

    //on crée la popup en elle même ....
	var popcont = document.createElement('div');
	popcont.id = 'popupContainer';

    //s'il y a un titre on l'affiche sinon non
    if(titre!='' || BtFermer){

        //on crée la barre du  haut ..
        var barreHaut = document.createElement('div');
        barreHaut.id = 'popupTitleBar';

        //dans la barre il y a le titre
        var barreTitre = document.createElement('div');
        barreTitre.id = 'popupTitle';
        barreHaut.appendChild(barreTitre);

        //et le bouton fermer
        if(BtFermer){
            var barreOptions = document.createElement('div');
            barreOptions.id = 'popupControls';
            barreOptions.setAttribute('onclick','closeModal();');
            barreOptions.onclick=closeModal;
            barreHaut.appendChild(barreOptions);
        }

        popcont.appendChild(barreHaut);
    }

    //on creer la div de contenu ..
    var contenuPop = document.createElement('div');
    contenuPop.id = 'contenuPop';

    popcont.appendChild(contenuPop);
	body.appendChild(popmask);
	body.appendChild(popcont);

    //hauteur total de l'ecran ....
    document.getElementById("popupMask").style.height = document.documentElement.scrollHeight + "px";
}


////// function pour centrer la fenetre sur la page ....
var gi = 0;
function centerPopWin(width, height) {


    if (activeCentreAuto == true) {
        if (width == null || isNaN(width)) width = document.getElementById("popupContainer").offsetWidth;
        if (height == null) height = document.getElementById("popupContainer").offsetHeight;

		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();

		// scLeft and scTop changes by Thomas Risberg
		var scLeft;

        if (self.pageYOffset) {
			scLeft = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			scLeft = document.documentElement.scrollLeft;
		} else if (document.body) {
			scLeft = document.body.scrollLeft;
		}


        if(isFirefox) document.getElementById("popupMask").style.width = (fullWidth-15) + "px";
        else document.getElementById("popupMask").style.width = fullWidth + "px";


        marginTop= ((fullHeight - height)/2) + document.documentElement.scrollTop;
		document.getElementById("popupContainer").style.top =  marginTop + "px";
		document.getElementById("popupContainer").style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";


    }
}

addEvent(window, "resize", centerPopWin);// quand on retaille la fenetre ...

// quand on se déplace avec la scroll Bar ....
window.onscroll = function() {
    var hauteurTotal = document.documentElement.scrollHeight;

    if(isMSIE)var hauteurScroll=document.body.scrollTop;
    else var hauteurScroll= document.documentElement.scrollTop;

    if(hauteurScroll<hauteurTotal){
        if(document.getElementById("popupContainer")!=null) centerPopWin();
    }
}

 /**
 * function pour afficher la fenetre ...
* @argument width - int in pixels
* @argument height - int in pixels
* @argument url - url to display
* @argument returnFunc - function to call when returning true from the window.
*/
function afficheModal(contenu, url, width, height, titre, classCss, BtFermer) {

    closeModal();//on ferme déja la fenetre avant d'en ouvrir une ..

    //on crée la div .... s'il y a pas de titre on affiche pas la barre du haut ....
    //si la pop existe on ne la crée pas ....
    if(document.getElementById("popupContainer")==null) initPopUp(BtFermer,titre)

    //on lui affecte le titre ..
    if(document.getElementById("popupTitle")!=null && titre!='') document.getElementById("popupTitle").innerHTML=titre;

    //permet de centrer la fenetre ....
    activeCentreAuto = true;
    centerPopWin(width, height);

    //chargement du contenu ....;
    if(url!=null && url!='') ajax_loadContent('contenuPop',url);
    else if(contenu!=null)  document.getElementById("contenuPop").innerHTML=contenu;

    //largeur et hauteur de la div .....
    document.getElementById("popupContainer").style.width = width + "px";


    if(document.getElementById("popupTitleBar")!=null) var hauteurBarre=22;//document.getElementById("popupTitleBar").offsetHeight;
    else var hauteurBarre=0;

    document.getElementById("popupContainer").style.height = (height*1)+(hauteurBarre*1) + "px";


    //hauteur de la div de contenu ....
    document.getElementById("contenuPop").style.height = height + "px";

    //changement de class si elle est précisé ...
    if(classCss!=null && classCss!=''){
        document.getElementById("popupContainer").className=classCss;
    }else{
        document.getElementById("popupContainer").className="modalDialog_contentDiv";
    }

    // si c'est ie on cache les slectbox .....
	if(isMSIE) hideSelectBoxes();


    //on les fait apparaitre ....
    document.getElementById("popupMask").style.display = "block";
	document.getElementById("popupContainer").style.display = "block";

}

/**
** Pour fermer la fenetre ......
*/
function closeModal() {
	activeCentreAuto = false;

    if(document.getElementById("popupMask")!=null) document.getElementById("popupMask").style.display = "none";
	if(document.getElementById("popupContainer")!=null) document.getElementById("popupContainer").style.display = "none";

    var body = document.getElementsByTagName('body')[0];
    //on les détruit
    if(document.getElementById("popupMask")!=null) body.removeChild(document.getElementById("popupMask"));
    if(document.getElementById("popupContainer")!=null) body.removeChild(document.getElementById("popupContainer"));

    // display all select boxes
	if(isMSIE) displaySelectBoxes();
}

/**
**Permet de cacher les select box sous ie ........ qui passe devant la div ..
*/
function hideSelectBoxes() {
    var elements = document.getElementsByTagName('body')[0].getElementsByTagName('SELECT');
    for(var i = 0; i < elements.length; i++) {
		elements[i].style.visibility="hidden";
	}
}

/**
** quand on referme la fentre remet  les select visibles ....
*/
function displaySelectBoxes() {
    var elements = document.getElementsByTagName('body')[0].getElementsByTagName('SELECT');
    for(var i = 0; i < elements.length; i++) {
		elements[i].style.visibility="visible";
	}
}


/**
 * ecouteur ..........
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
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;
 }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/ *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight;
	return window.undefined;
}

function getViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
	if (document.body) return document.body.clientWidth;
	return window.undefined;
}


// traitement AJAX des fenêtres modales (message)
//savoir si on garde en cache ou pas
////// chargement du contenu .......
var enableCache = false;
var jsCache = new Array();

var dynamicContent_ajaxObjects = new Array();

function ajax_showContent(divId,ajaxIndex,url){
	var targetObj = document.getElementById(divId);
	targetObj.innerHTML = dynamicContent_ajaxObjects[ajaxIndex].response;
	if(enableCache){
		jsCache[url] = 	dynamicContent_ajaxObjects[ajaxIndex].response;
	}
	dynamicContent_ajaxObjects[ajaxIndex] = false;

	ajax_parseJs(targetObj)
}


function ajax_loadContent(divId,url){
	
    if(enableCache && jsCache[url]){
		document.getElementById(divId).innerHTML = jsCache[url];
		return;
	}

	var ajaxIndex = dynamicContent_ajaxObjects.length;
	document.getElementById(divId).innerHTML = "Chargement du contenu";
	dynamicContent_ajaxObjects[ajaxIndex] = new sack();
	dynamicContent_ajaxObjects[ajaxIndex].requestFile = url;	// Specifying which file to get
	dynamicContent_ajaxObjects[ajaxIndex].onCompletion = function(){
        ajax_showContent(divId,ajaxIndex,url);

        //style pour les checkbox .......
        if(typeof(styleCheckboxesAndRadiobuttons)!='undefined') styleCheckboxesAndRadiobuttons();
        if(typeof(checkForLabels)!='undefined') checkForLabels();

    };	// Specify function that will be executed after file has been found

    dynamicContent_ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function


}


function ajax_parseJs(inputObj){
	var jsTags = inputObj.getElementsByTagName('SCRIPT');
	for(var no=0;no<jsTags.length;no++){
		eval(jsTags[no].innerHTML);
	}
}

