/**
* @author iioriatti
* @version 1.0 
* @class This Class provides to manage the Sinfo_Map Object
* @param {HTMLDocumentElement} par_oHtmlContainer Html Element to which the class instance will be added
* @param {Sinfo_Map} [par_oSinfo_Map] Object that provide to manage the application enviroment.<br> If this parameter is undefined the enviroment create a new Sinfo_ClientObjManager object. 
*/ 
Sinfo_MapConsole = function (par_oHtmlContainer, par_oSinfo_Map)
{
   this.extend = Sinfo_Application; 
   this.extend(par_oHtmlContainer);
   delete this.extend;
    
    var oSinfo_Map = par_oSinfo_Map;
    var oGmap2 = oSinfo_Map.load();
    
    function refreshInfo()
    {
        searcLocationLatVal.HtmlWrite("current val: " + oSinfo_Map.getLat());
        searcLocationLngVal.HtmlWrite("current val: " +oSinfo_Map.getLng());
        zoomLevelVal.HtmlWrite("current val: " + oSinfo_Map.getZoomLevel());
    }

    var searchLocationBox = this.createContainer();
        searchLocationBox.HtmlWrite("Search Location");
    var searchLocationLatBox = searchLocationBox.createContainer();
        searchLocationLatBox.HtmlWrite("Lat");
        var searcLocationInputLat = document.createElement("input");
        searcLocationInputLat.setAttribute('type', 'text');    
            searchLocationLatBox.HtmlContainer.appendChild(searcLocationInputLat);
        var searcLocationLatVal = searchLocationLatBox.createContainer();  
            
    var searchLocationLngBox = searchLocationBox.createContainer();
        searchLocationLngBox.HtmlWrite("Lng");
        var searcLocationInputLng = document.createElement("input");
        searcLocationInputLng.setAttribute('type', 'text');    
            searchLocationLngBox.HtmlContainer.appendChild(searcLocationInputLng);
        var searcLocationLngVal = searchLocationLngBox.createContainer();  
            
    var searchLocatioButton = document.createElement("input");
        searchLocatioButton.setAttribute('type', 'button');
        searchLocatioButton.setAttribute('value', 'Fly to...');
            searchLocationBox.HtmlContainer.appendChild(searchLocatioButton);

        var searchLocation = function ()
            {
                oSinfo_Map.searchLocation(searcLocationInputLat.value, searcLocationInputLng.value);
                refreshInfo();
            }
        this.Util.addEvent(searchLocatioButton, "click", searchLocation, true);
    
    var zoomLevelBox = this.createContainer();
        zoomLevelBox.HtmlWrite("Zoom level");
        var zoomLevelInput = document.createElement("input");
            zoomLevelInput.setAttribute('type', 'text');
            zoomLevelBox.HtmlContainer.appendChild(zoomLevelInput);
        var zoomLevelVal = zoomLevelBox.createContainer();
             
        var setZoom = function ()
            {
                oSinfo_Map.setZoomLevel(parseInt(zoomLevelInput.value));
                refreshInfo();
            }
        this.Util.addEvent(zoomLevelInput, "change", setZoom, true)
        
    var pinsBox = this.createContainer();
        pinsBox.HtmlWrite('Crate Pin');
        var newPinBox = pinsBox.createContainer(); 
            var newPinLatBox = newPinBox.createContainer();
                newPinLatBox.HtmlWrite("Lat"); 
                var newPinLatInput = document.createElement('input');
                    newPinLatInput.setAttribute('type','text');
                    newPinLatBox.HtmlContainer.appendChild(newPinLatInput);
                
            var newPinLngBox = newPinBox.createContainer();
                newPinLngBox.HtmlWrite("Lng");
                var newPinLngInput = document.createElement('input');
                    newPinLngInput.setAttribute('type','text');
                    newPinLngBox.HtmlContainer.appendChild(newPinLngInput);

            var newPinButton = document.createElement("input");
                newPinButton.setAttribute('type', 'button');
                newPinButton.setAttribute('value', 'new Pin');
                newPinBox.HtmlContainer.appendChild(newPinButton);
                var newPin = function ()
                    {   
                        var point = new Sinfo_MapPoint();
                        point.setLat(newPinLatInput.value);
                        point.setLng(newPinLngInput.value);
                        
                        var oPin = oSinfo_Map.createPin(point);
                            oPin.show();
                            oPin.openInfoWindowHtml("Text of the Sinfo_MapPoint");
                            shapesList.refresh();
                            newPinLatInput.value ="";
                            newPinLngInput.value ="";
                    }    
                this.Util.addEvent(newPinButton, 'click', newPin, true);
    
    var polygonsBox = this.createContainer();
        polygonsBox.HtmlWrite('Create polygon');
        var newPolygonAddPointsBox = polygonsBox.createContainer();
            var newPolygonAddButton = document.createElement('input');
                newPolygonAddButton.setAttribute('type', 'button');
                newPolygonAddButton.setAttribute('value','add Points');
                newPolygonAddPointsBox.HtmlContainer.appendChild(newPolygonAddButton);
                newPolygonAddButton.disabled = false;
                var points = new Array();
                var newPoint = function ()
                    {   
                        var eventHandler;
                        if(newPolygonAddButton.disabled == false)
                        {
                            newPolygonAddButton.disabled=true;
                            eventHandler = GEvent.addListener(oGmap2, "click", function(overlay, latlng) 
                            { 
                                var point = new Sinfo_MapPoint();
                                point.setLat(latlng.y);
                                point.setLng(latlng.x);
                                points[points.length] = point;
                                newPolygonPointsList.createContainer().HtmlWrite("lat: " + point.getLat() + " lng: " + point.getLng());
                                                  
                                //window.console.debug(latlng);
                            });                        
                        }else{                        
                            newPolygonAddButton.disabled=false;
                            if(!(!eventHandler))
                                GEvent.removeListener(eventHandler);
                        }    
                        
                    }    
                this.Util.addEvent(newPolygonAddButton, 'click', newPoint, true);
            
            var newPolygonPointsBox = newPolygonAddPointsBox.createContainer();    
                newPolygonPointsBox.HtmlWrite("Points list");
                var newPolygonPointsList = newPolygonPointsBox.createContainer();
            
            var newPolygonButton = document.createElement('input');
                newPolygonButton.setAttribute('type','button');
                newPolygonButton.setAttribute('value','create polygon');
                newPolygonAddPointsBox.HtmlContainer.appendChild(newPolygonButton);
                var createPolygon = function()
                    {
                        var oPolygon = oSinfo_Map.createPolygon(points);
                            oPolygon.show();
                        
                        points.length=0;
                        newPolygonPointsList.innerHtml="";
                        shapesList.refresh();
                        newPolygonAddButton.disabled = true;    
                    }   
                this.Util.addEvent(newPolygonButton, 'click', createPolygon, true);
                                         
    var shapeSelected;
                    
    var shapesBox =  this.createContainer();
        shapesBox.HtmlWrite("Shapes management");
        var shapesListBox = shapesBox.createContainer(); 
            var shapesList = document.createElement("select");
                shapesList.setAttribute('size','10');
                shapesList.setAttribute('style','width:250px');
                shapesListBox.HtmlContainer.appendChild(shapesList);
                
    shapesList.refresh = function ()
                        {
                            
                            for(var index=0; index<shapesList.length; index++) {
                            	shapesList.removeChild(shapesList[index]);
                            }
                            shapesList.length=0;
                            

                            for(var index=0; index<oSinfo_Map.getShapes().length; index++) {
                                var oShape = oSinfo_Map.getShapes()[index];
                            	var shapesListOption = document.createElement("option");
                            	   shapesListOption.setAttribute('value',index);
                            	   shapesListOption.innerHTML = index + " lat: " + oShape.getLat() + " lon: " + oShape.getLng();
                            	   shapesList.appendChild(shapesListOption);
                            }                
                            
                        }
                            
        var shapesHideButton = document.createElement("input");
            shapesHideButton.setAttribute('type', 'button');
            shapesHideButton.setAttribute('value', 'hide');
            shapesHideButton.disabled = true;
            shapesBox.HtmlContainer.appendChild(shapesHideButton);
            
            var shapesHide = function()
                {
                    shapeSelected.hide();
                }
            this.Util.addEvent(shapesHideButton, 'click', shapesHide, true)

        var shapesShowButton = document.createElement("input");
            shapesShowButton.setAttribute('type', 'button');
            shapesShowButton.setAttribute('value', 'show');
            shapesShowButton.disabled = true;
            shapesBox.HtmlContainer.appendChild(shapesShowButton);
            
            var shapesShow = function()
                {
                    shapeSelected.show();
                }
            this.Util.addEvent(shapesShowButton, 'click', shapesShow, true)


        var shapesCenter = document.createElement("input");
            shapesCenter.setAttribute('type', 'button');
            shapesCenter.setAttribute('value', 'center and zoom');
            shapesCenter.disabled = true;
            shapesBox.HtmlContainer.appendChild(shapesCenter);            
            var shapeCenter = function ()
                {
                    oSinfo_Map.searchLocation(shapeSelected.getLat(), shapeSelected.getLng(), shapeSelected.getBestZoom())
                } 
            this.Util.addEvent(shapesCenter, 'click', shapeCenter, true)
            
        var onSelectedPin = function()
            {
                shapeSelected = oSinfo_Map.getShapes()[shapesList.selectedIndex];
            	shapesShowButton.disabled = false;
            	shapesHideButton.disabled = false;
            	shapesCenter.disabled = false;
            }

        this.Util.addEvent(shapesList, 'focus', onSelectedPin, true);

        
    refreshInfo();                    
}