var http_request;
var townName;
var townId;

function getGigs(town_id, town)
{
//alert("going");
	townName = town;
	townId = town_id;
	http_request = false;
	
	document.getElementById("gigs_caption").innerHTML = "Gigs in " + town;

     if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
             http_request.overrideMimeType('text/xml');
         }
     } else if (window.ActiveXObject) { // IE
         try {
             http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
             try {
                 http_request = new ActiveXObject("Microsoft.XMLHTTP");
             } catch (e) {}
         }
     }

     if (!http_request) {
     
         return false;
     }
     
     //alert("done initializing");
     
     http_request.onreadystatechange = makeTable;
    
    //alert("opening connection");
    
     http_request.open('GET', '/actions/getgigs.php?per_page=10&town_id=' + town_id, true);
     http_request.send(null);

	//alert("done");
}

function makeTable() {
//alert(http_request.status);
try {
	if (http_request.status == 200) {
		if (http_request.readyState < 4) {
			document.getElementById("gigs_container").innerHTML = "<p>Getting gigs...</p>";
		}
		else {
			
			var output = "<table>";
			output += "<tr>" +
				"<th>Artist</th>" +
				"<th>Date</th>" +
				"<th>Venue</th>" +
				"</tr>";
			var lines = http_request.responseText.split("\n");
			var numLines = 0;	
			for (i = 0; i < lines.length; i ++) {
				var lineParts = lines[i].split("|");
				
				if (lineParts.length < 5) continue;
				
				numLines ++;
				
				output += "<tr>" +
					"<td>" + lineParts[1] + "</td>" +
					"<td>" + lineParts[2] + "</td>" +
					"<td><a href='/venue/" + lineParts[3] + "'>" + lineParts[4] + "</a></td>" +
					"</tr>";
			}
			
			output += "</table>";
			
			if (numLines == 10) {
				output += "<p id='more_gigs'><a href='/town/" + townId + "'>More gigs in " + townName + "...</a></p>";
			}
		
			document.getElementById("gigs_container").innerHTML = output;
		}	
	}
} catch (e) {}
}

function focusOnTown(town_id, town, longitude, latitude) {
	getGigs(town_id, town);
	focusOnPoint(longitude, latitude);
	
}
