/*
	Author: Alberto Asuero (alberto.asuero@gstgis.com)
	Date: 26-07-2011
	Enterprise: Geographica Studio
	Mail: alberto.asuero@gstgis.com
	Tfno: 645816025
*/

function sleep(millisegundos) {
var inicio = new Date().getTime();
while ((new Date().getTime() - inicio) < millisegundos);
}
/* Class Layer*/
function Layer(objectValues)
{
	if (!objectValues)
	{
		return;
	}

	/* id */
	this.id = (objectValues.id) ?  objectValues.id : null;
	/* kml_path */
	this.kml = (objectValues.kml) ?  objectValues.kml : null;
	/* priority */
	this.priority = (objectValues.priority) ?  objectValues.priority : null;
	/* zoomMax*/
	this.zoomMax = (objectValues.zoomMax) ?  objectValues.zoomMax : null;
	/* zoomMin*/
	this.zoomMin = (objectValues.zoomMin) ?  objectValues.zoomMin : null;
	/* visibility*/
	this.visible = (objectValues.visible) ?  objectValues.visible : null;
	/* reactive*/
	this.reactive = (objectValues.reactive) ?  objectValues.reactive : false;

	/*layer object*/
	this.layerObj = null;
	
	this.func_infowindow = function (kmlEvent){
		if (kmlEvent.featureData.description != undefined)
		{
			//alert(kmlEvent.featureData.description);
			var response = jQuery.evalJSON(kmlEvent.featureData.description);
			showVgiFromPulsar(response.type_interface,response.code_interface,kmlEvent.latLng.lat(),kmlEvent.latLng.lng());
		}
	};

	this.createLayerObject = function(kmlBasePath,gKmlOpts,featureInfo){
		this.layerObj = new google.maps.KmlLayer(kmlBasePath + "/" + this.kml,gKmlOpts);
		if (featureInfo)
		{
			google.maps.event.addListener(this.layerObj, 'click',this.func_infowindow);
		}
	};

	this.isVisible = function(map){
		if (!this.visible)
		{
			return false;
		}
		var zoom = map.getZoom();
		if(this.zoomMax>= zoom && zoom>=this.zoomMin)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	this.inScaleRange = function(map){
		var zoom = map.getZoom();
		if(this.zoomMax>= zoom && zoom>=this.zoomMin)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
}

function KmlClient(objectValues)
{
		if (!objectValues)
		{
			return;
		}
		/* mirador mashup */
		this.mashup = (objectValues.mashup) ?  objectValues.mashup : null;
		/* kml base path*/
		this.kmlBasePath = (objectValues.kmlBasePath) ?  objectValues.kmlBasePath : null;
		/*Enable user feature information*/
		this.userClick  = (objectValues.userClick) ?  objectValues.userClick : null;
		/*Array of layers object ordered by priority*/
		this.layers  = (objectValues.layers) ?  objectValues.layers : null;
		/* google kml options*/
		this.gKmlOpts  = (objectValues.gKmlOpts) ?  objectValues.gKmlOpts : null;

		// order layers by priority. The Bubble Sort method.
		var i,j;
		for(i = 0; i < this.layers.length; i++)
		{
			for(j = 0; j < (this.layers.length-1); j++)
			{
				if(this.layers[j].priority > this.layers[j+1].priority)
				{
					var holder = this.layers[j+1];
					this.layers[j+1] = this.layers[j];
					this.layers[j] = holder;
				}
			}
		}
		for(i=0; i<this.layers.length; i++)
		{
			var featureInfo;
			if (this.userClick)
			{
				// check boolean feature attribute on layer
				featureInfo = this.layers[i].reactive;
			}
			else
			{
				featureInfo = false;
			}
			this.layers[i].createLayerObject(this.kmlBasePath,this.gKmlOpts,featureInfo);
			
		}
		
		this.draw = function()
		{
			for(i=0; i<this.layers.length; i++)
			{
				//sleep(1000);
				// check visibility
				if ( !this.layers[i].isVisible(this.mashup.map) )
				{
					this.layers[i].layerObj.setMap(null);
				}			
			}
			//this.layers[0].layerObj.setMap(this.mashup.map);
			for(i=0; i<this.layers.length; i++)
			{
				if ( this.layers[i].isVisible(this.mashup.map))
				{
					this.layers[i].layerObj.setMap(this.mashup.map);
				}
			}
		
		};

		this.getLayerbyId = function(id_layer)
		{
			for(i = 0; i < this.layers.length; i++)
			{
				if (this.layers[i].id == id_layer)
				{
					return this.layers[i];
				}
			}
			return null;
		};

		this.isLayerInRange = function(id_layer)
		{
			var l = this.getLayerbyId(id_layer);
			return l.inScaleRange(this.mashup.map);
		};

		this.changeLayerVisibility = function(id_layer)
		{
			var l = this.getLayerbyId(id_layer);
			l.visible = !l.visible;
		}
}
