/*===================================================================================================================================================
This function allows us to add many things to the onload event
Based on Simon Willison's Executing JavaScript on page load - http://simon.incutio.com/archive/2004/05/26/addLoadEvent
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
//===================================================================================================================================================

addLoadEvent(initiate);

function initiate() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
    //map.setCenter(new GLatLng(51.532812, -0.110056), 16);
	//map.openInfoWindow(map.getCenter(),document.createTextNode("BigKid lives is here!"));
	
	var icon = new GIcon();
	icon.image = "http://www.bigkidlondon.com/images/bigkid_pin.png";
	icon.shadow = "http://www.bigkidlondon.com/images/bigkid_shadow.png";
	icon.iconSize = new GSize(80, 60);
	icon.shadowSize = new GSize(80, 60);
	icon.iconAnchor = new GPoint(24, 60);
	icon.infoWindowAnchor = new GPoint(0, 70);
	
	thingy();
  }

	function thingy () {
		map.clearOverlays();
		map.setCenter(new GLatLng(51.531800, -0.111050), 16);
		var center = map.getCenter();
		var marker = new GMarker(center, {icon: icon, title: "BigKid", draggable:true});
		map.addOverlay( marker );

		GEvent.addListener(marker, "dragstart", function() {
		  map.closeInfoWindow();
		  });

		GEvent.addListener(marker, "dragend", function() {
		map.panTo( marker.getPoint() );
		var theNum = Math.floor(Math.random()*10);
		var center = marker.getPoint();
		if( center == "(51.532882176446584, -0.10634422302246094)" ){
			var text = "This is where Pret is, ask for Alex to get free coffee!";
		}else{
			var text = getText( theNum );
		}
		  marker.openInfoWindowHtml('<strong style="text-align: center; color: #33cc00; margin: 20px auto 0 auto; display: block;">'+text+'</strong>' ); 
			window.setTimeout(function() { 
				map.closeInfoWindow();
				thingy();
			}, 2000);
		  });
	}
	
	function getText( number ){
		switch( number )
		{
		case 1:
		  return "...um no! We are not here!";
		  break    
		case 2:
		  return "...this isn't where we live!";
		  break
		case 3:
		  return "What are you doing, we don't live here!";
		  break    
		case 4:
		  return "I think we had the location right the first time :)";
		  break
		case 5:
		  return "I think there is a Pret around here...";
		  break    
		case 6:
		  return "It's not that easy to move our offices";
		  break
		case 7:
		  return "We are only here on Friday lunch times";
		  break    
		case 8:
		  return "Please don't move BigKid, it's not nice!";
		  break
		default:
		  return "I know what is here, and it's not BigKid";
		}
		
	}
}