var markersBounds;
var allMarkers = [];
var allPMs;
var map;
var overview;
var DELAY_IN_MS = 50;

function load() {
	if (GBrowserIsCompatible()) {
		var mapDiv = document.getElementById('map'); 
		
		map = new GMap2(mapDiv);
		if(zoomControl=="GSmallZoomControl") map.addControl(new GSmallZoomControl());
		else if(zoomControl=="GSmallMapControl") map.addControl(new GSmallMapControl());
		else if(zoomControl=="GLargeMapControl") map.addControl(new GLargeMapControl());
		
		if(mapTypeControl){
			map.addMapType(G_PHYSICAL_MAP); 
			map.addControl(new GHierarchicalMapTypeControl());
		}	
		if(scaleControl) map.addControl(new GScaleControl());
		
		if(overviewControl){ 
			overview = new GOverviewMapControl(new GSize(overviewWidth, overviewHeight)); 
			map.addControl(overview, new GControlPosition(overviewAlign)); 
			setTimeout("checkOverview()",100);
		}	
		
		if(wheelZoom){ 
		  GMap2.prototype.wheelZoom = function(event){ 
		  	if((event.detail || -event.wheelDelta) < 0) map.zoomIn(); 
		  	else map.zoomOut(); 
		  	return false; 
		  } 
		  GEvent.addDomListener(mapDiv, "DOMMouseScroll", map.wheelZoom); 
		  GEvent.addDomListener(mapDiv, "mousewheel", map.wheelZoom); 
		} 
		
		GEvent.addListener(map, "click", function(overlay, point) { 
			if(overlay && !isNaN(overlay.pmIndex)){ // clicked on overlay with pmIndex (thus position point)
				overlay.openInfoWindowHtml(tooltipCalls[overlay.pmIndex]);
			}
		}); 
		
		
		map.setCenter(new GLatLng(defaultLat, defaultLon), 11, googleMapType);
			
		mark();
		showTracks();
		showWaypoints();
	}
}

function checkOverview() {
    var overmap = overview.getOverviewMap();
    if (overmap) {      
     var mapDiv = document.getElementById('map');
     var overviewElement = document.getElementById('map_overview'); 
			mapDiv.appendChild(overviewElement); 
    }else{
    	setTimeout("checkOverview()",100);
  	}
}



/*
***********************
Tracks
***********************
*/
function showTracks(){
	if(polylines.length > 0){
		window.setTimeout("showTracksLater()",(allPMs.length+2)*DELAY_IN_MS);//show tracks after markers are displayed
	}	
} 

function showTracksLater(){
	myProgBar.setBar(0);
	document.getElementById("progressBar").style.display="inline";
	for (var i = 0; i < polylines.length; i++) {
		window.setTimeout("showTrack("+i+")",i*DELAY_IN_MS*5);//bigger timeout for tracks than markers enables pogressBar to draw continiously
	}
} 

function showTrack(i){
	map.addOverlay(polylines[i]);
	myProgBar.setBar((i+1)/polylines.length);
	if(i+1 >= polylines.length) document.getElementById("progressBar").style.display="none";
} 
/*
***********************
Waypoints
***********************
*/
function showWaypoints(){
	if(waypoints.length > 0){
		window.setTimeout("showWaypointsLater()",(allPMs.length+polylines.length+2)*DELAY_IN_MS);//show waypoints after markers and tracks are displayed
	}	
} 

function showWaypointsLater(){
	myProgBar.setBar(0);
	document.getElementById("progressBar").style.display="inline";
	for (var i = 0; i < waypoints.length; i++) {
		window.setTimeout("showWaypoint("+i+")",i*DELAY_IN_MS*5);
	}
} 

function showWaypoint(i){
	map.addOverlay(waypoints[i]);
	myProgBar.setBar((i+1)/waypoints.length);
	if(i+1 >= waypoints.length) document.getElementById("progressBar").style.display="none";
} 
/*
***********************
Markers
***********************
*/
function mark(){
	allPMs = document.getElementsByTagName("pm");
	if(allPMs.length > 0){
		myProgBar.setBar(0);	
		document.getElementById("progressBar").style.display="inline";
		markersBounds = new GLatLngBounds();
		
		for (var i = 0; i < allPMs.length; i++) {
		  window.setTimeout("markOne("+i+")",i*DELAY_IN_MS);//timeout enables pogressBar to draw continiously
		}
		window.setTimeout("markCleanup()",(allPMs.length+1)*DELAY_IN_MS);
	}	
} 

function markOne(i){
		// obtain the attribues of each marker
		var lat = parseFloat(allPMs[i].getAttribute("lat"));
		var lon = parseFloat(allPMs[i].getAttribute("lon"));
		if(!isNaN(lat) && !isNaN(lon)){
			
			var point = new GLatLng(lat,lon);
			//try to add area infos
			var lat2 = parseFloat(allPMs[i].getAttribute("lat2"));
			var lon2 = parseFloat(allPMs[i].getAttribute("lon2"));
			if(!isNaN(lat2) && !isNaN(lon2)){
				var diagonal = new GLatLng(lat2,lon2);
				var points=[point, new GLatLng(lat2,lon), diagonal, new GLatLng(lat,lon2), point];//make square
				if(drawMarkers) map.addOverlay(new GPolyline(points, areaBorderColor, areaStroke, 1));
				markersBounds.extend(diagonal);
			}
			//try to add view line infos
			var latV = parseFloat(allPMs[i].getAttribute("latV"));
			var lonV = parseFloat(allPMs[i].getAttribute("lonV"));
			if(!isNaN(latV) && !isNaN(lonV)){
				var viewLine = new GLatLng(latV,lonV);
				var points=[point, viewLine];//make line
				if(drawMarkers) map.addOverlay(new GPolyline(points, areaBorderColor, areaStroke, 1));
				markersBounds.extend(viewLine);
			}
			//try to add view angle
			var latV2 = parseFloat(allPMs[i].getAttribute("latV2"));
			var lonV2 = parseFloat(allPMs[i].getAttribute("lonV2"));
			if(!isNaN(latV2) && !isNaN(lonV2)){
				var viewLine2 = new GLatLng(latV2,lonV2);
				var points=[point, viewLine2];//make second line (angle)
				if(drawMarkers) map.addOverlay(new GPolyline(points, areaBorderColor, areaStroke, 1));
				markersBounds.extend(viewLine2);
			}
			markersBounds.extend(point);
			if(drawMarkers){
				allMarkers[i] = createClickableMarker(point, i);
				map.addOverlay(allMarkers[i]);
			}	
			
		}
		
		myProgBar.setBar((i+1)/allPMs.length);	
		
}

function createClickableMarker(point, i) {
  var marker = new GMarker(point);
  marker.pmIndex = i;
  return marker;
}

function showMark(i) {
	allMarkers[i].openInfoWindowHtml(tooltipCalls[i]);
}

function hideMark() {
   //map.closeInfoWindow();	
}

function markCleanup(){
	map.setCenter(markersBounds.getCenter(), map.getBoundsZoomLevel(markersBounds)-zoomOut);
	document.getElementById("progressBar").style.display="none";
}

function getCurrentMapTypeIndex(){
	var mapTypes = map.getMapTypes();
	for(var i = 0; i < mapTypes.length; i++){
		if(map.getCurrentMapType()==mapTypes[i]) 
			return i;
	}
	return 0;
}

/****************************************************************************************
                              DHTML progress bar script
                     Written by Mark Wilton-Jones, 1-2/10/2002
*****************************************************************************************

Please see http://www.howtocreate.co.uk/jslibs/ for details of this script
Please see http://www.howtocreate.co.uk/jslibs/termsOfUse.html for terms of use

Code copied from: http://www.howtocreate.co.uk/jslibs/htmlhigh/progressbar.html

*/

var MWJ_progBar = 0;

function getRefToDivNest( divID, oDoc ) {
	if( !oDoc ) { oDoc = document; }
	if( document.layers ) {
		if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
				y = getRefToDivNest(divID,oDoc.layers[x].document); }
			return y; } }
	if( document.getElementById ) { return document.getElementById(divID); }
	if( document.all ) { return document.all[divID]; }
	return document[divID];
}

function progressBar( oBt, oBc, oBg, oBa, oWi, oHi, oDr ) {
	MWJ_progBar++; this.id = 'MWJ_progBar' + MWJ_progBar; this.dir = oDr; this.width = oWi; this.height = oHi; this.amt = 0;
	//write the bar as a layer in an ilayer in two tables giving the border
	document.write( '<table border="0" cellspacing="0" cellpadding="'+oBt+'"><tr><td bgcolor="'+oBc+'">'+
		'<table border="0" cellspacing="0" cellpadding="0"><tr><td height="'+oHi+'" width="'+oWi+'" bgcolor="'+oBg+'">' );
	if( document.layers ) {
		document.write( '<ilayer height="'+oHi+'" width="'+oWi+'"><layer bgcolor="'+oBa+'" name="MWJ_progBar'+MWJ_progBar+'"></layer></ilayer>' );
	} else {
		document.write( '<div style="position:relative;top:0px;left:0px;height:'+oHi+'px;width:'+oWi+';">'+
			'<div style="position:absolute;top:0px;left:0px;height:0px;width:0;font-size:1px;background-color:'+oBa+';" id="MWJ_progBar'+MWJ_progBar+'"></div></div>' );
	}
	document.write( '</td></tr></table></td></tr></table>\n' );
	this.setBar = resetBar; //doing this inline causes unexpected bugs in early NS4
	this.setCol = setColour;
}
function resetBar( a, b ) {
	//work out the required size and use various methods to enforce it
	this.amt = ( typeof( b ) == 'undefined' ) ? a : b ? ( this.amt + a ) : ( this.amt - a );
	if( isNaN( this.amt ) ) { this.amt = 0; } if( this.amt > 1 ) { this.amt = 1; } if( this.amt < 0 ) { this.amt = 0; }
	var theWidth = Math.round( this.width * ( ( this.dir % 2 ) ? this.amt : 1 ) );
	var theHeight = Math.round( this.height * ( ( this.dir % 2 ) ? 1 : this.amt ) );
	var theDiv = getRefToDivNest( this.id ); if( !theDiv ) { window.status = 'Progress: ' + Math.round( 100 * this.amt ) + '%'; return; }
	if( theDiv.style ) { theDiv = theDiv.style; theDiv.clip = 'rect(0px '+theWidth+'px '+theHeight+'px 0px)'; }
	var oPix = document.childNodes ? 'px' : 0;
	theDiv.width = theWidth + oPix; theDiv.pixelWidth = theWidth; theDiv.height = theHeight + oPix; theDiv.pixelHeight = theHeight;
	if( theDiv.resizeTo ) { theDiv.resizeTo( theWidth, theHeight ); }
	theDiv.left = ( ( this.dir != 3 ) ? 0 : this.width - theWidth ) + oPix; theDiv.top = ( ( this.dir != 4 ) ? 0 : this.height - theHeight ) + oPix;
}
function setColour( a ) {
	//change all the different colour styles
	var theDiv = getRefToDivNest( this.id ); if( theDiv.style ) { theDiv = theDiv.style; }
	theDiv.bgColor = a; theDiv.backgroundColor = a; theDiv.background = a;
}

