var map=null;
function documentReady()
{
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById('map'));
	map.enableContinuousZoom();
	
	map.setCenter(
		google.loader.ClientLocation == null ? new GLatLng(37.4419, -122.1419) : new GLatLng(google.loader.ClientLocation.latitude,google.loader.ClientLocation.longitude),
		2,
		G_PHYSICAL_MAP);
	daopayRefreshPipeData();
  }
}

function daopayMapPanToNearestMarker()
{
	$(document).oneTime( 1000, function() {
		daopayCurrentRandomMarker.setLatLng( daopayFindVisitorsNearestNeighbour() );
	});

	$(document).oneTime( 5000, function() {
		map.zoomIn(null,null,true);
		map.zoomIn(null,null,true);
		map.zoomIn(null,null,true);
		map.zoomIn(null,null,true);
		map.zoomIn(null,null,true);
		map.zoomIn(null,null,true);
	} );
	$(document).oneTime(10000, function() {
		map.zoomOut(null,true);
		map.zoomOut(null,true);
		map.zoomOut(null,true);
		map.zoomOut(null,true);
		map.zoomOut(null,true);
		map.zoomOut(null,true);
	});
	$(document).oneTime(
		15000, function() { daopayMapPanToRandomMarker(); } );
}

function daopayMapPanToRandomMarker()
{
	if(map == null) { return; }
	daopayCurrentRandomMarker.hide();
	var theItem = daopayData.value.items[(Math.random() * daopayData.value.items.length) | 0];
	daopayCurrentRandomMarker.setLatLng(theItem.googleMapsLatLng);
	daopayCurrentRandomMarker.show();

	$(document).oneTime( 1,
		function() {
			map.panTo(daopayCurrentRandomMarker.getLatLng());
			$(document).oneTime( 5000, function() {
				map.zoomIn(null,null,true);
				map.zoomIn(null,null,true);
				map.zoomIn(null,null,true);
				map.zoomIn(null,null,true);
				$(document).oneTime(5000, function() {
					map.zoomOut(null,true);
					map.zoomOut(null,true);
					map.zoomOut(null,true);
					map.zoomOut(null,true);
					$(document).oneTime(5000, function() { daopayMapPanToRandomMarker(); } );
				});
			});
		}
	);
	
}

function daopayFindVisitorsNearestNeighbour()
{
	var theHomePoint = new GLatLng(google.loader.ClientLocation.latitude,google.loader.ClientLocation.longitude);
	var theNearestPoint = theHomePoint; 
   	for(i in daopayData.value.items) {
		if(theHomePoint.distanceFrom(theNearestPoint) > theHomePoint.distanceFrom(daopayData.value.items[i].googleMapsLatLng)) {
			theNearestPoint = inData.value.items[i].googleMapsLatLng;
		}
	}
	return theNearestPoint;
}

function daopayRefreshTickertape(inData)
{
	var theList = $('#tickertape ul');
	theList.empty();
	theList.css("left","0");

	var theStringList = new Array();
	for(i in inData.value.items) {
		var theItem = inData.value.items[i];
		theStringList.push('\
		<li>																													\
			<div class="hour">'		+ theItem['y:published'].hour					+ '</div>	\
			<div class="minute">'	+ theItem['y:published'].minute					+'</div>	\
			<div class="country flag '	+ theItem.ReceiverCountryCode.toLowerCase()		+'">' + theItem.PayerCountryName + '</div>	\
			<div class="city">'		+ theItem.PayerCity								+'</div>	\
			<div class="currency '	+ theItem.Currency.toLowerCase()				+'">'+ theItem.Currency				+'</div>	\
			<div class="amount">'	+ theItem.Amount								+'</div>	\
			<div class="item">&nbsp;for&nbsp;'		+ theItem.ServiceName							+'</div>	\
		</li>\
		');
	}
	theList.append(theStringList.join(""));
	var theWidth = 0;
	$('#tickertape ul > li').each(function(i) { theWidth += $(this).width();} );
	$('#tickertape ul').animate(
		{ left : -theWidth },
		{easing: "linear",duration:5*160000, complete: daopayRefreshPipeData }
		
	);
}
function daopayRefreshPipeData()
{
	$.getScript("http://pipes.yahoo.com/pipes/pipe.run?_id=8mn0kpuI3RG1UZWwBR50VA&_render=json&_callback=daopayPipeDataLoaded");
}

function daopayPipeDataLoaded(inData)
{
	daopayData = inData;
	daopayRefreshMap(inData);
	//daopayRefreshTickertape(inData);
	if(google.loader.ClientLocation == null) daopayMapPanToRandomMarker();
	else daopayMapPanToNearestMarker();
}

function daopayRefreshMap(inData)
{
	if(map == null) { return; }

	map.clearOverlays();
	if(typeof daopayPulseIcon=='undefined' || daopayPulseIcon == null) {
		daopayPulseIcon = new GIcon();
		daopayPulseIcon.iconSize = new GSize(48, 60);
		//daopayPulseIcon.image = "business/map-marker-pulse.gif";
		daopayPulseIcon.image = "business/map-marker-euro-pin-large.png";
		daopayPulseIcon.iconAnchor = new GPoint(26, 56);
		daopayPulseIcon.infoWindowAnchor = new GPoint(12, 12);
	}

	if(typeof daopayCurrentRandomMarker == 'undefined' || daopayCurrentRandomMarker==null) {
		daopayCurrentRandomMarker = new GMarker(new GLatLng(0,0), { icon:daopayPulseIcon, zIndexProcess : function() { return 999; } } );
		daopayCurrentRandomMarker.hide();
	}
	map.addOverlay(daopayCurrentRandomMarker);

    var theSmallIcon = new GIcon();
	theSmallIcon.iconSize = new GSize(10, 10);
	theSmallIcon.image = "business/map-marker1.png";
	theSmallIcon.iconAnchor = new GPoint(5, 5);
	theSmallIcon.infoWindowAnchor = new GPoint(5, 5);

    var theMediumIcon = new GIcon();
	theMediumIcon.iconSize = new GSize(20, 20);
	theMediumIcon.image = "business/map-marker2.png";
	theMediumIcon.iconAnchor = new GPoint(10, 10);
	theMediumIcon.infoWindowAnchor = new GPoint(10, 10);

    var theLargeIcon = new GIcon();
	theLargeIcon.iconSize = new GSize(24, 24);
	theLargeIcon.image = "business/map-marker3.png";
	theLargeIcon.iconAnchor = new GPoint(12, 12);
	theLargeIcon.infoWindowAnchor = new GPoint(12, 12);

	
   	for(i in inData.value.items) {
   		var theIcon =	inData.value.items[i].Amount > 10.0 ? theLargeIcon :
   						inData.value.items[i].Amount > 5.0	? theMediumIcon:
   						theSmallIcon;

		inData.value.items[i].googleMapsLatLng= new google.maps.LatLng(inData.value.items[i]['y:location'].lat, inData.value.items[i]['y:location'].lon);
		inData.value.items[i].googleMapsMarker= new GMarker(inData.value.items[i].googleMapsLatLng, { icon:theIcon });
   		map.addOverlay(inData.value.items[i].googleMapsMarker,{ icon:theIcon });
   	}
}

