//fonctions generales js utiles


var Prototype = {
	Version: '1.3.1',
	emptyFunction: function() {}
}

var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}

var Abstract = new Object();

Object.extend = function(destination, source) {
	for (property in source) {
		destination[property] = source[property];
	}
	return destination;
}

Object.prototype.extend = function(object) {
	return Object.extend.apply(this, [this, object]);
}

Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		__method.apply(object, arguments);
	}
}
//class de Gestionnnaire Evt
var GestEvt=Class.create();
GestEvt.prototype= {
	initialize: function(){
		//sauvegarde les fonctions de rappel
		this.tabFonc=new Array();
		//sauvegarde les evenements ecoutes
		this.tabListen=new Array();
		//sauvegarde les objets ecoutes, pour les appler avec la fonction de rappel
		this.tabObjet=new Array();
		this.nomEvt="";

	},

	ajouteEcouteur : function(evt,fonction,objetRetour) {
		if (!objetRetour){
			objetRetour = this;
		}
		var nonok=false;

		for(i=0;i<this.tabFonc.length;i++){
			if (this.tabFonc[i]==evt){
				nonok=true;
			}
		}
		if(!nonok){
			//	alert("impossible enregister evenement:fonction inconnue");
		}else{
			//fonctions
			var tabFonctemp=this.tabListen[evt];
			tabFonctemp[tabFonctemp.length]=fonction;
			this.tabListen[evt]=tabFonctemp;
			//objets
			var tabObjtemp=this.tabObjet[evt];
			tabObjtemp[tabObjtemp.length]=objetRetour;
			this.tabObjet[evt]=tabObjtemp;



		}

	},

	enleveEcouteur:function(nameEvent,fonction){
		var NewTab=new Array();
		var NewTabObj=new Array();
		var montableau=this.tabListen[nameEvent];
		var montableauObjet=this.tabObjet[nameEvent];
		for(i=0;i<montableau.length;i++){
			if(montableau[i]!=fonction){
				NewTab[NewTab.length]=montableau[i];
				NewTabObj[NewTabObj.length]=montableauObjet[i];
			}
		}
		this.tabListen[nameEvent]=NewTab;
		this.tabObjet[nameEvent]=NewTabObj;

	},

	ajouteEvenement:function(nameEven){
		this.tabFonc[this.tabFonc.length]=nameEven;
		this.tabListen[nameEven]= new Array();
		this.tabObjet[nameEven]=new Array();
	},

	lanceEvenement:function(nameEvent,Arretour){

		if(!Arretour){
			Arretour=new Array();

		}
		var montableau=this.tabListen[nameEvent];
		//	alert(montableau.length);
		var montabObjet=this.tabObjet[nameEvent];
		if(typeof(Arretour)=='string'){
			var Arrtmp=[Arretour];
			Arretour=Arrtmp;

		}
		//alert(typeof(Arretour));

		this.nomEvt=nameEvent;
		//alert(Arretour);

		if(montableau.length!=0){
			//alert(montableau.length);
			for(var i=0;i<montableau.length;i++){
				mafonction=montableau[i];
				monobjet=montabObjet[i];
				//alert('fonction appel sur '+this.nomEvt+' fonc'+mafonction);
				if(!(mafonction instanceof Function)&&0){
					alert("erreur:l'arguement passé au gestionnaire de fonctions n'est pas une fonction");
				}else{
					if(mafonction){
						try{
							//mafonction();

							mafonction.apply(monobjet,Arretour);
						}catch(e){

							try{
								var parameterStrings = new Array();
								var object=monobjet;

								for (var ia = 0; ia < Arretour.length; ia++){
									parameterStrings[ia] = 'Arretour[' + ia + ']';
								}
								object.__apply__ = mafonction;
								var result = eval('object.__apply__(' +parameterStrings.join(', ') + ')');
								object.__apply__ = null;

								return result;

							}catch(e){
								//alert(e.name);
								//alert(e.message);
								//alert("la fonction de retour  ne peut etre atteinte"+mafonction);
							}
						}
					}
				}
			}
		}

	}
};

GestEvtDyn=new GestEvt();
parent.GestEvtDyn=GestEvtDyn;
Function.prototype.lieEvenement = function(strevt) {

	GestEvtDyn.ajouteEcouteur(strevt,this);
	return 1;
};

parent.legendeCharge=legendeCharge;
parent.legCharge=legCharge;
var legCharge=0;

function legendeCharge(){

	legCharge=1;

	dataInitLoad(sbdatTemp);

}

var LAYERCONTROL_LOADED = '0';

//pour afficher la legende dynamiquement
function lanceLegende(logok){

	var DivLeg=$("TheLayerID");

  if(DivLeg){

      	if(logok=="1"){
      		DivLeg.innerHTML=CheminLegende;
      	}else{
      		DivLeg.innerHTML="";
      	}
 	}else{
 	  LAYERCONTROL_LOADED = CheminLegende;
  }
	//alert('lanceLeg');
}

//chargeur de fonction
var Charge=Class.create();
Charge.prototype= {
	initialize: function(){
	},
	//charge un script dans svg
	BlockSvg:function(idBlock,fonction){
		svgdoc=document.svgmap.getSVGDocument();
		root=svgdoc.getDocumentElement();

		if(!svgdoc.getElementById(idBlock)){
		var svgns = "http://www.w3.org/2000/svg";

			var Myscript = svgdoc.createElementNS(svgns, "script");
			Myscript.defer = false;
			Myscript.setAttribute('type', "text/ecmascript");
			//var srcScript = '/dynmap/loaderJsBlock.php?idBlock='+idBlock;
			//Myscript.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",srcScript);
			Myscript.setAttribute('id' , idBlock);
			scriptCode = svgdoc.createTextElement("alert('ttiti');");
			Myscript.appendChild(scriptCode);

			root.appendChild(Myscript);
			var scriptLoaded=svgdoc.getElementById(idBlock);

			alert(scriptLoaded.getAttribute('readyState'));

		}

		//alert(scriptLoaded);
		//alert(scriptLoaded2);
		//alert(svgdoc);
		var scriptLoad=svgdoc.getElementById(idBlock);
		alert(scriptLoad.firstChild);
		alert(scriptLoad.firstChild);
		try{
			test46();
			eval(fonction+'()');
		}catch(e){
			alert(e.message);
		}


	}
}
function WriteMainMap(mapUrlFin){
	document.write(mapUrlFin);
}
var C=new Charge();
var Utf8 = {

    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }
}