BusCms.addWidget({
	widgetType : 'buscms.com.widgets.stopsmap',
	widgetLocation : 'http://www.buscms.com/widgets/v2/stopsmap/w.js',
	defaultTemplateLocation : 'http://www.buscms.com/widgets/v2/stopsmap/default-template.txt',
	domid : qsToJson().domid,
	isInstance : ( (typeof qsToJson().domid != undefined) && qsToJson().domid!=null && qsToJson().domid!=''),
	clientid : 'netescape',
	arguments : qsToJson(),
	onLoad : function() {this.loadInitial(); this.onLoadCalled=true;},
	onLoadCalled : false,
	mapData:{},
	kmlUrl: "http://www.buscms.com/brightonbuses_all_stops.kmz?format=kml&v=9",
	registeredDepartureboard: {},
	showDepartureBoardInInfoWindow: false,
	showServicesInInfoWindow: true,
	showInfoWindow:true,
	zoom:15,
	map: {},
	infowindow: {},
	
	loadInitial : function()
	{
	
		// kmlUrl: "http://www.buscms.com/api/rest/html/stopsmap.aspx?format=kml&v=2.1",
		
		// create a div to hold the map
		$("#" + this.domid).html('<div class=\"stopsmap-gmap\"></div>');
		
		// instatiate a google map
		var myLatlng = new google.maps.LatLng(this.arguments.lat, this.arguments.lng);
		var myOptions = { zoom: 15, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControl: false };
    		this.map = new google.maps.Map($("#" + this.domid + " .stopsmap-gmap")[0], myOptions);	
	
		this.showMap();		
	
		this.infowindow = new google.maps.InfoWindow(); 
		
	},
	
	showMap : function()
	{
		$('.loading').show();
		this.map.setCenter(new google.maps.LatLng(this.arguments.lat,this.arguments.lng));
	
  		var l = new google.maps.KmlLayer(this.kmlUrl + '&clientid=' + this.arguments.clientid, {preserveViewport:true, suppressInfoWindows: true});

		try
		{
		this.kmlLayer.setMap(null);
		}
		catch (err)
		{
		
		}
		this.kmlLayer = l;
		this.kmlLayer.setMap(this.map);
		
   		// Add listener to kml layer
		google.maps.event.addListener(this.kmlLayer, 'click', function(e) { this.handleClick(e) }.bind(this) );	
		$('.loading').hide();
	},
	
	handleClick : function(kmlEvent) 
	{
		var text = kmlEvent.featureData.name;
		var desc = kmlEvent.featureData.description;
		var stopid = kmlEvent.featureData.snippet;
		var _naptancode = desc.substring(desc.indexOf("{")+1, desc.indexOf("}"));
		if (_naptancode=="")
		{
			try
			{
				var a = $(desc+'');
				_naptancode = a.text();
			}
			catch(err)
			{
			
			}
		}
		this.infowindow.close();
		
		if (this.showInfoWindow==true)
		{
			//desc = desc.replace('div', 'span');
			var infowindowcontent = "<div class='g-routemap-infoWindowContent'><strong>" + text + "</strong>";
			infowindowcontent+= "<img src=\"http://www.buscms.com/shim.gif\" width=\"500\" height=\"1\" /><img src=\"http://www.buscms.com/shim.gif\" width=\"1\" height=\"100\" align=\"left\" /><br />";
			infowindowcontent+= "(text " + _naptancode + " to 84268 for <a href=\"/travel/text.aspx\">times of next bus</a>)";
			
			if (this.showServicesInInfoWindow==true)
			{
				//infowindowcontent+=desc;
			}
			infowindowcontent+="</div>";
			
			this.infowindow.setOptions({
				content: infowindowcontent,
				position: kmlEvent.latLng
			});
		}
		
		this.infowindow.open(this.map); 
		this.updateRegisteredDepartureBoard(stopid);
	},
	
	
	registerDepartureboard : function(departureboard)
	{
		registeredDepartureboard = departureboard;
	},
	
	updateRegisteredDepartureBoard : function(stopid)
	{
		
		if (typeof this.registeredDepartureboard != 'undefined' && this.registeredDepartureboard.onLoadCalled==true)
		{
			this.registeredDepartureboard.loadStopTimesWithCallback(stopid, this, this.registeredDepartureBoardUpdated);

		}
	},
	
	registeredDepartureBoardUpdated : function(data)
	{
		if (this.showDepartureBoardInInfoWindow==true)
		{
			var newcontent = $('.g-routemap-infoWindowContent').html() + '<br /><br />Services that stop here:<br />' + $('#' + this.registeredDepartureboard.domid + ' .services').html();
			$('.g-routemap-infoWindowContent').html(newcontent);
		}
	},
	
	openStop : function(stopid, stopname, naptancode)
	{
	
		this.infowindow.close();
		
		if (this.showInfoWindow==true)
		{
			//naptancode = naptancode.replace('div', 'span');
			var infowindowcontent = "<div class='g-routemap-infoWindowContent'><strong>" + stopname + "</strong>";
			infowindowcontent+= "<img src=\"http://www.buscms.com/shim.gif\" width=\"500\" height=\"1\" /><img src=\"http://www.buscms.com/shim.gif\" width=\"1\" height=\"100\" align=\"left\" /><br />";

			infowindowcontent+= "(text " + naptancode + " to 84268 for <a href=\"/travel/text.aspx\">times of next bus</a>)";
			
			if (this.showServicesInInfoWindow==true)
			{
				//infowindowcontent+=desc;
			}
			infowindowcontent+="</div>";
			
			this.infowindow.setOptions({
				content: infowindowcontent,
				position: this.map.getCenter()
			});
		}
		
		this.infowindow.open(this.map); 

	}
	
	
});

