/**
* @author iioriatti
* @version 1.0
* @class This class provides to create one Sinfo_Pin 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_Point} oSinfo_Point Instance of Sinfo_Point class
*/ 
Sinfo_MapPin = function(oSinfo_Layer, oGMap2del, oSinfo_Point)
{
    var pinicon_unselected = '';
    var pinicon_selected = '';
    
    var oSinfo_Point;
    var baseIcon, icon, point, pin;  
    var pinId;
    
    var infoWindowHtml = "";
    
    var gotoUrl;
    
    var oThis = this;
    
    var visible = false;
    
    this.getShapeType = function()
    {
    	return 'Pin';
    }
    
    var areaId;
    
    this.setAreaId = function(par_AreaId)
    {
    	areaId = par_AreaId;
    }
    
    this.getAreaId = function()
    {
    	return areaId;
    }
    
    /**
     * 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.getInfoWindowHTML = function(){
    	return infoWindowHtml;
    }
    
    this.setGotoUrl = function(str_gotoUrl){
    	gotoUrl = str_gotoUrl;
    }
    this.getGotoUrl = function(){
    	return gotoUrl;
    }

    /**
    * This method provides to set the icon to view on the map when the Sinfo_MapPin is selected.
    * @param {String} par_url Url of the image.
    */        
    this.setSelectedIcon = function(par_url){pinicon_selected = par_url;}        

    /**
    * This method provides to set the icon to view on the map when the Sinfo_MapPin is unselected.
    * @param {String} par_url Url of the image.
    */        
    this.setUnselectedIcon = function(par_url){pinicon_unselected = par_url;}
    
    /**
    * This method provides to return the latitude value of the Sinfo_MapPin object.
    * @return {Double}
    */
    this.getLat = function(){return oSinfo_Point.getLat();} 
    /**
    * This method provides to return the longitude value of the Sinfo_MapPin object.
    * @return {Double}
    */
    this.getLng = function(){return oSinfo_Point.getLng();}    
    /**
    * This method provides to return the best zoom value of the Sinfo_MapPin object.
    * @return {Integer}
    */
    this.getBestZoom= function(){return 12;}
    
    
    /**
     * This method provides to assign one id to this Sinfo_MapPin object.
     * @param {str_pinId} pinId string that identifies this layer.
     */
     this.setId = function(str_pinId)
     {
    	 pinId = str_pinId;
     }
     
     /**
      * This method provides to retrieve the id for this Sinfo_MapPin object.
      * @return {pinId} pinId string that identifies this layer.
      */
      this.getId = function()
      {
    	  return pinId;
      }
    
    /**
    * This method provites to create and show one <a href="http://code.google.com/apis/maps/documentation/reference.html#GMarker">GMarker</a> object on the Map or only to show this when he is just created.
    */
    this.p_show = function()
    {
    	visible = true;
        if (!(!pin))      
        {
            pin.show()
        }else{
        	
            point = new GLatLng(oSinfo_Point.getLat(), oSinfo_Point.getLng());
        	
            if (pinicon_unselected != '')
            {
	            baseIcon = new GIcon();
	            baseIcon.iconAnchor = new GPoint(16, 36);
	            baseIcon.infoWindowAnchor = new GPoint(8, 2);
	                
	            icon = new GIcon(baseIcon);
	            icon.image = pinicon_unselected;
	                
	            pin   = new GMarker(point,icon);
            }
            else
            {
            	pin   = new GMarker(point);
            }
        	
            oSinfo_Layer.getGoogleMapObject().addOverlay(pin);
            /*
            GEvent.addListener(pin, "mouseover", function() {
            	if (infoWindowHtml) {
            		pin.openInfoWindowHtml(infoWindowHtml);
            	}
            	try
            	{
            		window.console.debug('over:' + pinicon_selected);
            	}
            	catch (e)
            	{
            	}
            	
				pin.setImage(pinicon_selected);
			  });
            GEvent.addListener(pin, "mouseout", function() 
            {
				pin.closeInfoWindow();
				try
				{
	            	window.console.debug('out:' + pinicon_unselected);
	            	/* unselected may be undefined * /
	            	pin.setImage(pinicon_unselected);
	            }
	        	catch (e)
	        	{
	        	}
				
			  });
            */
            
            GEvent.addListener(pin, "click", function() {
            	
            	if (infoWindowHtml) {
            		pin.openInfoWindowHtml(infoWindowHtml);
            	}
            	pin.setImage(pinicon_selected);
            	oSinfo_Layer.getSinfoMap().onShapeClick(oThis);
              });
            
            /*
            GEvent.addListener(pin, "click", function() {
				pin.closeInfoWindow();
				pin.setImage(pinicon_unselected);
				window.location='http://www.suedtirol.info';
			  });
            */

        }
    }

    /**
    * This method provites to hide the <a href="http://code.google.com/apis/maps/documentation/reference.html#GMarker">GMarker</a> object on the Map;
    */
    this.p_hide = function()
        {
    	   visible = false;
            //window.console.debug("Sinfo_MapPin.hide" + this)
            if(!(!pin))
                pin.hide()
            //else
                //window.console.debug("Sinfo_MapPin.hide")
        }
    /**
    * This method maps the same method of Google Maps API <a href="http://code.google.com/apis/maps/documentation/reference.html#GMarker">GMarker</a> 
    * @param {HtmlCode} par_HtmlCode Html code to publish in the window.
    */      
    this.openInfoWindowHtml = function (par_HtmlCode, par_virtualPolygonPin)
        {
            if(!(!par_HtmlCode))
            {
                if(!(!par_virtualPolygonPin))
                {
                    GEvent.addListener(par_virtualPolygonPin, "mouseover", function() 
                    {     
                        this.openInfoWindowHtml(par_HtmlCode);                
                    });
                    
                }else{
                    GEvent.addListener(pin, "mouseover", function() 
                    {     
                        this.openInfoWindowHtml(par_HtmlCode);                
                    });
                }                    
            }            
            //if(!(!par_HtmlCode))
            //    pin.openInfoWindowHtml(par_HtmlCode);   
        }
    /**
    * This method maps the same method of Google Maps API <a href="http://code.google.com/apis/maps/documentation/reference.html#GMarker">GMarker</a> 
    */      
    this.closeInfoWindow = function()
        {
            pin.closeInfoWindow();
        }
    
    this.setSelected = function(par_bSelected)
    {
    	if (par_bSelected)
    	{
    		oThis.setVisible(true);
    		try
			{
    			pin.setImage(pinicon_selected);
			}
        	catch (e)
        	{
        	}	
    	}
    	else
    	{
    		try
			{
            	window.console.debug('out:' + pinicon_unselected);
            	/* unselected may be undefined */
            	pin.setImage(pinicon_unselected);
            }
        	catch (e)
        	{
        	}
    	}
    		
    }
 
    this.isVisible = function()
    {
    	return visible;
    }
    this.setVisible = function (par_bVisible)
    {
    	visible = par_bVisible;
    	if (oSinfo_Layer.getGoogleMapObject() != null)
    	{
    		if (visible)
    		{
    			oThis.p_show();
    		}
    		else
    		{
    			oThis.p_hide();
    		}
    	}
    }
}