
	
preFeatureInsert = function(feature) {
  	
   		     		
}; 
  
	var map, toolbarItems = [], toolbar;
	var markers;

		var proj32717 = new OpenLayers.Projection("EPSG:32717");
		var proj4326 = new OpenLayers.Projection("EPSG:4326");
		var proj900913 = new OpenLayers.Projection("EPSG:900913");
		var proj24877 = new OpenLayers.Projection("EPSG:24877");
	
	
	    // reference local blank image
    Ext.BLANK_IMAGE_URL = '../../mfbase/ext/resources/images/default/s.gif';

	
    Ext.onReady(function() {

	
		OpenLayers.Util.onImageLoadErrorColor = "";
		bounds = coordMax;
		
		var coordMax = new OpenLayers.Bounds(-9580761.6242,-544824.2220,-8349908.8628, 156706.3181);

   
   
		
		//Defina le ruta de las imagenes
		//OpenLayers.ImgPath = '../share/javascript/OL/theme/default/img/';
		
		
		//var isIE = isInternetExplorer();
		//if (isIE) {
		//	document.getElementById("mapa").className = "mapaIE";
		//	document.getElementById("panel").className = "panelIE";
		//}
		
        //ajustaMapaIE();
	    
	  
 
		var options1 = {
            controls: [],
            maxExtent: coordMax,
            maxResolution: 78271.51695,
            numZoomLevels: 18,
            projection: proj900913,
            displayProjection: proj4326,
            units: 'm',
			scales: [2000000,1500000,800000,400000,200000,100000,75000,50000,40000,30000,25000,20000,15000,10000,5000,3000, 1000, 500]
		};
		
            
        map = new OpenLayers.Map(options1);
            
			
			
		map.addControl(new IdeLocal.PermalinkWindow('permalnk'));
		
	var gmap = new OpenLayers.Layer.Google(
            "Google Streets", {'sphericalMercator': true, numZoomLevels: 19}
    );

    var orto = new OpenLayers.Layer.Google(
            "Google Satellite",
            {type: G_SATELLITE_MAP, 'sphericalMercator': true, numZoomLevels: 19}
    );
	
	
	
		
	var ghyb = new OpenLayers.Layer.Google(
            "Google Satellite",
            {type: G_HYBRID_MAP, 'sphericalMercator': true, numZoomLevels: 19}
    );
	
	  // create OSM layer
	  
        var mapnik = new OpenLayers.Layer.TMS(
            "OpenStreetMap",
            "http://tile.openstreetmap.org/",
            {
                type: 'png', getURL: osm_getTileURL,
                displayOutsideMaxExtent: true
            }
        );

		
	//var osm = new OpenLayers.Layer.OSM.Mapnik("Mapnik");

	
	 //var osm = new OpenLayers.Layer.TMS(
     //           "OpenStreetMap (Mapnik)",
     //           "http://tile.openstreetmap.org/",
     //           {
     //               type: 'png', getURL: osm_getTileURL,
     //               displayOutsideMaxExtent: true,
     //               attribution: '<a href="http://www.openstreetmap.org/">OpenStreetMap</a>'
     //           }
     //       );
			
	
            map.addLayers([gmap, ghyb,orto, mapnik]);
			
			cargarWMS();
			
			//createLayers(map);
		
		
            
			
			//markers = new OpenLayers.Layer.Markers("Markers",{'displayInLayerSwitcher':false, srs:"EPSG:4326"});
			//markers = new OpenLayers.Layer.Vector("Markers",{'displayInLayerSwitcher':false});
			//markers.setVisibility(false);
			//map.addLayer(markers);
            

		
			
			
			
	
	
		
		
	//Controls
	//map.addControl( new OpenLayers.Control.Navigation());
	//map.addControl( new IdeLocal.PanZoomBar({slideFactor: 256}));
	//map.addControl( new OpenLayers.Control.ArgParser());
	//map.addControl( new OpenLayers.Control.Attribution());
	//map.addControl( new IdeLocal.LayerSwitcher({'div':OpenLayers.Util.getElement('panel3'), 'activeColor':'#FFFFFF'}));
    //posicion de mouse
    //map.addControl(new OpenLayers.Control.MousePosition({'div':OpenLayers.Util.getElement('position')}));
    //escala
    //map.addControl(new IdeLocal.ScaleBar({div: $('escala')}));
    //map.addControl(new OpenLayers.Control.ScaleLine({div: $('escala')}));
        
    //var overviewMapSize = new OpenLayers.Size(128, 123);
    //var genericMapOptions = {projection: proj900913, units: 'm', maxExtent: coordMax};
    //var genericMapOptions = {projection: "EPSG:23031", units: 'm', maxExtent: bounds};
	
	
		 var ol_wms = new OpenLayers.Layer.Google(
	            "Google Satellite",
	            {type:G_PHYSICAL_MAP, 'sphericalMercator': true, numZoomLevels: 19}
	    );
	
	
	//var overviewOptions = {
	//mapOptions: genericMapOptions,
	//	size: overviewMapSize,
	//	div: $('overview'),
	//	layers: [ol_wms]
	//}
	
	
	
	
	
	// Hack: Mapa de referčncia inamovible
	//var ovControl = new OpenLayers.Control.OverviewMap(overviewOptions);
	//ovControl.isSuitableOverview = function() { return true; };
    //toggleOverview();
    //map.addControl(ovControl);
    
    //var toolBar = new IdeLocal.NavToolbar({'div':OpenLayers.Util.getElement('controlDiv')});
    //map.addControl(toolBar);
    
    //var info = new IdeLocal.GetFeatureInfo();
    //map.addControl(info);
	//Fi controls
	//map.events.register("zoomend", map, controlEscala);

	//map.events.register("changebaselayer", map, cambiaBase);
			
	
		
	if (Fpar != ""){
		if (Fpar == "t" || Fpar == "T"){
			//F_ACTIVA = "TOPO";
			cambiaFondo('TOPO');
		}
		else if (Fpar == "o" || Fpar == "O"){
			//F_ACTIVA = "ORTO";
			cambiaFondo('ORTO');
		}
		else if (Fpar == "gm" || Fpar == "GM"){
			//F_ACTIVA = "MAPA";
			cambiaFondo('MAPA');
		}
		else if (Fpar == "gs" || Fpar == "GS"){
			//F_ACTIVA = "SATELIT";
			cambiaFondo('SATELIT');
		}
		else if (Fpar == "b" || Fpar == "B"){
			//F_ACTIVA = "BLANC";
			cambiaFondo('BLANC');
		}
		else {
			//F_ACTIVA = "ORTO";
			cambiaFondo('ORTO');
		}
	}
	
	var lat=-2.8936;
        var lon=-79.0246;
        var zoom=9;
	
	if( ! map.getCenter() ){
                var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
                map.setCenter (lonLat, zoom);
            }		
			
				
				var model = createTreeModel();
			
		//var tree = new mapfish.widgets.LayerTree({map: map, model: model,  showWmsLegend: true, 	plugins: [
	//var tree = new mapfish.widgets.LayerTree({map: map,  showWmsLegend: true, 	plugins: [	
	//			mapfish.widgets.LayerTree.createContextualMenuPlugin([
    //                                'opacitySlide',
    //                                'remove',
	//								'zoomToExtent'
    //                            ])
    //                        ]});
        //tree.render();
		
		
		//mapPanel.render();
		var mapPanel = new GeoExt.MapPanel({
			
	        region: 'center',
	        map: map,
	        zoom: 19
		});
		
		var legendPanel = new GeoExt.LegendPanel({
			//el: 'leyenda',
			region: "west",
			collapsible: true,
			title: "Leyenda",
			autoScroll: true,
			height: 200

			
		});

		//createToolbar();
		
		
		
	var overviewMapSize = new OpenLayers.Size(128, 123);
    var genericMapOptions = {
						projection: proj900913, 
						units: 'm', 
						maxExtent: coordMax,
						displayProjection: proj4326};
	
	var overviewOptions = {
		mapOptions: genericMapOptions,
		size: overviewMapSize,
		layers: [ol_wms]
	}
	
	
	
	
	
	// Hack: Mapa de referčncia inamovible
	var ovControl = new OpenLayers.Control.OverviewMap(overviewOptions);
	ovControl.isSuitableOverview = function() { return true; };
    //toggleOverview();
    map.addControl(ovControl);

	toolbar = new mapfish.widgets.toolbar.Toolbar({
        map: map,
        configurable: false
    });
		//legendPanel.render();
	//initToolbarContent();
	createToolbar();
	var viewport = new Ext.Viewport({
                    el: 'mapa',
					layout: "border",
                    listeners: {
                        scope: map
                    },
                    items: [ {  
             /* 
              * Panel con layout 'form' 
              */  
             region: 'north',  
             title: 'Panel superior',  
             xtype: 'panel',  
             layout: 'form',  
             height: 100,  
             minWidth: 300,  
             autoScroll: false,  
             split: false,
			 border: false

				},{
                        region: "west",
						collapsible: true,
                        id: "tools",
                        title: "Herramientas",
                        border: true,
                        width: 250,
						
                        items: [{
                            id: "tree",
                            title: "Servidores",
                            xtype: "layertree",
							border: false,
                            map: map,
                            height: 400,
                            showWmsLegend: true,
                            enableDD: true,
                            model: model,
							autoScroll: true,
                            plugins: [
                                mapfish.widgets.LayerTree.createContextualMenuPlugin([
                                    'opacitySlide',
                                    'remove'
                                ])
                            ]
                        }, legendPanel
						
						]
                    },{
                        region: "center",
                        id: "map",
                        title: "Map",
                        layout: "fit",
                        split: true,
                        xtype: 'mapcomponent',
                        map: map,
						tbar: toolbarItems

                    }]
                });
				//var mapPanel = Ext.getCmp("mappanel");

                viewport.doLayout();

		
		
		
    });

	
	
	window.onresize = function() {
	ajustaMapaIE();
}


	
		
	
		
		
		
function addSeparator(toolbar){
    toolbar.add(new Ext.Toolbar.Spacer());
    toolbar.add(new Ext.Toolbar.Separator());
    toolbar.add(new Ext.Toolbar.Spacer());
}
		

        
		
var createToolbar = function() {
            
			var action;

            var createSeparator = function() {
               toolbarItems.push(" ");
               toolbarItems.push("-");
               toolbarItems.push(" ");
            };

            action = new GeoExt.Action({
                control: new OpenLayers.Control.ZoomToMaxExtent(),
                map: map,
                iconCls: 'zoomfull',
                toggleGroup: 'map',
                tooltip: 'Zoom to full extent'
            });

            toolbarItems.push(action);

            createSeparator();

            action = new GeoExt.Action({
                control: new OpenLayers.Control.ZoomBox(),
                tooltip: 'Zoom in: click in the map or use the left mouse button and drag to create a rectangle',
                map: map,
                iconCls: 'zoomin',
                toggleGroup: 'map'
            });

            toolbarItems.push(action);

            action = new GeoExt.Action({
                control: new OpenLayers.Control.ZoomBox({
                    out: true
                }),
                tooltip: 'Zoom out: click in the map or use the left mouse button and drag to create a rectangle',
                map: map,
                iconCls: 'zoomout',
                toggleGroup: 'map'
            });

            toolbarItems.push(action);

            action = new GeoExt.Action({
                control: new OpenLayers.Control.DragPan({
                    isDefault: true
                }),
                tooltip: 'Pan map: keep the left mouse button pressed and drag the map',
                map: map,
                iconCls: 'pan',
                toggleGroup: 'map'
            });

            toolbarItems.push(action);

            createSeparator();

            

            createSeparator();

            ctrl = new OpenLayers.Control.NavigationHistory();
            map.addControl(ctrl);

            action = new GeoExt.Action({
                   tooltip: "Previous view",
                   control: ctrl.previous,
                   iconCls: 'back',
                   disabled: true
               });
            toolbarItems.push(action);

            action = new GeoExt.Action({
                   tooltip: "Next view",
                   control: ctrl.next,
                   iconCls: 'next',
                   disabled: true
               });
            toolbarItems.push(action);

		 createSeparator();	
			
			comboEscala=new Ext.form.ComboBox({
		    store:[2000000,1500000,800000,400000,200000,100000,75000,50000,
		        40000,30000,25000,20000,15000,10000,5000,3000, 1000, 500],
		    editable: false,
		    forceSelection: true,
		    emptyText: '2000000',
		    mode:'local',
		    triggerAction:'all',
		    cls:'x-combo-list-small',
		    listClass:'x-combo-list-small',
		    width: 80
		});

		toolbarItems.push(comboEscala);
		
		//evento de seleccion de escala
		comboEscala.on('select',function(){
		    var e=comboEscala.getValue();
		    map.zoomToScale(e, true);
		});
        createSeparator();
		comboBuscar=new Ext.form.ComboBox({
		    store:['Buscar lugares', 'Buscar calles'],
		    editable: false,
		    forceSelection: true,
		    emptyText: 'Buscar lugares',
		    mode:'local',
		    triggerAction:'all',
		    cls:'x-combo-list-small',
		    listClass:'x-combo-list-small',
		    width: 105,
			id: 'comboBuscar'
		});

		toolbarItems.push(comboBuscar);
		
		//toolbarItems.push(Ext.get('searchForm'));
		label=new Ext.form.TextField({
		    id            : 'tir',
            width        : 220,
            fieldLabel    : 'Hola mundo',
            name        : 'label'

		});
		toolbarItems.push(label);
		
		labelInfo=new Ext.form.Label({
		    id            : 'lblInfo',
            width        : 220,
            text: "Ej. Cuenca"

		});
		toolbarItems.push(labelInfo);
		createSeparator();
		
	 label.on("specialkey", function(field, ev){
        if(ev.getKey() == ev.ENTER){
            ev.preventDefault();
            // handle ENTER here 
			var vv=Ext.getDom('comboBuscar').value;//document.getElementById('cmbB').value;
			//algo;
			if(vv=="Buscar lugares"){
				buscaGeonames(Ext.getDom('tir').value) ;
				
			}else{
			
				var address = Ext.getDom('tir').value;
				geocoder.setBaseCountryCode("ec") ;
				geocoder.getLocations(address, buscarGeocoder);


			}
        }
		});
		
		comboBuscar.on('select', function(o, record, index) {
		if (this.getValue() == 'Buscar calles') {
			labelInfo.getEl().update("Ej:Mariscal Sucre,Cuenca"); 
			//Ext.getDom('lblInfo').update('Ej:Mariscal Sucre,Cuenca'); 
		}else{
			labelInfo.getEl().update("Ej: Cuenca"); 
		}	
		});
		
		
		
    }
        

	function osm_getTileURL(bounds) {
            var res = this.map.getResolution();
            var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
            var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
            var z = this.map.getZoom();
            var limit = Math.pow(2, z);

            if (y < 0 || y >= limit) {
                return OpenLayers.Util.getImagesLocation() + "404.png";
            } else {
                x = ((x % limit) + limit) % limit;
                return this.url + z + "/" + x + "/" + y + "." + this.type;
            }
        }


function isInternetExplorer(){
if (window.navigator.appName.toLowerCase().indexOf("microsoft")>=0) return true;
else return false;
}

function ajustaMapaIE() {
	//var isIE = isInternetExplorer();
	//if (window.navigator.appName.toLowerCase().indexOf("microsoft")>=0) { // Is IE
	//if (isIE) { // Is IE
		var mapIE = document.getElementById("mapa");
		var panIE = document.getElementById("panel");
		var spsizerIE = document.getElementById("spsizer"); 
		/*
		
		
		var pan3IE = document.getElementById("panel3");
		
		var opan0 = document.getElementById("opanel0");
		var opan3 = document.getElementById("opanel3");
		var pan0 = document.getElementById("panel0");
		
		*/
		//var mapH = document.body.clientHeight-mapIE.offsetTop+"px";
		var mapH = document.body.clientHeight-130+"px";
		if(mapIE.style.height != mapH) mapIE.style.height = mapH;
		if(panIE.style.height != mapH) panIE.style.height = mapH;
		if(spsizerIE.style.height != mapH) spsizerIE.style.height = mapH;
		/*
		
		
		if(pan3IE.style.height != mapH) pan3IE.style.height = mapH;
		
		if(opan0.style.height != mapH) opan0.style.height = mapH;
		if(opan3.style.height != mapH) opan3.style.height = mapH;
		if(pan0.style.height != mapH) pan0.style.height = mapH;
		
		*/
	//}
}

function replaceAll(text, strA, strB)
{
    return text.replace( new RegExp(strA,"g"), strB );    
}

var pos = 0;
function cargarServidorWMS(){
	
}





var posE=0;

	
