var showCurrent = 1;
var showPast = 1;
var showFuture = 1;

var map;
	  var pins = [];
	  var locations = [];
//	  var locations = [
//											{l:new Microsoft.Maps.Location(43.788476,-79.556487),pid:0},
//											{l:new Microsoft.Maps.Location(43.788476,-79.447487),pid:1},
//											{l:new Microsoft.Maps.Location(43.764476,-79.547487),pid:2},
//											{l:new Microsoft.Maps.Location(43.786476,-79.556487),pid:3}
//								]
		var pinCount = 0;
		var currentZoom = 15;
		var mood = "";
		var msgs = [];


	  function GetMap()
      {   
		pinCount = locations.length;

for (i=0; i< locations.length ;i++ )
{
	locations[i].txt = "";

//		var pastIcon = new GIcon(); 
		pastIcon = "images/icon-past.png"; 
		pastIcon = "images/pin.png"; 
//		pastIcon.iconSize = new GSize(26, 26); pastIcon.iconAnchor = new GPoint(1, 1);  pastIcon.infoWindowAnchor = new GPoint(13, 13); 

//		var presentIcon = new GIcon(); 
		presentIcon = "images/pin.png"; 
//		presentIcon.iconSize = new GSize(25, 25); presentIcon.iconAnchor = new GPoint(1, 1);  presentIcon.infoWindowAnchor = new GPoint(13, 13); 

//		var futureIcon = new GIcon(); 
		futureIcon = "images/pin-black.png"; 
		futureIcon = "images/pin.png"; 
//		futureIcon.iconSize = new GSize(26, 26); futureIcon.iconAnchor = new GPoint(1, 1);  futureIcon.infoWindowAnchor = new GPoint(12, 16); 

//		cafeIcon.shadow = "http://preview.thebrandfactory.com/viva/v2/images/icon_waterside_shadow.png"; 
//		cafeIcon.shadowSize = new GSize(26, 26); 
		// Set up our GMarkerOptions object literal 
		

		var myicon;
		switch (locations[i].status)
		{
			case "past": myicon = pastIcon; break;
			case "active": myicon = presentIcon; break;
			case "coming": myicon = futureIcon; break;
			case "future": myicon = futureIcon; break;
			default: myicon = futureIcon; break;
		}
//		point2 = new GLatLng(locations[i][1], areaamenities[i][2]); 
		txt = "";
		txt = "<div class='gmap-popup' onmouseover='resetAnimation()' onmouseout='closeBox()'><div class='left'><img src='" + locations[i].logo +"'>"  +locations[i].description + "</div><div clas='right'><img width='77' src='" + locations[i].img +  "'></div></div>";
//		switch (locations[i].status)
//		{
//		case "past":
////			txt = "<div style='min-height: 150px;'><h2 class='gmap'>"+ locations[i].name + "</h2><div class='gmap'>" + commImage  + locations[i].description + "</div></div>";
//			txt = "<div class='gmap-popup'><div class='left'>" + locations[i].logo  +locations[i].description + "</div><div clas='right'>" + locations[i].image +  "</div></div>";
//			break;
//		case "active":
//			if (locations[i].logo) commImage = "<img src='"+ locations[i].logo + "' style='float: left;margin: 0 10px 0px 0px'>";
//			txt = "<table cellpadding='0' cellspacing='0' border='0' ><tr><td colspan='2'><a href='index.php?s=community-"+locations[i].id+"'><h2 class='gmap'>" + locations[i].name + "</h2></a></td></tr><tr><td width='210' align='left' valign='top'>" + commImage  + "</td><td valign='top'>" +locations[i].description + "</td></tr></table>";
//			break;
//		case "future":
//		case "coming":
//			if (locations[i].logo) commImage = "<img src='"+ locations[i].logo + "' style='float: left;margin: 0 10px 0px 0px'>";
//			txt = "<table cellpadding='0' cellspacing='0' border='0' ><tr><td colspan='2'><a href='index.php?s=community-"+locations[i].id+"'><h2 class='gmap'>" + locations[i].name + "</h2></a></td></tr><tr><td width='210' align='left' valign='top'>" + commImage  + "</td><td valign='top'>" +locations[i].description + "</td></tr></table>";
////			txt = "<div style=''><a href='index.php?s=community-"+locations[i].id+"'><h2 class='gmap'>" + locations[i].name + "</h2></a><div class='gmap'>" + commImage  + locations[i].description + "</div></div>";
//			break;
//		}
	locations[i].txt = txt;
	locations[i].icon = myicon;

}

          map = new Microsoft.Maps.Map(document.getElementById("map1"), 
                           {credentials: "AnxXpXYj-C6xL_2E_hceY1PqdfvgzShMhvBAXSs-L8fQDRZAZo9i5wTcuWmPomj3",
                            center: new Microsoft.Maps.Location(43.75675,-79.438322),
                            mapTypeId: Microsoft.Maps.MapTypeId.road,
                            showCopyright : false,
                            showMapTypeSelector: false,
                            zoom: 11});
		o = {visible: false,typeName: 'pin-color'};
		for (i=0; i<pinCount ;i++ )
		{
				pins[i] = new Microsoft.Maps.Pushpin(locations[i].l,o);
				map.entities.push(pins[i]);
		}
		Microsoft.Maps.Events.addHandler(map, 'viewchangeend', checkPins);


		 var restrictZoom = function ()
		{
		  if (map.getZoom() <= map.getZoomRange().min) 
		  {
			alert("You cannot zoom out beyond the current level!");
			map.setView({
			  'zoom':       map.getZoomRange().min,
			  'animate':    false
			});
		  }
		  else if (map.getZoom() >= map.getZoomRange().max) 
		  {
			  alert("You cannot zoom in beyond the current level!");
			map.setView({
			  'zoom':       map.getZoomRange().max,
			  'animate':    false
			});
		  }
		};
		Microsoft.Maps.Events.addHandler(map,'viewchangestart',restrictZoom);



		// Set your min/max zoom levels here
		map.getZoomRange = function ()
		{
		  return {
			max:      20,
			min:      8
		  };
		};
      }


	  function checkPins(forceRefresh) {
			x = map.getZoom();

			if (x != currentZoom || forceRefresh) {
						for (i=pinCount;i<pinCount * 2 ;i++ )
						{
							map.entities.removeAt(pinCount);
						}


			newpins = []; //{loc:null,pix:null,cnt:0,msg:"bla bla"};
			
			firstPin = 0;
			for (i=0; i<pinCount ; i++ )
			{
				if (!showCurrent && locations[i].status == "active") continue;
				if (!showPast && locations[i].status == "past") continue;
//				if (!showFuture && locations[i].status == "coming") continue;
//				if (!showFuture && locations[i].status == "future") continue;
				if (firstPin==0)
				{
					firstPin = 1;
					l = map.entities.get(i).getLocation();
					pix = map.tryLocationToPixel(l);
					newpins[0] = {loc:l,px:pix,cnt:1,icon:locations[i].icon,msg:locations[i].txt};
				} else {
					// check point against all previous PLACED pins
					// if it's close to either of them, merge with them
					// otherwise place a new pin
					placed = false;
					for (k=0;k<newpins.length ;k++ )
					{
						l = map.entities.get(i).getLocation();
						pix = map.tryLocationToPixel(l);
						res = isTooClose(newpins[k].px, pix)
						if (res) {
							newpins[k].cnt ++;
							newpins[k].msg += "<div style='margin-top: 20px; height: 20px; border-top: 1px dotted #aaa'>&nbsp;</div>" + locations[i].txt;
							newpins[k].icon = "images/pin.png";
							placed = true;
							break;
						} 
					}
					// if no pin found, place new pin
					if (!placed) newpins[newpins.length] = {loc:l,px:pix,cnt:1,icon:locations[i].icon,msg:locations[i].txt};
				}
			}

			msgs = [];
			for (i=0; i< newpins.length ; i++ )
			{
				pin = new Microsoft.Maps.Pushpin(newpins[i].loc, {visible: true, icon: newpins[i].icon, text: "" + newpins[i].cnt,typeName: 'pin-color'});
				Microsoft.Maps.Events.addHandler(pin, 'click', eval("generateFunc("+i+")"));
				Microsoft.Maps.Events.addHandler(pin, 'mouseover', eval("generateFunc("+i+")"));
				msgs[i] = newpins[i].msg;
				map.entities.push(pin);

			}
		  }
		  currentZoom = x;
	  }

	  function generateFunc(ex) {
			v = function(e) {
				displayInfobox(e, ex);
			}
			return v;
	  }

		var closetimeout = "";
		var closeinterval = "";
		var fadeLevel = 100;
         function displayInfobox(e1, e2)
         {
//			message2("<div style='background-color: #fff; width: 250px; max-height: 150px; overflow: auto; font-weight: normal; text-align: left; padding: 20px;'>" + msgs[e2] + "</div>");
			boxAtPosition(e1, msgs[e2] );
			clearTimeout(closetimeout);
			clearInterval(closeinterval);
			closetimeout = setTimeout("closeBox()", 2000);
         }                    

		 function closeBox() {
			 fadeLevel = 100;
			 closeinterval = setInterval("fadeBox()", 50);
		 }

		 function resetAnimation() {
			clearTimeout(closetimeout);
			clearInterval(closeinterval);
		
		 }

		 function fadeBox() {
			el = document.getElementById(messageid);
			fadeLevel = fadeLevel - 5;
			if (fadeLevel > 20)
			{
				opacity(el, fadeLevel);
			} else {
				opacity(el, 100);
				fadeLevel = 100;
				closeWait();
				clearInterval(closeinterval);
			}

		 }





	  function isTooClose(p1, p2) {
		if (Math.abs(p1.x - p2.x) < 10 && Math.abs(p1.y - p2.y) < 15) return true;

		return false;
	  }



function toggleCurrent() {
	showCurrent = 1 - showCurrent;
	checkPins(true);
}
function togglePast() {
	showPast = 1 - showPast;
	checkPins(true);
}
function toggleFuture() {
	showFuture = 1 - showFuture;
	checkPins(true);
}
