﻿// JScript File
var map = null;
var count;

 function GetMap(id, width,height)
 {
        count = 0;
        //THIS SHOULD BE PUT IN SOMETHING ELSE
         //style="position:relative;width:550px;height:350px;"
         //Set the position, width, and height of the control
         var e = document.getElementById(id);
         e.style.position = 'relative';
         e.style.width = width + 'px';
         e.style.height = height + 'px';
         set_map_defaults(id,width,height); 
         
 }
function set_map_defaults(id ,width, height) 
{ 
        map = new VEMap(id);
        //map.DeleteAllShapes();
        map.LoadMap(new VELatLong(42.37, -71.07), 14 ,'r' ,false);
        //map.Resize(width + 'px',height + 'px');
        my_width = parseInt(width);
        my_height = parseInt(height);
        map.Resize(my_width,my_height);
         
        
}


//These are funcitons for the MLS areas 
function map_it(result)
{  
  //    map.Clear();
  if(result != null)
  {
    var result_array = result.split(',');
    points_counter = result_array.length - 1;
    for(i = 0; i < result_array.length; ++i)
    { 
        if(result_array[i] != '')
        {
           AJAX_Services.Return_Coords_And_MLS(result_array[i],map_control,ErrorHandler,TimeOutHandler);
        }
        
    }
  }
}

//Plots all the points
function plot_points(points)
{ 
    var points_array = points.split(',');
    for(i = 0; i < points_array.length; ++i)
    {
       if(points_array[i] != '')
        {
           AJAX_Services.Return_Coords_And_MLS(points_array[i],map_control,ErrorHandler,TimeOutHandler);
        }
    } 

}

//Map all the points at once
function map_all(result)
{ 
  map.Clear();
  if(result != null)
  {
    var result_array = result.split(',');
    points_counter = result_array.length - 1;
 
    for(i = 0; i < result_array.length; ++i)
    { 
        if(result_array[i] != '')
        {
        
           if(i == points_counter)
           { 
               AJAX_Services.Return_Coords_And_MLS(result_array[i],set_center,ErrorHandler,TimeOutHandler);
           }
           else
           { 
               AJAX_Services.Return_Coords_And_MLS(result_array[i],map_control_all,ErrorHandler,TimeOutHandler);
           }  
        }
        
    }
  }
}

//These are functions for the MLS areas
function map_it_single(result)
{
        //Call my service bitch
        AJAX_Services.Return_Coords_And_MLS(result,map_control_single,ErrorHandler,TimeOutHandler);
}

function ErrorHandler(result)
{
   var msg=result.get_exceptionType() + "\r\n";
   msg += result.get_message() + "\r\n";
   msg += result.get_stackTrace();
   //alert(msg);
}
function TimeOutHandler(result)
{
   //alert("Timeout :" + result);
}

function map_control_single(result)
{
//Address viewable
load_pins_single(result);
}

function map_control(result)
{
    //No Address Viewable
    load_pins(result);
}

function map_control_all(result)
{ 
    //Load al the pins
    load_all_pins(result);
}

function set_center(result)
{
    alert('in set center');
    //set_center_map(_lat,_long,14)
    
}

function load_all_pins(result)
{  
    var _lat = result[0];
    var _long = result[1];
    var _price = result[2];
    var _baths = result[3];
    var _bedrooms = result[4];
    var _squareft = result[5];
    var _mlsnum = result[6];
    var _streetnum = result[7];
    var _street = result[8];
    var point  = new VELatLong(_lat,_long);
    
    
    _price = formatCurrency(_price);
    
    var shape = new VEShape(VEShapeType.Pushpin,point);
    
    shape.SetTitle('<span class=\'view_details_link\'>' + _streetnum + ' ' + _street + '</span>');
    //shape.SetCustomIcon('<img src=\'http://www.bostoncondogroup.com/LookandFeel/Images/Boston_Condo_Icon.jpg\'/>');
    shape.SetDescription('<div style="width: 200px; background-color: white; color: grey">'+
         '<b>For Sale: </b>' + _price + '<br>' + 
         + _bedrooms + ' bd / ' + _baths + ' ba / ' + _squareft + ' sq. ft<br>' + 
         '<a href=\'http://www.bostoncondogroup.com/mls/mls.aspx?id=' + _mlsnum + '\'>View More Details!<br>' + 
         '<a href=\'#\' onclick=\"setbirdseye(' + _lat + ',' + _long + ');\">View Birdseye Image(SuperZoom)</a><br>' + 
    
         'MLS# ' + _mlsnum + '</div>');
    map.AddShape(shape);
    //map.AttachEvent("onclick",shapeInfo);
    //The reason we have to do this is we don't want to center all the pins
     
    //set_center_map(_lat,_long,14)
    
  
    
}   


//this is custom development for the condo pins Matt Powers 2.15.2008
function load_condo_pin(address)
{ 
//map.SetCenter(new VELatLong(42.37, -71.07));
//var e = document.getElementById('BCGMap');
//e.style.width = width + "px";
//e.style.height = height + "px";
//alert(document.documentElement.clientWidth);
//alert(document.documentElement.clientHeight);
//map.Resize(width,height);
//alert(address);
//ResizeMap(width,height);
//window.onload=function(){alert('hi')
 try
 {  
    map.Find(null, address, null, null, 0, 10, false, false, false, true, ProcessResults)
 }
 catch(e)
 {
    alert(e.message);
 }
 
 
 
}

function ProcessResults(layer, results, places, hasmore)
{
  CreatePin("Default", places[0].LatLong);
}

function CreatePin(type, point)
{
  if (point != 'Unavailable')
  {
    /*var icon = "<div style='font-size:12px;font-weight:bold;border:solid 2px Black;background-color:Aqua;width:200px'>";
    icon += type + ":" + point.Latitude + " : " + point.Longitude;
    icon += "<div>";
    var spec = new VECustomIconSpecification();
    spec.CustomHTML = icon;*/

    var pin = new VEShape(VEShapeType.Pushpin, point);
    //pin.SetCustomIcon(spec);
    map.AddShape(pin);
    map.SetCenterAndZoom(point, 14);
  }
}

function load_pins_single(result)
{
    var _lat = result[0];
    var _long = result[1];
    var _price = result[2];
    var _baths = result[3];
    var _bedrooms = result[4];
    var _squareft = result[5];
    var _mlsnum = result[6];
    var _streetnum = result[7];
    var _street = result[8];
    var point  = new VELatLong(_lat,_long);
    
    
    _price = formatCurrency(_price);
    
    var shape = new VEShape(VEShapeType.Pushpin,point);
    
    shape.SetTitle('<span class=\'view_details_link\'>' + _streetnum + ' ' + _street + '</span>');
    //shape.SetTitle('<span class=\'view_details_link\'>' + _streetnum + ' ' + _street + '</span>');
    //shape.SetCustomIcon('<img src=\'http://www.bostoncondogroup.com/LookandFeel/Images/Boston_Condo_Icon.jpg\'/>');
    shape.SetDescription('<div style="width: 200px; background-color: white; color: grey">'+
         '<b>For Sale: </b>' + _price + '<br>' + 
         + _bedrooms + ' bd / ' + _baths + ' ba / ' + _squareft + ' sq. ft<br>' + 
         '<a href=\'http://www.bostoncondogroup.com/mls/mls.aspx?id=' + _mlsnum + '\'>View More Details!<br>' + 
         '<a href=\'#\' onclick=\"setbirdseye(' + _lat + ',' + _long + ');\">View Birdseye Image(SuperZoom)</a><br>' + 
    
         'MLS# ' + _mlsnum + '</div>');
    map.AddShape(shape);
    
    //map.AttachEvent("onclick",shapeInfo);
    //The reason we have to do this is we don't want to center all the pins
     
    set_center_map(_lat,_long,14)
    
  
    
}   

function load_pins(result)
{ 
    var _lat = result[0];
    var _long = result[1];
    var _price = result[2];
    var _baths = result[3];
    var _bedrooms = result[4];
    var _squareft = result[5];
    var _mlsnum = result[6];
    var _streetnum = result[7];
    var _street = result[8];
    var point  = new VELatLong(_lat,_long);
    
    
    _price = formatCurrency(_price);
    
    var shape = new VEShape(VEShapeType.Pushpin,point);
    
    //shape.SetTitle('<span class=\'view_details_link\'>' + _streetnum + ' ' + _street + '</span>');
    cookyval = getCookie("Pins");
    
   
    if(cookyval != '')
    {
        shape.SetTitle('<span class=\'view_details_link\'>' + _streetnum + ' ' + _street + '</span>');
    
    }
    else
    {
        shape.SetTitle('<span class=\'view_details_link\'><b><a href=\'http://www.bostoncondogroup.com/mls/mls.aspx?id=' + _mlsnum + '\'>Login For Address</a></b></span>');
    }
    //shape.SetCustomIcon('<img src=\'http://www.bostoncondogroup.com/LookandFeel/Images/Boston_Condo_Icon.jpg\'/>');
    shape.SetDescription('<div style="width: 200px; background-color: white; color: grey">'+
         '<b>For Sale: </b>' + _price + '<br>' + 
         + _bedrooms + ' bd / ' + _baths + ' ba / ' + _squareft + ' sq. ft<br>' + 
         '<a href=\'http://www.bostoncondogroup.com/mls/mls.aspx?id=' + _mlsnum + '\'>View More Details!<br>' + 
         '<a href=\'#\' onclick=\"setbirdseye(' + _lat + ',' + _long + ');\">View Birdseye Image(SuperZoom)</a><br>' + 
    
         'MLS# ' + _mlsnum + '</div>');
    map.AddShape(shape);
    
    //map.AttachEvent("onclick",shapeInfo);
    //The reason we have to do this is we don't want to center all the pins
     
    set_center_map(_lat,_long,14)
    
  
    
}   

function setbirdseye(_lat,_long)
{
    if (map.IsBirdseyeAvailable())
    {
     var latLong = new VELatLong(_lat, _long);
     map.PanToLatLong(latLong);
     var be = map.GetBirdseyeScene();
     var id = be.GetID();
     map.SetBirdseyeScene(id);
    }
    else
    alert('not available');
            
}


function set_center_map(_lat,_long,zoom)
{
    
    map.SetCenterAndZoom(new VELatLong(_lat, _long), zoom);
}


function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    num = Math.floor(num/100).toString();
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
      num = num.substring(0,num.length-(4*i+3))+','+
      num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num);
}

function getCookie(NameOfCookie)
{

// First we check to see if there is a cookie stored.
// Otherwise the length of document.cookie would be zero.

if (document.cookie.length > 0)
{

// Second we check to see if the cookie's name is stored in the
// "document.cookie" object for the page.

// Since more than one cookie can be set on a
// single page it is possible that our cookie
// is not present, even though the "document.cookie" object
// is not just an empty text.
// If our cookie name is not present the value -1 is stored
// in the variable called "begin".

begin = document.cookie.indexOf(NameOfCookie+"=");
if (begin != -1) // Note: != means "is not equal to"
{

// Our cookie was set.
// The value stored in the cookie is returned from the function.

begin += NameOfCookie.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); }
}
return null;

// Our cookie was not set.
// The value "null" is returned from the function.

}