/****************************************************
* THE GLOBE TRADING JAVASCRIPT                      *
*                                                   *
****************************************************/


/***************************************************
* UNIVERSAL FUNCTIONS                              *
***************************************************/

//A universal add event function for cross-browser compatibility
function addEvent( elm, evType, fn, useCapture)
{
	
	if(elm.addEventListener)
	{
		
		elm.addEventListener(evType, fn, useCapture);
		
	}
	else if( elm.attachEvent)
	{
	
		var r = elm.attachEvent('on' + evType, fn);
		return r;
		
	}
	else 
	{
		elm['on' + evType ] = fn;
	}
	
}

//Get elements by Id shortcut function
function getElemId( elementName )
{
	return document.getElementById(elementName);
}

//Cancels bubbling action
function cancelBubble(e)
{
	//Cancels the default link action
	if(window.event)
	{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if(e && e.stopPropagation && e.preventDefault)
	{
		e.stopPropagation();
		e.preventDefault();
	}
	
}

function getETarget( e )
{

	if(window.event && window.event.srcElement)
	{
		return window.event.srcElement;
	}
	else if( e && e.target )
	{
		return e.target;
	}
	else
	{
		return false;
	}
	
}



/*****************************************************
* GLOBAL VARIABLES                                   *
*****************************************************/

var page = null;

function setPage( p )
{

	page = p;
	
}

function getPage( p )
{
	return p;
	
}


var map = null;

var locations = new Array();


//Stores a marker and address in HTML
GMarker.prototype.address = null;

locations[0] = new GMarker( new GLatLng( 52.2915, -1.5928 ));
locations[0].address = "<b>The Globe Trading</b><br/>Unit 3, Lock lane<br/>Warwick<br/>CV34 5AG"; //Lat/long of warehouse

locations[1] = new GMarker( new GLatLng( 51.9033, -2.0803 ) );
locations[1].address = "<b>The Globe Cheltenham</b><br/>322 High Street <br />Cheltenham <br />Gloucestershire <br />GL50 3JF <br />"; //Lat/long of Cheltenham supermarket

locations[2] = new GMarker( new GLatLng( 52.062, -1.3386 ) );
locations[2].address = "<b>The Globe Banbury</b><br/>26 Parsons Street<br />Banbury<br />Oxfordshire<br />OX16 5LY<br />01295 271 788<br />"; //Lat/long of Banbury supermarket

locations[3] = new GMarker( new GLatLng( 52.2842, -1.5307 ) );
locations[3].address = "<b>The Globe Leamington Spa</b><br/>23 High Street<br />Leamington Spa<br />Warwickshire<br />CV31 1LN<br />"; //Lat/long of Leamington Spa supermarket

locations[4] = new GMarker( new GLatLng( 52.23896, -0.89759 ) );
locations[4].address = "<b>The Globe Rothersthorpe Avenue</b><br/>Unit 40 Rothersthorpe Avenue<br />Northampton<br />NN4 8JH<br />"; 

locations[5] = new GMarker( new GLatLng( 52.22628, -0.91312 ) );
locations[5].address = "<b>The Globe Sheep Street</b><br/>12-14 Sheep Street<br />Northampton<br />NN1 2LU<br />";



//Function for calling Google map onto the page: requires MAP global var
function gMapLoad( elem ) {
  if (GBrowserIsCompatible()) 
  {
	
		map = new GMap2( elem );
		
		map.addControl(new GSmallMapControl()); //Adds map control
	
		GEvent.addListener( map, "click", gMapClickHandle);
  }
}

function gMapClickHandle( marker, point)
{
	
	if(marker)
	{
	
		marker.openInfoWindowHtml( marker.address);
		
	}
}

function gMapAddMarker( marker, makeCenter, openInfoWindow )
{
	if(makeCenter)
	{
		map.setCenter( marker.getPoint() , 13); //Sets initial google map starting point
	}
	
	
	//Adds the marker to the map
	map.addOverlay(marker);
	
	if(openInfoWindow)
	{
		
		marker.openInfoWindowHtml( marker.address);
	}
	
}



function changeSuperPage( e )
{
	var head = getElemId('superNames');
	var main = getElemId('mainAddress');
	var chelt = getElemId('cheltenhamAddress');
	var ban = getElemId('banburyAddress');
	var leam = getElemId('leamingtonAddress');
	var rother = getElemId('rothersthorpeAddress');
	var sheep = getElemId('sheepAddress');
	
	if( getETarget(e).id == 'cheltenhamLink')
	{
		head.childNodes[0].nodeValue = "Globe Supermarket, Cheltenham";
		map.panTo( locations[1].getPoint() );
		
		main.style.display = 'none';
		chelt.style.display = 'block';
		ban.style.display = 'none';
		leam.style.display = 'none';
		rother.style.display = 'none';
		sheep.style.display = 'none';
		
		
	}
	else if( getETarget(e).id == 'banburyLink')
	{
		head.childNodes[0].nodeValue = "Globe Supermarket, Banbury";
		map.panTo( locations[2].getPoint() );
		
		main.style.display = 'none';
		chelt.style.display = 'none';
		ban.style.display = 'block';
		leam.style.display = 'none';
		rother.style.display = 'none';
		sheep.style.display = 'none';
	}
	else if( getETarget(e).id == 'leamingtonLink')
	{
		head.childNodes[0].nodeValue = "Globe Supermarket, Leamington Spa";
		map.panTo( locations[3].getPoint() );
		
		main.style.display = 'none';
		chelt.style.display = 'none';
		ban.style.display = 'none';
		leam.style.display = 'block';
		rother.style.display = 'none';
		sheep.style.display = 'none';
	}
	else if( getETarget(e).id == 'mainoffice')
	{
		head.childNodes[0].nodeValue = "Main office, Globe Trading (UK) ";
		map.panTo( locations[0].getPoint() );
		
		main.style.display = 'block';
		chelt.style.display = 'none';
		ban.style.display = 'none';
		leam.style.display = 'none';
		rother.style.display = 'none';
		sheep.style.display = 'none';
	}
		else if( getETarget(e).id == 'rothersthorpeLink')
	{
		head.childNodes[0].nodeValue = "Globe Supermarket, Rothersthorpe Avenue";
		map.panTo( locations[4].getPoint() );
		
		main.style.display = 'none';
		chelt.style.display = 'none';
		ban.style.display = 'none';
		leam.style.display = 'none';
		rother.style.display = 'block';
		sheep.style.display = 'none';
	}
		else if( getETarget(e).id == 'sheepLink')
	{
		head.childNodes[0].nodeValue = "Globe Supermarket, Sheep Street";
		map.panTo( locations[5].getPoint() );
		
		main.style.display = 'none';
		chelt.style.display = 'none';
		ban.style.display = 'none';
		leam.style.display = 'none';
		rother.style.display = 'none';
		sheep.style.display = 'block';
	}
	
	//Cancels the event propegation
	cancelBubble(e);
	
}

//Initilizes the page and loads any functions that are needed
function initialize()
{
	
	if(page == 4)
	{
		
		gMapLoad( getElemId('map') );
		gMapAddMarker( locations[1], true, false);
		gMapAddMarker( locations[2], false, false);
		gMapAddMarker( locations[3], false, false);
		gMapAddMarker( locations[0], false, false);
		gMapAddMarker( locations[4], false, false);
		gMapAddMarker( locations[5], false, false);
		
		var mainoffice = getElemId('mainoffice');
		var cheltenhamClick = getElemId('cheltenhamLink');
		var banburyClick = getElemId('banburyLink');
		var leamingtonClick = getElemId('leamingtonLink');
		var rothersthorpeClick = getElemId('rothersthorpeLink');
		var sheepClick = getElemId('sheepLink');
				
		addEvent( mainoffice, 'click', changeSuperPage, false);
		addEvent( cheltenhamClick, 'click', changeSuperPage, false);
		addEvent( banburyClick, 'click', changeSuperPage, false);
		addEvent( leamingtonClick, 'click', changeSuperPage, false);
		addEvent( rothersthorpeClick, 'click', changeSuperPage, false);
		addEvent( sheepClick, 'click', changeSuperPage, false);
		
		
		
		
	}
	else if( page == 5)
	{
		
		gMapLoad( getElemId('map') );
		gMapAddMarker( locations[0] , true ,true);
		
	}
	
}

