function switchSearchCondition(condition) {
	$('li#' + condition + ' ul').toggle();
	$('li#' + condition).toggleClass('current');
}

function addSelection(selecting_element, selector, new_selection) {
	var current_value = $('#selector_'+selector).val();
	if ( current_value.indexOf(new_selection)>=0 ) return true;
	
	var current_selection = current_value!="" ? current_value.split("|") : new Array();
	current_selection[current_selection.length] = new_selection;
	
	selecting_element.style.fontWeight = 'bold';

	return $('#selector_'+selector).val( current_selection.join("|") );
}

function delSelection(selector, selection) {
	var current_value = $('#selector_'+selector).val();
	if ( current_value.indexOf(selection)<0 ) return true;
	
	var current_selection = (current_value!="" ? current_value.split("|") : new Array());
	var new_selection = new Array();
	for ( var cs_i=0; cs_i<current_selection.length; cs_i++ ) {
		if ( current_selection[cs_i] != selection ) {
			new_selection[new_selection.length] = current_selection[cs_i];
		}
	}
	
	return $('#selector_'+selector).val( new_selection.join("|") );
}


var currentMarker = null;


function createProjectMarker(country, map) {
    
	if( country.html.projects.length == 0 ) return;
	var infoHtml = {projects: []};

	for( var k in country.html ) {
		for( var i in country.html[k] ) {
			switch( k )
			{
				case 'projects':
					infoHtml[k].push('<li><a href="' + projectLink[k] + country.html[k][i].id + '&name=' + country.html[k][i].title.toLowerCase().replace(/[^a-zA-Z0-9]/g,'-') + '"">' + country.html[k][i].title + '</a></li>');
					break;
				default:
					infoHtml[k].push('<a href="' + projectLink[k] + country.html[k][i] + '">' + '</a>');
					break;
			}
		}
	}

	var icon = new GIcon();
	//console.log(base_dir);
	icon.shadow = base_dir+"img/maps/shadow50.png";
	icon.image  = base_dir+"img/maps/marker.png";
	icon.iconSize = new GSize(20, 34);
	icon.shadowSize = new GSize(37, 34);
	icon.iconAnchor = new GPoint(9, 34);
	icon.infoWindowAnchor = new GPoint(9, 2);
	icon.infoShadowAnchor = new GPoint(18, 25);

   var ttCountry = country.name;
   

	var point  = new GLatLng(country.lat, country.lng);
	var marker = new GMarker(point, {icon: icon, title: ttCountry});

	GEvent.addListener(marker, "click", function()
	{
		if( currentMarker && currentMarker.setImage ) {
			try {
				currentMarker.setImage(base_dir+'img/maps/marker.png');
			} catch(e){}
		}
		if( marker && marker.setImage ) {
			marker.setImage(base_dir+'img/maps/marker_on.png');
			currentMarker = marker;
		}

		document.getElementById('selected_country').innerHTML = 
			'<li class="nolink"><img src="img/icons/icon_marker_projects_search.gif" alt="" /> ' + country.name
				+ '<ul>'
					+ infoHtml.projects.join('\n')
					+ '<li class="more"><a href="' + projectLink.allprojects + 'country=' + country.code + '&name=' + country.name.toLowerCase().replace(/[^a-zA-Z0-9]/g,'-') + '">'
					+ document.getElementById('more_projects_in').innerHTML + ' (' + country.projectcount + ')</a></li>'
				+ '</ul>'
			+ '</li>';
			/*'<h2><img src="img/maps/marker.gif" align="bottom" width="13" height="22" style="margin-right: 1px;" valign="bottom" />' + country.name + '</h2>'
			+ '<ul>' + infoHtml.projects.join('\n') + '<li class="allprojects"><a href="' + projectLink.allprojects + 'country=' + country.code + '&name=' + country.name.toLowerCase().replace(/[^a-zA-Z0-9]/g,'-') + '">'
			+ document.getElementById('more_projects_in').innerHTML + ' ' + country.name + ' (' + country.projectcount + ') &rsaquo;</a></li></ul>';*/

		$('#selected_country').show();
		map.panTo(point);
		if( map.getZoom() < 4 ) {
			map.setZoom(4);
			map.setCenter(point);
		}
	});

	GEvent.addListener(marker, "dblclick", function()
	{
		if( map.getZoom() == 4 ) {
			document.location.href = projectLink.allprojects + '&country=' + country.code + '&name=' + country.name.toLowerCase().replace(/[^a-zA-Z0-9]/g,'-');
		}
	});

	return marker;
}
var map;
function initGoogleMaps() {
	if( GBrowserIsCompatible() ) {
		map = new GMap2(document.getElementById("projects_search_google_maps"));
		var mt  = map.getMapTypes();
		var zrn = [1, 4];

		customZoomControl = document.createElement('div');
		customZoomControl.style.position = 'absolute';
		customZoomControl.style.left = '5px';
		customZoomControl.style.top  = '5px';

		var customZoomOutControl = document.createElement('img');
		var customZoomInControl  = document.createElement('img');

		with( customZoomOutControl.style ) { display = 'block'; marginBottom = '3px'; cursor = 'pointer'; }
		with( customZoomInControl.style ) { display = 'block'; cursor = 'pointer'; }

		customZoomOutControl.src     = base_dir+'img/maps/gm_zoomout.png';
		customZoomInControl.src      = base_dir+'img/maps/gm_zoomin.png';
		customZoomInControl.onclick  = function() { map.zoomIn(); };
		customZoomOutControl.onclick = function() { map.zoomOut(); };
		customZoomInControl.title    = 'Inzoomen';
		customZoomOutControl.title   = 'Uitzoomen';

		customZoomControl.appendChild(customZoomInControl);
		customZoomControl.appendChild(customZoomOutControl);

		map.getContainer().appendChild(customZoomControl);
		map.enableScrollWheelZoom();
		map.enableContinuousZoom();
		new GKeyboardHandler(map);
		map.setCenter(new GLatLng(17.0000, 10.0000), 1);

		for( var i = 0; i < mt.length; i++ ) {
			mt[i].getMinimumResolution = function() { return zrn[0]; }
			mt[i].getMaximumResolution = function() { return zrn[1]; }
		}

		GEvent.addListener(map, 'move', function() { limitMapBounds(map); });
		GEvent.addListener(map, 'zoomend', function() { limitMapBounds(map); });

	}
}

function limitMapBounds(map) {
	var curCenter    = map.getCenter();
	var curZoom      = map.getZoom();
	var curLatitude  = curCenter.lat();
	var curLongitude = curCenter.lng();
	var maxLatTop    = [0.000000, 63.233627, 78.420193, 82.425629];
	var maxLatBottom = [0.000000, -64.168106, -78.630005, -82.494823];

	if( curLatitude > maxLatTop[curZoom] ) {
		map.setCenter(new GLatLng(maxLatTop[curZoom], curLongitude));
	} else if (curLatitude < maxLatBottom[curZoom] ) {
		map.setCenter(new GLatLng(maxLatBottom[curZoom], curLongitude));
	}
}

function jsonParseMarkers(countries) {
	//var json = eval(countries);
	if(!map) {
		setTimeout(function() {jsonParseMarkers(countries);}, 1000);
	} else {
		var markers = [];
		for( var i = 0; i < countries.length; i++ ) {
			markers.push(createProjectMarker(countries[i], map));
		}
		var cluster = new ClusterMarker(map, { 'markers': markers } );
		cluster.fitMapToMarkers();
	}
}

function hideProjectInfo() {
	$('#projectdata').hide();
}

function showProjectInfo() {
	$('#projectdata').show();
}

addEvent(window, 'load', initGoogleMaps);
