// Create geocoder
var loaded = false;
var start;
var markerItems = new Array();
var map;
var init = true;
var startplacingmarkers = false;
var t;

// ->
var first_item_lat;
var first_item_lng;
var mm;
var mapZoomLevel = 3;
var actual = '';

var slab_0 = [];
var slab_1 = [];      
var slab_2 = [];      
var slab_3 = [];      
var slab_4 = [];
var slab_5 = [];
var slab_6 = [];
var slab_7 = [];
var slab_8 = [];
var slab_9 = [];
var slab_10 = [];
var slab_11 = [];
var slab_12 = [];
var slab_13 = [];
var slab_14 = [];
var slab_15 = [];
var slab_16 = [];
var slab_17 = [];

var isInLoop=false;
var pageZoomLevel = 8;
var emptySearchRecordset = false;
		

// problem with MarkerManager -> http://groups.google.ws/group/Google-Maps-API/browse_thread/thread/0c0d31250392ee20/35cb2a08ae494e78
		
function initialize(country) {
	
	var zoomed = false;
	var queryZoom = '';
	
	if (GBrowserIsCompatible()) {

		// Create map element
		map = new GMap2(document.getElementById("map_content_area"));
		
		
		GEvent.addListener(map, "zoomend", function(oldzoomlevel,zoomlevel) {
			mapZoomLevel = zoomlevel;			
			if(typeof mm != 'undefined'){
				document.getElementById("zoomlevel").innerHTML=zoomlevel + " - " + mm.getMarkerCount(zoomlevel);
			}			
		});
		
		GEvent.addListener(map, "moveend", function() {			
			//getMarkersInArea();
		});
		
				
		// Got to active country
		setMapToCountry(country, map);
		
		// map.setCenter(center, 8);
		
		// enable mouse scrolling
		map.enableScrollWheelZoom();
		
		//map.setCenter(start, 8);
		
		// Add controls
		map.addControl(new GMapTypeControl());
		map.addControl(new GLargeMapControl());
		
		/* Check if there has been a search or the first call */
		if (getURLParameter('query_city') != '' || getURLParameter('themes') != '' || getURLParameter('isfilter') != '' || getURLParameter('keywords') != '' || getURLParameter('query_country') != '' || getURLParameter('query_city') != '') {
			init = false;
		}
		
		// Give the map time to load (since there's no no callback function from google maps)
		if(getURLParameter('keywords') != '')
			window.setTimeout('placeMarkers()', 1000);
		else
			window.setTimeout('placeHomeMarkers()', 1000);
		
	}
}

/*
function startPlacingMarkers(){
	if(startplacingmarkers==true)
		window.setTimeout('placeMarkers()', 2000);
}
*/


function placeHomeMarkers(){
	var pointer = '';
	if (markerItems.length > 0) {
		// Set markers that have been passed
		for (i = 0; i < markerItems.length; i++) {
			

			item_details = markerItems[i].split('[%%]');

			if (i == 0) {
				first_item_lat = item_details[0];
				first_item_lng = item_details[1];
			} 
						
			pointer = eval("slab_" + item_details[5]);
			output = setMarkerByCoordinates(item_details[0], item_details[1], item_details[2], item_details[3], item_details[4]);
			pointer.push(output);			
			
		}
		
		clearTimeout(t);
		
		mm = new GMarkerManager(map);
		for (i = 0; i <= 17; i++) {
			pointer = eval("slab_" + i);
			if(pointer.length>0)
				mm.addMarkers(pointer,i,17);				
		}
		mm.refresh();
		
		if(getURLParameter('keywords') == '')
			map.setCenter(new GLatLng(first_item_lat, first_item_lng), 2);
		else
			map.setCenter(new GLatLng(first_item_lat, first_item_lng), parseInt(pageZoomLevel));
			
		if(emptySearchRecordset)
			map.setCenter(new GLatLng(first_item_lat, first_item_lng), 2);
	} else {
		// just some random values				
		map.setCenter(new GLatLng("40.5208003", "-3.7756818"), 2);
	}

}

function placeMarkers() {

	placeHomeMarkers();
	
	return;
	
	var output = false;
			
	if (markerItems.length > 0) {
		
		// Set markers that have been passed
		for (i = 0; i < markerItems.length; i++) {
		
			item_details = markerItems[i].split('[%%]');

			if (i == 0) {
				first_item_lat = item_details[0];
				first_item_lng = item_details[1];
				map.setCenter(new GLatLng(first_item_lat, first_item_lng), 8);
			} 
						
			output = setMarkerByCoordinates(item_details[0], item_details[1], item_details[2], item_details[3], item_details[4]);
			/*
			if(output==false)
				output = setMarkerByCoordinates(item_details[0], item_details[1], item_details[2], item_details[3], item_details[4]);
			*/
		}
		
		/*
		if (init == false)
			map.setCenter(new GLatLng(first_item_lat, first_item_lng), 12);
					
		clearTimeout(t);
		*/
		
		
		
	}
}

function setMapZoomLevel(level){
	pageZoomLevel = level;
}

function setMapToCountry(country) {

	return;
	
	geocoder = new GClientGeocoder();
	
	// Get coordinates for active country 
	geocoder.getLatLng(
		country,
    	function(startpoint) {
      		if (!startpoint) {
      			// Set map to default
        		map.setCenter(new GLatLng(25, 10), 2);
      		} else {
      			// Set map to coordinates
        		map.setCenter(startpoint, 8);
      		}
    	}
  	);
  	
}

function setMarkerByAddress(item, map, country, lastitem) {
	
	if (item[1] != undefined) {
		geocoder.getLatLng(
	    	item[1] + ", " + country,
	    	function(point) {
	      		if (point) {
	      			
	      			//var marker = new GMarker(point);
	      			var marker = createCustomMarker(item, map, point);
	      			
	      			map.addOverlay(marker);
	      			
	      			GEvent.addListener(marker, "click", function() {
						openVideoWindow(marker, map, item);
					});
	      			
	      			if (lastitem == true) {
	      				map.setCenter(point);
	      			}   			
	      		}
	      	}
		);
	}
}

var clickedMarker = '';
function setMarkerByCoordinates(lat, lng, color, infoWindow, infoWindowShort) {
	
	if(infoWindowShort.length<20){
		return false;
	}
	
	addresspoint = new GLatLng(lat, lng);
	var marker = createCustomMarker(color, addresspoint);
	
	GEvent.addListener(marker, "click", function() {
		openVideoWindow(marker, infoWindow);
		clickedMarker = marker;
	});
	
	GEvent.addListener(marker, "mouseover", function() {
		 // Open infowindow only if it's closed or the mouse moves over another marker
		 if(map.getInfoWindow().isHidden() || actual != marker ) { 
		   openVideoWindow(marker, infoWindowShort);
		 }		 

		 actual = marker;		 
	});
	
	GEvent.addListener(marker, "mousedown", function() {
		 // Open infowindow only if it's closed or the mouse moves over another marker
		 if(map.getInfoWindow().isHidden() || actual != marker ) { 
		   openVideoWindow(marker, infoWindowShort);
		 }		 

		 actual = marker;		 
	});

	
	GEvent.addListener(marker, "mouseout", function() {
		if(marker!=actual)
			marker.closeInfoWindow();
		else if(clickedMarker!=marker)
			marker.closeInfoWindow();
		else
			clickedMarker = '';
	});		

	/* -> code commented */
	if (map.isLoaded() == true)
		map.addOverlay(marker);
	
	return marker;
}

function createMarkerByCoordinates(lat, lng, color, infoWindow, infoWindowShort, markerZoomLevel) {
	
	var markerItem = lat + '[%%]' + lng + '[%%]' + color + '[%%]' + infoWindow + '[%%]' + infoWindowShort + '[%%]' + markerZoomLevel + '[%%]';
	markerItems.push(markerItem);
	
}

function openVideoWindow(marker, infoWindow) {
	marker.openInfoWindowHtml(infoWindow);       
}

// we show address on mouse over
function openShortInfo(marker, shortInfos) {
	marker.openInfoWindowHtml(shortInfos);
}

var markerImportance = 1;

function createCustomMarker(color, point) {
	
	var icon = new GIcon(G_DEFAULT_ICON);
	icon.image = "/images/map/ARW_" + color + ".png";
	icon.iconSize = new GSize(36, 36);
	icon.imageMap = [0,0, 18,0, 18,36, 0,18]; // http://orb-of-knowledge.blogspot.com/2007/11/google-maps-gicon-imagemap-property.html
	markerOptions = {icon:icon, zIndexProcess:importanceOrder};
	marker = new GMarker(point, markerOptions);
	marker.importance = markerImportance;
	
	markerImportance++;	
	return marker;
		
}

function importanceOrder (marker,b) {
	return marker.importance;
}



function zoomTo(parameter) {
	geocoder = new GClientGeocoder();
	
	// Get coordinates for active country 
	geocoder.getLatLng(
		parameter,
    	function(startpoint) {
     		// Set map to coordinates
       		map.setCenter(startpoint, 12);
    	}
  	);
}

