var mapsObjects = new Array();

function Maps(id, startCoordX, startCoordY, controlType, terrainType, zoom, showInfo)
{
	// Vars
	this.mapObject;
	this.markerImages;
	this.markerList;
	this.locationList;
	this.infoDiv;
	this.ID;
	this.clusterer;
	this.popupHtml;
	this.showInfo;

	// Functions
	this.loadMap = function(startCoordX, startCoordY, controlType, terrainType, zoom)
	{
		this.mapObject = new GMap2($('googleMaps'+this.ID));

		switch (controlType)
		{
			case 'none':
				break;
			case 'small':
				this.mapObject.addControl(new GSmallMapControl());
				break;
			case 'large':
			default:
				this.mapObject.addControl(new GLargeMapControl3D());
				break;
		}

		this.mapObject.setCenter(new GLatLng(startCoordX, startCoordY), zoom);
		this.mapObject.ownerID = this.ID;		
		
//		var overviewControl = new GOverviewMapControl();
//		this.mapObject.addControl(overviewControl);
		
		if (!terrainType)
			return;
			
		this.mapObject.addControl(new GMapTypeControl());
		
//		if (terrainType.indexOf('map') == -1)
//			this.mapObject.removeMapType(G_NORMAL_MAP);
//		if (terrainType.indexOf('satellite') == -1)
			this.mapObject.removeMapType(G_SATELLITE_MAP);
//		if (terrainType.indexOf('both') == -1)
//			this.mapObject.removeMapType(G_HYBRID_MAP);
	}
	
	this.setCenterFromLocations = function()
	{
		if(this.locationList.length > 0)
		{
			var boundaryBox = new GLatLngBounds();
			
			for (i = 0; i < this.locationList.length; i++)
			{
				boundaryBox.extend(new GLatLng(this.locationList[i].coords[0], this.locationList[i].coords[1]));
			}
			
			this.mapObject.setCenter(boundaryBox.getCenter(), this.mapObject.getBoundsZoomLevel(boundaryBox));
		}
	}
	
	/*
	 * Load the unique markerimages in an array
	 */
	this.loadMarkerImages = function(imageList)
	{
		var i;
		var baseIcon;

		baseIcon 					= new GIcon();
	    baseIcon.iconSize 			= new GSize(21,34);
	    baseIcon.iconAnchor 		= new GPoint(8,30);
	    baseIcon.infoWindowAnchor 	= new GPoint(8, 0);
		
		for (i = 0; i < imageList.length; i++)
		{
			if (imageList[i] == false)
			{
				this.markerImages[i] = null;
				continue;
			}
	
			this.markerImages[i] 		= new GIcon(baseIcon);
			this.markerImages[i].image	= imageList[i];
		}	
	}
	
	/*
	 * Add the GoogleLocations as markers to the map, imageID is used as reference to the imagearray. 
	 */
	this.loadMarkers = function(imageList, locationList)
	{
		this.loadMarkerImages(imageList);
		this.locationList	= locationList;

		var i;
		var tmpPoint;
		
		for (i = 0; i < this.locationList.length; i++)
		{
			tmpPoint					= new GLatLng(this.locationList[i].coords[0], this.locationList[i].coords[1]);
			this.markerList[i]			= new GMarker(tmpPoint, this.markerImages[this.locationList[i].imageID]);
			this.markerList[i].iterator = i;
			this.markerList[i].ownerID 	= this.ID;

			if (this.showInfo != 'none')
			{
				GEvent.addListener(this.markerList[i], "mousedown", function() 
				{
					mapsObjects[this.ownerID].loadInfoHTML(this.ownerID, this.iterator);
				});
			}

			// Add the markers without the clusterer
			if (locationList.length < 150) // TODO: Change this number to a var?
				this.mapObject.addOverlay(this.markerList[i]);
			// Add markers via the clusterer
			else
			{
				if (!this.clusterers[this.locationList[i].imageID])
				{
					this.clusterers[this.locationList[i].imageID] = new Clusterer(this.mapObject);
					this.clusterers[this.locationList[i].imageID].SetIcon(this.markerImages[this.locationList[i].imageID]);
				}
					
				this.clusterers[this.locationList[i].imageID].AddMarker(this.markerList[i], this.locationList[i].id);
				
			}
		}
	}
	
	/*
	 * Hide the google logo and copyright text >:) 
	 */
	this.setLogo = function(logo, logoTitle, logoUrl)
	{
		for(var i = 0; i < document.images.length; i++)
		{
			var str = document.images[i].src;
			if(str.match('poweredby.png'))
			{
				document.images[i].parentNode.parentNode.nextSibling.style.display = 'none';
			
				if(logo)
				{
					document.images[i].src = logo;
					document.images[i].parentNode.title = logoTitle;
					document.images[i].parentNode.href = logoUrl;
				}
				else
					document.images[i].src = document.images[i].parentNode.removeChild(document.images[i]);			
				
				return;
			}
		}
	}
	
	this.loadInfoHTML = function(ownerID, markerID)
	{
		SmartyAjax.call(
			ajaxUrl, 
			'get', 
			'classID='+ownerID+'&markerID='+markerID+'&locationID='+ mapsObjects[ownerID].locationList[markerID].id +'&m=googlelocations&f=loadLocationHTML', 
			mapsObjects[ownerID].showInfoHTML,
			undefined, 
			true
		);
	}
	
	/*
	 * Put the googleLocation html in the info Div. 
	 */
	this.showInfoHTML = function(request)
	{
		eval('var result = ' + request.responseText);

		if (!mapsObjects[result['classID']].showInfo)
			mapsObjects[result['classID']].showInfo = 'balloon';

		if (mapsObjects[result['classID']].showInfo == 'div' && mapsObjects[result['classID']].infoDiv)
		{
			mapsObjects[result['classID']].infoDiv.innerHTML = result['infoHTML'];
		}
		if (mapsObjects[result['classID']].showInfo == 'balloon')
		{
			this.popupHtml = result['infoHTML'];
			mapsObjects[result['classID']].markerList[result['markerID']].openInfoWindowHtml(popupHtml);	
		}
		
		if(document.getElementById('locationDirection'))
			document.getElementById('locationDirection').style.display = 'block';
	}
	
	this.drawCircle = function(latitude, longitude, radius, strokeColor, strokeWeight, strokeOpacity, fillColor, fillOpacity)
	{
		if(!latitude || !longitude || !radius)
			return;
		
		var point = new GLatLng(latitude, longitude);
		var polyPoints = Array();
		var boundaryBox = new GLatLngBounds();
		
		radius *= 1.609344;
			
		var mapNormalProj = G_NORMAL_MAP.getProjection();
		var mapZoom = this.mapObject.getZoom();
		var pixel = mapNormalProj.fromLatLngToPixel(point, mapZoom);
		
		var polyNumSides = 90;
		var polySideLength = 4;
		
		for (var a = 0; a<(polyNumSides+1); a++) 
		{
			var aRad = polySideLength*a*(Math.PI/180);
			var polyRadius = radius; 
			var pixelX = pixel.x + polyRadius * Math.cos(aRad);
			var pixelY = pixel.y + polyRadius * Math.sin(aRad);
			var polyPixel = new GPoint(pixelX, pixelY);
			var polyPoint = mapNormalProj.fromPixelToLatLng(polyPixel, mapZoom);
			
			polyPoints.push(polyPoint);
			
			boundaryBox.extend(polyPoint);
		}
		
		var circle = new GPolygon(polyPoints, strokeColor, strokeWeight, strokeOpacity, fillColor, fillOpacity);
		this.mapObject.addOverlay(circle);
		
		this.mapObject.setCenter(boundaryBox.getCenter(), this.mapObject.getBoundsZoomLevel(boundaryBox));
	}
	
	/*
	 * Constructor 
	 */
	if (typeof GBrowserIsCompatible != 'function' || !GBrowserIsCompatible())
		return false;

	this.markerImages 	= new Array();
	this.markerList		= new Array();
	this.clusterers		= new Array();
	this.ID				= id;
	this.infoDiv		= $('locationInfo'+this.ID);
	this.showInfo		= showInfo;
	
	
	this.loadMap(startCoordX, startCoordY, controlType, terrainType, zoom);
}

function toggleCategoryDiv( element )
{
	if( $('categoryselect').style.height == 'auto' ){
		$('categoryselect').style.height = '85px';
		element.innerHTML = 'Bekijk meer';
		element.removeClassName('ui-buttonUp');
		element.addClassName('ui-buttonDown');
	}else{
		$('categoryselect').style.height = 'auto';
		element.innerHTML = 'Bekijk minder';
		element.removeClassName('ui-buttonDown');
		element.addClassName('ui-buttonUp');
	}
}
