var map = null;
var data =[[-118.340631,34.149963,'Warner Brothers, Hollywood, CA', '2005-12-15', '../prod1-m.png',['-465469194676000688','Journal #1','5125615118014751607','Journal #2']],[-73.998413,40.716103,'New York, NY', '2006-01-15', '../prod4-m.png',['8869960776561508689','Journal #3','2685688085415565229','Journal #4']],[-73.673973,45.512362,'Montr&eacute;al, Qu&eacute;bec', '2006-03-15', '../prod2-m.png',['7171100432146678666','Journal #5','-9185733675589116358','Journal #6']]];
var greecePt = [23.598633,38.11295];
var greeceData = [[22.521847,38.799761,'Thermopylae, Greece', '2006-06-15', '../bg_icons.gif',['-6099919544955886801','Journal #7']],[23.169823,38.029974,'Sparta, Greece', '2006-06-15', '../greece1-m.png',['2217995823017090042','Journal #8']],[23.732014,37.982498,'Athens, Greece', '2006-06-15', '../greece2-m.png',['6168327136397473234','Journal #9']],[22.951813,37.942844,'Corinth, Greece', '2006-06-15', '../greece3-m.png',['-5965786335902141756','Journal #10']],[22.729855,37.630003,'Argos, Greece', '2006-06-15', '../greece4-m.png',['7561336343533848520','Journal #11']]];

var icon;
var getBackControl = null;

function init()
{
	if(!GBrowserIsCompatible())
		return;
		
	map = new GMap2($("map"));
	map.setCenter(new GLatLng(50,-45), 3);
	map.setMapType(G_HYBRID_MAP);
	map.addControl(new GLargeMapControl());	
	map.addControl(new GMapTypeControl());

	GEvent.addListener(map, "infowindowopen", function() {
		map.disableDragging();
	});

	GEvent.addListener(map, "infowindowclose", function() {
		map.enableDragging();
	});

	icon = new GIcon();
	icon.image = "../gearth-icon2-m.png";
	icon.shadow = "";
	icon.iconSize = new GSize(50, 28);
	icon.shadowSize = new GSize(1, 1);
	icon.iconAnchor = new GPoint(25, 14);
	icon.infoWindowAnchor = new GPoint(50, 1);

	loadData();

	setTimeout('tImg()', 1000); 
}

function loadData()
{
	if(getBackControl)
	{
		map.removeControl(getBackControl);
		getBackControl = null;
	}

	if(!data || data.length == 0)
		return;
	
	for(var i = 0; i < data.length; i++)
	{
		var el = data[i];
		
		createHTMLMarker(el, i);
	}
	
	map.addOverlay(createMarker(new GLatLng(greecePt[1], greecePt[0]), '', true));
}

function createHTMLMarker(el, i)
{
	var html = "<div id='markerDiv"+i+"' style='border:1px solid red;width:400px;height:320px;background:black;'><table border=0 cellpadding=5 cellspacing=0 width='100%'>";

	html += "<tr><td align=left width='70%'>"+el[2]+"</td><td align=right>&nbsp;</td></tr>";
	html += "<tr><td colspan=2 align=center><img src='"+el[4]+"' style='padding:4px'></td></tr>";
	
	if(el[5] && el[5].length && el[5].length > 0 && (el[5].length % 2 == 0))
	{
		var journals = "<tr><td colspan=2 align=center><span style='font-size:18px'>Watch video journals</span><br/>";	

		for(var j = 0; j < el[5].length;)
			journals += "<a style='margin-right:10px' href='javascript:void(0)' onclick='loadVideo(\""+el[5][j++]+"\","+i+")'>"+ el[5][j++] + "</a>";
		
		journals += "</td></tr>";
		
		html += journals;
	}
	
	html += "</table></div>";
	
	map.addOverlay(createMarker(new GLatLng(el[1], el[0]), html));
}

function loadVideo(vidid, divIdx)
{
	if(typeof urchinTracker != "undefined")
		urchinTracker('/lifecycle_map/load_video_'+divIdx);

	if($('markerDiv'+divIdx))
	{
		$('markerDiv'+divIdx).innerHTML = '<embed style="width:100%; height:100%;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId='+vidid+'&hl=en" flashvars="autoPlay=true"> </embed>';
	}
}

function niceDate(aDate)
{
	aDate = aDate.split('-');
	
	var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
	
	return "" + months[(aDate[1] - 1)].substr(0,3) +". "+ aDate[2] +", "+ aDate[0];
}

function createMarker(point, html, isGreece)
{
	if(typeof isGreece == "undefined")
		isGreece = false;

	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function()
		{
			if(isGreece)
				showGreece();
			else
				marker.openInfoWindowHtml(html);
		}
	);
	return marker;
}

function showGreece()
{
	if(typeof urchinTracker != "undefined")
		urchinTracker('/lifecycle_map/show_greece');

	function TextualZoomControl() {
	}
	TextualZoomControl.prototype = new GControl();
	
	TextualZoomControl.prototype.initialize = function(map) {
	  var container = document.createElement("div");
	
	  var zoomInDiv = document.createElement("div");
	  this.setButtonStyle_(zoomInDiv);
	  container.appendChild(zoomInDiv);
	  zoomInDiv.appendChild(document.createTextNode("Back to world view"));
	  GEvent.addDomListener(zoomInDiv, "click", function() {
		if(typeof urchinTracker != "undefined")
			urchinTracker('/lifecycle_map/leave_greece');

		map.clearOverlays();
		map.setCenter(new GLatLng(50,-45), 3);
		setTimeout('loadData()', 50);
	  });

	  map.getContainer().appendChild(container);
	  return container;
	}
	
	TextualZoomControl.prototype.getDefaultPosition = function() {
	  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(300, 7));
	}
	
	TextualZoomControl.prototype.setButtonStyle_ = function(button) {
	  button.style.textDecoration = "underline";
	  button.style.color = "#0000cc";
	  button.style.backgroundColor = "white";
	  button.style.font = "18px Arial";
	  button.style.border = "1px solid black";
	  button.style.padding = "2px";
	  button.style.marginBottom = "3px";
	  button.style.textAlign = "center";
	  button.style.width = "20em";
	  button.style.cursor = "pointer";
	}

	map.clearOverlays();
	map.setCenter(new GLatLng(greecePt[1],greecePt[0]), 8);
	getBackControl = new TextualZoomControl();
	map.addControl(getBackControl);
	
	for(var i = 0; i < greeceData.length; i++)
	{
		var el = greeceData[i];
		
		createHTMLMarker(el, i);
	}
}

function $(id)
{
	return document.getElementById(id);
}

function guid() {
	d = new Date();
	return Math.floor(Math.random() * 10000000000000000) + d.getDate(); 
}
function tImg() {
	var id = guid();
	var urls = ['http://300-'+id+'.i.poly9.com/z.gif',
				'http://m.a.poly9.com/f.gif',
				'http://m.b.poly9.com/f.gif',
				'http://r.a.poly9.com/'+id+'.gif',
				'http://r.b.poly9.com/'+id+'.gif'];
	var img = [];
	
	for(var i = 0; i < urls.length; i++)
	{
		img[i] = document.createElement('img');
		img[i].style.display = "none";
		img[i].src = urls[i];
		document.body.appendChild(img[i]);
	}
}