﻿
var mapWidth;
var mapHeight;
var map = null;
var _itemsLoaded = false;
var pinID = 1;
var _mapDirections = null;
var _mapDirections_veCourseLatLong = null;
var _tabContainer = null;
var _profile_SiteMembersAddress = null;
var r_lat = 40.70;

// Init
CheckFirefox()
addLoadEvent(GetMap);

function CheckFirefox()
{
    // If the browser is Firefox get the version number
    var ffv = 0;
    var ffn = "Firefox/"
    var ffp = navigator.userAgent.indexOf(ffn);
    if (ffp != -1) ffv = parseFloat(navigator.userAgent.substring(ffp + ffn.length));
    if (ffv >= 1.5) {
      Msn.Drawing.Graphic.CreateGraphic=function(f,b) { return new Msn.Drawing.SVGGraphic(f,b) }
    }
}

function addLoadEvent(func) 
{ 
    var oldonload = window.onload; 
    if (typeof window.onload != 'function') 
        { window.onload = func; } 
    else 
        { window.onload = function() 
            { oldonload(); func(); } 
    }
}

function GetMap() {
	// default latlong
    map = new VEMap('div_map');
    
    if(r_lat)
       map.LoadMap(new VELatLong(40.70,-76),10,'r',false);
    else
        map.DeleteAllPushpins();
    
    map.AttachEvent('onmodenotavailable', modeNotAvailable);
    map.AttachEvent('oninitmode', doModeChange);
    map.AttachEvent("onmouseover", onMouseOverCallback);
    
    GetItems();
}

function modeNotAvailable()
{
    alert('Viewing mode is not available');
}
function doModeChange()
{
  //alert('You are now in ' + mode +'.');*/
}

function GetItems()
{
	wsrvJTMaloney.CommunityList(onGetItems_Complete, onGetItems_Error);
}

function onGetItems_Complete(items)
{
  var a = new Array;
  var pinUrl;
  var latLong;
  var pinID;
    
	for(var i =0;i<items.length;i++)
  {
		try
      {
				vAddress = items[i].LatLong.split(",");
        latLong = new VELatLong(vAddress[0],vAddress[1]);
        
        pinUrl = "../UserFiles/images/flags/red/" + (i + 1)  + "L.gif";
        
				var pin = new VEShape(VEShapeType.Pushpin, latLong);
				pin.SetCustomIcon(pinUrl);
				pin.SetTitle(items[i].Title);
        //pinId = items[i].ID;
        var v = " <a href='detail.aspx?id=" + items[i].ID + "' >... more</a>";
        //var vDirections = "<a href=\"javascript:onShowPrintableDirections('" + vAddress[0] + "','" + vAddress[1] + "','" + items[i].Title + "');\">directions</a>";
        var vDirections = "<a href=\"javascript:onShowPrintableDirections2('" + items[i].Address2 + "');\">directions</a>";
        pin.SetDescription(items[i].DescriptionFirst100 + v + "<div style='float:right;'>" + vDirections + "</div>");
        
        map.AddShape(pin);
        
      }
      catch(ex)
      {
          throw ex;
      }
  }
  
	// community phone numbers
  var v = "<table cellpadding='0' cellspacing='0' border='0'  width='100%'";
    for(var i=0;i<items.length;i++)
    {
			if( (i % 2) == 0)
          v += "<tr bgcolor='#efeded'>";
      else
          v+= "<tr bgcolor='white'>";
      v+="<td align='left' valign='middle'><img src='../UserFiles/images/flags/red/" + (i + 1)  + "L.gif' border='0' /></td>";
      v+="<td align='left' valign='middle' width='100%' nowrap>&nbsp;&nbsp;<a href='detail.aspx?id=" + items[i].ID + "' onmouseover='panTo(\"" + items[i].LatLong + "\");' >" + items[i].Title + "</a></td>";
      v+="<td align='right' valign='middle' width='100%'>" + items[i].PhoneNumber + "&nbsp;&nbsp;</td>";
      v+= "</tr>";
    }
    v+= "</table>";
    $get('div_CommunityPhonenumbers').innerHTML = v;
    
    
  _itemsLoaded = true;
  //panTo(items[0].LatLong)
  map.PanToLatLong(new VELatLong(40.65,-75.3));
  panTo()
}
function onGetItems_Error(err)
{
    alert(err.Title + "," + err.Description);
}

function panTo(v)
{
	vAddress = v.split(",");
	map.PanToLatLong(new VELatLong(vAddress[0],vAddress[1]));
}

function onMouseOverCallback(e)
{
	
	if(e.elementID !=null)
	{
	}
}

function NavVisible(v)
{
    if(v)
        map.ShowDashboard();
    else
        map.HideDashboard()
}


//////////////////////////////////////////////////////////////////////////////
// Directions

var popMap = null;
function onShowPrintableDirections2(vAddress2)
{
   if(popMap != null)
    popMap = null;

		var vURL =	"http://maps.live.com/default.aspx?v=2&rtp=adr.~adr." + vAddress2;									// + "&sp=" + vAddress + "_Title_Description";
		
		popMap = window.open(vURL);
}
function onShowPrintableDirections(vLat, vLong, vTitle)
{
   if(popMap != null)
    popMap = null;

		var vURL =	"http://maps.live.com/default.aspx?v=2&rtp=adr.~adr." + v;//~pos." + trimWhiteSpace(vLat) + "_" + trimWhiteSpace(vLong) + "_" + vTitle;
    //var vURL =	"http://maps.live.com/default.aspx?v=2&rtp=adr.~pos." + trimWhiteSpace(vLat) + "_" + trimWhiteSpace(vLong) + "_" + vTitle;
		//var vURL2 =	"http://maps.live.com/default.aspx?v=2&rtp=adr.~pos.40.53_-75.43_SignagureHomes";
		//alert(vURL + "\r\n" + vURL2);
    popMap = window.open(vURL);
   
    ///http://maps.live.com/default.aspx?v=2&rtp=adr.One%20Microsoft Way,%20Redmond,%20WA%2098052~pos.45.23423_-122.1232_MyPlace
}


//http://viavirtualearth.com/wiki/

function trimWhiteSpace(v)
{
	return v.replace(/^\s+|\s+$/g,"");
}

Sys.Application.notifyScriptLoaded();



/*<!--http://maps.live.com/?
v=2&
rtp=pos.qn6wkk8rhp1d_
701%20White%20Horse%20Road%20Suite%205%20Voorhees%20NJ%2008043~
pos.qmk3cg8qpfsp_2387%
Kings%20Highway%2c%20Woolwich%20Township%2c%20NJ%2008085

http://maps.live.com/?
v=2&
rtp=pos.qn6wkk8rhp1d_
701%20White%20Horse%20Road,Voorhees,NJ,08043~
pos.qmk3cg8qpfsp__2387%
4%20overlook%20drive,corinth,ny

http://maps.live.com/?v=2&rtp=pos.qn6wkk8rhp1d_701%20White%20Horse%20Road%2c%20Voorhees%2cNJ%2c08043~pos.qmk3cg8qpfsp_2387%20Kings%20Highway%2c%20Woolwich%20Township%2c%20NJ%2c%2008085%2c

%20 = space
%2c = comma -->*/


