﻿// JScript File

/////////////////////////////////////////////////////////////////////////////////
//This will get the Towns from the Server
var pointlcl = null;

function GetTowns() {
        ret2 = IDXService.GetTowns(GetTownsWebServiceCallBack,OnTimeOut, OnError); 	
        var busydiv = $get('SearchingDiv');
	    busydiv.style.visibility = "visible";	
}
 
function GetTownsWebServiceCallBack(Response)
{     
    if( pinid > 2) {
    	DeleteAllShape();
    	pinid = 1;
    	}
    for(var i = 0;i< Response.length;i++)
    {
        AddPushPinTown(Response[i], i);
        //AddRow(Response[i], i);        
               
    }        
    var busydiv = $get('SearchingDiv');
	busydiv.style.visibility = "hidden";                                
}

function ParseTown(TownName, Desc, pinidlcl, pinlatlnglcl) {
    var PopUpDesc;
    var DescInfo = Desc.split(",");
    //alert(DescInfo.toString());
    //alert(TownName);
    var Avgresult = CommaFormatted(CurrencyFormatted(DescInfo[11]));
    //Avgresult = CommaFormatted(Avgresult);
    PopUpDesc = "<table town='layout' summary='Town Info'><caption town='layout'>";
    PopUpDesc += TownName;
    PopUpDesc += "</caption><thead town='layout'><tr town='layout'><th town='layout' scope='col'>Beds</th><th scope='col'>Count</th><th town='layout' scope='col'>Ave Price</th></tr></thead>"
	PopUpDesc += "<tfoot><tr><th scope='row'>&nbsp;</th><td colspan='2'><a href='javascript:zoomtotown(";
    PopUpDesc += pinlatlnglcl;
    PopUpDesc += ");'>Zoom to Town</a></td></tr></tfoot><tbody town='layout'>";
    PopUpDesc += "<tr town='layout'><th town='layout' scope='row'>1</th><td>" + DescInfo[0] + "</td><td town='layout'>" + CommaFormatted(CurrencyFormatted(DescInfo[1])) + "</td></tr>";
    PopUpDesc += "<tr class='odd' town='layout'><th scope='row'>2</th><td town='layout'>" + DescInfo[2] + "</td><td>" + CommaFormatted(CurrencyFormatted(DescInfo[3])) + "</td></tr>";
    PopUpDesc += "<tr town='layout'><th town='layout' scope='row'>3</th><td town='layout'>" + DescInfo[4] + "</td><td>" + CommaFormatted(CurrencyFormatted(DescInfo[5])) + "</td></tr>";
    PopUpDesc += "<tr town='layout' class='odd'><th town='layout' scope='row'>4</th><td town='layout'>" + DescInfo[6] + "</td><td>" + CommaFormatted(CurrencyFormatted(DescInfo[7])) + "</td></tr>";
    PopUpDesc += "<tr town='layout'><th town='layout' scope='row'>5+</th><td town='layout'>" + DescInfo[8] + "</td><td>" + CommaFormatted(CurrencyFormatted(DescInfo[9])) + "</td></tr>";
    PopUpDesc += "<tr town='layout' class='odd'><th scope='row'>Total</th><td town='layout'>" + DescInfo[10] + "</td><td>" + CommaFormatted(CurrencyFormatted(DescInfo[11])) + "</td></tr>";
    PopUpDesc += "</tbody></table>"
//    PopUpDesc = "<table><td><tr style='color:Blue;'>" + TownName + "</tr></td>";
//    PopUpDesc += "<td><tr>Listings:" + DescInfo[10] + "</tr></td>";
//    PopUpDesc += "<td><tr>Ave Price: $" + Avgresult + "</tr></td>";
//    //PopUpDesc += "<td><tr>PinID:" + pinidlcl + "</tr></td>";
//    PopUpDesc += "<td><tr><a href='javascript:zoomtotown(";
//    PopUpDesc += pinlatlnglcl;
//    PopUpDesc += ");'>Zoom to Town</a></tr></td>";
//    PopUpDesc += "</table>";
return PopUpDesc;
}

//This function is used to Zoom into the Town level ad get Properties
function zoomtotown(townlat, townlng) {
    pointlcl = new VELatLong(townlat, townlng);
    map.SetZoomLevel(13);
    map.SetCenter(pointlcl);
    GetInfo();
}    

function DeleteAllShape()
      {
         map.DeleteAllShapes();
      }
      
function AddPushPinTown(point, index)
{
    var townpoint = new VELatLong(point.Latitude, point.Longitude);
	var shape = new VEShape(VEShapeType.Pushpin,townpoint );
	 var icon = "<div style='font-size:12px;font-weight:bold;border:solid 2px Black;background-color:#e0ffff;width:50px;'>" + point.Name + "</div>";      
	//Set the icon         
	shape.SetCustomIcon(icon);
	
	//Set the info box
	map.ClearInfoBoxStyles();
	
	
	//shape.SetTitle(point.Name);
	shape.SetDescription(ParseTown(point.Name, point.Description, pinid, townpoint));
	pinid++;
    townlayer.AddShape(shape);
    
}

//Add a Custom Push Pin with Right Click Menu
function AddPersonalPushPin() {
    if (usrAuthenticated) {
        alert("You can add you own Push Pin");
        }
        else {
        alert("Only Registered Users can add own pins");
    }
}

    


function AddRow(point, index)
{
    var tablediv = $get("page"); 
    
    var row = tablediv.insertRow(tablediv.rows.length);
    var idCell = row.insertCell(0);
    idCell.appendChild(document.createTextNode(index));
    
    var nameCell = row.insertCell(1);
    nameCell.appendChild(document.createTextNode(point.Name));
    
    point.Description
     var PopUpDesc;
    var DescInfo = point.Description.split(",");
    
    var ListingCell = row.insertCell(2);
    ListingCell.appendChild(document.createTextNode(DescInfo[10]));
    
    var AvePriceCell = row.insertCell(3);    
    var Avgresult = CurrencyFormatted(DescInfo[11]);
    Avgresult = CommaFormatted(Avgresult);
    AvePriceCell.appendChild(document.createTextNode("$" + Avgresult));
  
    var latCell = row.insertCell(4);
    latCell.appendChild(document.createTextNode(point.Latitude));
    latCell.style.visibility = "hidden";
    latCell.style.width = "0px";
    
    var lngCell = row.insertCell(5);
    lngCell.appendChild(document.createTextNode(point.Longitude));        
    lngCell.style.visibility = "hidden";
    latCell.style.width = "0px";
    $addHandler(row,"click", TownRowClick);
    
}

function TownRowClick(e)
{
   //Center the map where the thing was just clicked   
   map.PanToLatLong(new VELatLong(e.target.parentElement.cells[4].innerText,e.target.parentElement.cells[5].innerText));
   
}

