var xmlHttp;
var t;

function addMachine(locationid, gameid, condition, pricepercredit, songspercredit) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?add=1";
	url += "&action=machine";
	url += "&locationid="+locationid;
	url += "&gameid="+gameid;
	url += "&condition="+condition;
	url += "&pricepercredit="+pricepercredit;
	url += "&songspercredit="+songspercredit;
	xmlHttp.onreadystatechange = updateMachines;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function deleteMachine(locationid, gameid, pricepercredit, songspercredit) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?delete=1";
	url += "&action=machine";
	url += "&locationid="+locationid;
	url += "&gameid="+gameid;
	url += "&pricepercredit="+pricepercredit;
	url += "&songspercredit="+songspercredit;
	xmlHttp.onreadystatechange = updateMachines;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function addVisitor(locationid) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?add=1";
	url += "&action=visitor";
	url += "&locationid="+locationid;
	xmlHttp.onreadystatechange = updateVisitors;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function deleteVisitor(locationid) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?delete=1";
	url += "&action=visitor";
	url += "&locationid="+locationid;
	xmlHttp.onreadystatechange = updateVisitors;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function addNRVisitor(locationid) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?add=1";
	url += "&action=nrvisitor";
	url += "&locationid="+locationid;
	xmlHttp.onreadystatechange = updateNRVisitors;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function deleteNRVisitor(locationid) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?delete=1";
	url += "&action=nrvisitor";
	url += "&locationid="+locationid;
	xmlHttp.onreadystatechange = updateNRVisitors;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}



function addPicture(locationid, pictureid) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?add=1";
	url += "&action=picture";
	url += "&locationid="+locationid;
	url += "&pictureid="+pictureid;
	xmlHttp.onreadystatechange = updatePictures;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function deletePicture(locationid, pictureid) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?delete=1";
	url += "&action=picture";
	url += "&locationid="+locationid;
	url += "&pictureid="+pictureid;
	xmlHttp.onreadystatechange = updatePictures;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function addComment(locationid, userid, content) {

	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}

	params = "add=1";
	params += "&action=comment";
	params += "&locationid="+locationid;
	params += "&userid="+userid;
	params += "&content="+encodeURIComponent(content);

	var url = "arcadelocations_ajax.php";
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange = updateComments;
	xmlHttp.send(params);
}

function deleteComment(locationid, commentid) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?delete=1";
	url += "&action=comment";
	url += "&locationid="+locationid;
	url += "&commentid="+commentid;
	xmlHttp.onreadystatechange = updateComments;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function addCountry(continentid, countryname) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?add=1";
	url += "&action=country";
	url += "&continentid="+continentid;
	url += "&countryname="+countryname;
	xmlHttp.onreadystatechange = updateCountries;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function addCounty(countryname, countyname) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?add=1";
	url += "&action=county";
	url += "&countryname="+countryname;
	url += "&countyname="+countyname;
	xmlHttp.onreadystatechange = updateCounties;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function addCity(countyid, cityname) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?add=1";
	url += "&action=city";
	url += "&countyid="+countyid;
	url += "&cityname="+cityname;
	xmlHttp.onreadystatechange = updateCities;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function searchGame(gameid, continentid, countryname, countyid, cityid) {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url = "arcadelocations_ajax.php";
	url += "?action=search";
	url += "&gameid="+gameid;
	url += "&continentid="+continentid;
	url += "&countryname="+countryname;
	url += "&countyid="+countyid;
	url += "&cityid="+cityid;
	xmlHttp.onreadystatechange = updateSearch;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function updateMachines() {
	if (xmlHttp.readyState == 1) {
		document.getElementById("arcadelocationmachines").innerHTML = "Updating...";
	}
	else if (xmlHttp.readyState == 4) {
		document.getElementById("arcadelocationmachines").innerHTML = xmlHttp.responseText;
	}
}

function updateVisitors() {
	if (xmlHttp.readyState == 1) {
		document.getElementById("arcadelocationvisitors").innerHTML = "Updating...";
	}
	else if (xmlHttp.readyState == 4) {
		document.getElementById("arcadelocationvisitors").innerHTML = xmlHttp.responseText;
	}
}

function updateNRVisitors() {
	if (xmlHttp.readyState == 1) {
		document.getElementById("arcadelocationnrvisitors").innerHTML = "Updating...";
	}
	else if (xmlHttp.readyState == 4) {
		document.getElementById("arcadelocationnrvisitors").innerHTML = xmlHttp.responseText;
	}
}

function updatePictures() {
	if (xmlHttp.readyState == 1) {
		document.getElementById("arcadelocationpictures").innerHTML = "Updating...";
	}
	else if (xmlHttp.readyState == 4) {
		document.getElementById("arcadelocationpictures").innerHTML = xmlHttp.responseText;
	}
}


function updateComments() {
	if (xmlHttp.readyState == 1) {
	}
	else if (xmlHttp.readyState == 4) {
		document.getElementById("arcadelocationcomments").innerHTML = xmlHttp.responseText;
	}
}

function updateCountries() {
	if (xmlHttp.readyState == 1) {
	}
	else if (xmlHttp.readyState == 4) {
		document.getElementById("arcadelocationcountries").innerHTML = xmlHttp.responseText;
	}
}

function updateCounties() {
	if (xmlHttp.readyState == 1) {
	}
	else if (xmlHttp.readyState == 4) {
		document.getElementById("arcadelocationcounties").innerHTML = xmlHttp.responseText;
	}
}

function updateCities() {
	if (xmlHttp.readyState == 1) {
	}
	else if (xmlHttp.readyState == 4) {
		document.getElementById("arcadelocationcities").innerHTML = xmlHttp.responseText;
	}
}

function updateSearch() {
	if (xmlHttp.readyState == 1) {
		document.getElementById("arcadelocationsearch").innerHTML = "Searching...";
	}
	else if (xmlHttp.readyState == 4) {
		document.getElementById("arcadelocationsearch").innerHTML = xmlHttp.responseText;
	}
}

function getMarkers() {


	var arcades = arcadeLocations[0].arcades;
	var markers = [];
	
	for(var i=0; i<arcades.length; i++) {

		var arcade = arcades[i];
		markers.push(createMarker(arcade));

	}

	return markers;
	
}

function createMarker(object) {

	var arcade = false;
	if(object["r1id"])  arcade = true;

	var id = object["id"];
	var n = object["n"];
	var p = object["p"];

	if(arcade) {
		var r1id = object["r1id"];
		var r2id = object["r2id"];
		var r3id = object["r3id"];
		var r4id = object["r4id"];
		var games = object["games"];
		var lastupdate = object["lastupdate"];
		
		var r1n = continent_items[r1id]["n"];
		var r3n = county_items[r3id]["n"];
		var r4n = city_items[r4id]["n"];

		var marker = new google.maps.Marker({
			position: new google.maps.LatLng(p[0], p[1]),
			title: n
		});
		google.maps.event.addListener(marker, "click", function() {
			var link = "<strong><a href=\"arcadelocations.php?locationid="+id+"\">"+n+"</a></strong><br />";
			link += "<a href=\"arcadelocations.php?cityid="+r4id+"\">"+r4n+"</a>, ";
			link += "<a href=\"arcadelocations.php?countyid="+r3id+"\">"+r3n+"</a>, ";
			link += "<a href=\"arcadelocations.php?countryname="+r2id+"\">"+r2id+"</a>, ";
			link += "<a href=\"arcadelocations.php?continentid="+r1id+"\">"+r1n+"</a><br />";
			link += "<strong>Last Updated:</strong> "+lastupdate+"<br />";
			link += "<br />";
			link += games+"";
			
			if(infoWindow) {
				infoWindow.close();
			}
			
			infoWindow = new google.maps.InfoWindow({
				content: link
			});
			
			infoWindow.open(map, marker);
		});
	}
	
	return marker;

}

function getMachineStyle(condition) {

	switch(condition) {

		case 5: {
			return "background: #00ff00;";
		}
		case 4: {
			return "background: #80ff00;";
		}
		case 3: {
			return "background: #ffff00;";
		}
		case 2: {
			return "background: #ff8000;";
		}
		case 1: {
			return "background: #ff0000;";
		}

		default: {
			return "";
		}
	}
}

var map;
var infoWindow;

function initialiseMap(latLng, zoom) {
	
	var myOptions = {
	  zoom: zoom,
	  center: latLng,
	  mapTypeId: google.maps.MapTypeId.HYBRID,
	  streetViewControl: true
	};
	map = new google.maps.Map(document.getElementById("map"), myOptions);
	var mcOptions = {gridSize: 50, maxZoom: 8};
	var markers = getMarkers();
	var mc = new MarkerClusterer(map, markers, mcOptions);
	google.maps.event.addListener(map, "click", function() {
		if(infoWindow) {
			infoWindow.close();
		}
	});
}

function arcadeLocationsHandleMouseEvent(event) {

	var latlng = event.latLng;
	
	if (latlng) {
		
		if(marker) {
			marker.setPosition(new google.maps.LatLng(latlng.lat(), latlng.lng()));
		}
		else {
			marker = new google.maps.Marker({
				position: new google.maps.LatLng(latlng.lat(), latlng.lng()),
				map: map
			});
		}
	}
}