/**
* @author iioriatti
* @version 1.0
* @class This class provides to create one Sinfo_Polygon object that can be placed from the Sinfo_Map object on the Map.
* @param{Sinfo_Map} oSinfo_Map Instance of Sinfo_map class
* @param{<a href="http://code.google.com/apis/maps/documentation/reference.html#GMap2">GMap2</a>} oGMap2 Instance of GMap2 class (by Google Maps API)
* @param{Sinfo_MapPoint} oSinfo_MapPoint Instance of Sinfo_MapPoint class
*/ 
Sinfo_MapPolygon = function(oSinfo_Map, oGMap2del, aSinfo_MapPoint, strUrlGeoRssXml)
{
    var aPoint = aSinfo_MapPoint;
    var strGeoXml = strUrlGeoRssXml;
    var oPin;
    var polygon;
    var zoomlevel;
    var aGLatLng = new Array();
    var polygonId;
    
    var infoWindowHtml = "";
    
    var gotoUrl;
    
    var areaId;
    
    var visible = false;
    
    var oThis = this;
    
    this.setAreaId = function(par_AreaId)
    {
    	areaId = par_AreaId;
    }
    
    this.getAreaId = function()
    {
    	return areaId;
    }
    
    this.getShapeType = function()
    {
    	return 'Polygon';
    }

    var borderColor='', borderWidth='', borderOpacity='', fillColor='#C6D665', fillOpacity='0.8';
	//AreaFillColor = new VEColor(252,219,53,0.3);
	//SkiAreaFillColor = new VEColor(158,210,241,0.5);
    
            
    if(!(!(aPoint))){
	    for(var iPoint=0; iPoint<aPoint.length; iPoint++) {    	
	    	aGLatLng[aGLatLng.length]= new GLatLng(aPoint[iPoint].getLat(), aPoint[iPoint].getLng())
	    }    
    }
    
    var trimVertex = function(str) {
    	str = str.replace(/^\s+/, '');
    	for (var i = str.length - 1; i >= 0; i--) {
    		if (/\S/.test(str.charAt(i))) {
    			str = str.substring(0, i + 1);
    			break;
    		}
    	}
    	return str;
    }
    
    /**
    * This method provides to return the latitude value of the center of the Sinfo_MapPolygon object.
    * @return {Double}
    */
    this.getLat = function(){
    	var Bounds = polygon.getBounds();
  	  var Center = Bounds.getCenter();
        return Center.lat();
    } 
    /**
    * This method provides to return the longitude value of the center of the Sinfo_MapPolygon object.
    * @return {Double}
    */
    this.getLng = function(){
    	var Bounds = polygon.getBounds();
    	  var Center = Bounds.getCenter();
          return Center.lng();
          }      	
    /**
    * This method provides to return the best zoom value of the Sinfo_MapPolygon object.
    * @return {Integer}
    */
    this.getBestZoom= function(){
    	var Bounds = polygon.getBounds();
    	return  oSinfo_Map.getGoogleMapObject().getBoundsZoomLevel(Bounds);}

    /**
    * This method provides to set the border color of the Sinfo_MapPolygon object.
    * @param {String} par_borderColor Color of the border
    */    
    this.setBorderColor = function(par_borderColor){borderColor = par_borderColor;}
    /**
    * This method provides to set the border width of the Sinfo_MapPolygon object.
    * @param {Integer} par_borderWidth Value of the border width
    */    
    this.setBorderWidth = function(par_borderWidth){borderWidth = par_borderWidth;}
    /**
    * This method provides to set the border opacity of the Sinfo_MapPolygon object.
    * @param {Double} par_borderOpacity Value of the border opacity.
    */    
    this.setBorderOpacity = function(par_borderOpacity){borderOpacity = par_borderOpacity;}
    /**
    * This method provides to set the color of the Sinfo_MapPolygon object.
    * @param {String} par_borderColor Color of the Sinfo_MapPolygon
    */    
    this.setFillColor = function(par_borderColor){fillColor = par_borderColor;}
    /**
    * This method provides to set the opacity of the Sinfo_MapPolygon object.
    * @param {Double} par_fillOpacity Value of the opacity.
    */    
    this.setFillOpacity = function(par_fillOpacity){fillOpacity = par_fillOpacity;}

    /**
     * This method provides to assign one id to this Sinfo_MapPolygon object.
     * @param {str_polygonId} polygonId string that identifies this layer.
     */
     this.setId = function(str_polygonId)
     {
    	 polygonId = str_polygonId;
     }
     
     /**
      * This method provides to retrieve the id for this Sinfo_MapPolygon object.
      * @return {polygonId} polygonId string that identifies this layer.
      */
      this.getId = function()
      {
    	  return polygonId;
      }
    
      /**
       * This method provides to set the string for this infoWindowHtml.
       * @param {str_InfoWindowHtml} str_InfoWindowHtml string passed to the infoWindowHtml. 
       */
      this.setInfoWindowHTML = function(str_InfoWindowHtml){
    	  infoWindowHtml = str_InfoWindowHtml;
      }
      
      this.setGotoUrl = function(str_gotoUrl){
      	gotoUrl = str_gotoUrl;
      }
      this.getGotoUrl = function(){
      	return gotoUrl;
      }
      
  

    /**
    * This method provites to create and show one <a href="http://code.google.com/apis/maps/documentation/reference.html#GPolygon">GPolygon</a> object on the Map or only to show this when he is just created.
    */
  	this.p_show = function()
    {
    
                function buildPolygon()
                {
                    if (request.readyState == 4 && request.status == 200) 
                    {
                        var match = new RegExp(/<georss:polygon ?[^>]*>([^<]+)<\/georss:polygon>/ig).exec(request.responseText);
                        var coordsText = match[1];            			  
                        var vertex = trimVertex(coordsText);
                        var re = /[ \n\t]+/;
                        var vertexPoint = vertex.split(re);
                        var points = new Array();
                        simplyfyingFactor=1;
                        for (var v = 0; v < vertexPoint.length; v+= 1 + (2*simplyfyingFactor) )
                        {
                            var point = new Sinfo_MapPoint();
                            strLat = vertexPoint[v++];
                            strLong = vertexPoint[v];
                            point.setLat(strLat);
                            point.setLng(strLong);
                            points[points.length] = point;
                        }
                        for(var iPoint=0; iPoint<points.length; iPoint++)
                        {    	
                            aGLatLng[aGLatLng.length]= new GLatLng(points[iPoint].getLat(), points[iPoint].getLng())
                        }  
                        			      
                        polygon = new GPolygon(aGLatLng, borderColor, borderWidth, borderOpacity, fillColor, fillOpacity);

                        var bounds = polygon.getBounds();
                        var boundsCenter = bounds.getCenter();
                        var zoomlevel = oSinfo_Map.getGoogleMapObject().getBoundsZoomLevel(bounds);

                        oSinfo_Map.getGoogleMapObject().setCenter(boundsCenter, zoomlevel);
                        polygon.hide();
                                          
                        oSinfo_Map.getGoogleMapObject().addOverlay(polygon);
                    // le icone del pin del centro sono 2 pin trasparenti
                        if(infoWindowHtml!="")
                        {
                            var Bounds = polygon.getBounds();
                            var Center = Bounds.getCenter();
                            point = new GLatLng(Center.lat(), Center.lng());
                            /* 
                            var baseIcon = new GIcon();
                            baseIcon.iconSize = new GSize(6, 6);
                            baseIcon.image = "/sinfo-impl/images/transparent_pin.gif";
                            var baseIcon2 = new GIcon(baseIcon);
                            */
                            oPin   = new GMarker(point);
                            oSinfo_Map.getGoogleMapObject().addOverlay(oPin);
                	        	              
                            GEvent.addListener( polygon, 
                                                "mouseover", 
                                                    function() 
                                                    {
                                                        oPin.openInfoWindowHtml(infoWindowHtml);
                                                        //polygon.setImage(pinicon_selected);
                                                    }
                                                );
                            GEvent.addListener( polygon, 
                                                "mouseout", 
                                                    function() 
                                                    {
                                                        oPin.closeInfoWindow();
                                                        //polygon.setImage(pinicon_unselected);
                                                    }
                                                );
                        }
                    // infoHtml != '';
                        GEvent.addListener(
                                            polygon,
                                            "click", 
                                            function()
                                            {
                                                //polygon.closeInfoWindow();
                                                //polygon.setImage(pinicon_unselected);
                                                //window.location='http://www.suedtirol.info';
                                            }
                                        );
                        	              
                        GEvent.addListener( polygon, 
                                            "click", 
                                            function() 
                                            {
                                                if (gotoUrl){document.location=gotoUrl;}
                                            }
                                        );
                    }
                }  
    
    
  	    visible = true;
        if (!(!polygon))      
        {
            polygon.show();
        }
        else
        {
            if(!aPoint)
            {
                var request = GXmlHttp.create();
            	request.open("GET", strGeoXml, false);
                request.send('');
                buildPolygon();
            		
            }
            else
            {
        		polygon = new GPolygon(aGLatLng, borderColor, borderWidth, borderOpacity, fillColor, fillOpacity);
               	var Bounds = polygon.getBounds();
               	var Center = Bounds.getCenter();
               	zoomlevel = oSinfo_Map.getGoogleMapObject().getBoundsZoomLevel(Bounds);
            
                /*var point = new Sinfo_MapPoint();
                point.setLat(Center.y);
                point.setLng(Center.x);
                
                oPin = new Sinfo_MapPin(oSinfo_Map, oGMap2, point);
                oPin.show();
                oPin.hide();
                
                //GEvent.addListener(polygon, "mouseover", function() 
                //    {     
                        oPin.openInfoWindowHtml("html content", oPin);                
                //    });
                GEvent.addListener(polygon, "mouseout", function() 
                    {
                        oPin.closeInfoWindow();
                    });
                  
                GEvent.addListener(polygon, "click", function() 
                    {
                        window.open('${shape.getInfoUrl()}','${shape.getInfoUrlTarget()}');
                    });
                */
                oSinfo_Map.getGoogleMapObject().setCenter(Center, zoomlevel);
                oSinfo_Map.getGoogleMapObject().addOverlay(polygon);
            }
        }
    }
    /**
    * This method provites to hide the <a href="http://code.google.com/apis/maps/documentation/reference.html#GPolygon">#GPolygon</a> object on the Map;
    */
    this.p_hide = function(){
    	
    	visible = false;
    	// this case can occours when there is a problem loading polygon coordinates with ajax
    	if (!(!polygon))      
        {
    	   polygon.hide()
        }
    	}
    
    this.setSelected = function(par_bSelected)
    {
    	/* nothing to do */
    }
    this.isVisible = function()
    {
    	return visible;
    }
    this.setVisible = function (par_bVisible)
    {
    	visible = par_bVisible;
    	if (oSinfo_Map.getGoogleMapObject() != null)
    	{
    		if (visible)
    		{
    			oThis.p_show();
    		}
    		else
    		{
    			oThis.p_hide();
    		}
    	}
    }
}
