/**
* @author iioriatti
* @version 1.0 
* @class This generic Class provides the basic functions for manage the Map object
* @param {HTMLDocumentElement} par_oHtmlContainer Html Element to which the class instance will be added
* @param {Sinfo_ClientObjManager} [par_oClientObjManager] Object that provide to manage the application enviroment.<br> If this parameter is undefined the enviroment create a new Sinfo_ClientObjManager object. 
* @param {String} [par_sId] Id of the object
*/ 
Sinfo_Map = function (par_oHtmlContainer, par_oClientObjManager, par_sId)
{
    this.extend = Sinfo_Application; 
    this.extend(par_oHtmlContainer, par_oClientObjManager);
    delete this.extend;
   
    this.toolbar_selected_shape = null;
   
    var oThis = this;

    var lat = 46.694667307773095, lng = 11.44226074218751, zoom = 8;
    var oGMap2 = null; 
    
    var layers = new Array();
    
    var selectedAreaId = '';
    
    var mapAreasToolbar;
    
    var scaleControlVisible;
    var typeControlVisible;
    var overviewControlVisible;
    
    var gotoUrlEnabled;
    
    var serverSideAjaxFunction = null;
    
    this.getGoogleMapObject = function()
    {
    	return oGMap2;
    }
    
    /**
     * Associate the location toolbar with the map!
     */
    this.setMapAreasToolbar = function(par_MapToolbar)
    {
    	mapAreasToolbar = par_MapToolbar;
    }

    function flyTo()
    {
        oGMap2.setCenter(new GLatLng(lat, lng), zoom);
    }

    /**
    * This method provides to load the map (by Google Maps API).
    * @return {<a href="http://code.google.com/apis/maps/documentation/reference.html#GMap2">GMap2</a>} 
    */
    this.load = function ()
      
    {
    	if (oGMap2 == null)
    	{
            oGMap2 = new GMap2(this.HtmlContainer);
            flyTo();
            /*oGMap2.enableScrollWheelZoom();*/
            oGMap2.addControl(new GSmallMapControl())
            if (typeControlVisible) {
            	oGMap2.addControl(new GMapTypeControl());
            }
            if (overviewControlVisible)
            {
            	oGMap2.addControl(new GOverviewMapControl());
            }
            if (scaleControlVisible) {
            	oGMap2.addControl(new GScaleControl());
            }
            
            // browse all map visible informations and show it!
            
            for(i=0;i<layers.length;i++)  {
       	    	var layer = layers[i];
       	    	
       	    	for(s=0;s< layer.getShapes().length;s++)
       	    	{
       	    	   var shape = layer.getShapes()[s];
       	    	   if (shape.isVisible())
       	    	   {
       	    		   shape.p_show();
       	    	   }
       	    	  
       	    	  
       	    	  
       	    	}
       	    }
    	}
        return oGMap2;
        }

    /**
    * This method provides to set the zoom level. 
    * @param {Integer} par_iZoom Value of zoom.
    */
    this.setZoomLevel = function (par_iZoom)
        {
            if(!(!par_iZoom))zoom = par_iZoom;
            //flyTo();
        }
    
    /**
     * This method provides to set the lat. 
     * @param {Integer} par_iLat Value of lat.
     */
     this.setLat = function (par_iLat)
         {
             if(!(!par_iLat))lat = par_iLat;
         }
     
     /**
      * This method provides to set the lat. 
      * @param {Integer} par_iLat Value of lat.
      */
      this.setLng = function (par_iLng)
          {
              if(!(!par_iLng))lng = par_iLng;
          }

    
    /**
    * This method return the current zoom level.
    * @return {Integer} Current zoom level.
    */            
    this.getZoomLevel = function(){return zoom;}

    /**
    * This method provide to show on the Map the requested location.
    * @param {Integer} par_lat Value of latitude 
    * @param {Integer} par_lng Value of longitude
    * @param {Integer} [par_zoom] value of zoom. If no set the zoom level is the current level (default = 8).
    * @return
    */         
    this.searchLocation = function (par_lat, par_lng, par_zoom )
        {
            if(!(!par_lat))lat = par_lat; 
            if(!(!par_lng))lng = par_lng;
            if(!(!par_zoom))zoom = par_zoom; 
            flyTo();               
        }
    /**
    * This method provides to return the current latitude of the Map.
    * @return {Integer}
    */
    this.getLat = function (){return lat;}

    /**
    * This method provides to return the current longitude of the Map.
    * @return {Integer}
    */
    this.getLng = function (){return lng;}
    
    
    
    /**
     * This method provides to create one Sinfo_MapLayer object.
     * @return {Sinfo_MapLayer}
     */
     this.createLayer = function()
                 {
                     var oLayer = new Sinfo_MapLayer(this, oGMap2);
                     layers[layers.length]= oLayer;
                     return oLayer;
                 }
    
      /**
      * This method provides to get a layer passing the id.
      * @param {String} str_id id to search 
      * @return {layer}
      */
      this.getLayerById = function(str_id)
	  {
	    for(layer in layers)  {
	    	if (layer.getId() == str_id) 	return layer;
	    }
		return null;
	  } 
    
      /**
       * This method provides to get the layer length.
       * @return {layer}
       */
       this.getLayerIndexLength = function()
       {
         return layers.length;
       }
       
       /**
        * This method provides to get a layer passing the index.
        * @param {Integer} int_index index to search 
        * @return {layer}
        */
        this.getLayerByIndex = function(int_index)
        {
          return layers[int_index];
        }
      
       /**
        * This method provides to get a shape passing the id.
        * @param {String} str_id id to search 
        * @return {shape}
        */
        this.getShapeById = function(str_id)
  	  {
        	
  	    for(i=0;i<layers.length;i++)  {
  	    	var layer = layers[i];
  	    	
  	    	for(s=0;s< layer.getShapes().length;s++){
  	    	  var shape = layer.getShapes()[s];
  	    	  if (shape.getId() == str_id) 	return shape;
  	    	}
  	    }
  		return null;
  	  } 
    /**
     * This method provides to get a shape passing the id.
     * @param {String} str_id id to search 
     * @return {shape}
     */
     this.getShapeByAreaId = function(str_id)
   	  {
        //alert("getShapeByAreaId: " + str_id + " - layer.length" + layers.length); 	
   	    for(i=0;i<layers.length;i++)  {
   	    	var layer = layers[i];
   	    	
   	    	for(s=0;s< layer.getShapes().length;s++){
   	    	  var shape = layer.getShapes()[s];
   	    	  if (shape.getAreaId() == str_id)
   	    		{
   	    		  //alert("getShapeByAreaId shape" + shape); 	
   	    		  return shape;
   	    		}
   	    	}
   	    }
   		return null;
   	  } 
      
    /**
    * This attribute return the type of the object.
    * @property 
    * @type String
    */  
   this.type = "Sinfo_Map";
      
   var pId = par_sId;

   this.destroy = function ()
   {
   }
   
   this.getId = function()
   {
      return pId;
   }
   this.getAttrs = function()
   {
      var ret = new Array();
      ret['selectedAreaId'] = selectedAreaId;
      return ret; 
   }

   this.setSelectedAreaId = function(par_AreaId)
   {
	   // par_AreaId can be an empty string!
	   // sync pins and legend with the new selection
	   selectedAreaId = par_AreaId;
	   //alert("setSelectedAreaId: par_AreaId=" + par_AreaId)
	   if (mapAreasToolbar)
	   {
		   try
		   {
			   mapAreasToolbar.unselect();
			   var shapeid =  mapAreasToolbar.getShapeByAreaId(par_AreaId).getShapeId();
			   //alert("setSelectedAreaId: shapeid=" + shapeid);
			   mapAreasToolbar.setSelectedShape(shapeid)
			   
			   
		   } catch (e) {}
	   }
   }
   this.getSelectedAreaId = function()
   {
	   return selectedAreaId;
   }
   this.onShapeClick = function(par_Sinfo_Pin_or_Polygon)
   {
	   if (gotoUrlEnabled)
	   {
		   if (par_Sinfo_Pin_or_Polygon.getGotoUrl().indexOf('tb_show') >= 0){
			   eval(par_Sinfo_Pin_or_Polygon.getGotoUrl());
		   }
	   }
	   else
	   {
          this.setSelectedAreaId(par_Sinfo_Pin_or_Polygon.getAreaId());
          if (serverSideAjaxFunction != null)
        	  serverSideAjaxFunction();
	   }
   }
   
   this.onLegendClick = function(par_Sinfo_Pin_or_PolygonId, par_AreadId)
   {
	   
	   if (gotoUrlEnabled)
	   {
		   //alert(par_Sinfo_Pin_or_Polygon.getGotoUrl());
		   var par_Sinfo_Pin_or_Polygon = oThis.getShapeById(par_Sinfo_Pin_or_PolygonId);
		   if (par_Sinfo_Pin_or_Polygon.getGotoUrl() != '')
		      window.location = par_Sinfo_Pin_or_Polygon.getGotoUrl();
	   }
	   else
	   {
          this.setSelectedAreaId(par_AreadId);
          if (serverSideAjaxFunction != null)
        	  serverSideAjaxFunction();
	   }
   }
   this.onLegendSelect = function(par_AreadId)
   {
      //this.setSelectedAreaId(par_AreadId);
  	  selectedAreaId = par_AreadId;

      if (serverSideAjaxFunction != null)
    	  serverSideAjaxFunction();
   }
   
   this.isScaleControlVisible = function()
   {
      return scaleControlVisible;
   }

   this.setScaleControlVisible = function (boolean_scaleControlVisible)
   {
      scaleControlVisible = boolean_scaleControlVisible;
   }

   this.isTypeControlVisible = function()
   {
      return typeControlVisible;
   }

   this.setTypeControlVisible = function(boolean_typeControlVisible)
   {
      typeControlVisible = boolean_typeControlVisible;
   }

   
   this.isOverviewControlVisible = function()
   {
      return overviewControlVisible;
   }

   this.setOverviewControlVisible = function(boolean_overviewControlVisible)
   {
      overviewControlVisible = boolean_overviewControlVisible;
   }

   this.setGotoUrlEnabled = function (val)
   {
	   gotoUrlEnabled = val;
   }
   this.isGotoUrlEnabled = function ()
   {
	   return gotoUrlEnabled;
   }
   this.setServerSideAjaxFunction = function(par_function)
   {
	   serverSideAjaxFunction = par_function;
   }

}
