﻿// ==================================
// JScript File
// mapsEngine.js
// APEX Technologies
// Elson J. Frontany
// ==================================

//	var gmarker;
//	var gmap;
//	var gcontextmenu;
//	var gmarkersmall;
//	var gmapsmall;

//	function mapSingleRightClick(point, src, overlay) {
//		alert(gmap.fromContainerPixelToLatLng(point)); 
//	} 
//	// === functions that perform the context menu options ===
//	function zoomIn() {
//		// perform the requested operation
//		gmap.zoomIn();
//		// hide the context menu now that it has been used
//		gcontextmenu.style.visibility="hidden";
//	}      
//	function zoomOut() {
//		// perform the requested operation
//		gmap.zoomOut();
//		// hide the context menu now that it has been used
//		gcontextmenu.style.visibility="hidden";
//	}      
//	function zoomInHere() {
//		// perform the requested operation
//		var point = gmap.fromContainerPixelToLatLng(clickedPixel)
//		gmap.zoomIn(point,true);
//		// hide the context menu now that it has been used
//		gcontextmenu.style.visibility="hidden";
//	}      
//	function zoomOutHere() {
//		// perform the requested operation
//		var point = gmap.fromContainerPixelToLatLng(clickedPixel)
//		gmap.setCenter(point,map.getZoom()-1); // There is no map.zoomOut() equivalent
//		// hide the context menu now that it has been used
//		gcontextmenu.style.visibility="hidden";
//	}      
//	function centreMapHere() {
//		// perform the requested operation
//		var point = gmap.fromContainerPixelToLatLng(clickedPixel)
//		gmap.setCenter(point);
//		// hide the context menu now that it has been used
//		gcontextmenu.style.visibility="hidden";
//	}
//	function clearMarkers(){
//	    gmap.clearOverlays();
//		gcontextmenu.style.visibility="hidden";
//	}
//	
//	function getMapLegendPos(mapLevel){
//		var returnVal = 0;
//		switch(mapLevel){
//			case 19:
//			case 18:
//			case 17: returnVal = 7; break;
//			case 16:
//			case 15: returnVal = 6; break;
//			case 14:
//			case 13: returnVal = 5; break;
//			case 12:
//			case 11: returnVal = 4; break;
//			case 10:
//			case 9: returnVal = 3; break;
//			case 8:
//			case 7: returnVal = 2; break;
//			case 6:
//			case 5:
//			case 4: returnVal = 1; break;
//			case 3:
//			case 2:
//			case 1: returnVal = 0; break;
//			default: returnVal = 0; break;
//		}
//		return returnVal;
//	}
//	function setMapLegend(zoomLevel){ 
//		var tmpMapLegendPos = getMapLegendPos(zoomLevel);
//		$('#mapZoomLegend').css('background-position', '0px -' + (tmpMapLegendPos*20) + 'px' );
//	}

//    function initialize() {
//      if (GBrowserIsCompatible()) {
//        var map = new GMap2(document.getElementById("map_canvas"));
//        
////        alert('document.getElementById("map_canvas") ' + document.getElementById("map_canvas"));
////        alert('document.getElementById("map_canvas_small") ' + document.getElementById("map_canvas_small"));
//        
//        var map2 = new GMap2(document.getElementById("map_canvas_small"));

//		// Add Controls
//        //map.addControl(new GSmallMapControl());
//        map.addControl(new GMapTypeControl());
//		map.setMapType(G_HYBRID_MAP); 

//		map2.setMapType(G_NORMAL_MAP); 

//      // === create the context menu div ===
//      var contextmenu = document.createElement("div");
//      contextmenu.style.visibility="hidden";
//      contextmenu.style.background="#ffffff";
//      contextmenu.style.border="1px solid #8888FF";

//      // RIGHT CLICK MENU
//      contextmenu.innerHTML = '<a href="javascript:zoomIn()"><div class="context">&nbsp;&nbsp;Zoom in&nbsp;&nbsp;</div></a>'
//                            + '<a href="javascript:zoomOut()"><div class="context">&nbsp;&nbsp;Zoom out&nbsp;&nbsp;</div></a>'
//                            + '<a href="javascript:zoomInHere()"><div class="context">&nbsp;&nbsp;Zoom in here&nbsp;&nbsp;</div></a>'
//                            + '<a href="javascript:zoomOutHere()"><div class="context">&nbsp;&nbsp;Zoom out here&nbsp;&nbsp;</div></a>'
//                            + '<a href="javascript:centreMapHere()"><div class="context">&nbsp;&nbsp;Centre map here&nbsp;&nbsp;</div></a>'
//                            ;

//      map.getContainer().appendChild(contextmenu);


//	

//      // === listen for singlerightclick ===
//      GEvent.addListener(map,"singlerightclick",function(pixel,tile) {
//        // store the "pixel" info in case we need it later
//        // adjust the context menu location if near an egde
//        // create a GControlPosition
//        // apply it to the context menu, and make the context menu visible
//        clickedPixel = pixel;
//        var x=pixel.x;
//        var y=pixel.y;
//        if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }
//        if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }
//        var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
//        pos.apply(contextmenu);
//        contextmenu.style.visibility = "visible";
//      });



//      // === If the user clicks on the map, close the context menu ===
//      GEvent.addListener(map, "click", function() {
//        contextmenu.style.visibility="hidden";
//      });
//		//GEvent.addListener(map, "singlerightclick", mapSingleRightClick); 


//		

//        // Create a base icon for all of our markers that specifies the
//        // shadow, icon dimensions, etc.
//        var baseIcon = new GIcon();
//        //baseIcon.shadow = "WebHandlers/_images/shadow50.png";
//        baseIcon.iconSize = new GSize(27, 46);
//        //baseIcon.shadowSize = new GSize(37, 34);
//        baseIcon.iconAnchor = new GPoint(3, 35);
//        baseIcon.infoWindowAnchor = new GPoint(16, 8);
//        //baseIcon.infoShadowAnchor = new GPoint(18, 25);
//        var pointerIcon = new GIcon(baseIcon);
//        pointerIcon.image = "App_Images/Maps/pin-yellow.png";
//        // Set up our GMarkerOptions object
//        markerOptions = { icon:pointerIcon, draggable: true };
//        markerOptions2 = { icon:pointerIcon, draggable: false };

//		//2009-05-04	SS	added logic when edit an advertisement,  bEditing was setted in pautaanunciopaso1registrado.aspx.cs

//		var center;
//		var center2;
//		if(bEditing)
//		{
//			
//			center = new GLatLng(stdLAT, stdLNG); 
//			center2 = new GLatLng(stdLAT, stdLNG); 
//		}
//		else
//		{
////			var center = new GLatLng(18.469311019232617, -66.12226724624634); // SAN JUAN
////			var center2 = new GLatLng(18.245003052249714, -66.48651123046875); // SAN JUAN
//			center = new GLatLng(18.469311019232617, -66.12226724624634); // SAN JUAN
//			center2 = new GLatLng(18.245003052249714, -66.48651123046875); // SAN JUAN
//		}
////        var center = new GLatLng(18.376767, -66.044333);
//        var marker = new GMarker(center, markerOptions);
//        var marker2 = new GMarker(center2, markerOptions2);
//		
//		marker.bindInfoWindow('<div style="padding:4px; background-color:#F2EDDB;font-weight:bold;font-size:14px;">Drag me ...</div>');
//        map.setCenter(center, 14);
//		map2.setCenter(center2, 8);


//        GEvent.addListener(marker, "dragstart", function() {
//          map.closeInfoWindow();
//        });

//        GEvent.addListener(marker, "dragend", function() {
//			var point = marker.getPoint();
//			$('#mapLatLng').html("LATITUDE : " + point.lat() +  ", LONGITUDE : " + point.lng());
//			//if(document.getElementById("latitude")){ document.getElementById("latitude").value = point.lat() }
//			//if(document.getElementById("longitude")){ document.getElementById("longitude").value = point.lng() }
////          marker.openInfoWindowHtml("Address HERE..." + ((point)?point:""));
////          marker.openInfoWindowHtml("Address HERE...");
//			map.setZoom(18);
//			map.panTo(new GLatLng(point.lat(), point.lng()));
//        });

//        map.addOverlay(marker);
//        map2.addOverlay(marker2);
//        // 2009-05-04	SS		prevent var 'a' is null in google api
//        if (!bEditing)
//        {
//			marker.openInfoWindow('<div style="padding:4px; background-color:#F2EDDB;font-weight:bold;font-size:14px;">Drag me ...</div>');
//		}
//		gmarker = marker;
//		gmap = map;
//		gcontextmenu = contextmenu;
//		gmarkersmall = marker2;
//		gmapsmall = map2;
//		
//		setMapLegend(gmap.getZoom());
//      }
//      
//    }
//	
//	
//	// ============================================ CONFIG =============================================================
//	function openMap(){
//		// reset Selects
//		
//		resetSelect('mapUrb', ' - Select Urb-Bo - ');
//		resetSelect('mapStreet', ' - Selecione la Calle - ');
//		resetSelect('mapBlq', ' - Select Blq - ');
//		
//		// Fill Zipcode Select
//		var value = $('#'+ ddlCiudad ).val();
//		
//		fillZipcodes($('#'+ ddlCiudad +' option[value='+value+']').text());
//		
//		
//		//fill Map leyend
//		$('#statusLabel #country').html($('#'+ ddlPais  + ' option[value='+$('#'+ ddlPais ).val()+']').text());
//		$('#statusLabel #state').html($('#'+ ddlEstado  + ' option[value='+$('#'+ ddlEstado ).val()+']').text());
//		$('#statusLabel #city').html($('#'+ ddlCiudad  + ' option[value='+value+']').text());
//		
//		// Show Black curtain
//		$('#mapOverlay').css('visibility', 'visible');
//		
//		// Show Map
//		$('#mapContainer').css('top', $(window).scrollTop()+0+'px');
//		$('#mapContainer').css('visibility', 'visible');
//	}

//	// Page On Load 
////	$(document).ready(function() {

////		// Set Map Overlay ============================================
//////	   <div id="mapOverlay"></div>
//////		<div id="mapContainer">
//////			<div id="mapBox">
//////				<div id="mapFindControls"></div>
//////				<div id="mapTop"></div>
//////				<div id="mapMain"><div id="map_canvas"></div></div>
//////				<div id="mapBottom"></div>
//////			</div>
//////		</div>

/////*		var bodyDiv, element;

////		bodyDiv = document.createElement("div");
////		bodyDiv.setAttribute('id', 'mapOverlay');
////		$('body').append(bodyDiv);
////		
////		bodyDiv = document.createElement("div");
////		bodyDiv.setAttribute('id', 'mapContainer');
////		$('body').append(bodyDiv);

////		bodyDiv = document.createElement("div");
////		bodyDiv.setAttribute('id', 'mapBox');
////		$('#mapContainer').append(bodyDiv);
////		
////			bodyDiv = document.createElement("div");
////			bodyDiv.setAttribute('id', 'mapFindControls');
////			$('#mapBox').append(bodyDiv);
////				element = document.createElement("select");
////				element.setAttribute('id', 'mapZipcode');
////				element.setAttribute('id', 'mapZipcode');
////				$('#mapFindControls').append(element);
////				var option = document.createElement('option');
////				option.value = 'value';
////				option.appendChild(document.createTextNode('Name'));
////				$('#mapZipcode').append(option);
////				
////			bodyDiv = document.createElement("div");
////			bodyDiv.setAttribute('id', 'mapTop');
////			$('#mapBox').append(bodyDiv);
////			bodyDiv = document.createElement("div");
////			bodyDiv.setAttribute('id', 'mapMain');
////			$('#mapBox').append(bodyDiv);
////				bodyDiv = document.createElement("div");
////				bodyDiv.setAttribute('id', 'map_canvas');
////				$('#mapMain').append(bodyDiv);
////			bodyDiv = document.createElement("div");
////			bodyDiv.setAttribute('id', 'mapBottom');
////			$('#mapBox').append(bodyDiv);
////		// Set Map Overlay ============================================ */
////		
////		
////		var calcMapPos = $('#mapContainer').outerWidth();
////		calcMapPos = (calcMapPos/2)-($('#mapBox').width()/2) -100;
////		$('#mapOverlay').height($(document).height());
////		$('#mapOverlay').css('opacity',0.8);
////		$('#mapBox').css('left', calcMapPos+'px');

////		// Init Map
////		initialize();

////		// Assign Click Events ===============
////		$('#mapPlaceHolder').click(function(){
////			openMap()
////		});
////		$('#mapZoomIN').click(function(){
////			zoomIn();
////			setMapLegend(gmap.getZoom());
////		});
////		$('#mapZoomOUT').click(function(){
////			zoomOut();
////			setMapLegend(gmap.getZoom());
////		});
////	
////		$('#mapTop').click(function(){
////			// Hide Black curtain
////			$('#mapOverlay').css('visibility', 'hidden');
////			
////			// Hide Map
////			$('#mapContainer').css('visibility', 'hidden');
////			var point = gmarker.getPoint();
////			$('#mapLatLng').html("LATITUDE : " + point.lat() +  ", LONGITUDE : " + point.lng());
////			$('#avisoLat').attr('value', point.lat());
////			$('#avisoLng').attr('value', point.lng());
////			
////			gmarker.setLatLng(point);
////			gmap.setCenter(point);
////			gmarkersmall.setLatLng(point);
////			gmapsmall.setCenter(point);
////		});
////		
////		
////		// Select onChange() mapZipcodes ================================
////		$('#mapZipcode').change(function(){
////			// reset Selects
////			resetSelect('mapUrb', ' - Select Urb-Bo - ');
////			resetSelect('mapStreet', ' - Selecione la Calle - ');
////			resetSelect('mapBlq', ' - Select Blq - ');
////			// Fill Zipcode Select
////			var value = $('#mapZipcode').val();
////			$('#mapLoadingtWheel').show();
////			fillUrb($('#mapZipcode option[value='+value+']').text());

////			var point = new GLatLng(value.split(',')[0], value.split(',')[1]);
////			if(point.lat()!=0 && point.lng()!=0){
////				gmarker.setLatLng(point);
////				gmap.setCenter(point);
////				gmarkersmall.setLatLng(point);
////				gmapsmall.setCenter(point);
////			}
////		});
////		// Select onChange() mapUrb ================================
////		$('#mapUrb').change(function(){
////			// reset Selects
////			resetSelect('mapStreet', ' - Selecione la Calle - ');
////			resetSelect('mapBlq', ' - Selecione Blq - ');
////			// Fill Zipcode Select
////			var value = $('#mapZipcode').val();
////			var value2 = $('#mapUrb').val();
////			$('#mapLoadingtWheel').show();
////			fillStreet($('#mapUrb option[value='+value2+']').text(), $('#mapZipcode option[value='+value+']').text());
////			
////			var point = new GLatLng(value.split(',')[0], value.split(',')[1]);
////			if(point.lat()!=0 && point.lng()!=0){
////				gmarker.setLatLng(point);
////				gmap.setCenter(point);
////				gmarkersmall.setLatLng(point);
////				gmapsmall.setCenter(point);
////			}
////		});
////		// Select onChange() mapStreet ================================
////		$('#mapStreet').change(function(){
////			// reset Selects
////			resetSelect('mapBlq', ' - Selecione Blq - ');
////			// Fill Streets Select
////			var value = $('#mapUrb').val();
////			var value2 = $('#mapZipcode').val();
////			var value3 = $('#mapStreet').val();
////			$('#mapLoadingtWheel').show();
////			fillBlq($('#mapUrb option[value='+value+']').text(), $('#mapZipcode option[value='+value2+']').text(), $('#mapStreet option[value='+value3+']').text());
////			
////			var point = new GLatLng(value.split(',')[0], value.split(',')[1]);
////			if(point.lat()!=0 && point.lng()!=0){
////				gmarker.setLatLng(point);
////				gmap.setCenter(point);
////				gmarkersmall.setLatLng(point);
////				gmapsmall.setCenter(point);
////				gmap.setZoom(16);
////			}
////		});

////		// Select onChange() mapBlq ================================
////		$('#mapBlq').change(function(){
////			// Get Value
////			var value = $('#mapBlq').val();
////			
////			var point = new GLatLng(value.split(',')[0], value.split(',')[1]);
////			if(point.lat()!=0 && point.lng()!=0){
////				gmarker.setLatLng(point);
////				gmap.setCenter(point);
////				gmarkersmall.setLatLng(point);
////				gmapsmall.setCenter(point);
////				gmap.setZoom(19);
////			}
////		});
////		
////		$('#mapInfo').click(function(){
////			$(this).hide();
////		});

////	});// END onLoad
//	
//	function resetSelect(objName, defaultText){
//		$('#'+objName + " option").each(function(i){
//			//alert( $(this).text() + " : " + $(this).val() );
//			$(this).remove();
//		});
//		$('#'+objName).html('<option selected>'+defaultText+'</option>');
//		$('#'+objName).removeAttr("enabled");
//		$('#'+objName).attr("disabled","disabled");
//	}
//	function normilizeChar(text){
//		text = text.replace("á", "a");
//		text = text.replace("é", "e");
//		text = text.replace("í", "i");
//		text = text.replace("ó", "o");
//		text = text.replace("ú", "u");

//		text = text.replace("Á", "A");
//		text = text.replace("É", "E");
//		text = text.replace("Í", "I");
//		text = text.replace("Ó", "O");
//		text = text.replace("Ú", "U");

//		text = text.replace("ñ", "n");
//		text = text.replace("Ñ", "N");
//		
//		return text;
//	}

//	function fillZipcodes(_city){
//		var selectname = 'mapZipcode';
//		var defaultText = ' - Selecione Zipcode - ';

//		_city = normilizeChar(_city);
//		$.getJSON("WebHandlers/getZipcodesByCity.ashx",{cty: _city.toUpperCase()}, function(j){
//			var options = '';
//			var defaultOption = '<option selected>'+defaultText+'</option>';
//			$('#'+selectname).html(defaultOption);
//			j = j.zipcodes;
//			for (var i = 0; i < j.length; i++) {
//				options += '<option value="' + j[i].stdLAT+","+ j[i].stdLNG + '">' + j[i].stdZipCode + '</option>';
//			}
//			$('#'+selectname).html(defaultOption + options);
//			$('#mapLoadingtWheel').hide();
//		});

//	}

//	function fillUrb(_zipcode){
//		var selectname = 'mapUrb';
//		var defaultText = ' - Selecione Urb o Bo - ';

//		$.getJSON("WebHandlers/getUrbByZipcode.ashx",{zipcode: _zipcode}, function(j){
//			var options = '';
//			var defaultOption = '<option selected>'+defaultText+'</option>';
//			$('#'+selectname).html(defaultOption);
//			j = j.urbsBo;
//			for (var i = 0; i < j.length; i++) {
//				options += '<option value="' + j[i].stdLAT+","+ j[i].stdLNG + '">' + j[i].urbBo + '</option>';
//			}
//			$('#'+selectname).html(defaultOption + options);
//			if(i>0){
//				$('#'+selectname).removeAttr("disabled");
//				$('#'+selectname).attr("enabled","enabled");
//			}
//			$('#mapLoadingtWheel').hide();
//		});

//	}


//	function fillStreet(_urb, _zipcode){
//		var selectname = 'mapStreet';
//		var defaultText = ' - Selecione la Calle - ';

//		$.getJSON("WebHandlers/getStreetByUrb.ashx",{urb: _urb, zipcode: _zipcode}, function(j){
//			var options = '';
//			var defaultOption = '<option selected>'+defaultText+'</option>';
//			$('#'+selectname).html(defaultOption);
//			j = j.streets;
//			for (var i = 0; i < j.length; i++) {
//				options += '<option value="' + j[i].stdLAT+","+ j[i].stdLNG + '">' + j[i].street + '</option>';
//			}
//			$('#'+selectname).html(defaultOption + options);
//			if(i>0){
//				$('#'+selectname).removeAttr("disabled");
//				$('#'+selectname).attr("enabled","enabled");
//			}
//			$('#mapLoadingtWheel').hide();
//		});

//	}


//	function fillBlq(_urb, _zipcode, _street){
//		var selectname = 'mapBlq';
//		var defaultText = ' - Selecione Blq - ';

//		$.getJSON("WebHandlers/getBlqByStreet.ashx",{urb: _urb, zipcode:_zipcode,street:_street}, function(j){
//			var options = '';
//			var defaultOption = '<option selected>'+defaultText+'</option>';
//			$('#'+selectname).html(defaultOption);
//			j = j.blqs;
//			for (var i = 0; i < j.length; i++) {
//				options += '<option value="' + j[i].stdLAT+","+ j[i].stdLNG + '">' + j[i].blq + '</option>';
//			}
//			$('#'+selectname).html(defaultOption + options);
//			if(i>0){
//				$('#'+selectname).removeAttr("disabled");
//				$('#'+selectname).attr("enabled","enabled");
//			}
//			$('#mapLoadingtWheel').hide();
//		});

//	}

//	// 2009-05-04	SS		added logic to edit
//	
//	function placeProperties() { 
//	    var i=0;
////	    alert('entre ' + avisosList);
//		totalMarkers = $.evalJSON(avisosList).Properties.length;
////		 alert('totalMarkers ' + totalMarkers);
////		 alert('gmap ' + gmap);
//       // Create a base icon for all of our markers that specifies the
//        // shadow, icon dimensions, etc.
//        var baseIcon = new GIcon();
//        baseIcon.iconSize = new GSize(27, 46);
//        baseIcon.iconAnchor = new GPoint(9, 34);
//        baseIcon.infoWindowAnchor = new GPoint(9, 2);
//        var pointerIcon = new GIcon(baseIcon);
//        pointerIcon.image = "App_Images/Maps/pin-yellow.png";
//        // Set up our GMarkerOptions object
//        markerOptions = { icon:pointerIcon, draggable: false };
//		
//		// Iteract with array
//		var index=0;
//		for (var marks in $.evalJSON(avisosList).Properties){
//		if(index == totalMarkers)break;
//			index++;
//			marker = $.evalJSON(avisosList).Properties[marks];
//		   lat = marker.stdLAT ;
//		   lng = marker.stdLNG ;
//		  //alert("LAT:" + lat + "; LNG:" + lng);
//		  var point = new GLatLng(lat, lng);
//		  gmap.addOverlay(new GMarker(point, markerOptions));
//		}
//		
////		alert('point ' + point);
//		gmap.setCenter(point);
//		
//	}



var gmarker;
	var gmap;
	var gcontextmenu;
	var gmarkersmall;
	var gmapsmall;

	function mapSingleRightClick(point, src, overlay) {
		alert(gmap.fromContainerPixelToLatLng(point)); 
	} 
	// === functions that perform the context menu options ===
	function zoomIn() {
		// perform the requested operation
		gmap.zoomIn();
		// hide the context menu now that it has been used
		gcontextmenu.style.visibility="hidden";
	}      
	function zoomOut() {
		// perform the requested operation
		gmap.zoomOut();
		// hide the context menu now that it has been used
		gcontextmenu.style.visibility="hidden";
	}      
	function zoomInHere() {
		// perform the requested operation
		var point = gmap.fromContainerPixelToLatLng(clickedPixel)
		gmap.zoomIn(point,true);
		// hide the context menu now that it has been used
		gcontextmenu.style.visibility="hidden";
	}      
	function zoomOutHere() {
		// perform the requested operation
		var point = gmap.fromContainerPixelToLatLng(clickedPixel)
		gmap.setCenter(point,map.getZoom()-1); // There is no map.zoomOut() equivalent
		// hide the context menu now that it has been used
		gcontextmenu.style.visibility="hidden";
	}      
	function centreMapHere() {
		// perform the requested operation
		var point = gmap.fromContainerPixelToLatLng(clickedPixel)
		gmap.setCenter(point);
		// hide the context menu now that it has been used
		gcontextmenu.style.visibility="hidden";
	}
	function clearMarkers(){
	    gmap.clearOverlays();
		gcontextmenu.style.visibility="hidden";
	}
	
	function getMapLegendPos(mapLevel){
		var returnVal = 0;
		switch(mapLevel){
			case 19:
			case 18:
			case 17: returnVal = 7; break;
			case 16:
			case 15: returnVal = 6; break;
			case 14:
			case 13: returnVal = 5; break;
			case 12:
			case 11: returnVal = 4; break;
			case 10:
			case 9: returnVal = 3; break;
			case 8:
			case 7: returnVal = 2; break;
			case 6:
			case 5:
			case 4: returnVal = 1; break;
			case 3:
			case 2:
			case 1: returnVal = 0; break;
			default: returnVal = 0; break;
		}
		return returnVal;
	}
	function setMapLegend(zoomLevel){ 
		var tmpMapLegendPos = getMapLegendPos(zoomLevel);
		$('#mapZoomLegend').css('background-position', '0px -' + (tmpMapLegendPos*20) + 'px' );
	}

    function initialize() {
      if (GBrowserIsCompatible()) {
        //var map = new GMap2(document.getElementById("map_canvas"));
        //var map2 = new GMap2(document.getElementById("map_canvas_small")); 
        var map = new GMap2($("#map_canvas").get(0));
        var map2 = new GMap2($("#map_canvas_small").get(0));

		// Add Controls
        //map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
		map.setMapType(G_HYBRID_MAP); 

		map2.setMapType(G_NORMAL_MAP); 

      // === create the context menu div ===
      var contextmenu = document.createElement("div");
      contextmenu.style.visibility="hidden";
      contextmenu.style.background="#ffffff";
      contextmenu.style.border="1px solid #8888FF";

      // RIGHT CLICK MENU
      contextmenu.innerHTML = '<a href="javascript:zoomIn()"><div class="context">&nbsp;&nbsp;Zoom in&nbsp;&nbsp;</div></a>'
                            + '<a href="javascript:zoomOut()"><div class="context">&nbsp;&nbsp;Zoom out&nbsp;&nbsp;</div></a>'
                            + '<a href="javascript:zoomInHere()"><div class="context">&nbsp;&nbsp;Zoom in here&nbsp;&nbsp;</div></a>'
                            + '<a href="javascript:zoomOutHere()"><div class="context">&nbsp;&nbsp;Zoom out here&nbsp;&nbsp;</div></a>'
                            + '<a href="javascript:centreMapHere()"><div class="context">&nbsp;&nbsp;Centre map here&nbsp;&nbsp;</div></a>'
                            ;

      map.getContainer().appendChild(contextmenu);

      // === listen for singlerightclick ===
      GEvent.addListener(map,"singlerightclick",function(pixel,tile) {
        // store the "pixel" info in case we need it later
        // adjust the context menu location if near an egde
        // create a GControlPosition
        // apply it to the context menu, and make the context menu visible
        clickedPixel = pixel;
        var x=pixel.x;
        var y=pixel.y;
        if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }
        if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }
        var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
        pos.apply(contextmenu);
        contextmenu.style.visibility = "visible";
      });



      // === If the user clicks on the map, close the context menu ===
      GEvent.addListener(map, "click", function() {
        contextmenu.style.visibility="hidden";
      });
		//GEvent.addListener(map, "singlerightclick", mapSingleRightClick); 


		

        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon();
        //baseIcon.shadow = "http://localhost/geoClass/_images/shadow50.png";
        baseIcon.iconSize = new GSize(27, 46);
        //baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(3, 35);
        baseIcon.infoWindowAnchor = new GPoint(16, 8);
        //baseIcon.infoShadowAnchor = new GPoint(18, 25);
        var pointerIcon = new GIcon(baseIcon);
        pointerIcon.image = "App_Images/Maps/pin-yellow.png";
        // Set up our GMarkerOptions object
        markerOptions = { icon:pointerIcon, draggable: true };
        markerOptions2 = { icon:pointerIcon, draggable: false };

//2009-05-04	SS	added logic when edit an advertisement,  bEditing was setted in pautaanunciopaso1registrado.aspx.cs

		var center;
		var center2;
		
		if(bEditing && stdLAT != 0 && stdLNG != 0)
		{
			
			
			center = new GLatLng(stdLAT, stdLNG); 
			center2 = new GLatLng(stdLAT, stdLNG); 
			document.getElementById('avisoLng').value = stdLNG;
			document.getElementById('avisoLat').value = stdLAT;
		}
		else
		{
//			var center = new GLatLng(18.469311019232617, -66.12226724624634); // SAN JUAN
//			var center2 = new GLatLng(18.245003052249714, -66.48651123046875); // SAN JUAN
			center = new GLatLng(18.245003052249714, -66.48651123046875); // SAN JUAN
			center2 = new GLatLng(18.245003052249714, -66.48651123046875); // SAN JUAN
		}

//        var center = new GLatLng(18.376767, -66.044333);
        var marker = new GMarker(center, markerOptions);
        var marker2 = new GMarker(center2, markerOptions2);
		
		marker.bindInfoWindow('<div style="padding:4px; background-color:#F2EDDB;font-weight:bold;font-size:14px;">Muévame al Punto Exacto</div>');
        map.setCenter(center, 9);
		map2.setCenter(center2, 8);


        GEvent.addListener(marker, "dragstart", function() {
          map.closeInfoWindow();
        });

        GEvent.addListener(marker, "dragend", function() {
			var point = marker.getPoint();
			$('#mapLatLng').html("LATITUDE : " + point.lat() +  ", LONGITUDE : " + point.lng());
			//if(document.getElementById("latitude")){ document.getElementById("latitude").value = point.lat() }
			//if(document.getElementById("longitude")){ document.getElementById("longitude").value = point.lng() }
//          marker.openInfoWindowHtml("Address HERE..." + ((point)?point:""));
//          marker.openInfoWindowHtml("Address HERE...");
//			map.setZoom(18);
			map.panTo(new GLatLng(point.lat(), point.lng()));
        });

        map.addOverlay(marker);
        map2.addOverlay(marker2);
        // 2009-05-04	SS		prevent var 'a' is null in google api
        if(!bEditing)
        {
        
			marker.openInfoWindow('<div style="padding:4px; background-color:#F2EDDB;font-weight:bold;font-size:14px;">Muévame al Punto Exacto</div>');
		}
		gmarker = marker;
		gmap = map;
		gcontextmenu = contextmenu;
		gmarkersmall = marker2;
		gmapsmall = map2;
		
		setMapLegend(gmap.getZoom());
      }
    }
	
	
	// ============================================ CONFIG =============================================================
	function openMap(){
		
		var value = $('#'+ ddlCiudad ).val();
		
		if(value=="0"){alert("Para continuar debe seleccionar la Ciudad"); closeMap(); return;}
		
		// reset Selects
		resetSelect('mapUrb', ' - Select Urb-Bo - ');
		resetSelect('mapStreet', ' - Selecione la Calle - ');
		resetSelect('mapBlq', ' - Select Blq - ');
		
		/////////IE 6/////////
		if (getInternetExplorerVersion() == 6)
		{
		    var selects = document.getElementsByTagName("select");
    		 
             for (var i=0; i < selects.length; i++)
            {
               
                selects[i].style.visibility="hidden"; 
            } 
			    document.getElementById('frmCampos').style.display = "none";				

		    document.getElementById('mapZipcode').style.visibility = "visible";				
		    document.getElementById('mapUrb').style.visibility = "visible";				
		    document.getElementById('mapStreet').style.visibility = "visible";				
		    document.getElementById('mapBlq').style.visibility = "visible";		
		}		
		/////////IE 6/////////
        
		
		
//		// Fill Zipcode Select
//		var value = $('#ctl00_Content_ddlCiudad_').val();
//		fillZipcodes($('#ctl00_Content_ddlCiudad_ option[value='+value+']').text());
//		
//		//fill Map leyend
//		$('#statusLabel #country').html("US");
//		$('#statusLabel #state').html("Puerto Rico");
//		$('#statusLabel #city').html($('#ctl00_Content_ddlCiudad_ option[value='+value+']').text());
//		

// Fill Zipcode Select
	
if(value=="0"){alert("Para continuar debe seleccionar la Ciudad"); return;}		
		fillZipcodes($('#'+ ddlCiudad +' option[value='+value+']').text());
		
		
		//fill Map leyend
		$('#statusLabel #country').html($('#'+ ddlPais  + ' option[value='+$('#'+ ddlPais ).val()+']').text());
		$('#statusLabel #state').html($('#'+ ddlEstado  + ' option[value='+$('#'+ ddlEstado ).val()+']').text());
		$('#statusLabel #city').html($('#'+ ddlCiudad  + ' option[value='+value+']').text());

		// Show Black curtain
		$('#mapOverlay').css('visibility', 'visible');
		
		// Show Map
		$('#mapContainer').css('top', $(window).scrollTop()+0+'px');
		$('#mapContainer').css('visibility', 'visible');
	}
	function closeMap(){
		// Hide Black curtain
		$('#mapOverlay').css('visibility', 'hidden');
		
		// Hide Map
		$('#mapContainer').css('visibility', 'hidden');
		var point = gmarker.getPoint();
		if( point.lat()!='18.24500305' || point.lng()!='-66.48651123' ){ // Test For Default Coordinates (center)
			$('#mapLatLng').html("LATITUDE : " + point.lat() +  ", LONGITUDE : " + point.lng());
			$('#avisoLat').attr('value', point.lat());
			$('#avisoLng').attr('value', point.lng());
		}
		
		gmarker.setLatLng(point);
		gmap.setCenter(point);
		gmarkersmall.setLatLng(point);
		gmapsmall.setCenter(point);
		
		
		/////////IE 6/////////
		if (getInternetExplorerVersion() == 6)
		{
//		    alert(navigator.userAgent);
    		
		    document.getElementById(divMap).style.marginBotton = "0px";
    		
		    var selects = document.getElementsByTagName("select");
    		 
             for (var i=0; i < selects.length; i++)
		    {
               
                selects[i].style.visibility="visible"; 
            } 
			    document.getElementById('frmCampos').style.display = "block";				

		    document.getElementById('mapZipcode').style.visibility = "hidden";				
		    document.getElementById('mapUrb').style.visibility = "hidden";				
		    document.getElementById('mapStreet').style.visibility = "hidden";				
		    document.getElementById('mapBlq').style.visibility = "hidden";	
		}
		/////////IE 6/////////
	}
//	// Page On Load 
//	$(document).ready(function() {

//		// Set Map Overlay ============================================
//		var DIVs_mapOverlay = '';
//	   DIVs_mapOverlay = DIVs_mapOverlay + '<div id="mapOverlay"></div>';
//		DIVs_mapOverlay = DIVs_mapOverlay + '<div id="mapContainer">';
//		DIVs_mapOverlay = DIVs_mapOverlay + '	<div id="mapBox">';
//        DIVs_mapOverlay = DIVs_mapOverlay + '        <div id="mapLoadingtWheel"></div>';
//        DIVs_mapOverlay = DIVs_mapOverlay + '        <div id="mapInfo">En este mapa puede aproximar la localización referente a este anuncio.  Complete la información solicitada y para más precisión puede seleccionar el punto y moverlo al lugar deseado en el mapa.</div>';
//		DIVs_mapOverlay = DIVs_mapOverlay + '		<div id="mapTop"></div>';
//        DIVs_mapOverlay = DIVs_mapOverlay + '    	<div id="statusLabel"><label>País:</label> <span id="country"></span> &nbsp;&nbsp;&nbsp;&nbsp; <label>Estado:</label> <span id="state"></span> &nbsp;&nbsp;&nbsp;&nbsp; <label>Ciudad:</label> <span id="city"></span> </div>';
//		DIVs_mapOverlay = DIVs_mapOverlay + '		<div id="mapFindControls">';
//        DIVs_mapOverlay = DIVs_mapOverlay + '        	<div><label>Código Postal</label><select id="mapZipcode"></select></div>';
//        DIVs_mapOverlay = DIVs_mapOverlay + '        	<div><label>Urb. o Bo.</label><select id="mapUrb" disabled><option selected> - Seleciona Urb o Bo - </option></select></div>';
//        DIVs_mapOverlay = DIVs_mapOverlay + '        	<div><label>Calle</label><select id="mapStreet" disabled><option selected> - Seleciona la Calle - </option></select></div>';
//        DIVs_mapOverlay = DIVs_mapOverlay + '        	<div><label>Blq + #Casa</label><select id="mapBlq" disabled><option selected> - Seleciona el Blq y #Casa - </option></select></div>';
//        DIVs_mapOverlay = DIVs_mapOverlay + '        </div>';
//		DIVs_mapOverlay = DIVs_mapOverlay + '		<div id="mapMain"><div id="map_canvas"></div></div>';
//		DIVs_mapOverlay = DIVs_mapOverlay + '		<div id="mapBottom"><div id="mapZoomIN" ></div><div id="mapZoomLegend" ></div><div id="mapZoomOUT" /></div></div>';
//        DIVs_mapOverlay = DIVs_mapOverlay + '        <input type="hidden" id="avisoLat" name="avisoLat" value="" /><input type="hidden" id="avisoLng" name="avisoLng" value="" />';
//		DIVs_mapOverlay = DIVs_mapOverlay + '	</div>';
//		DIVs_mapOverlay = DIVs_mapOverlay + '</div>';
//		$('body').append(DIVs_mapOverlay);
//		// Set Map Overlay ============================================ */
//		
//		
//		var calcMapPos = $('#mapContainer').outerWidth();
//		calcMapPos = (calcMapPos/2)-($('#mapBox').width()/2) -100;
//		$('#mapOverlay').height($(document).height());
//		$('#mapOverlay').css('opacity',0.8);
//		$('#mapBox').css('left', calcMapPos+'px');

//		// Init Map
//		initialize();

//		// Assign Click Events ===============
//		$('#mapPlaceHolder').click(function(){
//			openMap()
//		});
//		$('#mapZoomIN').click(function(){
//			zoomIn();
//			setMapLegend(gmap.getZoom());
//		});
//		$('#mapZoomOUT').click(function(){
//			zoomOut();
//			setMapLegend(gmap.getZoom());
//		});
//	
//		$('#mapTop').click(function(){
//			// Hide Black curtain
//			$('#mapOverlay').css('visibility', 'hidden');
//			
//			// Hide Map
//			$('#mapContainer').css('visibility', 'hidden');
//			var point = gmarker.getPoint();
//			$('#mapLatLng').html("LATITUDE : " + point.lat() +  ", LONGITUDE : " + point.lng());
//			$('#avisoLat').attr('value', point.lat());
//			$('#avisoLng').attr('value', point.lng());
//			
//			gmarker.setLatLng(point);
//			gmap.setCenter(point);
//			gmarkersmall.setLatLng(point);
//			gmapsmall.setCenter(point);
//		});
//		
//		
//		// Select onChange() mapZipcodes ================================
//		$('#mapZipcode').change(function(){
//			// reset Selects
//			resetSelect('mapUrb', ' - Select Urb-Bo - ');
//			resetSelect('mapStreet', ' - Selecione la Calle - ');
//			resetSelect('mapBlq', ' - Select Blq - ');
//			// Fill Zipcode Select
//			var value = $('#mapZipcode').val();
//			$('#mapLoadingtWheel').show();
//			fillUrb($('#mapZipcode option[value='+value+']').text());

//			var point = new GLatLng(value.split(',')[0], value.split(',')[1]);
//			if(point.lat()!=0 && point.lng()!=0){
//				gmarker.setLatLng(point);
//				gmap.setCenter(point);
//				gmarkersmall.setLatLng(point);
//				gmapsmall.setCenter(point);
//			}
//		});
//		// Select onChange() mapUrb ================================
//		$('#mapUrb').change(function(){
//			// reset Selects
//			resetSelect('mapStreet', ' - Selecione la Calle - ');
//			resetSelect('mapBlq', ' - Selecione Blq - ');
//			// Fill Zipcode Select
//			var value = $('#mapZipcode').val();
//			var value2 = $('#mapUrb').val();
//			$('#mapLoadingtWheel').show();
//			fillStreet($('#mapUrb option[value='+value2+']').text(), $('#mapZipcode option[value='+value+']').text());
//			
//			var point = new GLatLng(value.split(',')[0], value.split(',')[1]);
//			if(point.lat()!=0 && point.lng()!=0){
//				gmarker.setLatLng(point);
//				gmap.setCenter(point);
//				gmarkersmall.setLatLng(point);
//				gmapsmall.setCenter(point);
//			}
//		});
//		// Select onChange() mapStreet ================================
//		$('#mapStreet').change(function(){
//			// reset Selects
//			resetSelect('mapBlq', ' - Selecione Blq - ');
//			// Fill Streets Select
//			var value = $('#mapUrb').val();
//			var value2 = $('#mapZipcode').val();
//			var value3 = $('#mapStreet').val();
//			$('#mapLoadingtWheel').show();
//			fillBlq($('#mapUrb option[value='+value+']').text(), $('#mapZipcode option[value='+value2+']').text(), $('#mapStreet option[value='+value3+']').text());
//			
//			var point = new GLatLng(value.split(',')[0], value.split(',')[1]);
//			if(point.lat()!=0 && point.lng()!=0){
//				gmarker.setLatLng(point);
//				gmap.setCenter(point);
//				gmarkersmall.setLatLng(point);
//				gmapsmall.setCenter(point);
//				gmap.setZoom(16);
//			}
//		});

//		// Select onChange() mapBlq ================================
//		$('#mapBlq').change(function(){
//			// Get Value
//			var value = $('#mapBlq').val();
//			
//			var point = new GLatLng(value.split(',')[0], value.split(',')[1]);
//			if(point.lat()!=0 && point.lng()!=0){
//				gmarker.setLatLng(point);
//				gmap.setCenter(point);
//				gmarkersmall.setLatLng(point);
//				gmapsmall.setCenter(point);
//				gmap.setZoom(19);
//			}
//		});
//		
//		$('#mapInfo').click(function(){
//			$(this).hide();
//		});

//	});// END onLoad
	
	function resetSelect(objName, defaultText){
		$('#'+objName + " option").each(function(i){
			//alert( $(this).text() + " : " + $(this).val() );
			$(this).remove();
		});
		$('#'+objName).html('<option selected>'+defaultText+'</option>');
		$('#'+objName).removeAttr("enabled");
		$('#'+objName).attr("disabled","disabled");
	}
	function normilizeChar(text){
		text = text.replace("á", "a");
		text = text.replace("é", "e");
		text = text.replace("í", "i");
		text = text.replace("ó", "o");
		text = text.replace("ú", "u");

		text = text.replace("Á", "A");
		text = text.replace("É", "E");
		text = text.replace("Í", "I");
		text = text.replace("Ó", "O");
		text = text.replace("Ú", "U");

		text = text.replace("ñ", "n");
		text = text.replace("Ñ", "N");
		
		return text;
	}

	function fillZipcodes(_city){
		var selectname = 'mapZipcode';
		var defaultText = ' - Selecione Zipcode - ';

		_city = normilizeChar(_city);
		$.getJSON("WebHandlers/getZipcodesByCity.ashx",{cty: _city.toUpperCase()}, function(j){
			var options = '';
			var defaultOption = '<option selected>'+defaultText+'</option>';
			$('#'+selectname).html(defaultOption);
			j = j.zipcodes;
			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].stdLAT+","+ j[i].stdLNG + '">' + j[i].stdZipCode + '</option>';
			}
			$('#'+selectname).html(defaultOption + options);
			$('#mapLoadingtWheel').hide();
		});

	}

	function fillUrb(_zipcode){
		var selectname = 'mapUrb';
		var defaultText = ' - Selecione Urb o Bo - ';

		$.getJSON("WebHandlers/getUrbByZipcode.ashx",{zipcode: _zipcode}, function(j){
			var options = '';
			var defaultOption = '<option selected>'+defaultText+'</option>';
			$('#'+selectname).html(defaultOption);
			j = j.urbsBo;
			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].stdLAT+","+ j[i].stdLNG + '">' + j[i].urbBo + '</option>';
			}
			$('#'+selectname).html(defaultOption + options);
			if(i>0){
				$('#'+selectname).removeAttr("disabled");
				$('#'+selectname).attr("enabled","enabled");
			}
			$('#mapLoadingtWheel').hide();
		});

	}


	function fillStreet(_urb, _zipcode){
		var selectname = 'mapStreet';
		var defaultText = ' - Selecione la Calle - ';

		$.getJSON("WebHandlers/getStreetByUrb.ashx",{urb: _urb, zipcode: _zipcode}, function(j){
			var options = '';
			var defaultOption = '<option selected>'+defaultText+'</option>';
			$('#'+selectname).html(defaultOption);
			j = j.streets;
			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].stdLAT+","+ j[i].stdLNG + '">' + j[i].street + '</option>';
			}
			$('#'+selectname).html(defaultOption + options);
			if(i>0){
				$('#'+selectname).removeAttr("disabled");
				$('#'+selectname).attr("enabled","enabled");
			}
			$('#mapLoadingtWheel').hide();
		});

	}


	function fillBlq(_urb, _zipcode, _street){
		var selectname = 'mapBlq';
		var defaultText = ' - Selecione Blq - ';

		$.getJSON("WebHandlers/getBlqByStreet.ashx",{urb: _urb, zipcode:_zipcode,street:_street}, function(j){
			var options = '';
			var defaultOption = '<option selected>'+defaultText+'</option>';
			$('#'+selectname).html(defaultOption);
			j = j.blqs;
			for (var i = 0; i < j.length; i++) {
				options += '<option value="' + j[i].stdLAT+","+ j[i].stdLNG + '">' + j[i].blq + '</option>';
			}
			$('#'+selectname).html(defaultOption + options);
			if(i>0){
				$('#'+selectname).removeAttr("disabled");
				$('#'+selectname).attr("enabled","enabled");
			}
			$('#mapLoadingtWheel').hide();
		});

	}



