var map;
var geocoder;

var marker = [];
var infowindow = [];
var yearIndex = 0;

var geocodes = [];
var index = 0;
var numLocations = 0;
var loadYearIterator = 0;

var interval = 0;

function loadScript() {
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
	document.body.appendChild(script);
}

function initialize() {
	loadYear();
}

//load locations for years
function loadYear() {		
	geocodes = [];
	index = 0;
	numLocations = 0;
	geocoder = new google.maps.Geocoder();
	var mapCenter = new google.maps.LatLng(29.840643899834436, 8.4375);
	var stylez = [
		{
			featureType: "all",
			stylers: [
				{ hue: "#000000" },
				{ saturation: -100 },
				{ lightness: -60 }
			]
		},
		{
			featureType: "landscape",
			elementType: "all",
			stylers: [
				{ hue: "#000000" },
				{ saturation: -100 },
				{ lightness: -10 }
			]
		},
		{
			featureType: "water",
			elementType: "all",
			stylers: [
				{ hue: "#000000" },
				{ saturation: -100 },
				{ lightness: 50 }
			]
		},
		{
			featureType: "all",
			elementType: "labels",
			stylers: [
				{ visibility: "simplified" }
			]
		}
	];
	var mapOptions = {
		zoom: 2,
		center: mapCenter,
		navigationControl: false,
		scrollwheel: false,
		disableDoubleClickZoom: false,
		draggable: false,
		streetViewControl: false,
	    mapTypeControl: false,
		mapTypeControlOptions: {
			mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'hiphop']
		}
	};
	map =  new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
	var styledMapOptions = {
		name: "Hip-Hop"
	}
	var jayzMapType = new google.maps.StyledMapType(stylez, styledMapOptions);
	map.mapTypes.set('hiphop', jayzMapType);
	map.setMapTypeId('hiphop');
	numLocations = _years.length;
	interval = setInterval( 'geocodeLocations(_years)', 600);
}

function geocodeLocations(locations) {
	//alert(index);
	if(index == _years.length) {
		clearInterval(interval);
		//alert("done = " + index);
	} else {
		fellowLocation = locations[index];
		geocoder.geocode( { 'address': fellowLocation }, setMarkers);
		index++;
	}
}

function storeGeocodes(results) {
	//alert("Results = " + results);
	if(results != null) {
		//alert("results = " + results[0].geometry.location);
		geocodes[index] = results[0].geometry.location;
	}
}

function setMarkers(results) {
	//alert("geocode");
	if(results != null) {
		var i = index;
		//alert(results[0].formatted_address);
		var image = new google.maps.MarkerImage(_templatePath + '/images/marker.png',
			new google.maps.Size(21, 34),
			new google.maps.Point(0,0),
			new google.maps.Point(0, 34));
		var shadow = new google.maps.MarkerImage(_templatePath + '/images/marker_shadow.png',
			new google.maps.Size(42, 34),
			new google.maps.Point(0,0),
			new google.maps.Point(0, 34));
			marker = new google.maps.Marker({
				position: results[0].geometry.location,
				map: map,
				icon: image,
				shadow: shadow,
				clickable: false
			});
			geocodes.push(marker);
	}
}

function onMouseOverEvent(id) {
	//alert(id + " " + geocodes[id]);
	geocodes[id].setIcon(_templatePath + '/images/marker_over.png');
}

function onMouseOutEvent(id) {
	//alert(id + " " + geocodes[id]);
	geocodes[id].setIcon(_templatePath + '/images/marker.png');
}
