//var document = "peuplade.fr";

/***********************************************************************

******	  PLabel - DISPLAY CONTENT LABEL ON MAP				      ******

************************************************************************

* @autor : Nicolas LEGOUFFE <n.legouffe@gmail.com>

* @compagny : Les Ingénieurs Sociaux - Peuplade.fr

* @create : by Nicolas LEGOUFFE, 18/04/2008

* @update : --

***********************************************************************/

PLabel = function(point, html, classname) {

	this.point = point;

	this.html = html;

	// Optional parameters

	this.classname = classname||"labelContent";

	this.pixelOffset = new GSize(0,0);

	this.hidden = false;

}

PLabel.prototype = new GOverlay();

/*

*/

PLabel.prototype.initialize = function(map) {

  var div = document.createElement("div");

  div.style.position = "absolute";

  div.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>' ;

  map.getPane(G_MAP_FLOAT_SHADOW_PANE).appendChild(div);

  this.map_ = map;

  this.div_ = div;

  var z = GOverlay.getZIndex(this.point.lat());

  this.div_.style.zIndex = z;

}

/*

*/

PLabel.prototype.remove = function() {

	this.div_.parentNode.removeChild(this.div_);

}

/*

*/

PLabel.prototype.copy = function() {

	return new PLabel(this.point, this.html, this.classname);

}

/*

*/

PLabel.prototype.redraw = function(force) {

	var p = this.map_.fromLatLngToDivPixel(this.point);

	var h = parseInt(this.div_.clientHeight);

	this.div_.style.left = (p.x + this.pixelOffset.width + 30) + "px";

	this.div_.style.top = (p.y +this.pixelOffset.height - h -15) + "px";

}

/*

*/

PLabel.prototype.show = function() {

	if (this.div_) {

		this.div_.style.display="";

		this.hidden = false;

		this.redraw();

	}

}

/*

*/     

PLabel.prototype.hide = function() {

	if (this.div_) {

		this.div_.style.display="none";

		this.hidden = true;

	}

}

/*

*/

PLabel.prototype.isHidden = function() {

	return this.hidden;

}

/*

*/

PLabel.prototype.setContents = function(html) {

this.html = html;

this.div_.innerHTML = '<div class="' + this.classname + '">' + this.html + '</div>' ;

this.redraw(true);

}

/*

*/

PLabel.prototype.setPoint = function(point) {

	this.point = point;

	var z = GOverlay.getZIndex(this.point.lat());

	this.div_.style.zIndex = z;

	this.redraw(true);

}

/*

*/

PLabel.prototype.getPoint = function() {

	return this.point;

}

/***********************************************************************

******	  Pxml - DISPLAY CONTENT PEUPLADE ON GOOGLE MAP		      ******

************************************************************************

* @autor : Nicolas LEGOUFFE <n.legouffe@gmail.com>

* @compagny : Les Ingénieurs Sociaux - Peuplade.fr

* @create : by Nicolas LEGOUFFE, 18/04/2008

* @update : --

***********************************************************************/

Pxml = function(map, href, options){

	var me = this;
	
	this.map = map;

	this.parameter = '';
	
	this.dataFieldContent = new Array();
	
	this.mkm = new GMarkerManager(map);

	for (key in options){ this.parameter = this.parameter+"&"+key+"="+options[key];	}
	
	
	//--

	var downloadXML = GDownloadUrl(href+"?coord="+map.Enveloppe().toMysql()+"&center="+map.getCenter().lat()+" "+map.getCenter().lng()+"&zoom="+map.getZoom()+""+this.parameter, function (result) {

			var xml = GXml.parse(result);

			var data = xml.documentElement.getElementsByTagName("data");
		
			if(data.length > 0) {

				var markers = new Array();
				
				for(var i = 0 ; i < data.length ; ++i) {

					var icons = data[i].getElementsByTagName("icon");
					var field = data[i].getElementsByTagName("field");
					me.dataFieldContent.push(field);
					//console.debug(this.dataFieldContent);
					
					//--
					var Coords = data[i].getElementsByTagName("point");

						var CoordX 	= Coords[0].getElementsByTagName("x")[0].firstChild.nodeValue;

						var CoordY 	= Coords[0].getElementsByTagName("y")[0].firstChild.nodeValue;

					//--

					var id = data[i].getElementsByTagName("id")[0].firstChild.nodeValue;

					var type = data[i].getElementsByTagName("type")[0].firstChild.nodeValue;

					var description = data[i].getElementsByTagName("description")[0].firstChild.nodeValue;

					//--

					var currentMarker = me.getMarker(new GPoint(CoordY, CoordX),{'icons': icons[0],'type': type, 'id': id, 'description': description })

					markers.push(currentMarker);
				}

				me.map.clearOverlays();

				me.mkm.addMarkers(markers, 0);

				me.mkm.refresh();

				me.mkm = null;
				
			}else{
				//alert("no Data !");
			}

		}																																		   

	);

	downloadXML = null;

}

Pxml.prototype.getFieldContent = function (){
	return this.dataFieldContent;
}


/*
*/

Pxml.prototype.getMarker = function (point,options){
	var me = this;

	var baseIcon = new GIcon();

	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";

	baseIcon.shadowSize = new GSize(37, 34);

	baseIcon.iconAnchor = new GPoint(9, 34);

	baseIcon.infoWindowAnchor = new GPoint(9, 2);

	baseIcon.infoShadowAnchor = new GPoint(18, 25);

	

	var iconOut 		= options['icons'].getElementsByTagName("out");

	var iconOutHref 	= iconOut[0].getElementsByTagName("href")[0].firstChild.nodeValue;

	var iconOutWidth 	= iconOut[0].getElementsByTagName("w")[0].firstChild.nodeValue;

	var iconOutHeight 	= iconOut[0].getElementsByTagName("h")[0].firstChild.nodeValue;

	var iconOutAction	 	= iconOut[0].getElementsByTagName("action");

	var iconOutActionClick 	= iconOutAction[0].getElementsByTagName("onclick")[0].firstChild.nodeValue;

	//--

	baseIcon.iconSize = new GSize(iconOutWidth, iconOutHeight);

	var iconOut = new GIcon(baseIcon);

	iconOut.image = iconOutHref;

	//--

	var markerOut = new GMarker(point,iconOut);

	

	//--

	var iconOver 		= options['icons'].getElementsByTagName("over");

	var iconOverHref 	= iconOver[0].getElementsByTagName("href")[0].firstChild.nodeValue;

	var iconOverWidth 	= iconOver[0].getElementsByTagName("w")[0].firstChild.nodeValue;

	var iconOverHeight 	= iconOver[0].getElementsByTagName("h")[0].firstChild.nodeValue;

	var iconOverAction	 	= iconOver[0].getElementsByTagName("action");

	var iconOverActionClick = iconOverAction[0].getElementsByTagName("onclick")[0].firstChild.nodeValue;

	//--

	baseIcon.iconSize = new GSize(iconOverWidth, iconOverHeight);

	var iconOver = new GIcon(baseIcon);

	iconOver.image = iconOverHref;

	//--

	var markerOver = new GMarker(point,iconOver);

	var label = new PLabel(markerOver.getLatLng(), options['description']);

	

	//--

	GEvent.addListener(markerOut, "mouseover", function() {

		me.map.removeOverlay(markerOut);

		me.map.addOverlay(markerOver);

		me.map.addOverlay(label);

	});

	GEvent.addListener(markerOut, "click", function() {
		document.location.href = iconOutActionClick;
		//window.open(iconOutActionClick);
	});

	//--

	GEvent.addListener(markerOver, "mouseout", function() {

		me.map.removeOverlay(label);

		me.map.removeOverlay(markerOver);

		me.map.addOverlay(markerOut);

	});

	GEvent.addListener(markerOver, "click", function() {
		document.location.href = iconOverActionClick;
		//window.open(iconOverActionClick);
	});

	

	//--

	return markerOut;

}