/**************************************************************************
Script:	jPart.BingList.js
Author:	SharejPoint.com
Purpose:  Plot sharepoint list field on a Bing map
Dependencies:
	jPointLoader.js has to be on the same page or form editor webpart
*************************************************************************/
//Add include extension
//Include CSS and JS
jP.include(['http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&mkt=en-us']);

myjPart.ShowConfigDefaults = [	
	["LookupSiteURL","Absolute path to lookup list site location","/"]
    ,["LookupListName","Lookup list name","LookupList"]
    ,["LookupMaxDisplay","Maximum List Items to Display","10"]
    ,["LookupViewName","View to use; blank to use default",""]
    ,["LookupCAML","Custom CAML (optional)",""]
    ,["MapWidth","Map Width","400"]
    ,["MapHeight","Map Height","400"]
    ,["MapZoomLevel","Map Zoom Level","3"]    
	];

myjPart.setjPartOptions({Plugin:{Name:'Bing List Map', Version:'2.0', Origin:'SharejPoint', Description:'Allows user with contributor rights to display contents of a SharePoint list on a Bing map', Developer:'Samir Sijercic'}});

var ZoomLEvel = 1;
var map = null;
var BingMapJpartProcessing = null;

$(document).ready(function() { //Wait page to load and then apply webpart logic
	if(myjParts[JpartProcessing] != null)
	{
		var thisWebpart = myjParts[JpartProcessing];
		
		BingMapJpartProcessing = JpartProcessing;
		
		try
		{
			//jPart user code goes here ---------------------------
			if(thisWebpart.Options.Local != null)
			{
				drawMap();
			}		
					
			//-----------------------------------------------------
		}catch(err){};
		JpartProcessing = JpartProcessing + 1;
	}
});

function drawMap()
{
	var thisWebpart = myjParts[BingMapJpartProcessing];
	var Options = thisWebpart.Options.Local;
	DataJSONObj= getjPointData(Options.LookupSiteURL, Options.LookupListName, Options.LookupViewName, Options.LookupCAML, Options.LookupMaxDisplay, null);
	var MapResults = new Array();
	$.each(DataJSONObj, function(idx, itemData)
	{
		var MapResult = {};
		MapResult['Description'] = $(itemData).attr('ows_LinkTitle');
		MapResult['Address'] = $(itemData).attr('ows_Address');
		MapResults.push(MapResult);
	});
	thisWebpart.MainDiv.append("<div id='myMap' style='position:relative; width:" + Options.MapWidth + "px; height:" + Options.MapHeight + "px;'></div>");
	ZoomLEvel = Options.MapZoomLevel;
	GetMap();
	$.each(MapResults, function(idx, itemData)
	{
		Geocode(itemData.Address)
	});
}
function getjPointData(siteURL, listName, viewName, cAML, maxDisplay, viewFields)
{
	var Data = null;
	try
	{
		var thisSPObj = jP.Lists.getList(siteURL, listName);
		thisSPObj.ViewFields=viewFields;
		if(viewName != null && viewName.length > 0)
		{	
			thisSPObj.getSPView(viewName);}
		else
		{
			thisSPObj.getSPItemsWithQuery(cAML, maxDisplay);
		}	
		Data = thisSPObj.JQueryData;		
	}
	catch(err)
	{
	}
	return Data;
}
				
function GetMap()
{
	map = new VEMap('myMap');
	map.LoadMap();
}

function Geocode(address)
{
	//alert('Attempting to Geocode: ' + address);
	map.Find(null, address, null, null, 0, 10, false, false, false, true, ProcessResults)
}

function ProcessResults(layer, results, places, hasmore)
{
	if(places != null)CreatePin("Default", places[0]);
	//alert("Latitude: " + places[0].LatLong.Latitude + " Longitude= " + places[0].LatLong.Longitude);
}

function CreatePin(type, point)
{
	if (point != 'Unavailable')
	{
		var pin = new VEShape(VEShapeType.Pushpin, point.LatLong);
		pin.SetTitle(point.Name);
		map.AddShape(pin);
		map.SetZoomLevel(ZoomLEvel)
	}
}
//End Webpart logic code -----------------------------------