﻿
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                         /*Map Search Section*/                                                       
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
   
   
    /*Map Variables*/
    var map;  
    var zoom;
    var center;
    var view;
    var bed;
    var url="";
    var paramList;
   
   /*Favorites Page Variables*/
     var b;
     var c;
      
    function FirstLoad()
    {       
       map = new VEMap("Map");
       var LA = new VELatLong(36.0725, -79.7922222); 
       map.LoadMap(LA, 13);      
       map.AttachEvent("onendzoom",GetListings);      
       map.AttachEvent("onendpan",GetListings);       
       GetListings();
    }
    
    function GetListings(e)
    { 
        if (map.GetMapStyle() == VEMapStyle.Birdseye || map.GetMapStyle() == VEMapStyle.BirdseyeHybrid)
        {
           $(".msg").css("color", "Red"); 
           $(".msg").html("No Property Found at this Zoom");
           map.Clear();           
        }        
        else
        {   
           $("#wMsg").show();  
             
           SetParam();            
            
           if($(".Rewrite").val() == "On")  
           {  
                  url = "";
                  url = "Pages/JSONHandler.ashx?";
                  GetUrl(paramList);                
           }
           else
           {
                  url = "";
                  url="JSONHandler.ashx?";
                  GetUrl(paramList);               
           } 
                    
            AJAXcall();                        
        } 
             
    }
    
    function AJAXcall()
    {          
        $.ajax
        ({                   
            type: "GET",
            url:  url,        
            success: ParseData,
            error:ReportError
            
         });   
    }
    
    function ParseData (res) 
    {     
         if(res != "")
         {        
           var listing = eval('(' + res + ')');                       
           BindMap(listing);
           $(".msg").css("color", "Green");
           $(".msg").html(listing.length+" Properties Found");
        } 
        else
        {
            $("#wMsg").hide();
            $(".msg").css("color", "Red");
            $(".msg").html("Sorry No Property Found");            
            map.Clear();          
        }       
    }  
    
    function BindMap(listing)
    {
        var head;
        var desc;
        var imgUrl;
        
        zoom = map.GetZoomLevel();
        center = map.GetCenter();  
                    
        map.Clear();                
        map.AddShapeLayer(GetProperties(listing));
        map.SetCenterAndZoom(center, zoom); 
        $("#wMsg").hide();
        
     } 
     
     
    /*Helper Functions Area*/ 
    
     
     function ReportError (e)
     {
       alert("Error Occured while Getting Data  " + e.responseText);
       $("#wMsg").hide(); 
     }
     
      function param (key, value)
      {
          this.key = key;
          this.value= value;        
      } 
      
      
      function SetParam()
      {
         view = map.GetMapView();    
         bed = $(".select").val();   
           
         paramList = new Array(5);                
         paramList[0]= new param("id", bed);
         paramList[1]= new param("nlat", view.TopLeftLatLong.Latitude);
         paramList[2]= new param("wlong", view.TopLeftLatLong.Longitude);
         paramList[3]= new param("slat", view.BottomRightLatLong.Latitude);
         paramList[4]= new param("elong", view.BottomRightLatLong.Longitude);
      }
            
      function GetUrl (paramList)
      {        
         for(var i=0; i<paramList.length; i++)
         {   
           if( i == 0)
           {              
             url += paramList[i].key+"="+paramList[i].value;                
           }
           else
           {
             url += "&"+paramList[i].key+"="+paramList[i].value; 
           }
         }
      }  
     
     
      function GetProperties (listing)
      {
        var sLayer = new VEShapeLayer(); 
        sLayer.SetClusteringConfiguration(VEClusteringType.Grid);   
           
        for(var i=0; i<listing.length; i++)
        {            
            var position = new VELatLong(listing[i].Latitude, listing[i].Longitude);
            if($(".Rewrite").val() == "On")
            {
               head  = "<a href='Property-Detail/"+listing[i].ListingId+".aspx'><img src='Pictures/"+listing[i].FileName+"' style='width:160px; height:100px; border:none'/></a>";
               desc = "<p><b>Price :</b> $"+listing[i].Price+" <b>  Bed: </b>"+listing[i].Bed+"</p><p>"+listing[i].StreetAddress+" "+listing[i].City+"</p><p><a href='Property-Detail/"+listing[i].ListingId+".aspx'>View Detail</a></p>";
               imgUrl = "Images/house-icon.gif";
            }
            else
            {
                head = "<a href='PropertyDetail.aspx?ListingId="+listing[i].ListingId+"'><img src='../Pictures/"+listing[i].FileName+"' style='width:160px; height:100px; border:none'/></a>";
                desc = "<p><b>Price :</b> $"+listing[i].Price+" <b>  Bed: </b>"+listing[i].Bed+"</p><p>"+listing[i].StreetAddress+" "+listing[i].City+"</p><p><a href='PropertyDetail.aspx?ListingId="+listing[i].ListingId+"'>View Detail</a></p>";
                imgUrl = "../Images/house-icon.gif";
            }  
                      
            var pin = new VEShape(VEShapeType.Pushpin, position);
            pin.SetTitle(head);
            pin.SetDescription(desc);
            pin.SetCustomIcon(imgUrl);
                        
            sLayer.AddShape(pin);     
        }    
        return sLayer;
      }
     
     
     
//////////////////////////Property Detail Page javascript Section////////////////////////
  
  var src;
  
  function MouseIn ()
  {
     src= $(".mainImg").attr("src");
     var lsrc=$(this).attr("src");
     $(".mainImg").attr("src", lsrc);
  }
  
  function MouseOut ()
  {              
    $(".mainImg").attr("src", src);
  }
  
  
  /*Function to open popup slide show window*/
   function OpenPopUp()
     {
        var vleft   = (screen.width  - 600)/2;
        var vtop    = (screen.height - 600)/2;       

        var url = $(".urlstr").attr('value');
        window.open(url,"SlideShow", "toolbar=no,menubar=no,width=600,height=600, left="+vleft+","+"top="+vtop);
        return false;
     }
     
     
//////////////////////////////////Slide Show Page Javascript///////////////////////////////////////////////////   
    
  function SlideShow() 
  {
        $(".Show").cycle({
           fx:        'fade', // one of: cover, uncover, fade, shuffle, zoom, scrollLeft, etc
           delay:    -2000,
           continuous:    0,
           direction: 'left',
           cssBefore: { opacity: 1 },
           animOut:   { opacity: 0 }
        });

    }
    
    function ScaleImage(obj)
    {       
         var w=460;
         var h= 318;   
         var img =  $(obj);
             var oh= img.attr('height');
             var ow = img.attr('width');
             var rh = oh / ow;
             var rw = ow / oh;
             oh = h;
             ow = oh * rw;
             if (ow > w) 
             {
	           ow = w;
	           oh = w * rh;                   
             }
             img.attr('height', oh);
             img.attr('width', ow);
         
         
            
//         var img = $('#SlideContainer').find('img'); 
//         for(var i = 0; i <img.length; i++)
//         {
//            var oh= img[i].height;
//            var ow = img[i].width;
//            var rh = oh / ow;
//            var rw = ow / oh;
//            oh = h;
//            ow = oh * rw;
//            if (ow > w) 
//            {
//	        ow = w;
//	        oh = w * rh;                   
//            }
//            img[i].height = oh;
//            img[i].width= ow;
//           }
    }  
  ///////////////////////////////////////////////Property List Page Javascript////////////////////////////
  
   function ShowOnMap(Lat, Long)
   {
     map = new VEMap("Map");
     var points = new VELatLong(parseFloat(Lat) , parseFloat(Long));      
     map.LoadMap(points, 13);
     
     imgUrl = "../Images/house-icon.gif";
     
      var pin = new VEShape(VEShapeType.Pushpin, points);
      pin.SetCustomIcon(imgUrl);      
      map.AddShape(pin);
      
      var newView = new VEMapViewSpecification(points, 16, 1000, -90, 0);      
      map.SetMapView(newView);
      map.SetMapStyle(VEMapStyle.Birdseye);
         
   }               
  
 /////////////////////My Favorites//////////////////////////////////
 function OpenPopup(url)
  {
     var vleft   = (screen.width  - 600)/2;
     var vtop    = (screen.height - 600)/2;       
     
     window.open(url,"SlideShow", "toolbar=no,menubar=no,width=600,height=600, left="+vleft+","+"top="+vtop);   
  }
  function OpenMapPopup(url)
  {
     var vleft   = (screen.width  - 600)/2;
     var vtop    = (screen.height - 600)/2;       
     
     window.open(url,"ShowOnMap", "toolbar=no,menubar=no,width=600,height=600, left="+vleft+","+"top="+vtop);   
  }
 //////////////////for tooltip Script start////////////////////////
     
      
 
        
            function ietruebody(){
            return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
            }

            function ddrivetip(thetext, thewidth, thecolor){            
            if (ns6||ie){
            if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
            if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
            tipobj.innerHTML=thetext
            enabletip=true
            return false
            }
            }

            function positiontip(e){
            if (enabletip){
            var nondefaultpos=false
            var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
            var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
            //Find out how close the mouse is to the corner of the window
            var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20
            var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20

            var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX
            var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY

            var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000

            //if the horizontal distance isn't enough to accomodate the width of the context menu
            if (rightedge<tipobj.offsetWidth){
            //move the horizontal position of the menu to the left by it's width
            tipobj.style.left=curX-tipobj.offsetWidth+"px"
            nondefaultpos=true
            }
            else if (curX<leftedge)
            tipobj.style.left="5px"
            else{
            //position the horizontal position of the menu where the mouse is positioned
            tipobj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px"
            pointerobj.style.left=curX+offsetfromcursorX+"px"
            }

            //same concept with the vertical position
            if (bottomedge<tipobj.offsetHeight){
            tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
            nondefaultpos=true
            }
            else{
            tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px"
            pointerobj.style.top=curY+offsetfromcursorY+"px"
            }
            tipobj.style.visibility="visible"
            if (!nondefaultpos)
            pointerobj.style.visibility="visible"
            else
            pointerobj.style.visibility="hidden"
            }
            }

            function hideddrivetip(){
            if (ns6||ie){
            enabletip=false
            tipobj.style.visibility="hidden"
            pointerobj.style.visibility="hidden"
            tipobj.style.left="-1000px"
            tipobj.style.backgroundColor=''
            tipobj.style.width=''
            }
            }

          
           
   function Prep(b, c)
   {
        
        var offsetfromcursorX=-115 //Customize x offset of tooltip
        var offsetfromcursorY=10 //Customize y offset of tooltip

        var offsetdivfrompointerX=-10 //Customize x offset of tooltip DIV relative to pointer image
        var offsetdivfrompointerY=0 //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).
        
          
        
        document.write(c) //write out tooltip DIV
        document.write(b) //write out pointer image            

        var ie=document.all
        var ns6=document.getElementById && !document.all
        var enabletip=false
        if (ie||ns6)
        var tipobj= document.getElementById("dhtmltooltip");

        var pointerobj=document.getElementById("dhtmlpointer");
    }        
            
            //document.onmousemove=positiontip
//////////////////for tooltip Script end////////////////////////

