var map;
		var icon;
		var customMarker1;
		var customMarker2;
		var initualCustomArea = true;
		var customAreaReset = false;
		
	    function onLoad() {
	    
	    	map = new GMap2(document.getElementById("map"));
       	 	map.setCenter(new GLatLng(0,0), 3);
			map.setMapType(G_HYBRID_MAP);
			map.addControl(new GLargeMapControl());	 
	    	map.enableScrollWheelZoom();
	    	
	    	icon = new GIcon();
			icon.image = "icon.png";
			icon.shadow = "icon-shadow.png";
			icon.iconSize = new GSize(45, 45);
			icon.shadowSize = new GSize(70, 45);
			icon.iconAnchor = new GPoint(0, 45);
			icon.infoWindowAnchor = new GPoint(45, 0);
			
			var tinyIcon = new GIcon();
			tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
			tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			tinyIcon.iconSize = new GSize(12, 20);
			tinyIcon.shadowSize = new GSize(22, 20);
			tinyIcon.iconAnchor = new GPoint(6, 20);
			tinyIcon.infoWindowAnchor = new GPoint(5, 1);
			
			customMarker1 = new GMarker(new GLatLng(-60, -160), {draggable: true, icon: tinyIcon});
			customMarker2 = new GMarker(new GLatLng(-60, 160), {draggable: true, icon: tinyIcon});
			customMarker3 = new GMarker(new GLatLng(60, 160), {draggable: true, icon: tinyIcon});
			customMarker4 = new GMarker(new GLatLng(60, -160), {draggable: true, icon: tinyIcon});

			GEvent.addListener(customMarker1, "drag", function() {
	    		long1 = customMarker1.getLatLng().lng();
	    		lat1 = customMarker1.getLatLng().lat();	    		
	    		customMarker2.setLatLng(new GLatLng(lat1, customMarker2.getLatLng().lng()));
	    		customMarker4.setLatLng(new GLatLng(customMarker4.getLatLng().lat(),long1));
	    		drawLine();
  			});
  			
			GEvent.addListener(customMarker1, "dragend", function() {
	    		long1 = customMarker1.getLatLng().lng();
	    		lat1 = customMarker1.getLatLng().lat();
	    		customMarker2.setLatLng(new GLatLng(lat1, customMarker2.getLatLng().lng()));
	    		customMarker4.setLatLng(new GLatLng(customMarker4.getLatLng().lat(),long1));
	    		drawLine();
  			});
  			
			GEvent.addListener(customMarker2, "drag", function() {
	    		long2 = customMarker2.getLatLng().lng();
	    		lat1 = customMarker2.getLatLng().lat();
	    		customMarker1.setLatLng(new GLatLng(lat1,customMarker1.getLatLng().lng()));
	    		customMarker3.setLatLng(new GLatLng(customMarker3.getLatLng().lat(),long2));
	    		drawLine();
  			});
  			
			GEvent.addListener(customMarker2, "dragend", function() {
	    		long2 = customMarker2.getLatLng().lng();
	    		lat1 = customMarker2.getLatLng().lat();
	    		customMarker1.setLatLng(new GLatLng(lat1,customMarker1.getLatLng().lng()));
	    		customMarker3.setLatLng(new GLatLng(customMarker3.getLatLng().lat(),long2));
	    		drawLine();
  			});
  			
  			GEvent.addListener(customMarker3, "drag", function() {
	    		long2 = customMarker3.getLatLng().lng();
	    		lat2 = customMarker3.getLatLng().lat();
	    		customMarker2.setLatLng(new GLatLng(customMarker2.getLatLng().lat(),long2));
	    		customMarker4.setLatLng(new GLatLng(lat2, customMarker4.getLatLng().lng()));
	    		drawLine();
  			});
  			
			GEvent.addListener(customMarker3, "dragend", function() {
	    		long2 = customMarker3.getLatLng().lng();
	    		lat2 = customMarker3.getLatLng().lat();
	    		customMarker2.setLatLng(new GLatLng(customMarker2.getLatLng().lat(),long2));
	    		customMarker4.setLatLng(new GLatLng(lat2, customMarker4.getLatLng().lng()));
	    		drawLine();
  			});
  			GEvent.addListener(customMarker4, "drag", function() {
	    		long1 = customMarker4.getLatLng().lng();
	    		lat2 = customMarker4.getLatLng().lat();
	    		customMarker1.setLatLng(new GLatLng(customMarker1.getLatLng().lat(),long1));
	    		customMarker3.setLatLng(new GLatLng(lat2,customMarker3.getLatLng().lng()));
	    		drawLine();
  			});
  			
			GEvent.addListener(customMarker4, "dragend", function() {
	    		long1 = customMarker4.getLatLng().lng();
	    		lat2 = customMarker4.getLatLng().lat();
	    		customMarker1.setLatLng(new GLatLng(customMarker1.getLatLng().lat(),long1));
	    		customMarker3.setLatLng(new GLatLng(lat2,customMarker3.getLatLng().lng()));
	    		drawLine();
  			});
	      	
			polyline=null;
			
			window.onload = externalLinks;
	    	//refreshPosition();
			
			
	    }
	    
	    function refreshPosition() {
	    	document.getElementById('welcome').style.visibility='hidden';	
	    	document.getElementById('adspace').style.visibility='visible';	
	    
	    	map.clearOverlays();
	    	
	    	if(document.getElementById("northamerica").checked==true){
	    		long1 = -169;
	    		lat1 = 11;
	    		long2 = -52;
	    		lat2 = 73;
	    		showBounds = true;
	    	}else if(document.getElementById("southamerica").checked==true){
	    		long1 = -92;
	    		lat1 = -56;
	    		long2 = -34;
	    		lat2 = 14;
	    		showBounds = true;
	    	}else if(document.getElementById("europe").checked==true){
	    		long1 = -30;
	    		lat1 = 34;
	    		long2 = 37;
	    		lat2 = 72;
	    		showBounds = true;
	    	}else if(document.getElementById("africa").checked==true){
	    		long1 = -18;
	    		lat1 = -35;
	    		long2 = 52;
	    		lat2 = 38;
	    		showBounds = true;
	    	}else if(document.getElementById("asia").checked==true){
	    		long1 = 30;
	    		lat1 = 0;
	    		long2 = 180;
	    		lat2 = 73;
	    		showBounds = true;
	    	}else if(document.getElementById("oceania").checked==true){
	    		long1 = 92;
	    		lat1 = -47;
	    		long2 = 180;
	    		lat2 = 8;
	    		showBounds = true;
	    	}else if(document.getElementById("custom").checked==true){
	    		if(initualCustomArea){	
   					fitCustomAreaToScreen();
		   			initualCustomArea = false;
		   			customAreaReset = true;
	    		}else{
	    			long1 = customMarker1.getLatLng().lng();
	    			lat1 = customMarker1.getLatLng().lat();
	    			long2 = customMarker3.getLatLng().lng();
	    			lat2 = customMarker3.getLatLng().lat();	    			
	    		}
	    		
	    		showBounds = true;
	    		map.addOverlay(customMarker1);
	    		map.addOverlay(customMarker2);
	    		map.addOverlay(customMarker3);
	    		map.addOverlay(customMarker4);
	    		
	    	}else{
		    	long1 = -180;
		    	lat1 = -80;
		    	long2 = 180;
		    	lat2 = 80;
		    	showBounds = false;
	    	}
	    	
	    	
	    	if(showBounds==true){
		    	drawLine();
			}	
			
	    	var long = Math.random()*(long2-long1)+long1;
	    	var lat = Math.random()*(lat2-lat1)+lat1;
	    
	    	if(document.getElementById("custom").checked==true){
	    		if(!customAreaReset){
	    			map.setCenter(new GLatLng(lat,long));
	    		}else{
	    			customAreaReset = false;
	    		}
	    		document.getElementById("resetCustomArea").style.visibility = "visible";
	    	}else{
	    		map.setCenter(new GLatLng(lat,long), 3);
	    		document.getElementById("resetCustomArea").style.visibility = "hidden";
	    	}
	    	    	
	    	var pointer = new GMarker(new GLatLng(lat,long),icon);
	    	
			map.addOverlay(pointer);
			
			var text = 'Latitude: '+lat+' N;<br />Longitude: '+long+' E';
	       	GEvent.addListener(pointer, 'click', function() {
	        	pointer.openInfoWindowHtml(text);
	      	});
	      	
		}
		
		function drawLine(){
			if(polyline){
				map.removeOverlay(polyline);
			}
			
			polyline = new GPolyline([
			    new GLatLng(lat1, long1),
			    new GLatLng(lat1, long1*0.5+long2*0.5),
			    new GLatLng(lat1, long2),
			    new GLatLng(lat2, long2),
			    new GLatLng(lat2, long1*0.5+long2*0.5),
			    new GLatLng(lat2, long1),
			    new GLatLng(lat1, long1)
				], "#FFFF00", 3);
			
			map.addOverlay(polyline);
		}
		
		function resetCustomArea(){			
   			fitCustomAreaToScreen();
		   	customAreaReset = true;   			
   			refreshPosition();
	    }
	    
	    function fitCustomAreaToScreen(){
			if(map.getZoom()<2){
				map.setZoom(2);
			}
   			
   			var screen1 = map.fromContainerPixelToLatLng(new GPoint(100, 50));
   			var screen2 = map.fromContainerPixelToLatLng(new GPoint(map.getSize().width-200, map.getSize().height-100));
   			
   			long1 = screen1.lng();
		   	lat1 = screen1.lat();
		    long2 = screen2.lng();
		   	lat2 = screen2.lat();	
		   		
   			customMarker1.setLatLng(new GLatLng(lat1,long1));
   			customMarker2.setLatLng(new GLatLng(lat1,long2));
   			customMarker3.setLatLng(new GLatLng(lat2,long2));
   			customMarker4.setLatLng(new GLatLng(lat2,long1));
   		}
   		
   		function showAboutWindow(){
   			document.getElementById("about").style.visibility = "visible";
   			showContact();
   		}
   		
   		function closeAboutWindow(){
   			document.getElementById("about").style.visibility = "hidden";
   			document.getElementById("about-text-contact").className = "about-content";
   			document.getElementById("about-text-about").className = "about-content";
   			document.getElementById("about-text-privacy").className = "about-content";
   		}
   		
   		function showContact(){
   			document.getElementById("about-header-contact").className = "about-header-active";
   			document.getElementById("about-header-about").className = "about-header";
   			document.getElementById("about-header-privacy").className = "about-header";
   			document.getElementById("about-text-contact").className = "about-content-active";
   			document.getElementById("about-text-about").className = "about-content";
   			document.getElementById("about-text-privacy").className = "about-content";
   		}
   		
   		function showAbout(){
   			document.getElementById("about-header-contact").className = "about-header";
   			document.getElementById("about-header-about").className = "about-header-active";
   			document.getElementById("about-header-privacy").className = "about-header";
   			document.getElementById("about-text-contact").className = "about-content";
   			document.getElementById("about-text-about").className = "about-content-active";
   			document.getElementById("about-text-privacy").className = "about-content";
   		}
   		
   		function showPrivacy(){
   			document.getElementById("about-header-contact").className = "about-header";
   			document.getElementById("about-header-about").className = "about-header";
   			document.getElementById("about-header-privacy").className = "about-header-active";
   			document.getElementById("about-text-contact").className = "about-content";
   			document.getElementById("about-text-about").className = "about-content";
   			document.getElementById("about-text-privacy").className = "about-content-active";
   		}
   		
   		function externalLinks() {
		 if (!document.getElementsByTagName) return;
		 var anchors = document.getElementsByTagName("a");
		 for (var i=0; i<anchors.length; i++) {
		   var anchor = anchors[i];
		   if (anchor.getAttribute("href") &&
		       anchor.getAttribute("rel") == "external")
		     anchor.target = "_blank";
		 }
		}