﻿
function GoogleMap(pElementMap, pInitLat, pInitLong, pZoom, pMapType, pOJSon)
{
    
    this.sMapKey = new String("ABQIAAAA8EzhCqhOv69o5mhfYSBXvhQmPo_g4odJD2NXBrGw6d_QQPrKTBRhvekuZc6cSrKKkZeemhQvJY33ZA");
    this.sElementMap = new String(pElementMap);
    this.sInitLat = new Number(pInitLat);
    this.sInitLong = new Number(pInitLong);
    this.sZoom = new Number(pZoom);
    this.sMapType = pMapType;
    this.oJSon = new String(pOJSon);
    this.oMap;
    var oPlaceAddress;
    this.minLat = 100;
    this.maxLat = 0;
    this.minLong = 100;
    this.maxLong = 0;
    this.arrPins = new Array();
    this.arrMarkers = new Array();
    
    
    this.Initialize = function()
    {
        
        try
        {
            if (GBrowserIsCompatible())
            {
                // centraliza e coloca mapa
                
                this.oMap = new GMap2(document.getElementById(this.sElementMap));
                this.oMap.setCenter(new GLatLng(this.sInitLat, this.sInitLong), parseFloat(this.sZoom));
                this.oMap.setMapType(this.sMapType);
                this.oMap.setUIToDefault();
          
                

                
                
                
            }
        }
        catch(e)
        {
            //throw(e);
            return false;
        }
    }
    
    /*
    * Transforma o Json com as informacoes do BD em Cordenadas latitude Longitude
    * chama metodo que coloco os Pins
    */
    this.ConvertCoordinates = function()
    {
        
        try
        {
        
            oPlaceAddress = GoogleMap.GetObjJson(this.oJSon);

			if(oPlaceAddress)
			{
			
			GoogleMap.AddPinMap();
			/*
				for(var i=0; i<oPlaceAddress.length; i++)
				{
	                
	                
					if(oPlaceAddress[i]){
						var sAddress = new String();
						sAddress =  (oPlaceAddress[i].Address != "")? oPlaceAddress[i].Address : "";
						sAddress += (oPlaceAddress[i].AddressNumber != "")? ", " + oPlaceAddress[i].AddressNumber : "";
						
	                
						var oParams =  "Address=" + sAddress;
						$this = this;
	     
						$$$.NewSystemRequest("Service/GMap.aspx", oParams, GoogleMap.AddPinMap, i);
	                    
					}
	                
				}
				*/
			}

        }
        catch(e)
        {
            //throw(e);
            return false;
        }
    }

    this.AddPinInfo = function(arrProperties, i, addPopInfo, showPhoto, sExtraDetailLink, pCustomPop) {

        var custIcon = new GIcon(G_DEFAULT_ICON);
        custIcon.iconSize = new GSize(23, 21);
        custIcon.shadowSize = new GSize(0, 0);
        custIcon.iconAnchor = new GPoint(0, 0);
        if (sExtraDetailLink == undefined)
            sExtraDetailLink = "1=1";
        var oPlace = arrProperties[i];
        if (oPlace.Lat != 0) {
            if (oPlace.Lat < this.minLat) {
                this.minLat = oPlace.Lat;
            }
            if (oPlace.Lat > this.maxLat) {
                this.maxLat = oPlace.Lat;
            }

            if (oPlace.Long < this.minLong) {
                this.minLong = oPlace.Long;
            }
            if (oPlace.Long > this.maxLong) {
                this.maxLong = oPlace.Long;
            }

            var myPoint = new GLatLng(oPlace.Lat, oPlace.Long);
            this.arrPins.push(myPoint);
            var myMarker;
            if (oPlace.IsOpenHouse == "1") {
                myMarker = new GMarker(myPoint, { icon: new GIcon(custIcon, "/Images/agora_map_pinIcon_openhouse.gif") });
            } else if (oPlace.IsNewListing == "1") {
                myMarker = new GMarker(myPoint, { icon: new GIcon(custIcon, "/Images/agora_map_pinIcon_newlisting.gif") });
            } else {
            myMarker = new GMarker(myPoint, { icon: new GIcon(custIcon, "/Images/agora_map_pinIcon.gif") });
            //myMarker = new GMarker(myPoint, { icon: new GIcon(custIcon, "/images/SectionImage.aspx?ImageName=agora_map_pinIcon1.png&Text="+i) });
            }


            var markerContentStatic = "";
            var sPostOffice = "";
            if (oPlace.PostOffice != undefined && oPlace.PostOffice != "")
                sPostOffice = "<br/><b>Post Office: </b>" + oPlace.PostOffice;

            if (showPhoto) {
                markerContentStatic = "<div class='mapBalloon'><div class='photo'><img src='" + oPlace.Photo + "' width='80' /><br/><br/><a href='PropertyDetail.aspx?" + DataFloat.Agora.Util.Request.QueryStringText + "&PropertyDetail=" + oPlace.Mls + "&" + sExtraDetailLink + "' class='listtextlink'>&gt;&gt; View details</a></div><div class='info'><b>" + oPlace.Address + "</b><br/>" + oPlace.Price + "<br/><br/><b>Mls #: </b>" + oPlace.Mls + "<br/><b>City/Town: </b>" + oPlace.City + sPostOffice + "<br/><b>School District: </b>" + oPlace.SchoolDistrict + "<br/><b>Bedrooms: </b>" + oPlace.Beds + "<br/><b>Baths: </b>" + oPlace.Baths + "</div></div>";
            } else {
                //this.openInfoWindowHtml("<div class='mapBalloon'><div class='info'><b>" + oPlace.Address + "</b><br/>$" + oPlace.Price + "<br/><br/><b>Mls #: </b>" + oPlace.Mls + "<br/><b>City/Town: </b>" + oPlace.City + "<br/><b>School District: </b>" + oPlace.SchoolDistrict + "<br/><b>Bedrooms: </b>" + oPlace.Beds + "<br/><b>Baths: </b>" + oPlace.Baths + "<br/><a href='PropertyDetail.aspx?" + DataFloat.Agora.Util.Request.QueryStringText + "&PropertyDetail=" + oPlace.Mls + "&" + sExtraDetailLink + "' class='listtextlink'>&gt;&gt; View details</a></div></div>");
                markerContentStatic = "<div class='mapBalloon'><div class='info'><b>" + oPlace.Address + "</b><b>City/Town: </b>" + oPlace.City + "<br/>" + oPlace.Price + "<br/><b>Mls #: </b>" + oPlace.Mls + "<br/><a href='PropertyDetail.aspx?" + DataFloat.Agora.Util.Request.QueryStringText + "&PropertyDetail=" + oPlace.Mls + "&" + sExtraDetailLink + "' class='listtextlink'>&gt;&gt; View details</a></div></div>";
            }

            if (addPopInfo) {
                GEvent.addListener(myMarker, "mouseover", function() {
                    if (DataFloat.Agora.Util.Request.QueryStringText == "")
                        DataFloat.Agora.Util.Request.QueryStringText = "1=1";

                    var sLinkDetail;
                    if (pCustomPop) {
                        sLinkDetail = "";
                    } else {
                        //sLinkDetail = "<a href='PropertyDetail.aspx?" + DataFloat.Agora.Util.Request.QueryStringText + "&PropertyDetail=" + oPlace.Mls + "&" + sExtraDetailLink + "' class='listtextlink'>&gt;&gt; View details</a>";
                        sLinkDetail = "<a href='/real-estate/propertydetail/" + oPlace.Mls.replace(/ /g,"-") + "/" + oPlace.City.replace(/ /g,"-").replace(".","")+"/"+oPlace.Address.replace(/ /g,"-")+ "'class='listtextlink'>&gt;&gt; View details</a>";
                    }
                    if (showPhoto) {
                        //target='_blank'
                        markerContent = "<div class='mapBalloon'><div class='photo'><img src='" + oPlace.Photo + "' width='80' /><br/><br/>" + sLinkDetail + "</div><div class='info'><b>" + oPlace.Address + "</b><br/>" + oPlace.Price + "<br/><br/><b>Mls #: </b>" + oPlace.Mls + "<br/><b>City/Town: </b>" + oPlace.City + sPostOffice + "<br/><b>School District: </b>" + oPlace.SchoolDistrict + "<br/><b>Bedrooms: </b>" + oPlace.Beds + "<br/><b>Baths: </b>" + oPlace.Baths + "</div></div>";
                    } else {
                        //target='_blank'
                        //this.openInfoWindowHtml("<div class='mapBalloon'><div class='info'><b>" + oPlace.Address + "</b><br/>$" + oPlace.Price + "<br/><br/><b>Mls #: </b>" + oPlace.Mls + "<br/><b>City/Town: </b>" + oPlace.City + "<br/><b>School District: </b>" + oPlace.SchoolDistrict + "<br/><b>Bedrooms: </b>" + oPlace.Beds + "<br/><b>Baths: </b>" + oPlace.Baths + "<br/><a href='PropertyDetail.aspx?" + DataFloat.Agora.Util.Request.QueryStringText + "&PropertyDetail=" + oPlace.Mls + "&" + sExtraDetailLink + "' class='listtextlink'>&gt;&gt; View details</a></div></div>");
                        markerContent = "<div class='mapBalloon'><div class='info'><b>" + oPlace.Address + "</b><b>City/Town: </b>" + oPlace.City + sPostOffice + "<br/>" + oPlace.Price + "<br/><b>Mls #: </b>" + oPlace.Mls + "<br/>" + sLinkDetail + "</div></div>";
                    }
                    //this.openInfoWindowHtml(markerContent);
                    if (pCustomPop) {
                        $("#ExtraInfo").show();
                        $("#ExtraInfo").html(markerContent);
                    } else {
                        this.openInfoWindowHtml(markerContent);
                    }
                });

                GEvent.addListener(myMarker, "mouseout", function() {
                    $("#ExtraInfo").hide();
                });
            }

            if (addPopInfo) {
                if (pCustomPop) {
                    GEvent.addListener(myMarker, "click", function() {
                        document.location.href = "PropertyDetail.aspx?" + DataFloat.Agora.Util.Request.QueryStringText + "&PropertyDetail=" + oPlace.Mls + "&" + sExtraDetailLink;
                    });
                }
            }

            myMarker.popHtmlContent = markerContentStatic;
            this.arrMarkers.push(myMarker);
            MapView.oMap.addOverlay(myMarker);
        }
    }

    this.CentralizeMap = function() {
        var latlngbounds = new GLatLngBounds();
        for (var i = 0; i < this.arrPins.length; i++) {
            latlngbounds.extend(this.arrPins[i]);
        }
        MapView.oMap.setCenter(latlngbounds.getCenter(), MapView.oMap.getBoundsZoomLevel(latlngbounds));

           //var centerLatitude = this.minLat + (this.maxLat – this.minLat) / 2; 
           //var centerLongitude = this.minLong + (this.maxLat – this.minLong) / 2;
    }

    this.AddPinMap = function(pContent, addPopInfo, showPhoto, sExtraDetailLink, pCustomPop) {

        try {

            var oPlace = JSON.parse(pContent);
            this.arrMarkers = new Array();
            for (i = 0; i < oPlace.length; i++) {

                MapView.AddPinInfo(oPlace, i, addPopInfo, showPhoto, sExtraDetailLink, pCustomPop);

            }




        }
        catch (e) {
            alert(e);
            //throw(e);
            return false;
        }
    }
    
    /*
    * Configura o Icon base padrao
    */
    this.ConfigBaseIcon = function()
    {
        try
        {
            var baseIcon = new GIcon();
            baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
            baseIcon.iconSize = new GSize(20, 34);
            baseIcon.shadowSize = new GSize(37, 34);
            baseIcon.iconAnchor = new GPoint(9, 34);
            baseIcon.infoWindowAnchor = new GPoint(9, 2);
            baseIcon.infoShadowAnchor = new GPoint(18, 25);
            
            return baseIcon;
        }
        catch(e)
        {
            //throw(e);
            return false;
        }
    }  
    
    /*
    * Cria Pin nos Comentaios
    */   
    this.CreatePinCommentary = function(pSrc, pImgId, pCoordinates)
    {
        try
        {        
            var sImgPin = "imgCommentaryPin" + pImgId;
            var sBoxPin = "boxCommentaryPin" + pImgId;
            
			var imgMarker = document.createElement("img");
			imgMarker.setAttribute("src", pSrc);
			imgMarker.setAttribute("id", sImgPin);
			imgMarker.setAttribute("alt", "Click aqui e veja o local no mapa");
			imgMarker.setAttribute("title", "Click aqui e veja o local no mapa");
			imgMarker.onclick=function(){GoogleMap.SetMapCenterPosition(pCoordinates.Point.coordinates[1], pCoordinates.Point.coordinates[0]), location.href = "#map_canvas"; }; 
			
			NetBiis.ID(sBoxPin).appendChild(imgMarker);
        }
        catch(e)
        {
            //throw(e);
            return false;
        }
    }
    

    /*
    * Cria Imagem Estatica com o Pin na posicao passada por cordenadas
    */   
    this.CreateStaticMap = function(pCoordinates, pImgId)
    {
        try
        {
            var sImgMap = "imgMap" + pImgId;
            NetBiis.ID(sImgMap).style.display = "block";
            NetBiis.ID(sImgMap).src = "http://maps.google.com/staticmap?zoom=15&size=158x120&markers=" + pCoordinates.Point.coordinates[1] + "," + pCoordinates.Point.coordinates[0] + ",red&maptype=mobile&key=" + this.sMapKey;
        }
        catch(e)
        {
            //throw(e);
            return false;
        }
    }
    
    /*
    * Centraliza o Mapa conforme as cordenadas
    */  
    this.SetMapCenterPosition = function(pCoordinates1, pCoordinates0)
    {
        try
        {	
			GoogleMap.oMap.panTo(new GLatLng(pCoordinates1, pCoordinates0));
        }
        catch(e)
        {
            //throw(e);
            return false;
        }		
    }
    
    /*
    * Transforma o Json em Objeto
    */
    this.GetObjJson = function(pResponseJson)
    {
        try
        {
            var oJson = json_parse(pResponseJson);
            return oJson;
        }
        catch(e)
        {
            throw(e);
            return false;
        }
    }
    
}

function NetBiis()
{
    
    /*
    * document.getElementById
    */
    this.ID = function(id)
    {
        try
        {
            return document.getElementById(id);
        }
        catch(e)
        {
            //throw(e);
            return false;
        }
    
    }
    
    /*
    * Adiciona e Remove '-act' de imagens pAct parametro boll
    */
    this.AddRemoveAct = function(obj, pAct)
    {
        try
        {
            var srcImage = obj.src;

            var start = srcImage.lastIndexOf("/") + 1;
            var end = srcImage.lastIndexOf(".");
            var srcSub = srcImage.substring(start, end);
            var srcAct = srcSub.search("-act");
            var src = (srcAct == -1)? srcSub : srcSub.substring(0, srcAct);
            
            return (pAct == true)? "Img/" + src + "-act.gif":"Img/" +  src + ".gif";
         }
         catch(e)
         {
            //throw(e);
            return false;
         }
    }
    
	/*
    * Altera a classe css do objeto referenciado por id para className
    */
	this.ChangeCss = function( obj, className ) {
		try {
		
			if ( obj != "" ) {
				obj.setAttribute("class", className);
				obj.setAttribute("className", className);
			}
		} catch(e) {
			//throw(e);
			return false;
		}
	}
    
	/*
    * Recebe um objeto e retorna o mesmo com o evento informado
    */    
    this.CreateEventListener = function( obj, eventIE, eventOthers, metodo ) {
        try {
            if (obj.addEventListener){
                //Outros
                eval("obj.addEventListener(\"" + eventOthers + "\", " + metodo + ", false);");
            } else if (obj.attachEvent){
                //Internet Explorer
                eval("obj.attachEvent(\"" + eventIE + "\", " + metodo + ");");
            }
            
            return obj;
        } catch(e) {
            //throw(e);
            return false;
        }
    }    
    
    /*
    * Reseta os campos do Formulario
    */
    this.FormReset = function(pForm)
    {
        try
        {
            NetBiis.ID(pForm).reset();
        }
        catch(e)
        {
            //throw(e);
            return false;
        }            
    }
}
