function Pins(container, templates) {
	this.$container = $(container);
	this.$templates = {};
	for (var key in templates) {
	  this.$templates[key] = $(templates[key]);
	  this.$templates[key].hide();
	}
}

Pins.prototype = $.extend(Pins.prototype, {
	create: function(x, y, title, klass) {
		var realx = Math.floor(x * 800) - 9;
		var realy = Math.floor(y * 600) - 33;
		var pin = document.createElement("a");
		var $pin = $(pin);
		if (!klass || !this.$templates[klass]) klass='A';
		$pin.html(this.$templates[klass].html());
		$pin.addClass("marker");
		$pin.css("left", realx);
		$pin.css("top", realy);
		$pin.attr("title", title);
		$pin.attr("href", "http://www.last.fm/music/"+escape(title));
		this.$container.append($pin);
	},

	delete_all: function() {
		this.$container.empty();
	},

	fit_all: function() {}
});

function DeepZoomPinHandler(viewer, templates) {
	this.viewer = viewer;
	this.$templates = {};
	for (var key in templates) {
	  this.$templates[key] = $(templates[key]);
	  this.$templates[key].hide();
	}
	this.overlays = [];
}

DeepZoomPinHandler.prototype = $.extend(DeepZoomPinHandler.prototype, {
	create: function(x, y, title, klass) {
		var realx = x+0.0001;
		var realy = y * 0.75;
		var point = new Seadragon.Point(realx, realy);
		var placement = Seadragon.OverlayPlacement.BOTTOM;
		var pin = document.createElement("img");
		var $pin = $(pin);
		if (!klass || !this.$templates[klass]) klass='A';
		pin.src = $("img", this.$templates[klass])[0].src;
		// $pin.attr("href", "http://www.last.fm/music/"+escape(title));
		this.viewer.drawer.addOverlay(pin, point, placement);
		this.overlays.push({x: realx, y: realy});
	},

	delete_all: function() {
		this.viewer.drawer.clearOverlays();
		this.overlays = [];
	},

	fit_all: function() {
		if (this.overlays.length < 1) return;
		/* find bounds of current overlays shown */
		var minx = this.overlays[0].x;
		var miny = this.overlays[0].y;
		var maxx = minx;
		var maxy = miny;
		var i;
		for (i=1; i<this.overlays.length; i++) {
			var x = this.overlays[i].x;
			var y = this.overlays[i].y;
			if (x < minx) minx = x;
			if (y < miny) miny = y;
			if (x > maxx) maxx = x;
			if (y > maxy) maxy = y;
		}
		/* don't zoom in more than 1:1 */
		if (minx == maxx) {
			var cs = this.viewer.viewport.getContainerSize();
			minx -= (cs.x/20000.0) / 2;
			maxx += (cs.x/20000.0) / 2;
		}
		if (miny == maxy) {
			var cs = this.viewer.viewport.getContainerSize();
			miny -= (cs.y/20000.0) / 2;
			maxy += (cs.y/20000.0) / 2;
		}
		/* add a small border */
		minx -= 0.01;
		miny -= 0.025;
		maxx += 0.01;
		maxy += 0.025;
		var w = maxx-minx;
		var h = maxy-miny;
		rect = new Seadragon.Rect(minx, miny, w, h);
		this.viewer.viewport.fitBounds(rect);
	}
});


function progress_marker_visibility(b) {
	var marker = $("#progress");
	marker.css("display", b ? "inline" : "none");
}

function show_artist_on_map(name, username) {
	if ((name == '' || name == null) && (username == '' || username == null)) {
		alert('You must specify the name of the artist or user you are looking for.');
		return;
	}

	progress_marker_visibility(true);
	pins_on_map.delete_all();

	var urlroot = "http://sixdegrees.hu/last.fm/get_coordinates.php?";
	var url = urlroot;
	var single = true;

	if (name != '' && name != null) 
		url += "name="+encodeURIComponent(name);
	else {
		url += "user="+encodeURIComponent(username);
		single = false;
	}

	url += "&jsoncallback=?";

	$.getJSON(url, function(data) {
		progress_marker_visibility(false);
		if (data.result == "error") {
		  alert(data.error);
		  return;
		}
		if (data.markers == null) {
		  data.markers = [ { x: data.x, y: data.y, name: data.name } ];
		}
		$.each(data.markers, function() {
			pins_on_map.create(this.x, this.y, this.name, this.klass);
		});
		pins_on_map.fit_all();
		if (single) {
			document.forms['search'].elements['artist'].focus();
			document.forms['search'].elements['artist'].select();
		} else {
			document.forms['search'].elements['username'].focus();
			document.forms['search'].elements['username'].select();
		}
	});
}

document.forms['search'].elements['artist'].focus();
document.forms['search'].elements['artist'].select();

var pins_on_map = null;
var viewer = null;

$(function() {
  var templates = {
    A: '#marker-template-A',
    B: '#marker-template-B',
    AB: '#marker-template-AB'
  };
  if (document.getElementById('deep-zoom-map')) {
    // Seadragon.Config.proxyUrl="proxy.php?url=";
    viewer = new Seadragon.Viewer("deep-zoom-map");
    pins_on_map = new DeepZoomPinHandler(viewer, templates);
	viewer.openDzi("/last.fm/images/zoomable/lastfm-map.dzi");
	viewer.addEventListener("open", function() {
    });
  } else {
    pins_on_map = new Pins('#marker-container', templates);
  }
});


