/* 
	GOOGLE MAPS JAVASCRIPTS FOR JELLY AND CUSTARD
*/

// Start up the scripts
function addLoadEvent(func) 
{
	var oldonload = window.onload;
  	if (typeof window.onload != 'function') 
  	{
    	window.onload = func;
  	}
  	else 
  	{
    	window.onload = function() 
    	{
      		if (oldonload) 
      		{
        		oldonload();
      		}
      		func();
    	}
  	}
}

// Prepare the Gallery Links
/*
function prepareLinks()
{
	// Change all links with showGallery class
	var links = document.getElementsByTagName("a");
	for (var i=0; i<links.length; i++)
	{	
		// Work around for IE7
		if(links[i].getAttributeNode("class").value)
		{
			if(links[i].getAttributeNode("class").value == "jsShowMap")
			{
				links[i].onclick = function()
				{
					showLocationMap(this.getAttribute("href"));
					return false;
				}
			}
		}
		
		// Read the className
		if(links[i].getAttribute("class") == "jsShowMap")
		{
			links[i].onclick = function()
			{
				showLocationMap(this.getAttribute("href"));
				return false;
			}
		}
		
	}		
}
*/

// Open popup window
function showLocationMap(url)
{
	window.open(url, 'locationMap', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=550');
	
}

//addLoadEvent(prepareLinks); 



var map = null;
var geocoder = null;


function initialize() 
{
	if (GBrowserIsCompatible()) 
    {
    	// Start the map object
        map = new GMap2(document.getElementById("map_canvas"));

		// Start the Geo Coder
        geocoder = new GClientGeocoder();
        
        // Send the postcode, event name and venue to the map object
        startMap(getPostcode(), getEventName(), getVenue()); 
      }
}

function startMap(postcode, event, venue) 
{
	if (geocoder) 
    {
    	geocoder.getLatLng(postcode, function(point) 
        {
        	if (!point) 
        	{
              alert("Sorry we could not locate this event");
            } 
            else 
            {
              // Firstly Centre this map
              map.setCenter(point, 9);
              
              // Add small map in right	
              var smallMap = new GOverviewMapControl();
			  map.addControl(smallMap);
            
              // Add zoom controls
              var mapControl = new GLargeMapControl();
			  map.addControl(mapControl);
			  
              // Add marker
              var marker = new GMarker(point);
              map.addOverlay(marker);
              
              // Add info window
              marker.openInfoWindowHtml("<h1>" + event + "</h1>" + "<p>" + venue + "</p>");
            }
         }
         );
    }
}

function Querystring(qs) 
{ 
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}

function getPostcode()
{
	var qs = new Querystring();
	return qs.get("postcode");
}

function getEventName()
{
	var qs = new Querystring();
	return qs.get("event");
}

function getVenue()
{
	var qs = new Querystring();
	return qs.get("venue");
}