//<![CDATA[
    var map;
    var icon;
    var to_htmls = [];
    var from_htmls = [];
    var marker = new Array();

    function clearMap() {
      var map = new GMap2(document.getElementById("map"));
    }

    function wait(msecs) {
      var start = new Date().getTime();
      var cur = start
      while(cur - start < msecs) {
	    cur = new Date().getTime();
      }
    } 

    function createMarker(x, y, artist, id, title, loc, iconid, photo, width, height) {
    var icon = new GIcon();
    icon.image = 'img/ico/' + iconid + '.png';
    icon.shadow = 'img/ico/shadow.png';
    icon.iconSize = new GSize(20.0, 34.0);
    icon.shadowSize = new GSize(38.0, 34.0);
    icon.iconAnchor = new GPoint(10.0, 17.0);
    icon.infoWindowAnchor = new GPoint(10.0, 17.0);

      var point = new GLatLng(x,y);
      marker[id] = new GMarker(point, {title: title, icon:icon});
      map.addOverlay(marker[id]);
      //Open the infobubble
      addInfoMarker(x, y, photo, id, width, height, artist, loc, title)
      //Add the infobubble click event
      GEvent.addListener(marker[id], 'click', function() {
      	addInfoMarker(x, y, photo, id, width, height, artist, loc, title)
        map.panTo(new GLatLng(x,y),3);
      });
      map.panTo(new GLatLng(x,y),3);
    }

  function addInfoMarker(x, y, photo, id, width, height, artist, loc, title) {
    var img = new Array();
    if (photo) {
    	img[id] = '<img src=\"' + photo + '\" alt=\"photo\" id=\"' + id + '\" width=\"' + width + '\" height=\"' + height + '\" class=\"bubble\" />';
    } else {
    	img[id] = '';
    }

	// The info window version with the "to here" form open
        to_htmls[id] = '<span class="dir">Directions: <b>To here</b> - <a href="javascript:fromhere(' + id + ')">From here</a>' +
           '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + x + ',' + y + 
                  // "(" + name + ")" + 
           '"/></span>';
        // The info window version with the "to here" form open
        from_htmls[id] = '<span class="dir">Directions: <a href="javascript:tohere(' + id + ')">To here</a> - <b>From here</b>' +
           '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + x + ',' + y +
                  // "(" + name + ")" + 
           '"/></span>';
        // The inactive version of the direction info
        html = '<br />Directions: <a href="javascript:tohere('+ id +')">To here</a> - <a href="javascript:fromhere('+ id +')">From here</a>';

      marker[id].openInfoWindowHtml('<div style="white-space:nowrap">' + img[id] + '<div class="bubbletxt"><span><strong>' + artist + '</strong></span><br /><span><a href="http://www.last.fm/event/' + id + '" target="_BLANK">' + title + '</a></span><br /><span>Location: ' + loc + '</span><div class="zoom">Zoom: <a href="javascript:zoomIt(\'' + x + '\', \'' + y + '\', \'in\')">In</a> - <a href="javascript:zoomIt(\'' + x + '\', \'' + y + '\', \'out\')">Out</a>' + html + '</div></div></div>');
  }

  // functions that open the directions forms
  function tohere(i) {
     marker[i].openInfoWindowHtml(to_htmls[i]);
  }
  function fromhere(i) {
     marker[i].openInfoWindowHtml(from_htmls[i]);
  }

  function zoomIt(x, y, type) {
      if (type == "in") { level = 15 } else { level = 3 }
      map.setCenter(new GLatLng(x, y),level);
  }

	function searchlocation(loc, page) {
   	$("#initial").fadeOut("fast");
   	$("#mapDigg").slideDown("slow");
   	$("#similar").fadeOut("slow", function () {
		        $(this).remove();
	 });
	   if (!loc) {
	           var location = $('#searchbox').attr('value');
	   } else {
	   	   var location = loc;
	   }
	   if (!page) {
	           var page = 1;
	   }
		//alert("intercepted!" + artist); 
                 $.ajax({
                  url: "submit.php?location=" + location + "&page=" + page,
                  async: true,
		  beforeSend: function(html){
	   		$('#loading').fadeIn('slow');
			$('#searchbox').attr('disabled', true); 
		        },
		  success: function(html){
	   	      $('#searchbox').removeAttr('disabled');
		      $("#results").append(html);
			document.f.artist.focus();
	   	      $('#searchbox').val('');
		      }
                 })
		return false; 
	}

	function searchevents(name) {
   	$("#initial").fadeOut("fast");
   	$("#mapDigg").slideDown("slow");
   	$("#similar").fadeOut("slow", function () {
		        $(this).remove();
	 });
	   if (!name) {
	           var artist = $('#searchbox').attr('value');
	   } else {
	   	   var artist = name;
	   }
		//alert("intercepted!" + artist); 
                 $.ajax({
                  url: "submit.php?artist=" + artist,
                  async: true,
		  beforeSend: function(html){
	   		$('#loading').fadeIn('slow');
			$('#searchbox').attr('disabled', true); 
		        },
		  success: function(html){
	   	      $('#searchbox').removeAttr('disabled');
		      $("#results").append(html);
			document.f.artist.focus();
	   	      $('#searchbox').val('');
		      }
                 })
		return false; 
	}

	function openDialog(artist) {
                 $.ajax({
                  url: "submit.php?dialog=1&artist=" + artist,
                  async: true,
		  success: function(html){
		      $("#results").append(html);
		      $("#dialog").dialog({
		      	width: 470,
			height: 270,
			title: "Top Albums for " + artist
			});
		        }
                 })
		return false; 
	}

	$(function(){
		$('a.new-window').click(function(){
	        	window.open(this.href);
		        return false;
        	});
	});

  jQuery(document).ready(function($){

	$('#theform').submit(function() { 
	        var choose = $('#choice').attr('value');
		if (choose == 'searchArtist') {
			searchevents();
		} else {
			searchlocation();
		}
	}); 

	$("#searchbox").autocomplete("db.php", { 
		minChars:2, 
		maxItemsToShow:25, 
		cacheLength:10, 
		onItemSelect:function(){
			searchevents();
                },
		autoFill:true 
	});

  });
	
	function stopSearch() {
  	jQuery(document).ready(function($){
	if (typeof( window[ 'queue' ] ) != "undefined") {
	   queue.abort();
	}
	   $('#loading').fadeOut('slow'); 
	   $('#searchbox').removeAttr('disabled');
	   $('#similar').slideDown(2000);
	   $('#searchbox').click(function() {
		this.value = "";
		document.f.artist.focus();
		});
  	});
	}

	function emptySearch() {
  	jQuery(document).ready(function($){
	   $('#loading').fadeOut('slow'); 
	   $('#searchbox').removeAttr('disabled');
	   $("#notfound").fadeIn(2000);
	   $("#notfound").fadeOut(1000);
	   $('#similar').slideDown(2000);
	   $('#searchbox').click(function() {
		this.value = "";
		document.f.artist.focus()
		});
  	});
	}

	function sf(){
		document.f.artist.focus()
	   	$("#initial").fadeIn("slow");
	}
//]]>

