	
//==========================================================================================
//
// map_script.js 
// Provides: map, cookie, logo and menu stuff
//
//==========================================================================================


	var map = null;
	
	var infoSrc = null;
	var contentWin;
	
	window.onload = load;
	window.onunload = unload;
	 
	 
	function loadInfoSrc() {
		
		contentWin.src = infoSrc;
		
	}
	
//==========================================================================================	
// Create our "w4s" marker icon
//

	var w4sIcon = new GIcon();
	w4sIcon.image = "http://walks4softies.co.uk/map/Gicon_image.png";
	w4sIcon.shadow = "http://walks4softies.co.uk/map/Gicon_bimage.png";
	w4sIcon.iconSize = new GSize(32, 31);
	w4sIcon.shadowSize = new GSize(46, 31);
	w4sIcon.iconAnchor = new GPoint(16, 31);
	w4sIcon.infoWindowAnchor = new GPoint(16, 31);
	
	
//==========================================================================================	
// Cookie stuff to save map position
//

	function cookieVal( cookieName ) {
		
		var thisCookie = document.cookie.split("; ");
		
		for (var i=0; i<thisCookie.length; i++ ) {
		
			if ( cookieName == thisCookie[i].split( "=" )[0]) {
				return  thisCookie[i].split( "=" )[1];
			}
		}
		
		return 0;
	}


	function savePositionCookie() {
		
		var latLng = map.getCenter();
		var lat = latLng.lat();
		var lng = latLng.lng();
		
		var zoom = map.getZoom();
		
		var type = map.getCurrentMapType();
		var typeInt = 999999;
		if ( type == G_NORMAL_MAP ) { typeInt = 1; }
		if ( type == G_SATELLITE_MAP ) { typeInt = 2; }
		if ( type == G_HYBRID_MAP ) { typeInt = 3; }
		
		if ( typeInt == 999999 ) { alert( "Unknown map type !!!!" ); }
		
		var expiryDate = new Date();
		expiryDate.setTime ( expiryDate.getTime() + 3600000 );
		
		var cookieString = "||||pos||||" + lat + "||||" + lng + "||||" + zoom + "||||" + typeInt  + "||||";
		
		document.cookie = "savedMapPosition=" + cookieString + ";expires=" + expiryDate.toGMTString();
		
	}
	
	
	function restorePosition() {
	
		var cookieString = cookieVal( "savedMapPosition" );
		
		if ( cookieString == 0 ) {
		
			// cookie not defined so set up default position.
			
			// alert( "'savedMapPosition' not found !" );
			
			map.setCenter(new GLatLng(50.7291340827,-1.7858812807), 11);
		
		} else {
		
			// cookie is defined so restore saved position.
		
			var parArray = cookieString.split("||||");
			
			if ( parArray[1] == "pos" ) {
			
				var lat = parseFloat( parArray[2] );
				var lng = parseFloat( parArray[3] );
				var zoom = parseInt( parArray[4] );
				map.setCenter(new GLatLng(lat, lng), zoom );
				
				var typeInt = parseInt( parArray[5] );
				
				if ( typeInt == 1 ) { map.setMapType(G_NORMAL_MAP); }
				if ( typeInt == 2 ) { map.setMapType(G_SATELLITE_MAP); }
				if ( typeInt == 3 ) { map.setMapType(G_HYBRID_MAP); }
			
			} else {
			
				alert( "'Pos' not found on cookie !!!!" );
				
			}
		}
	}
	
	
//==========================================================================================	


	function W2Gmarker( w, o, u ) {
	
		this.where = w;
		this.opts = o;
		this.murl = u;
	
		this.marker = new GMarker( this.where, this.opts );
		map.addOverlay(this.marker);
		
		GEvent.bind( this.marker, "infowindowclose", this, this.markerInfoWindowClose);
		GEvent.bind( this.marker, "click", this, this.clicked );
		
		return this;
		
	}		 
	
		
	W2Gmarker.prototype.markerInfoWindowClose = function() {
	
		this.marker.show();
		
	};
	
	
	W2Gmarker.prototype.clicked = function() {
	
		this.marker.hide();
		
		contentWin = createInfoWindow( this.murl );
		this.marker.openInfoWindow( contentWin );
		
		setTimeout( "loadInfoSrc()", 100 );
	}
	
	
	function createInfoWindow ( u ) {
		var aGraph = document.createElement("iframe");
		infoSrc = u ;
		//aGraph.src = u;
		aGraph.id = "w4sframe";
		//aGraph.name = "w4sframe";
		aGraph.height = "300";
		aGraph.width = "510";
		aGraph.scrolling = "auto";
		aGraph.frameborder = "1";

		return aGraph;
	}
	
//==========================================================================================	


	function MyRightPane() {}
	
	MyRightPane.prototype = new GControl;
	
	MyRightPane.prototype.initialize = function(map) {
		var me = this;
		me.panel = document.createElement("div");
		me.panel.className = "navigation";
		map.getContainer().appendChild(me.panel);
		
		var a1Text = document.createTextNode("Home");
		var a1Graph = document.createElement("a");
		a1Graph.appendChild( a1Text );
		a1Graph.href = "http://walks4softies.co.uk/";
		a1Graph.title = "walks4softies home page";
		me.panel.appendChild( a1Graph );
		
		var newText1 = document.createTextNode(" - ");
		me.panel.appendChild(newText1);
		
		var a2Text = document.createTextNode("Find Walks");
		var a2Graph = document.createElement("a");
		a2Graph.appendChild( a2Text );
		a2Graph.href = "http://walks4softies.co.uk/scripts/walk_filter.pl";
		a2Graph.title = "Find a walk on walks4softies";
		me.panel.appendChild( a2Graph );
		
		var newText2 = document.createTextNode(" - ");
		me.panel.appendChild(newText2);
		
		var a3Text = document.createTextNode("Map");
		var a3Graph = document.createElement("a");
		a3Graph.appendChild( a3Text );
		a3Graph.href = "http://walks4softies.co.uk/map/index.html";
		a3Graph.title = "Find a walk on our interactive map";
		me.panel.appendChild( a3Graph );
		
		var newText3 = document.createTextNode(" - ");
		me.panel.appendChild(newText3);
		
		var a4Text = document.createTextNode("Contact Us");
		var a4Graph = document.createElement("a");
		a4Graph.appendChild( a4Text );
		a4Graph.href = "http://walks4softies.co.uk/feedbk.html";
		a4Graph.title = "Feedback, subscribe or unsubscribe";
		me.panel.appendChild( a4Graph );
		
		var newText4 = document.createTextNode(" - ");
		me.panel.appendChild(newText4);
		
		var a5Text = document.createTextNode("More");
		var a5Graph = document.createElement("a");
		a5Graph.appendChild( a5Text );
		a5Graph.href = "http://walks4softies.co.uk/more.html";
		a5Graph.title = "Links, walking for health, sponsor a walk and more";
		me.panel.appendChild( a5Graph );
				
		return me.panel;
	};
	
	
	MyRightPane.prototype.getDefaultPosition = function() {
		return new GControlPosition(
		G_ANCHOR_TOP_RIGHT, new GSize(0, 0));
	};
	
	
	MyRightPane.prototype.getPanel = function() {
		return me.panel;
	}
	
	
//==========================================================================================	

				
	function MyLeftPane() {}
	
	MyLeftPane.prototype = new GControl;
	
	MyLeftPane.prototype.initialize = function(map) {
		var me = this;
		me.panel = document.createElement("div");
		me.panel.className = "logo";
		map.getContainer().appendChild(me.panel);
		
		var imageEl = document.createElement("img");
		imageEl.src = "http://walks4softies.co.uk/map/logoTopLeftFore.png";
		imageEl.alt = "walks4softies logo";
		imageEl.border = "0";
		
		var a1Graph = document.createElement("a");
		a1Graph.appendChild( imageEl );
		a1Graph.href = "http://walks4softies.co.uk/";
		a1Graph.title = "walks4softies home page";
		me.panel.appendChild( a1Graph );
		
		return me.panel;
	};
	
	MyLeftPane.prototype.getDefaultPosition = function() {
		return new GControlPosition(
		G_ANCHOR_TOP_LEFT, new GSize(0, 0));
	};
	
	
	MyLeftPane.prototype.getPanel = function() {
		return me.panel;
	}
	
	
//==========================================================================================	


	var lpane;
	var rpane;

	function load() {
		if (GBrowserIsCompatible()) {
		
			map = new GMap2(document.getElementById("map"));
			restorePosition();
			
			var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(8, 88));
			map.addControl(new GSmallMapControl(), topLeft);
			
			var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(8, 35));
			map.addControl(new GMapTypeControl(), topRight);

			lpane = new MyLeftPane();
			map.addControl(lpane);
			
			rpane = new MyRightPane();
			map.addControl(rpane);
		
			window.onresize = resizeMap;
			
			window.setTimeout(loadAreaMarkers, 100);

		}
	}
	
	
	function unload() {
	
		savePositionCookie();
		GUnload();
		map = null;
		
	}
	
	
	function resizeMap() {
		
		map.checkResize();
		
	}


//==========================================================================================	


