//Google Maps Functions 


var longi = 30.234375;
var zoom = 2;
var lat = 2.1088986592431382;
var markerList;
var markerConf;
var moveEndInfo;
var countryArray = [];
var map;



function init(){
	
	var centre = new GLatLng(0,77);
	
	test = $('map');
	
	if(test != 'null'){
	
		if(GBrowserIsCompatible()){
	
			map = new GMap2(document.getElementById('map'), mapTypes = G_HYBRID_MAP);
		
			map.enableDoubleClickZoom();
		
			var loc = new GLatLng(lat, longi);
		
			map.setCenter(loc, zoom, G_HYBRID_MAP);
		
			map.addControl(new GSmallMapControl);
		
			getAllProjs(map);
		
			//beginning of listener for clicks
			/*
				GEvent.addListener(map, 'dblclick', function(overlay, latlng){	
				
					GEvent.clearListeners(map, 'moveend');
						
						if(typeof(latlng) != 'undefined'){	
						
							lat = latlng.lat();
			
							longi = latlng.lng();
					
							loc = new GLatLng(lat, longi);
					
						}else{
						
							loc = latlng;
						
						}
				
						map.panTo(loc);
			
						new Ajax.Request('admin/maps_module/code/geocode_request.php', {method:'get', parameters: {lat: lat, lng: longi}, onSuccess:function(transport){
							
							//alert(transport.responseText);
						
							if(transport.responseText == 'no'){
							
								lat = lat.toFixed(3);
							
								longi = longi.toFixed(3);
							
								//alert(lat+'|'+longi);
							
								new Ajax.Request('admin/maps_module/code/geocode_request.php', {method:'get', parameters: {lat: lat, lng: longi}, onSuccess:function(transport){
								
									if(transport.responseText == 'no'){
									
										alert('We\'re sorry we were not able to Geocode your request correctly.\nPlease select another country.');
									
										if(map.getZoom() > 2){

											map.setZoom(2);

										}
									
										map.panTo(centre);
									
									}else{
									
									//map.setZoom(3);
								
									setCountrySize(transport.responseText, map);
									
									getMarkers(transport.responseText, map);
								
									}	
								
								}});
							
							}else{
							
								setCountrySize(transport.responseText, map);
							
								getMarkers(transport.responseText, map);
	
							}
						
					
						
						}});
		
				});
			
			
			GEvent.addListener(map, 'dragend', function(){
		
				map.closeInfoWindow();
		
			});*/
		
		
		}
	
	}
	
	
	
}



window.onload = init;	
window.onunload = GUnload();

function getMarkers(countryCode, map){
	
		var flag = 0;
		//checks the array if the country has been populated with this countrycode.
		for(i=0; i < countryArray.length; i++){
			
			if(countryArray[i] == countryCode){
				
				flag++;
				
			}
			
		}
	
	if(flag == 0){
	
			new Ajax.Request('cms_includes/project_manager/code/getProjectsJson.php', {method:'get', parameters: {country: countryCode}, onSuccess:function(transport){
		
				json = transport.responseText;
		
				eval(json);
		
				if(markers.length == 0){
				
						//map.closeInfoWindow();
				
						GEvent.addListener(map, 'moveend', function(){
						
							map.openInfoWindowHtml(map.getCenter(), '<p style="color:black;">There are no projects for this country,<br/> please select another.</p>');
		
						});
				
						res = map.getInfoWindow();
				
						infoWindowListen = GEvent.addListener(res, 'closeclick', function(){
					
							if(map.getZoom() > 3){
						
								map.setZoom(3);	
							}
					
							map.closeInfoWindow();
					
							GEvent.removeListener(infoWindowListen);
					
						});
			
				}else{
		
					for(id in markers){
			
						if(typeof(markers[id].lat) != 'undefined'){
			
						content = '<p class="infoWindowTitle">'+markers[id].proj_name+'</p><p class="infoWindowDesc">'+markers[id].desc+'</p><p><a class="infoLink" href="project:'+ encodeURI(markers[id].proj_name)+'">Click to View</a></p>';
			
						m = mMarker(markers[id].lat, markers[id].lng, content, '');
			
						map.addOverlay(m);
			
						}
			
					}
		
				}
	
			}});
			//Pushes the country code into an array 
			countryArray.push(countryCode);
		}
	}

function setCountrySize(code, map){
		
		new Ajax.Request('admin/maps_module/code/getCountryBounds.php', {method:'get', parameters: {countryCode : code}, onSuccess:function(transport){

			var bounds = transport.responseText.split('|');
			
			var ne = new GLatLng(bounds[1], bounds[2]);
			
			var sw = new GLatLng(bounds[3], bounds[0]);
			
			box = new GLatLngBounds(sw, ne);
			
			zoomLevel = map.getBoundsZoomLevel(box);

			map.setZoom(zoomLevel);
			
			map.panTo(box.getCenter());
			
			$('zoom_level').innerText = 'Zoom Level: '+map.getZoom();
			
		}});
	
	}
	
	
function mMarker(lat, lng, info, notFound){

	if(notFound == ''){
	
		randomNo = Math.floor(Math.random()*12);
		
		opts = {icon: baseIcons[randomNo]};
	
		var marker = new GMarker(new GLatLng(lat, lng), opts);
	
		GEvent.addListener(marker, 'click', function(){
		
			marker.openInfoWindowHtml(unescape(info));
		
		});
	
	}else{
		
		var marker = new GMarker(lat, lng);
		
		marker.openInfoWindowHtml(info);
		
	}
	
	return marker;
	
	}
	
function getAllProjs(map){
	
		if(typeof(allProjs) != 'undefined'){
			
			for(id in allProjs){
				
				content = '<p class="infoWindowTitle">'+allProjs[id].proj_name+'</p><p class="infoWindowDesc">'+allProjs[id].desc+'</p><p><a class="infoLink" href="project.'+encodeURI(allProjs[id].proj_name)+'">Click to View</a></p>';
				
				if(typeof(allProjs[id].proj_name) != 'undefined'){
	
					m = mMarker(allProjs[id].lat, allProjs[id].lng, content, '');
				
					map.addOverlay(m);
				
				}
				if((allProjs[id].countryCode != '') || (allProjs[id].countryCode != '0')){	
				
					countryArray.push(allProjs[id].countryCode);
					
				}
				
			}
			
		}
	
	}
	
function showClients(id){
		
		$('global_projects_p').style.display = 'none';
		
		$('expert_global_p').style.display = 'none';
		
		map.clearOverlays();
		
		map.setZoom(2);
		
		map.closeInfoWindow();
		
		countryArray.length = 0;
		
		value = $F(id);
		
		new Ajax.Request('cms_includes/project_manager/code/getClientResults.php',{ method : 'get', parameters : {id : value}, onSuccess:function(transport){
			
			eval(transport.responseText);
			
			//alert(results);
			
			if(results.length == 0){
				
				map.openInfoWindowHtml(map.getCenter(), '<p class="infoWindowDesc">There are no regional projects for this client</p>');
				
			}else{
				
				for(id in results){
					
					//alert(results[id].pType);
					
					if(results[id].pType != 'global'){
					
						if(typeof(results[id].pName) != 'undefined'){
				
							content = '<p class="infoWindowTitle">'+results[id].pName+'</p><p class="infoWindowDesc">'+results[id].desc+'</p><p><a class="infoLink" href="project.'+results[id].pName+'">Click to View</a></p>';
					
							m = mMarker(results[id].lat, results[id].lng, content, '');
				
							map.addOverlay(m);
													
						}
				
					}
				
						
					
				}
			
				if(globalResults.length != 0){
			
					$('global_projects_p').style.display = 'block';
					
					globSelect = $('global_projects');
					
					if(globSelect.options.length != 0){
						
						globSelect.options.length = 0;
						
					}
					
					globSelect.options[globSelect.options.length] = new Option('Please Choose...', '');
					
					for(id in globalResults){	
						
						//alert(typeof(globalResults[id].pName));
						
						if(typeof(globalResults[id].pName) != 'undefined'){
						
							globSelect.options[globSelect.options.length] = new Option(globalResults[id].pName, globalResults[id].pName);
						
						}
						
					}
					
				}
					
				
				
			}
			
		}});
	
	
	
	}
	
	
	
	function showExperts(id){

			value = $F(id);

			new Ajax.Request('cms_includes/project_manager/code/getExpertResults.php',{ method : 'get', parameters : {id : value}, onSuccess:function(transport){

				//alert(transport.responseText);

				eval(transport.responseText);

				if(results.length == 0){

					map.openInfoWindowHtml(map.getCenter(), '<p class="infoWindowDesc">There are no regional projects for this client</p>');

				}else{

					for(id in results){

						//alert(results[id].pType);

						if(results[id].pType != 'global'){

							if(typeof(results[id].pName) != 'undefined'){

								content = '<p class="infoWindowTitle">'+results[id].pName+'</p><p class="infoWindowDesc">'+results[id].desc+'</p><p><a class="infoLink" href="project.'+results[id].pName+'">Click to View</a></p>';

								m = mMarker(results[id].lat, results[id].lng, content, '');

								map.addOverlay(m);

							}

						}



					}

					if(globalResults.length != 0){

						$('expert_global_p').style.display = 'block';

						globSelect = $('expert_global');

						if(globSelect.options.length != 0){

							globSelect.options.length = 0;

						}

						globSelect.options[globSelect.options.length] = new Option('Please Choose...', '');

						for(id in globalResults){	

							//alert(typeof(globalResults[id].pName));

							if(typeof(globalResults[id].pName) != 'undefined'){

								globSelect.options[globSelect.options.length] = new Option(globalResults[id].pName, globalResults[id].pName);

							}

						}

					}



				}

			}});



		}
		
		
function goTo(id){
	
		val = $F(id);
	
		url = 'project.'+val;
	
		location.href = url;
	
	}