Event.observe(window, 'load', function () {
	History.Observer.start();
	History.Observer.delay = 0.5;
	
	var sPrevTabState = '';



	//Add this id to history registry
	History.Registry.set({
	    id: 'homeTab',
	    onStateChange: function(state) { 
			loadTab(state.toString());
		}
	});
	
	var uriTab = History.get('homeTab');
	if(typeof(uriTab) != 'undefined' && uriTab != '' && uriTab != null)
	{
		loadTab(uriTab);
	}
});

var oMap;

function clickTabButton(eID)
{
	if(typeof(eID) == 'undefined' || eID == '')
	{
		return;
	}
	if($(eID) != null)
	{
		$(eID).onclick();
	}
}

function startTabButton(eID)
{
	sActiveId = '';
	
	var uriTabBtn = null;
	if(typeof(uriTabBtn) != 'undefined' && uriTabBtn != '' && uriTabBtn != null)
	{
		clickTabButton(uriTabBtn);
	}
	else if(typeof(eID) != 'undefined' && eID != '')
	{
		if($(eID) != null)
		{
			clickTabButton(eID);
		}
	}
}

/*
 * Request a template for dynamic loading of homepage data
 */
function loadTab( sTabName )
{
	if(sTabName == 'welkom')
	{
		window.location = '/home/'+sTabName;
	}
	History.set('homeTab', sTabName);

	oCurrent = $('homemenu_'+sTabName);
	
	//doe iets met knopeffect
	sActiveId = '';

	$('content').update('<div id="loading_ajax"></div>');
	var oCurrentLi = oCurrent.ancestors()[2];
	
	//remove and add infodiv
	var oElement = $$('#content_menu_list div[class~=hidden]');
	if(typeof(oElement) == 'object' && oElement.length > 0)
	{
		oElement[0].removeClassName('hidden');
	}
	oCurrentLi.getElementsBySelector('div[class~=content_menu_info]')[0].addClassName('hidden');
	//remove and add active class
	oElement = $$('#content_menu_list li[class~=active]');
	if(typeof(oElement) == 'object' && oElement.length > 0)
	{
		oElement[0].removeClassName('active');
	}
	oCurrentLi.addClassName('active');
	
	var _url = '/output/htmlxml/home/'+sTabName;
	new Ajax.Request( _url, 
	{
        method: 'get',
		asynchronous:true,
		evalScripts:true,
		encoding: 'UTF-8',
		onComplete:function(transport)
		{
	    	$('content').update(transport.responseText);
		},
		onException: function(transport)
		{
    		$('content').update('&nbsp;');
    		window.location = '/home/'+sTabName;
		}
	});
	
	return false;
}

function setMap()
{
	oMap = new GoogleMap('map_canvas', { width: 343, height: 340 });
	setTimeout("setMapDelayed();", 1200 );
}

function setMapDelayed()
{
	oMap.refresh();
	loadDwellingsOnMap();
}

function loadDwellingsOnMap()
{
	//Get the GET parameters from the URL
	var sGetParams	= this.location.search.substring(1);
	var sURL 		= '/output/xml/woningaanbod/aanbod/spoedopkaart';
	sURL			+= this.location.search.substring(1).length > 0 ? '?'+sGetParams : '';
	
	//var oStatus	= $('map_canvas_mapstatus');
	//oStatus.insert('De woningen worden ingeladen.');

	//Make an ajax request to retreive the locations
	new Ajax.Request
	(
		sURL,
		{
			method: 	'get',
			onSuccess:	function(oResponse)
			{
				//Get the values from the XML response
				var oXML 	= oResponse.responseXML.documentElement;
				var oFields = $A(oXML.getElementsByTagName('map'));
				var iTotal 	= oFields.length;
				var oIcon 	= oMap.getGoogleIcon('pal2', 'icon10');
				var aMarker	= new Array();

				//Loop trough all fields
				oFields.each
				(
					function(oField, iIndex)
					{
						//Get the address so we can use it for geocoding
						oAddressField	= oField.getElementsByTagName('address')[0];
						sAddress		= oAddressField.firstChild.data;

						//Get info so we can show it on the info window
						oInfoField		= oField.getElementsByTagName('info')[0];
						sInfo			= oInfoField.firstChild.data;

						//Get the ID so we can use it for the link
						oIDField		= oField.getElementsByTagName('ID')[0];
						iID				= oIDField.firstChild.data;

						oLatitudeField	= oField.getElementsByTagName('latitude')[0];
						fLatitude		= oLatitudeField.firstChild.data;
						
						oLongitudeField	= oField.getElementsByTagName('longitude')[0];
						fLongitude		= oLongitudeField.firstChild.data;
						
						oPoint			= null;
						if(fLatitude > 0 && fLongitude > 0)
						{
							oPoint		= new GPoint(fLongitude, fLatitude);
						}

						if(oPoint != null)
						{
							//Create a marker for the point
							var oMarkerOptions = { icon: oIcon };
							var oMarker 	= oMap.getMarker(oPoint, oMarkerOptions);
							oMarker.iID 	= iID;
							oMarker.sInfo 	= sInfo;
							
							//Add a onmouseover event listener to show the infobox
							GEvent.addListener(oMarker, 'mouseover', function() 
							{
								this.openInfoWindowHtml(this.sInfo);
							});

							//Add onclick event listener to navigate to the details page
							GEvent.addListener(oMarker, 'click', function()
							{
								window.location = '/woningaanbod/aanbod/details/'+this.iID+'?spdsearch=1';
							});
							
							//Add the marker to the array
							aMarker.push(oMarker);
						}
						
						//When all the markers are processed, add them to the map, center and zoom on all the markers
						if((iTotal == (iIndex+1)) && (aMarker.length > 0))
						{
							oMap.addMarkers(aMarker);
							oMap.centerOnMarkers();
							oMap.zoomOnMarkers();
							oStatus.remove();
						}						
					}
				);
			}
		}
	);
}
