// ensemble de fonctions javascript de base

var JS_inc_files = new Array();
var memo_obj = new Array();

// les codes claviers
KEY_BACKSPACE= 8;
KEY_TAB=       9;
KEY_RETURN=   13;
KEY_ESC=      27;
KEY_LEFT=     37;
KEY_UP=       38;
KEY_RIGHT=    39;
KEY_DOWN=     40;
KEY_DELETE=   46;

// Trouver un objet dans la page HTML

function findObj(theObj, theDoc) {
	var p, i, foundObj;
	
	if (typeof( theObj )=='object') {
		theObj = theObj.name;
	}
	
	if ( memo_obj[ theObj ] ) {
		foundObj = memo_obj[ theObj ];
	}

	if (!theDoc)
		theDoc = document;
	
	if (((p = theObj.indexOf("?")) > 0) && parent.frames.length) {
		theDoc = parent.frames[theObj.substring(p+1)].document;
		theObj = theObj.substring(0,p);
	}
	
	if (!foundObj) {
		foundObj = theDoc[theObj];
	}
		
	if ((!foundObj) && (theDoc.all)) {
		foundObj = theDoc.all[theObj];
	}
	
	for (i=0; (!foundObj) && (i < theDoc.forms.length); i++) {
		foundObj = theDoc.forms[i][theObj];
	}
	
	for (i=0; (!foundObj) && (theDoc.layers) && (i < theDoc.layers.length); i++)
		foundObj = findObj(theObj,theDoc.layers[i].document);
	
	if ((!foundObj) && (document.getElementById))
		foundObj = document.getElementById(theObj);
		
 	
	if ((!memo_obj[ theObj ]) && (foundObj) ) {
		memo_obj[ theObj ] = foundObj;
	}

	return foundObj;
}

// teste l'existence d'une fonction javascript
function is_func( functionName ) {
	return (typeOf( functionName )=="function");
}

// vrai si variable est un tableau
function is_array( obj ) {
	try {
		return ((obj instanceof Array)||( obj.constructor == Array ));
	}
	catch (e) {
		return false;
	}
}

// var_dump pour les tableaux
Array.prototype.var_dump = function() {
 if (arguments.length) var indent = arguments[0];
 else var indent = "   "; // indentation des éléments du tableau
 html = "a{";
 var i = 0;
 for (var elt in this) {
   var typeelt = typeof this[elt];
   if (typeelt == "function") continue;
   html += i ? ", " : "";
   if (typeelt == "object") html += "\n" + indent + "[" + elt + "]:" + this[elt].var_dump(indent +
indent);
   else {
       html += "[" + elt + "]:";
       if (typeelt == "string") html += "\"" + this[elt] + "\"";
       else html += this[elt];
   }
   i++;
 }
 html += "}";
 return html;
}

// inclut un script à la page en cours
function JS_inc( script_filename ) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

// n'inclut qu'une fois un script javascript à la page
function JS_inc_once( script_filename ) {
    if (!in_array( script_filename, JS_inc_files )) {
        JS_inc_files[ JS_inc_files.length ] = script_filename;
        JS_inc( script_filename );
    }
}

// inclut une feuille de style à la page en cours
function CSS_inc( css_filename ) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('link');
    js.setAttribute('rel', 'stylesheet');
    js.setAttribute('type', 'text/css');
    js.setAttribute('href', css_filename);
    html_doc.appendChild(js);
    return false;
}

// renvoi vrai si la valeur existe dans le tableau
function in_array( valeur, tableau ) {
    for (var i = 0; i < tableau.length; i++) {
        if ( tableau[i] == valeur ) {
            return true;
        }
    }
    return false;
}

function JScreateID( basename ) {
	// construction de l'id
	var n = String( Math.round( Math.random() *32768 ) );
	var nArr = new Array(n);
	if (n.length<8) {
		for(i= n.length; i<8; i++) {
			nArr.unshift("0");
		}
	}
	n = nArr.join('');
	var ID = basename.replace(/\W/,"_");
	while ( ID.search(/\W/) != -1 ) {
		ID = ID.replace(/\W/,"_");
	}
	ID += n.substr(4,8);
	return ID;
}

function patchContenuHeight() {

// 1. attend le chargement complet (notamment, celui des images)
/*
 * pas encore au point donc on ne le fait pas plutôt... perte de temps
 *
	var res;
	do{
		res = checkImgLoad();
	} while ( !res );
*/

// 2. modifie les éléments
	// d'abord on récupère les objets
	var DivGche = findObj( "SPEGpano_gche");
	var DivChem = findObj( "SPEGchemin");
	var DivCont = findObj( "SPEGcontenu" );
	var DivDrte = findObj( "SPEGpano_droite" );
	
	// ensuite les hauteurs de ces ibjets
	var hGche = Math.max( DivGche.offsetHeight, DivGche.height);
	var hChem = Math.max( DivChem.offsetHeight, DivChem.height);
	var hCont = Math.max( DivCont.offsetHeight, DivCont.height);
	var hDrte = Math.max( DivDrte.offsetHeight, DivDrte.height);
	
	// puis on rectifie uniquement si la hauteut de la cellule centrale et celle de doite
	// sont différente d'au moins 4 pixels
	if (Math.abs( hCont - hDrte)>4) {
		DivCont.height = hGche-hChem;
	}

}

function checkImgLoad() {
	if (document.images.length == 0) return false;
	for ( i=0; i<document.images.length; i++) {
		if (!document.images[i].complete)
			return false;
	}
	return true;
}
