

// Mouse over a marker on the map
function mouseOverMarker(id) {
	if(id !== null){
		$("li[id=" + id + "]").addClass("selected");
	}
}

// Mouse out a marker on the map
function mouseOutMarker(id){
	if(id !== null){
		$("li[id=" + id + "]").removeClass("selected");
	}
}


// Add a HTML block to the mapContentList
function setMapList(request)
{
	$("#mapContentList").append(request);
}

/**
 * Plaats Maps object
 */
$.plaatsMaps = {

	/**
	 * The categories which are plotted on the map
	 */
	categories: null,

	/**
	 * The categories which are plotted on the map
	 */
	mapReady: false,

	onReady: null,

	/**
	* Save the javascript markers
	*/
	markers: new Array(),
	markerQeue: true,

	/**
	 * Initialize the plaatsMaps object and load the Google map
	 */
	init: function() {

		/**
		* Bind maximize button
		*/
		$('#maximize').bind('click', function() {
			$.plaatsMaps.setContainerSize('max');
		});

		/**
		* Bind maximize button
		*/
		$('#minimize').bind('click', function() {
			$.plaatsMaps.setContainerSize('normal');
		});

		/**
		* Bind the hover of a item which is on the map
		*/
		$("div#mapList li").hover(function () {
			// get the category and the category id
			var itemData = $(this).attr('id').split('_');

			if(itemData.length == 2) {
				var category = itemData[0];
				var id = itemData[1];

				// set the flex hover
				var flexApp = FABridge.mapBridge.root();
				flexApp.setMarkerOverlay(id, category, 'over');
				mouseOverMarker(category + "_" + id);
			}

		},

		function () {
			// get the community id
			var itemData = $(this).attr('id').split('_');

			if(itemData.length == 2) {
				var category = itemData[0];
				var id = itemData[1];

				// clear the flex hover
				var flexApp = FABridge.mapBridge.root();
				flexApp.setMarkerOverlay(id, category, 'out');
				mouseOutMarker(category + "_" + id);
			}
		} );

	},
	// END INIT


	onMapReady: function() {

		this.mapReady = true;
		$("#categories input:checkbox:checked, #leisureCategories input:checkbox:checked").each(function (i) {
			//var category = $(this).val();
    		//$.plaatsMaps.toggleCategory(category);
    	});
    	this.plotMarkersQeued();

    	if ($.plaatsMaps.onReady != null) {
    		$.plaatsMaps.onReady();
    	}
	},

	enableControls: function(){
		var flexApp = FABridge.mapBridge.root();
		flexApp.enableControls(null,true);
	},

	/**
	* Create af marker. Set in que if the map is not ready yet
	* Plot on map if it is ready
	*/
	createMarker: function(point, category, identifier, markerImage, url, html) {
		marker = new Array();
		marker['point'] = point;
		marker['category'] = category;
		marker['identifier'] = identifier;
		marker['markerImage'] = markerImage;
		marker['url'] = url;
		marker['html'] = html;

		// If the map not is loaded, safe the marker in a list
		if (!this.mapReady){
			this.markers.push(marker);
		} else {
			$.plaatsMaps.plotMarker(marker);
		}
	},


	/**
	 * Center the map on the given coordinate (latlng).
	 */
	setCenter: function(point) {
		var flexApp = FABridge.mapBridge.root();
		flexApp.setCenter(point);
	},


	/**
	 * Deletes the marken with the given identifier.
	 */
	clearMarker: function(identifier) {
		var flexApp = FABridge.mapBridge.root();
		flexApp.clearMarker(identifier);
	},


	/**
	* Plot the marker on the queue. Only through the create marker method!!!!
	*/
	plotMarker: function(marker){
		var flexApp = FABridge.mapBridge.root();
		flexApp.createMarker(marker['point'], marker['category'],marker['identifier'],marker['markerImage'],marker['url'],marker['html']);
		//marker['point'],marker['category'],marker['identifier'],marker['markerImage'],marker['url'],marker['html']
	},

	/**
	* Place the markers in the queue
	*/
	plotMarkersQeued: function()
	{
		if (this.markerQeue && this.mapReady && this.markers.length > 0) {
			this.markerQeue = false;
			// Plot each queued marker on the map
			for (i = 0; i < this.markers.length; i++)
			{
				addMarker = this.markers[i];
				$.plaatsMaps.plotMarker(addMarker);
			}
			// Clear the queue
			this.markers = array();
		}
	},


	togglePolygons: function(locationType) {
		var flexApp = FABridge.mapBridge.root();
		// To do, get province id's dynamic
		flexApp.togglePolygons(locationType,new Array(1,2,3,4,5,6,7,8,9,10,11,12));
	},


	/**
	* Set the location string
	*/
	geoReverse: function(location) {

		var calbackFunctionSuccess = 'geoReverseSuccess';
		var callbackFunctionFailure = 'geoReverseFailure';

		var flexApp = FABridge.mapBridge.root();
		flexApp.geoReverse(location,callbackFunctionSuccess, callbackFunctionFailure);
	},

	// Check if an object is an array
	isArray: function(obj) {
	 	 if (obj.constructor.toString().indexOf("Array") == -1) {
			return false;
	 	 } else {
			return true;
	 	 }
	}
};