﻿// JScript File
    

    function GetMap()
    {       
        map = new VEMap('MyMap');
        map.LoadMap();
        map.Find(null,'toronto');                        
        //map.AttachEvent("onmousedown", MouseDownPosition);
        //map.AttachEvent("onmouseup", DrawShape);
        map.AttachEvent("onmouseover", SetOpacityUp); 
        map.AttachEvent("onmouseout", SetOpacityDown);
        map.AttachEvent("onclick", SetOpacityUp);
         ShowMap();
        //SetZoom();            
    }    
        
    function GetAllCities()
    {        
        GetData('GetData/GetExternalUrl.aspx','url=GetCities','3');    
    }
    function GetCondo(condoId)
    {            
        GetData('GetData/GetExternalUrl.aspx','url=GetCondo&condoId='+condoId,'13');    
    }
    function LoadCondo(condolat,condolong,condoid)
    {
        map = new VEMap('MyMap');
        map.LoadMap(new VELatLong(parseFloat(condolat),parseFloat(condolong)));
        //map.Find(null,'toronto');                        
        //map.AttachEvent("onmousedown", MouseDownPosition);
        //map.AttachEvent("onmouseup", DrawShape);
        map.SetMapStyle(VEMapStyle.Birdseye);
        map.AttachEvent("onmouseover", SetOpacityUp); 
        map.AttachEvent("onmouseout", SetOpacityDown);
        map.AttachEvent("onclick", SetOpacityUp);
        ShowMap();
        
    }
    function LoadAllCity(cities,cnt,name)
        {                 
            GetMap();
            var centerLatLon = null;
            var description='';
            allCityNames = name;                   
            for(n=0;n<cities.length;n++)
            {                
                var dbPoints = new Array();
                var latLongPoints = new Array();
                var myShape = null;
                dbPoints = cities[n].split(';');
                var cornerOne = null;
                var pushPinShape = null;
                var layer = null;                                 
                for(i=0;i<dbPoints.length;i++)
                {                 
                    if(dbPoints[i] != '')
                    {
                        var latlongs = new Array();
                        latlongs = dbPoints[i].split(',');
                        cornerOne = new VELatLong(parseFloat(latlongs[0]),parseFloat(latlongs[1]));
                        latLongPoints.push(cornerOne);                    
                    }                  
                }               
                layer = new VEShapeLayer();                     
                myShape = new VEShape(VEShapeType.Polygon, latLongPoints);                              
                myShape.SetCustomIcon("images/subregion.gif");                         
                myShape.SetLineWidth(3);
                myShape.SetLineColor(new VEColor(0,150,100,0.0));
                myShape.SetFillColor(new VEColor(0,150,200,0.0));                
                description = '';
                if(name[n]!='' && name[n]!=null)
                {
                    description = '<b>' + name[n] + '<b><br>';  // alert('description2');  
                }
                if(cnt[n]!='' && cnt[n]!=null)
                {
                    description += '<b>' + cnt[n] + '<b><br>';// alert('description3');
                }               
                myShape.SetDescription(description);
                //myShape.SetMoreInfoURL("http://msdn.microsoft.com","aaaa");
                //myShape.SetPhotoURL("http://dev.live.com/Themes/default/images/Mix08/service/logo_virtualearth.jpg");
                layer.AddShape(myShape);
                map.AddShapeLayer(layer);                
            }
            map.SetZoomLevel(9);
        }
        
        function SetOpacityUp(e)
        {                                           
            if(e.eventName=='onclick')
            {                        
                var pinShape = map.GetShapeByID(e.elementID);                
                             
                if(pinShape!=null)
                {                                      
                    var pinShapeType = pinShape.GetShapeType();
                    if(pinShapeType == 'Point')
                    {                  
                        map.DetachEvent("onmouseover", SetOpacityUp); 
                        map.DetachEvent("onmouseout", SetOpacityDown);
                        var ele = document.getElementById('detailDiv');
                        var mX = e.mapX;
                        var mY = e.mapY;
                        var cX = e.clientX;
                        var cY = e.clientY;
                        var point = pinShape.GetPoints();
                        pixel = new VEPixel(mX, mY);
                        var LL = map.PixelToLatLong(pixel);                    
                        map.SetCenterAndZoom(point[0],parseInt(16));
                        var center = map.LatLongToPixel(map.GetCenter(),map.GetZoomLevel());
                        ele.style.top = (parseInt(center.y) - 200)+ 'px';
                        ele.style.left = (parseInt(center.x) - 150) + 'px';
                        map.AddControl(ele);
                        oldDescription = pinShape.GetDescription();                   
                        pinShape.SetTitle('');
                        pinShape.SetDescription('');                    
                        DivShow();
                        GetData('GetData/GetExternalUrl.aspx','url=getListingDetails&description='+oldDescription,'5');
                        
                    }
                    else
                    {
                        map.AttachEvent("onmouseover", SetOpacityUp); 
                        map.AttachEvent("onmouseout", SetOpacityDown);
                        DivHide();
                        if(IsMapInListingView() && e.elementID == selectedElementID)
                        {
                            state = 1;
                        }
                        else if(!IsMapInListingView() && e.elementID == selectedElementID)
                        {
                            state = 2;
                        }
                        else if(IsMapInListingView() && e.elementID != selectedElementID)
                        {
                            ClearListings();
                            state = 3;
                        }
                        else if(!IsMapInListingView() && e.elementID != selectedElementID)
                        {
                            state = 4;
                        }
                        if(e.elementID != null)
                        {
                            if(selectedElementID != '')
                            {                      
                                var preSelectedShape = map.GetShapeByID(selectedElementID);
                                if(preSelectedShape!=null)
                                {   
                                    preSelectedShape.SetLineColor(new VEColor(0,150,100,0.0));
                                    preSelectedShape.SetFillColor(new VEColor(0,150,200,0.0));
                                }
                            }
                            if(state==2 || linkState == 2)
                            {
                                isZoomedView = true;
                                selectedElementID = e.elementID;                                                
                                var cityPoints = new Array();
                                cityShape = map.GetShapeByID(e.elementID);
                                cityPoints = cityShape.GetPoints();
                                map.SetMapView(cityPoints);
                                cityShape.SetLineColor(new VEColor(0,150,100,1.0));
                                cityShape.SetFillColor(new VEColor(0,150,200,0.3));
                                var qsCityName = '';
                                for(i=0;i<allCityNames.length;i++)
                                {
                                    var cityDes = cityShape.GetDescription();
                                    if(cityDes.match(allCityNames[i]))
                                    {
                                        qsCityName = allCityNames[i];
                                    }
                                }
                                var sPath = window.location.pathname;
                                var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
                                //alert(sPage);
                                if(sPage.toLowerCase().match('mls-listing.aspx')!=null)
                                {                                    
                                    GetRequest(MAPUCCITYSELECTED,qsCityName);
                                }
                                else if(sPage.toLowerCase().match('just-listed.aspx')!=null)
                                {                          
                                    GetRequest(JUSTLISTEDPAGE,qsCityName);
                                }
                                else
                                {
                                    GetRequest(CONDOANDLOFTPAGE,qsCityName);
                                }
                                //LoadMapCityListing(qsCityName);
                                //GetData('http://99.225.152.30/be-at-home/www/GetData/getlistings.aspx','cityname='+qsCityName,'4');
                            }
                            else if(state==4)
                            {
                                isZoomedView = true;
                                selectedElementID = e.elementID;                    
                                var cityPoints = new Array();
                                cityShape = map.GetShapeByID(e.elementID);
                                cityPoints = cityShape.GetPoints();
                                map.SetMapView(cityPoints);
                                cityShape.SetLineColor(new VEColor(0,150,100,1.0));
                                cityShape.SetFillColor(new VEColor(0,150,200,0.3));
                            }
                            else if(state==3)
                            {
                                isZoomedView = true;
                                selectedElementID = e.elementID;                    
                                var cityPoints = new Array();
                                cityShape = map.GetShapeByID(e.elementID);
                                if(cityShape!=null)
                                {
                                    cityPoints = cityShape.GetPoints();
                                }
                                map.SetMapView(cityPoints);
                                cityShape.SetLineColor(new VEColor(0,150,100,1.0));
                                cityShape.SetFillColor(new VEColor(0,150,200,0.3));
                            }
                        }                    
                    }
                }                
            }
            else if(e.eventName=='onmouseover')
            {
                if(e.elementID != null)
                {                                   
                    shape = map.GetShapeByID(e.elementID);
                    shape.SetLineColor(new VEColor(0,150,100,1.0));
                    shape.SetFillColor(new VEColor(0,150,200,0.3));
                }else
                {
                    shape = map.GetShapeByID(selectedElementID);
                    if(shape!=null)
                    {
                        shape.SetLineColor(new VEColor(0,150,100,1.0));
                        shape.SetFillColor(new VEColor(0,150,200,0.3));
                    }
                }
            }            
        }
        function SetOpacityDown(e)
        {
            if(e.elementID != null)
            {
                shape = map.GetShapeByID(e.elementID);               
                shape.SetLineColor(new VEColor(0,150,100,0.0));
                shape.SetFillColor(new VEColor(0,150,200,0.0));               
            }
            if(selectedElementID != '')
            {
                shape = map.GetShapeByID(selectedElementID);
                if(shape!=null)
                {
                    shape.SetLineColor(new VEColor(0,150,100,1.0));
                    shape.SetFillColor(new VEColor(0,150,200,0.3)); 
                }
            }
        }        
        function LoadListings(latslongs,ids,name,type,bed,bath,price)
        {
          if(map ==null)
          {
              GetMap();
          }
            ClearListings();
            var layer = new VEShapeLayer();   
            
            for(i=0;i<latslongs.length;i++)
            {  
            try{              
                if(latslongs[i]!='')
                {               
                    var latlongs = new Array();
                    var descript = '';
                    descript += '<div id="hideId" style="visibility:hidden;">' + ids[i] + '</div><b>' + name[i] + '</b><br><b>' + type[i] + '</b><br><br>Bedrooms : ' + bed[i] + '<br>Bathrooms : ' + bath[i] + '<br><br><b>Price : $' + price[i] + '</b>';                                        
                    latlongs = latslongs[i].split(',');    
                     
                    if( latlongs[0]!= null && latlongs[1]!=null)
                    {                                
                        cornerOne = new VELatLong(parseFloat(latlongs[0]),parseFloat(latlongs[1]));                            
                        var pushPin = new VEShape(VEShapeType.Pushpin, cornerOne);               
                        pushPin.SetCustomIcon("images/condo.jpg");                  
                        pushPin.SetDescription(descript);
                        layer.AddShape(pushPin);                                        
                    }
                 }
                }
                catch(err)
                {
                   // txt="There was an error on this page("+i+").\n\n";
                   // txt+="Error description: " + err.description + "\n\n";
                   // txt+="Click OK to continue.\n\n";
                   // alert(ids[i]+ err.description);  
                }                
            }
 
          //  map.AddShapeLayer(layer);                                       
          map.AddShapeLayer(layer);        
        }
        function ClearListings()
        {    
        if(map!=null){        
            for(i=1;i<map.GetShapeLayerCount();i++)
            {
                var mapLayer = map.GetShapeLayerByIndex(i);
                for(j=0;j<mapLayer.GetShapeCount();j++)
                {
                    var layerShape = mapLayer.GetShapeByIndex(j);
                    var type = layerShape.GetShapeType();
                    if(type == 'Point')
                    {
                        map.DeleteShapeLayer(mapLayer);
                        break;
                    }
                }               
           }
           }
       }
    function IsMapInListingView()
    {
        for(i=1;i<map.GetShapeLayerCount();i++)
        {
            var mapLayer = map.GetShapeLayerByIndex(i);
            for(j=0;j<mapLayer.GetShapeCount();j++)
            {
                var layerShape = mapLayer.GetShapeByIndex(j);
                var type = layerShape.GetShapeType();
                if(type == 'Point')
                {               
                    return true;
                    //alert(layerShape.GetID());
                }
            }               
        }
        return false;  
    }       
    function DivShow()
    {
        var d = document.getElementById('detailDiv');
        xVisibility(d,true);
    }
    function DivHide()
    {
        var d = document.getElementById('detailDiv');
        if(d!=null)
        {
            xVisibility(d,false);
        }
    }
    function showDetailsPage()
    {            
        window.location='Map Full Listing.aspx?listing=' + detailID;
    }
    function LoadListingDetails(location,beds,baths,img,id)
    {   
        detailID = id;
        document.getElementById('detail').onclick=showDetailsPage;
        document.getElementById('listingImage').src = img;
        document.getElementById('listingDetail').innerHTML = document.getElementById('listingDetail').htmlContent = '<b>' + location + '</b><br>Bedrooms : ' + beds + '<br>Bathrooms : ' + baths; 
    }
    function MapSetCity(cityId)
        {
           ClearListings();
           for(i=1;i<map.GetShapeLayerCount();i++)
           {
               var mapLayer = map.GetShapeLayerByIndex(i);
               for(j=0;j<mapLayer.GetShapeCount();j++)
               {
                   var layerShape = mapLayer.GetShapeByIndex(j);
                   if(layerShape.GetID()!=null)
                   {
                       var des = layerShape.GetDescription(); 
                       if(des.match(cityId))
                       {
                           if(selectedElementID != '')
                           {
                               var preSelectedShape = map.GetShapeByID(selectedElementID);
                               preSelectedShape.SetLineColor(new VEColor(0,150,100,0.0));
                               preSelectedShape.SetFillColor(new VEColor(0,150,200,0.0));
                           }
                           isZoomedView = true;                           
                           selectedElementID = layerShape.GetID();                            
                           //alert(selectedElementID);
                           var cityPoints = new Array();
                           var shape = map.GetShapeByID(layerShape.GetID());
                           cityPoints = shape.GetPoints();
                           map.SetMapView(cityPoints);
                           shape.SetLineColor(new VEColor(0,150,100,1.0));
                           shape.SetFillColor(new VEColor(0,150,200,0.3));
                       }
                   }
               }                
           }
           linkState = 2;
       }
       
       function LoadMapCityListing(mapCityName)
       {
            GetData('GetData/GetExternalUrl.aspx','url=getlistings&cityname='+mapCityName,'4'); 
       }
      function LoadMapCityFilteredListing(mapCityName)
       {
            GetData('GetData/GetExternalUrl.aspx','url=getlistings&cityname='+mapCityName+'&condofilterd=1','4'); 
       }
       
      function LoadMapCity2DaysListing(mapCityName)
       {                                                                 
            GetData('GetData/GetExternalUrl.aspx','url=getlistings&cityname='+mapCityName+'&newlisting=1','4'); 
       }
       function LoadMap2DaysListing()
       {                                                         
            GetData('GetData/GetExternalUrl.aspx','url=getlistings&newlisting=1','4'); 
       }
       function LoadNeibourhoods(lats,longs,titles)
       {
            if(lats.length>0)
            {
                map.Clear();
                var layer = new VEShapeLayer();
                for(i=0;i<lats.length;i++)
                {
                    cornerOne = new VELatLong(parseFloat(lats[i]),parseFloat(longs[i]));
                    var pushPin = new VEShape(VEShapeType.Pushpin, cornerOne);               
                    //pushPin.SetCustomIcon("images/listing_icon.gif");                  
                    pushPin.SetDescription(titles[i]);
                    layer.AddShape(pushPin);
                }
                map.AddShapeLayer(layer);
            }
       }
       
        function HideMap()
        {
            if(map==null)
              {
                GetMap();
              }
             map.Clear(); 
             document.getElementById('detailDiv').style.display='none'; 
            // document.getElementById('MyMap').style.display='none'; 
             document.getElementById('mappTable').style.display='none';        
        }  
        
        function ShowMap()
        {                        
             
             document.getElementById('detailDiv').style.display='block'; 
             document.getElementById('MyMap').style.display='block';             
             document.getElementById('mappTable').style.display='block';   
           //  alert( document.getElementById('MyMap').style.width);          
        }
        function xVisibility(e,s)
        {
            if(!(e=xGetElementById(e))) return null;
            var v = 'visible', h = 'hidden';
            if(e.style && xDef(e.style.visibility)) 
            {
                if (xDef(s)) 
                {
                    // try to maintain backwards compatibility (???)
                    if (xStr(s)) e.style.visibility = s;
                    else e.style.visibility = s ? v : h;
                }
                return e.style.visibility;
                // or...
                // if (e.style.visibility.length) return e.style.visibility;
                // else return xGetComputedStyle(e, 'visibility');
            }
            else if (xDef(e.visibility)) 
            { // NN4
                if (xDef(s)) 
                {
                    // try to maintain backwards compatibility
                    if (xStr(s)) e.visibility = (s == v) ? 'show' : 'hide';
                    else e.visibility = s ? v : h;
                }
                return (e.visibility == 'show') ? v : h;
            }
             return null;
        }
        function xGetElementById(e)
        {
            if(typeof(e)=='string') {
                if(document.getElementById) e=document.getElementById(e);
                else if(document.all) e=document.all[e];
            else e=null;
            }
            return e;
        }
        function xDef()
        {
            for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
            return true;
        }
        function xStr(s)
        {
            for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])!='string') return false;}
            return true;
        }
        function xNum()
        {
            for(var i=0; i<arguments.length; ++i)
            {
                if(isNaN(arguments[i]) || typeof(arguments[i])!='number')
                return false;
            }
            return true;
        }   
       

