/*
	- 05.02.2005 	added layer type separation 0 - POI , 1 - Graphics
*/

var ROOT_DIR = "http://www2.comune.venezia.it/mappacomune/Map/EXTPOIS/";
var GET_LAYERS = "getLayers.php";
var GET_POIS = "getPois.php";
var PLUGIN = "http://www2.comune.venezia.it/mappacomune/Map/Plugins/DrawPlugin2/Drawer2.swf";

function CreateExternalPoisManager(name, oVMap) {
	//this function can be calle if HxVMap wrapper exists
	//insert one iframe like serverside script runner mus be placed in main page javascript scope
	//because all serverside generated javascript will be runned inside
	document.write("<iframe name='SRV_POI_RUN' id='SRV_POI_RUN' width='0' height='0' border='1'></iframe>");
	return (new HxExtPois(name, oVMap));
}
function HxExtPois(name, oVMap) {
	if (name != 0) {
		this.Name = name;
		this.RFrame = document.getElementById("SRV_POI_RUN");
		this.oVMap = oVMap;
		this.vLayers = new Array();
		oVMap.backRefPois = this;
		//hook onPluginLoaded function of oVMap if exists
		oVMap.origVMapOnPluginLoaded = oVMap.onPluginLoaded;
		oVMap.onPluginLoaded = this.pluginsLoaded;
		this.plgsLoaded = 0;
		this.vPois = new Array();
		this.oTimer = null;
		
		//hook onMapPosChanged function of oVMap if exists
		oVMap.origVMapOnMapPosChanged = oVMap.onMapPosChanged;
		oVMap.onMapPosChanged = this.onMapPosChanged;
		this.plgsLoaded = 0;
		this.lastPos = null;
	}
}
new HxExtPois(0, 0);
pr = HxExtPois.prototype;
//fields and events
/*
	Even: when all actual layers loading is finished
*/
pr.onLayersLoaded = null;

/*
	Event: when some poi was clicked
*/
pr.onPoiClicked = null;

/*
	Event: when layers list was changed
*/
pr.onLayersUpdate = null
//functions
pr.toString = new Function("return(this.Name);");

//hook handler for onMapPosChanged
function mapPosChanged(o) {
	if(o.l == o.r) return;
	//alert(o);
	//alert("new handler");
	this.backRefPois.lastPos = o;
	this.backRefPois.getPois(o.l, o.t, o.r, o.b, o.z);
	if (this.origVMapOnMapPosChanged != null) {
		this.origVMapOnMapPosChanged(o);
	}
}
pr.onMapPosChanged = mapPosChanged;

//hook handler for onPluginsLoaded --------------------------------------------------------------------------------------------------------
function plgsLoaded(n, t) {
	//alert("POI plugin loaded:"+n);
	if (this.backRefPois.vLayers == null) {
		if(	this.origVMapOnPluginLoaded!= null){
			this.origVMapOnPluginLoaded(n, t);
		}
		return;
	}
	var o = this.backRefPois.getLayer_PLN(n);
	if (o == null) {
		//if is not my plugin so call orig handler
		this.origVMapOnPluginLoaded(n, t);
	} else {
		if (o.loaded == 1) {
			this[n+"_onPoiClicked"] = this.backRefPois.onPoiClickedCentral;
			//alert("fn:"+eval("this." + n +"_onPoiClicked"));
			this.backRefPois.plgsLoaded++;
			o.loaded = 2;
			if (this.backRefPois.vLayers.length == this.backRefPois.plgsLoaded) {
				//loading sucessfully finished
				if (this.backRefPois.onLayersLoaded != null) {
					this.backRefPois.onLayersLoaded();
				}
			}
		} else {
			alert("Error:"+n+"PoiLayers already loaded!");
		}
	}
}
pr.pluginsLoaded = plgsLoaded;

//add layer to internal structure
// Par: identification, type, parent, name, description, visible_on_off zoom_minimum zoom_max
function aL(i, p, n, d, v, zmi, zma, type) {
	if(this.getLayer_ID(i) == null) {
		//alert("Layer:"+i+","+n+","+d+","+v);
		this.vLayers.push({ID: i, Type: type, Group: p, Name: n, Desc: d, Visible: v==1 ? true : false, loaded: false, plName: 'DR_'+i, zMin: zmi, zMax: zma, loaded: 0 /* 0-nothing happend jet, 1-load was called, 2-all done*/, toString:new Function("return(this.ID+'#'+this.Group+'#'+this.Name+'#'+this.Desc+'#'+this.Visible);")});
	}	
}
pr.addLayer = aL;

//return layers vector
pr.getLayers = new Function("return(this.vLayers);");
//finish loading layers
function fl() {
	//load plugins one for each layer
	this.plgsLoaded = 0;
	//reset test counter for loading
	for (var i in this.vLayers) {
		if(this.vLayers[i].loaded == 0){
		 	this.vLayers[i].loaded = 1;
			//alert(PLUGIN+","+this.vLayers[i].plName);
			this.oVMap.loadPlugin(PLUGIN, this.vLayers[i].plName);
			//document.write(this.oVMap.Name);
		}
	}
}
pr.finishLayersLoading = fl;

//get layer object trough its plugin name
function gl_n(n) {
	var i = 0;
	//alert(this.vLayers);
	while ((i<this.vLayers.length) && (this.vLayers[i].plName != n)) {
		i++;
	}
	if (i == this.vLayers.length) {
		return null;
	} else {
		return this.vLayers[i];
	}
}
pr.getLayer_PLN = gl_n;
function gl_i(n) {
	var i = 0;
	while ((i<this.vLayers.length) && (this.vLayers[i].ID != n)) {
		i++;
	}
	if (i == this.vLayers.length) {
		return null;
	} else {
		return this.vLayers[i];
	}
}
pr.getLayer_ID = gl_i;
function gvl() {
	var ret = "";
	var sep = "";
	for (var i = 0; i<this.vLayers.length; i++) {
		if (this.vLayers[i].Visible) {
			ret += sep+this.vLayers[i].ID+","+this.vLayers[i].Type;
			sep = "§";
		}
	}
	return ret;
}
pr.getVisibleLayers = gvl;
function ggl() {
	var ret = new Array();
	for (var i = 0; i<this.vLayers.length; i++) {
		ret[this.vLayers[i].Group] = this.vLayers[i].Group;
	}
	var sz = "";
	var div = "";
	for (k in ret) {
		sz += div+ret[k];
		if (div == "") {
			div = ",";
		}
	}
	return sz;
}
pr.getParents = ggl;
function sP(id, sz, type) {
	var o = this.getLayer_ID(id);
	//alert(o+"\n"+id+"\n"+sz);
	if(o.loaded == 2){
			switch(type){
				case 1:
					this.oVMap.callPlugin('DR_'+o.ID, 'drawGraphics', sz);
					break;
				case 0:
					this.oVMap.callPlugin('DR_'+o.ID, 'redrawPois', sz);
					break;
			}
	}else{
			this.vPois.push({id: id, sz: sz, type: type});
			this.loadPois();
	}
}
pr.showPois = sP;
function rL(i) {
	this.oVMap.callPlugin('DR_'+i, 'Reset', '');
}
pr.resetLayer= rL;

function clkevt(ix, sender) {
	if (sender.backRefPois.onPoiClicked != null) {
		sender.backRefPois.onPoiClicked(ix);
	}
}
pr.onPoiClickedCentral = clkevt;
function sT(id, bVal) {
	var l = this.getLayer_ID(id);
	if (l != null) {
		l.Visible = bVal;
		
		if (bVal) {
			if (this.lastPos != null) {
				this.getPois(this.lastPos.l, this.lastPos.t, this.lastPos.r, this.lastPos.b, this.lastPos.z, String(id)+","+l.Type);
			}
		} else {
			this.oVMap.callPlugin('DR_'+id, 'Reset', '');
		}
	}
}
pr.showLayer = sT;
function sTs(ids, bVal) {
	var vId = ids.split(",");
	for (var id = 0; id<vId.length; id++) {
		var l = this.getLayer_ID(vId[id]);
		if (l != null) {
			l.Visible = bVal;
			if (!bVal) {
				this.oVMap.callPlugin('DR_'+vId[id], 'Reset', '');
			}
		}
	}
	if (bVal) {
		this.getPois(this.lastPos.l, this.lastPos.t, this.lastPos.r, this.lastPos.b, this.lastPos.z);
	}
}
pr.showLayers = sTs;

function refrP(){
	this.getPois(this.lastPos.l, this.lastPos.t, this.lastPos.r, this.lastPos.b, this.lastPos.z);
}
pr.refreshPois = refrP;

function lclk(id) {
	var l = this.getLayer_ID(id);
	if (l.Visible) {
		this.showLayer(id, false);
	} else {
		this.showLayer(id, true);
	}
}
pr.layerClicked = lclk;

function rml(id){
	var i = 0;
	while ((i<this.vLayers.length) && (this.vLayers[i].ID != id)) {
		i++;
	}
	if (i == this.vLayers.length) {
		return;
	} else {
		this.oVMap.removePlugin('DR_'+this.vLayers[i].ID);
		this.plgsLoaded--;
		this.vLayers.splice(i,1);
		return this.vLayers[i];
	}	
}
pr.removeLayer = rml;

function loadP(){
	
	var toWorkAgain = new Array();
	for(k in this.vPois){
		var o = this.getLayer_ID(this.vPois[k].id);
		if(o.loaded == 2){
			switch(this.vPois[k].type){
				case 1:
					this.oVMap.callPlugin('DR_'+o.ID, 'drawGraphics', this.vPois[k].sz);
					break;
				case 0:
					this.oVMap.callPlugin('DR_'+o.ID, 'redrawPois', this.vPois[k].sz);
					break;
			}
		}else{
			toWorkAgain.push(this.vPois[k]);
		}
	}
	delete this.vPois;
	this.vPois = toWorkAgain;
	if(toWorkAgain.length > 0){
		if(this.oTimer)clearTimeout(this.oTimer);
		this.oTimer = setTimeout(this.Name+".loadPois()", 200);
	}else{
		try{
			if(this.onLayersUpdate!=null){
				this.onLayersUpdate(); //parent.updatePoiLayers();
			}
		}catch(e){
			alert(e.message);
		}
	}
}
pr.loadPois = loadP;

//loadLayers from server db
function mPL() {
	SRV_POI_RUN.location.replace(ROOT_DIR+GET_LAYERS+"?OBJ="+this.Name);
}
pr.loadLayers = mPL;

function ref(l, t, r, b, z, szLayers) {
	if (szLayers) {
		var la = szLayers;
		//alert("1");
	} else {
		var la = this.getVisibleLayers();
		//alert("2");
	}
	if (la == "") {
		return;
	}
	var m = t<b ? t : b;
	var b = t<b ? b : t;
	t = m;
	//this.RFrame.src = ROOT_DIR+GET_POIS+"?OBJ="+this.Name+"&Layers="+la+"&Rect="+l+"§"+t+"§"+r+"§"+b+"§"+z+"&C="+new Date();
	SRV_POI_RUN.location.replace(ROOT_DIR+GET_POIS+"?OBJ="+this.Name+"&Layers="+la+"&Rect="+l+"§"+t+"§"+r+"§"+b+"§"+z+"&C="+new Date());
	//alert(la);
}
pr.getPois = ref;


delete pr;
