/* venue gMap cache */
  var venue = [
    {
      name: "The Fillmore",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "1805 Geary Blvd<br/>San Francisco, CA 94115",
          TEL: "(415) 346-6000",
          Point: {
            coordinates: [-122.433092,37.784229,0]
          }
		}
      ]
    },
    {
      name: "Bimbos",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "1025 Columbus Ave<br/>San Francisco, CA 94133",
          TEL: "(415) 474-0365",
          Point: {
            coordinates: [-122.415389,37.803835,0]
          }
        }
      ]
    },
    {
      name: "Bottom of the Hill",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "1233 17th St<br/>San Francisco, CA",
          TEL: "(415) 621-4455",
          Point: {
            coordinates: [-122.396141,37.765203,0]
          }
        }
      ]
    },
    {
      name: "The Great American Music Hall",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "859 O'Farrell St<br/>San Francisco, CA 94109",
          TEL: "(415) 885-0750",
          Point: {
            coordinates: [-122.418891,37.785094,0]
          }
        }
      ]
    },
    {
      name: "Greek Theatre",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "Gayley Rd<br/>Berkeley, CA 94720",
          TEL: "N/A",
          Point: {
            coordinates: [-122.254640,37.873090,0]
          }
        }
      ]
    },
    {
      name: "SF Weekly Warfield",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "982 Market St<br/>San Francisco, CA 94102",
          TEL: "(415) 567-2060",
          Point: {
            coordinates: [-122.409925,37.782604,0]
          }
        }
      ]
    },
    {
      name: "Great American Music Hall",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "859 O'Farrell St<br/>San Francisco, CA 94109",
          TEL: "(415) 885-0750",
          Point: {
            coordinates: [-122.418891,37.785094,0]
          }
        }
      ]
    },
	{
      name: "Bill Graham Civic Auditorium",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "99 Grove St<br/>San Francisco, CA 94102",
          TEL: "(415) 974-4060",
          Point: {
            coordinates: [-122.418153,37.778526,0]
          }
        }
      ]
    },
	{
      name: "The Independent",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "628 Divisadero St<br/>San Francisco, CA 94117",
          TEL: "(415) 771-1421",
          Point: {
            coordinates: [-122.437849,37.775344,0]
          }
        }
      ]
    },
	{
      name: "Slims",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "333 11th St<br/>San Francisco, CA 94103",
          TEL: "(415) 255-0333",
          Point: {
            coordinates: [-122.413719,37.771633,0]
          }
        }
      ]
    },
	{
      name: "The Rickshaw Stop",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "155 Fell St<br/>San Francisco, CA 94102",
          TEL: "(415) 861-2011",
          Point: {
            coordinates: [-122.420451,37.776283,0]
          }
        }
      ]
    },
	{
      name: "Mezzanine",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "444 Jessie St<br/>San Francisco, CA 94013",
          TEL: "(415) 625-8880",
          Point: {
            coordinates: [-122.408337,37.782206,0]
          }
        }
      ]
    },
	{
      name: "Popscene",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "333 Ritch St<br/>San Francisco, CA 94107",
          TEL: "N/A",
          Point: {
            coordinates: [-122.394382,37.779252,0]
          }
        }
      ]
    },
	{
      name: "Mighty",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "119 Utah St<br/>San Francisco, CA 94103",
          TEL: "(415) 762-0151",
          Point: {
            coordinates: [-122.406785,37.768155,0]
          }
        }
      ]
    },
	{
      name: "Cafe Du Nord",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "2170 Market St<br/>San Francisco, CA 94114",
          TEL: "(415) 861-5016",
          Point: {
            coordinates: [-122.406785,37.768155,0]
          }
        }
      ]
    },
	{
      name: "Swedish American Hall",
      Status: {
        code: 200,
        request: "geocode"
      },
      Placemark: [
        {
          address: "2170 Market St<br/>San Francisco, CA 94114",
          TEL: "(415) 861-5016",
          Point: {
            coordinates: [-122.430629,37.766350,0]
          }
        }
      ]
    }
  ];
/* venue gMap cache */

    var map;
    var geocoder;
	
	function venueCache() {
      GGeocodeCache.apply(this);
    }
	
    venueCache.prototype = new GGeocodeCache();
	
	venueCache.prototype.reset = function() {
      GGeocodeCache.prototype.reset.call(this);
      for (var i in venue) {
        this.put(venue[i].name, venue[i]);
      }
    }
	
    function load() {
      map = new GMap2(document.getElementById("map"));
      map.setCenter(new GLatLng(37.77, -122.41), 15);
	  map.addControl(new GSmallMapControl());
      geocoder = new GClientGeocoder();
	  geocoder.setCache(new venueCache());
	  var mapelement = document.getElementById("gMapId");
 	  if(mapelement != null)
		mapelement.style.display = 'none';	  	  
    }

	function show_visibility(id) {
		var mapelement = document.getElementById(id);
		if(mapelement != null) {
			if(mapelement.style.display == 'none')
				mapelement.style.display = 'block';
		}
	}
	function close_visibility(id) {
		var mapelement = document.getElementById(id);
		if(mapelement != null) {
			if(mapelement.style.display == 'block')
				mapelement.style.display = 'none';
		}
	}
	
	function close_links() {
		var linkelement = document.getElementById('linkcontainer');
		if (linkelement != null)
			result = linkelement.hasChildNodes();
		if (result == true)
		{
			while (linkelement.firstChild) {
				linkelement.removeChild(linkelement.firstChild);
			}			
		}				
	}
	
	
    // addAddressToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
    function addAddressToMap(response) {
      map.clearOverlays();
      if (response && response.Status.code != 200) {
        alert("Sorry, we were unable to locate that address");
      } else {
		var place = response.Placemark[0];
        var point = new GLatLng(place.Point.coordinates[1],
                                place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.setCenter(point, 15);
        map.addOverlay(marker);
		GEvent.addListener(marker, "click", function() 
		{marker.openInfoWindowHtml("<b>Venue:<br/></b> " + response.name + "<br><b>Address:<br/></b> " + place.address + "<br><b>Phone:<br/></b> " + place.TEL);});
      }
    }

    // showLocation() is called when you click on the Search button
    // in the form.  It geocodes the address entered into the form
    // and adds a marker to the map at that location.
    function showLocation(address) {
      geocoder.getLocations(address, addAddressToMap);
    }

   // findLocation() is used to enter the sample addresses into the form.
    function findLocation(address) {
      showLocation(address);
    }
	
	function findvenue(venueStr) {
		var found = 0;
		for (var i = 0; i < venue.length; i++){
			var venueKey = venue[i].name;
			if (venueKey == venueStr)
			{			
				found = 1;
				break;
			}
		}
		
      if (found == 1) {
  		close_links();
  		show_visibility('gMapId');
        geocoder.getLocations(venueKey, addAddressToMap);
      }
    }
	
	function getlinkpost(bandstr)
	{		
		var totallinks = 0;
		var ul = document.createElement('ul');
		
		ul.appendChild(document.createTextNode(bandstr));		
		ul.appendChild(document.createElement("br"));	
		for (var i=0, post; post = Delicious.posts[i]; i++) {	
			var bandkey;
			if (post.d > bandstr)
				bandkey = post.d.substr(0, bandstr.length);
			else
				bandkey = post.d;
			if (bandkey.toLowerCase() == bandstr.toLowerCase())
			{
				//var li = document.createElement('li')
				var a = document.createElement('a')
				a.style.marginLeft = '10px'
				a.setAttribute('href', post.u)
			    a.setAttribute('target', '_blank')							
				a.appendChild(document.createTextNode(post.d));
				//li.appendChild(a)
				ul.appendChild(a);
				ul.appendChild(document.createElement("br"));
				totallinks++;
			}
			else
			{
				continue;
			}
			//li.appendChild(img)

		}
		if (totallinks == 0)
		{
				//var nolinkstr = 'cannot find ' + bandstr +' in my del.ico.us';
				//ul.appendChild(document.createTextNode(nolinkstr));
				//ul.appendChild(document.createElement("br"));
				var a = document.createElement('a');
				var googlestr = 'http://www.google.com/search?q=' + bandstr;
				a.style.marginLeft = '10px'
				a.setAttribute('href', googlestr);
				a.setAttribute('target', '_blank');							
				a.appendChild(document.createTextNode('Google it'));
				ul.appendChild(a);
		}		
		document.getElementById('link').appendChild(ul)
	}
	
	function findbandlink(bandfullStr) {
		close_links();
		close_visibility('gMapId');
		var found = 0;
		var bands = [];
		var j = 0;
		var start=true;
		/* parsing the event description */
		for (var i = 0; i< bandfullStr.length; i++)
		{			
			if (bandfullStr.charAt(i) == ',')
			{
				i++;
				j++;
				start = true;
				continue;
			}
			else
			{
				if (start == true)
				{
					bands[j] = bandfullStr.charAt(i);
					start = false;
				}
				else
				{
					bands[j] += bandfullStr.charAt(i);
				}				
			}
		}
		/* create the container div for link window */
		var linkcontainerId = document.createElement("div")
		linkcontainerId.id = 'gLinkId';
		document.getElementById('linkcontainer').appendChild(linkcontainerId);
	
		/* create the div for closelink */
		var closelinkId = document.createElement("div")
		closelinkId.id = 'closelink';
		document.getElementById('gLinkId').appendChild(closelinkId);
		var closelink = document.createElement('img')
		closelink.src = 'http://wanderingdays.com/calendar/event/json/cross.gif';
		closelink.onclick = close_links;
		document.getElementById('closelink').appendChild(closelink);
		var linksHeader = document.createTextNode(' Close Links');
		document.getElementById('closelink').appendChild(linksHeader);

		/* create the div for link */
		var linkId = document.createElement("div")
		linkId.id = 'link';
		document.getElementById('gLinkId').appendChild(linkId);
		/*document.getElementById('gLinkId').appendChild(document.createElement("br"));
		document.getElementById('gLinkId').appendChild(document.createElement("br")); */

		for (j=0; j< bands.length; j++)
		{
			getlinkpost(bands[j]);
		}
	
    }
	

	

