//-------------------------------------------------------------------------------------------
//  STATIC GOOGLE MAPS OBJECT
var adeMap = {
	categories: {}, //organize the categories by name. Icons and marker references are stored here
	theMap: '',        //reference to the actual map object
	url: parse_url(),

	initMap: function () {
		//start the map, add controls and set the zoom level
		adeMap.theMap = new GMap2($('map'), {size: new GSize(560, 420)});
		adeMap.theMap.addControl(new GMapTypeControl());
		adeMap.theMap.addControl(new GLargeMapControl());
		adeMap.theMap.setCenter(new GLatLng(52.369473, 4.894723), 13);

		adeMap.categories['festival-locations'] = {};
		adeMap.categories['festival-locations'].isShown = true;
		adeMap.categories['festival-locations'].markers = []
		adeMap.categories['festival-locations'].icon = new GIcon();
		adeMap.categories['festival-locations'].icon.image = '/img/festival_icon.png';
		adeMap.categories['festival-locations'].icon.iconSize = new GSize(34, 34);
		adeMap.categories['festival-locations'].icon.iconAnchor = new GPoint(16, 16);
		adeMap.categories['festival-locations'].icon.infoWindowAnchor = new GPoint(16, 16);

		adeMap.categories['felix-meritis'] = {};
		adeMap.categories['felix-meritis'].isShown = true;
		adeMap.categories['felix-meritis'].markers = []
		adeMap.categories['felix-meritis'].icon = new GIcon();
		adeMap.categories['felix-meritis'].icon.image = '/img/felixmeritis_icon.png';
		adeMap.categories['felix-meritis'].icon.iconSize = new GSize(49, 49);
		adeMap.categories['felix-meritis'].icon.iconAnchor = new GPoint(24, 24);
		adeMap.categories['felix-meritis'].icon.infoWindowAnchor = new GPoint(24, 24);

		adeMap.categories['d3'] = {};
		adeMap.categories['d3'].isShown = true;
		adeMap.categories['d3'].markers = []
		adeMap.categories['d3'].icon = new GIcon();
		adeMap.categories['d3'].icon.image = '/img/d3_icon.png';
		adeMap.categories['d3'].icon.iconSize = new GSize(34, 34);
		adeMap.categories['d3'].icon.iconAnchor = new GPoint(16, 16);
		adeMap.categories['d3'].icon.infoWindowAnchor = new GPoint(16, 16);

		adeMap.categories['iamsterdam'] = {};
		adeMap.categories['iamsterdam'].isShown = true;
		adeMap.categories['iamsterdam'].markers = []
		adeMap.categories['iamsterdam'].icon = new GIcon();
		adeMap.categories['iamsterdam'].icon.image = '/img/iamsterdam_icon.png';
		adeMap.categories['iamsterdam'].icon.iconSize = new GSize(34, 34);
		adeMap.categories['iamsterdam'].icon.iconAnchor = new GPoint(16, 16);
		adeMap.categories['iamsterdam'].icon.infoWindowAnchor = new GPoint(16, 16);

		adeMap.categories['record-stores'] = {};
		adeMap.categories['record-stores'].isShown = true;
		adeMap.categories['record-stores'].markers = []
		adeMap.categories['record-stores'].icon = new GIcon();
		adeMap.categories['record-stores'].icon.image = '/img/recordstores_icon.png';
		adeMap.categories['record-stores'].icon.iconSize = new GSize(34, 34);
		adeMap.categories['record-stores'].icon.iconAnchor = new GPoint(16, 16);
		adeMap.categories['record-stores'].icon.infoWindowAnchor = new GPoint(16, 16);

		adeMap.categories['stayokay'] = {};
		adeMap.categories['stayokay'].isShown = true;
		adeMap.categories['stayokay'].markers = []
		adeMap.categories['stayokay'].icon = new GIcon();
		adeMap.categories['stayokay'].icon.image = '/img/hotel-hostel_icon.png';
		adeMap.categories['stayokay'].icon.iconSize = new GSize(34, 34);
		adeMap.categories['stayokay'].icon.iconAnchor = new GPoint(16, 16);
		adeMap.categories['stayokay'].icon.infoWindowAnchor = new GPoint(16, 16);

		adeMap.categories['ahbr'] = {};
		adeMap.categories['ahbr'].isShown = true;
		adeMap.categories['ahbr'].markers = []
		adeMap.categories['ahbr'].icon = new GIcon();
		adeMap.categories['ahbr'].icon.image = '/img/hotel-hostel_icon.png';
		adeMap.categories['ahbr'].icon.iconSize = new GSize(34, 34);
		adeMap.categories['ahbr'].icon.iconAnchor = new GPoint(16, 16);
		adeMap.categories['ahbr'].icon.infoWindowAnchor = new GPoint(16, 16);

		adeMap.categories['presale'] = {};
		adeMap.categories['presale'].isShown = true;
		adeMap.categories['presale'].markers = []
		adeMap.categories['presale'].icon = new GIcon();
		adeMap.categories['presale'].icon.image = '/img/presale_icon.png';
		adeMap.categories['presale'].icon.iconSize = new GSize(34, 34);
		adeMap.categories['presale'].icon.iconAnchor = new GPoint(16, 16);
		adeMap.categories['presale'].icon.infoWindowAnchor = new GPoint(16, 16);

		//download the markerdata and call plotMarkers()
		if (adeMap.url.parts[2] == 'locations') {
			GDownloadUrl('/ajax/getmarkers.php?locations=' + adeMap.url.parts[0], adeMap.plotMarkers);
 		} else {
			GDownloadUrl('/ajax/getmarkers.php?site=' + adeMap.url.parts[0], adeMap.plotMarkers);
		}
		
		//minimize the effect of memory leaks
		window.onunload = GUnload;
	},

	//callback to plot the markers on the map
	plotMarkers: function (jsonData) {
		eval('var markerData = ' + jsonData);

		var marker;
		var markerPosition;

		//walk through the marker data, place the marker on the map and save a reference
		for (var i=0; i<markerData.length; i++) {
			markerPosition = new GLatLng(markerData[i].lat, markerData[i].lng)
			marker = new GMarker(markerPosition, adeMap.categories[markerData[i].category].icon);
			//marker = new GMarker(markerPosition);
			marker.adeInfo = {};
			marker.adeInfo.webtitle = markerData[i].webtitle;
			marker.adeInfo.category = markerData[i].category;
			marker.adeInfo.title = markerData[i].title;

			//open an infowindow when the marker is clicked
			GEvent.addListener(marker, 'click', function () {
				var container = document.createElement('div');
				container.id = 'infowindow';

				var boldThing = document.createElement('strong');
				boldThing.appendChild(document.createTextNode(this.adeInfo.title));

				var paragraph = document.createElement('p');
				paragraph.appendChild(boldThing);
				paragraph.appendChild(document.createElement('br'));

				var moreInfo = document.createElement('span');
				moreInfo.className = 'pseudolink';
				moreInfo.appendChild(document.createTextNode('More information'))
				eval('moreInfo.onclick = function () {adeMap.showInfo("' + this.adeInfo.webtitle + '", "' + this.adeInfo.category + '")}');

				paragraph.appendChild(moreInfo);
				container.appendChild(paragraph);

				this.openInfoWindow(container);
			});

			//add the marker to the map
			adeMap.theMap.addOverlay(marker);
			adeMap.categories[markerData[i].category].markers.push(marker);

			//TODO: figure out on which (if any) marker to center.
			//if the webtitle of the marker matches a part of the URL, center on it and open the infowindow
			if (marker.adeInfo.webtitle == adeMap.url.parts[adeMap.url.parts.length-1]) {
				adeMap.theMap.setCenter(marker.getPoint());
				GEvent.trigger(marker, 'click');
			}
		}
	},

	//show or hide all the markers from a certain category
	toggleCategory: function (category) {
		if (typeof adeMap.categories[category] != 'undefined') {
			if (adeMap.categories[category].isShown == true) {
				for (var i=0; i<adeMap.categories[category].markers.length; i++) {
					adeMap.categories[category].markers[i].hide();
				}

			} else {
				for (var i=0; i<adeMap.categories[category].markers.length; i++) {
					adeMap.categories[category].markers[i].show();
				}
			}
		}

		//toggle between true and false
		adeMap.categories[category].isShown = !adeMap.categories[category].isShown;
	},

	//get the information from the venue and place in the page
	showInfo: function (webtitle, category) {
		GDownloadUrl('/ajax/getvenueinfo.php?webtitle=' + webtitle + '&category=' + category, function (venueInfo) {
			if (!$('venueinfo')) {
				var theRow = document.createElement('div');
				theRow.className = 'row';

				var infoHolder = document.createElement('div');
				infoHolder.id = 'venueinfo';
				infoHolder.className = 'four_col_wide last striping';

				theRow.appendChild(infoHolder);

				$('left-col').appendChild(theRow);
			}

			$('venueinfo').innerHTML = venueInfo;
		});
	}
}

//-------------------------------------------------------------------------------------------
//  FUNCTION THAT PARSES THE URL
function parse_url() {
	var url = {};
	url.rawUrl = document.location.href.substring(7);
	url.parts = url.rawUrl.split('#')[0].split('/');
	url.hash = document.location.hash;
	url.domain = url.parts.shift();

	if (url.parts[0] == '') url.parts.shift();
	if (url.parts[url.parts.length-1] == '') url.parts.pop();

	return url;
}
