//Global var to remember the onload Functions
var onloadFunctions = Array();

/**
 * Onload function Load all functions stored in onloadFunctions
 */
function doOnload()
{
	for(var myIndex = 0; myIndex < onloadFunctions.length; myIndex++ )
	{
		if(onloadFunctions[myIndex])
		{
			onloadFunctions[myIndex]( );
		}
	}
}


/**
 * Store a function to run in window.Onload
 */
window.addOnloadFunction = function(sName)
{
	iElement = onloadFunctions.length + 1;
	onloadFunctions[iElement] = sName;
}

function hideProjectData()
{
	if($('projectData'))
	{
		iActiveCatID = 0;
		$('projectData').addClassName('hide');
		$('projectData').innerHTML = "";
	}
}
 
function setGoogleMap()
{
	oMap = new GoogleMap('googleMap', { width: 440, height: 416, controls: [new GLargeMapControl3D()] });
	loadCitiesOnMap();
	/*setTimeout("setGoogleMapDelayed();", 1200 );*/
}

function setGoogleMapDelayed()
{
	oMap.refresh();
	loadCitiesOnMap();
}

function loadCitiesOnMap()
{
	//Get the GET parameters from the URL
	var sGetParams	= this.location.search.substring(1);
	var sURL 		= '/output/xml/overdekernen/woningbezit/getmarkers';
	sURL			+= this.location.search.substring(1).length > 0 ? '?'+sGetParams : '';

	//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 info so we can show it on the info window
						oInfoField		= oField.getElementsByTagName('info')[0];
						sInfo			= oInfoField.firstChild.data;

						//Get the ID
						oIDField		= oField.getElementsByTagName('ID')[0];
						iID				= oIDField.firstChild.data;

						//Get the URI so we can use it for the link
						oURIField		= oField.getElementsByTagName('selectURI')[0];
						sURI			= oURIField.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.sURI 	= sURI;
							oMarker.sInfo 	= sInfo;
							
							GEvent.addListener(oMarker, 'mouseover', function(){ 
								oMarker.openExtInfoWindow(
									oMap.oMap,
									"gmaps_marker_info",
									this.sInfo,
									{beakOffset: 3}
								); 
							});
							
							//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();
						}						
					}
				);
			}
		}
	);
}

function show(id)
{
	if($(id))
	{
		$(id).style.display = "block";
	}
}

function hide(id)
{
	if($(id))
	{
		$(id).style.display = "none";
	}
}

/*
*	functie om een text zo breed te maken als een image
*/
function truncateText(text, imgID)
{
	var newImage = new Image;
	newImage.src = $('image_'+imgID).src;

	var truncatedText = "";
	
	if(newImage.width < 1)
		newImage.width = '100';
			
	//1 letter = 0,16px	
	if(text.length > (newImage.width * 0.14))
	{
		for(var y=0; y <= ((newImage.width * 0.14) - 0.5); y++)
		{
			if(text.charAt(y))
				truncatedText  = truncatedText + text.charAt(y);
		}
		
		truncatedText += '...';
	}
	else
	{
		truncatedText = text;
	}

	$('truncatedText_'+imgID).innerHTML =  truncatedText;
}


/*	Functies voor het fotoboek
*
*/	
var oldPicNavigator = false;
var oldPicNavigatorCat = false;
var currentCatID;
var currentPicID;

function showPicNavigator(catID, picID, lastPicID)
{
	$('picNavigator_'+picID).addClassName('active');
	
	if(currentPicID)
	{
		$('picNavigator_'+currentPicID).removeClassName('active');
	}
	
	currentCatID = catID;
	currentPicID = picID;
	
	//eerste foto? dan de 'vorige link' verbergen
	if(picID == 1)
		$('picNavigatorLeft_'+catID).style.visibility = 'hidden';
	else
		$('picNavigatorLeft_'+catID).style.visibility = 'visible';
	
	//laatste foto? dan de 'volgende link' verbergen
	if(picID == lastPicID)
		$('picNavigatorRight_'+catID).style.visibility = 'hidden';
	else
		$('picNavigatorRight_'+catID).style.visibility = 'visible';
	
	
	//image we want
	newImage= $('image_'+currentCatID+'_'+currentPicID);
	
	//src pakken
	src = newImage.src.replace(/http:\/\/(.+?)\//gi, "/", newImage.src);

	aSrc = src.split(".");
	iKey = aSrc.length - 3;

	sExt = aSrc[iKey];
	
	sUrl = '';
	for(iN in aSrc)
	{
		if(iN < iKey)
		{
			sUrl += aSrc[iN]+'.';
		}
	}

	$('activePic').src = sUrl+sExt+'.500x350.'+sExt;

	//push the description
	$('picNavigatorDescription').innerHTML = $('longPicDescription_'+currentCatID+'_'+currentPicID).innerHTML;
	
	//if there is an navigatorcat hide it
	if(oldPicNavigatorCat)
	{
		oldPicNavigatorCat.style.display = 'none';	
	}
	
	//store our new navigatorcat
	oldPicNavigatorCat = $('picNavigatorCat_'+catID);
	
	//hide the new one
	show('picNavigatorCat_'+catID);
	
	//and show our layer
	show('picNavigator');
}

/*	
*	functie om volgende foto te laten zien
*/
function navPic(direction)
{
	if(direction == "prev")
		var nextPic = (currentPicID - 1);
	else if(direction == "next")
		var nextPic = currentPicID + 1;
		
	if($('image_'+currentCatID+'_'+nextPic))
	{
		$('image_'+currentCatID+'_'+nextPic).onclick();
	}		
}

