/**
 * Copyright 2006 Jeffrey Palm.
 */

Jeff.Schedule = Class.create();
Jeff.Schedule.prototype = {

	initialize: function() {

	},

	showBand: function(band) {
		//
		// First clear everything
		//
		this.clearOverlays(band);
		//
		// Make the request
		//
		var thiz = this;
		var url = '/cgi-bin/pollstar2.pl';
		var params = 'band=' + band;
		var callback = function(res) {
			if (!res) return;
			var doc = res.responseXML.documentElement;
			
			var results = doc.getElementsByTagName("result");
			var result = results[0];
			var query = result.getAttribute("query");

			// Put the name of the band
			var wroteBandName = false;
			
			//
			// For every show make a marker
			//
			var bs = document.getElementById("bandSchedule");
			bs.innerHTML = "";
			var p = $n("p",bs);
			var ul = $n("table",p);
			ul.border = "0";
			ul.cellSpacing = "2";
			ul.cellPadding = "2";
			var shows = doc.getElementsByTagName('show');
			for (var i=0; i<shows.length; i++) {

				var show = shows[i];
				var tr = $n("tr",ul);

				var date = show.getAttribute("date");
				var city = show.getAttribute("city");
				var cityLink = show.getAttribute("cityLink");
				var venue = show.getAttribute("venue");
				var venueLink = show.getAttribute("venueLink");

				var linkStart = "http://pollstar.com/tour/";

				var show = shows[i];
				var artist = show.getAttribute("artist");
				var artistLink = show.getAttribute("artistLink");
				var googleLink = "http://local.google.com/?q=" + urlencode(venue+" "+city);
				if (!wroteBandName) {
					document.getElementById("bandName").innerHTML = "<a target='_' href='"+linkStart+artistLink+"'>"+band+"</a>";
					wroteBandName = true;
					//
					// Make the links in the upper right active now
					//
					for (var i=0; i<TYPES.length; i++) {
						var type = TYPES[i];
						var link = "<a href='" + type + "/?band=" + urlencode(band) + ">" + type + "</a>";
						document.getElementById(type + "Link").innerHTML = link;
					}
				}

				var td;

				td = $n("td",tr);
				td.innerHTML = date;

				td = $n("td",tr);
				td.innerHTML = city;

				td = $n("td",tr);
				td.innerHTML = "<a target='_' href='"+linkStart+venueLink+"'>"+venue + "</a>";

				td = $n("td",tr);
				td.innerHTML = "[<a target='_' href='"+googleLink+"'>google</a>]";

				db("Found " + thiz.em(band));
			}
			
		}
		db("Looking up " + this.em(band));
		new Ajax.Request (url,
											{   method: 'get', 
													parameters: params, 
													onSuccess: function(res) {try{callback(res);}catch(e){alert(e);}}
													});		
	},

	em: function(s) {
		return "<b>" + s + "</b>";
	},

	clearOverlays: function(band) {
		document.getElementById("bandName").innerHTML = "Searching for " + this.em(band) + "...";
		document.getElementById("bandSchedule").innerHTML = "";
		//
		// Also clear the links in the upper right corner
		//
		for (var i=0; i<TYPES.length; i++) {
			var type = TYPES[i];
			document.getElementById(type + "Link").innerHTML = type;
		}
	},
	
	/**
	 * View interface
	 */
	viewBand: function(band) {
		this.showBand(band);
	},

	viewName: function() {
		return "schedule";
	},

	toString: function() {
		return "Schedule";
	}


};

