/*
	National Nurse Map object:
	
	JavaScript object creates and displays a Google Map from geographic locations stored in an external javascript file
	
	File dependency =>  endorsersDB.js -- Endorsers database with geographic locatin data
	File dependency =>  http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAESTRpYgYap0BUK4NPwklzBQPy1W16bFJG3qc00mr0FM5_LfRnhQyTvxT0e1tt6sBQTP8oaLThqEdsg --
		Google scripts for generating the map with the live server "key".
	
*/

NNMap = {
	Map	 : null, //Google will initalize this
	Locs : null, //use the external javascript database
	initialize	:	function() {
		
		if (GBrowserIsCompatible()) {
			var mapCenter = new GLatLng(38.4419, -93.1419);
			
			NNList.sortLocations2(NNDB,'sortName'); //sort the database
			NNMap.Locs = NNDB; //use the javascript database in another file 
			
			NNMap.Map = new GMap2(document.getElementById("googleMap"));
			map = NNMap.Map;
			map.setCenter(mapCenter,4);
			GEvent.addListener(map, 'click', function() { map.setCenter(mapCenter,4); } );
			
			
			for(var i = 0; i < NNMap.Locs.length; i++){
				aLoc = NNMap.Locs[i];
				if(aLoc.name && aLoc.geocode) {
					aLoc.infoBox = NNMap.createInfoBox(aLoc);
					NNMap.addMarker(aLoc);
					NNMap.addToMapUl(aLoc);
				}
			}//end for	
       }//end if compatible
	}, // end initialize
	
	
	
	createInfoBox	:	function(locObj) {
		
		var xhtml = document.createElement('div');
		xhtml.id = "infoBox";
		
		var sName = document.createElement('h2');
		sName.innerHTML = locObj.short;
		xhtml.appendChild(sName);
	
		if(locObj.logo && locObj.web){
			appendToInfoWindow(createTextAnchor());
			
			var tempTag = anchorWrap(createImgTag(true));
			appendToInfoWindow(tempTag, 'center');
		}
		else if(locObj.logo && !locObj.web) {
			appendToInfoWindow(createImgTag(false), 'center');
		}
		else if(!locObj.logo && locObj.web) {
			appendToInfoWindow(createTextAnchor() );
		}
		else if(!locObj.logo && !locObj.web) {
			appendToInfoWindow(document.createTextNode(locObj.name));
		}
		return xhtml;
		
		
		function createTextAnchor() {
			var a = document.createElement('a');
			a.href = locObj.web;
			a.innerHTML = locObj.name;
			return a
		}
		
		function appendToInfoWindow(domElem, classNameStr) {
			var pTag = document.createElement('p');
			if(classNameStr) pTag.className = classNameStr;
			pTag.appendChild(domElem);
			xhtml.appendChild(pTag);
		}
		
		function anchorWrap(domNode) {
			var a = document.createElement('a');
			a.href = locObj.web;
			a.appendChild(domNode);
			return a;
		}//end anchorWrap
		
		function createImgTag(addMouseHandlers) {
			var logoImage = new Image();
			logoImage.src = locObj.logo;
			logoImage.alt = locObj.name;
			if(addMouseHandlers){
				logoImage.onmouseover = function() { this.src = this.overSrc; };
				logoImage.onmouseout = function() { this.src = this.offSrc; };
				logoImage.offSrc = logoImage.src;
				logoImage.overSrc = logoImage.src.replace('off', 'over');
			}
			return logoImage;
		}//end createImgTag
		
		function scaleImage(image) {
			while (image.height > 155) {
				image.height *= .95;
				image.width *= .95;
			}                        
		}//end scaleImage
	}, //end createInfoBox
	
	
	
	getLatLng	:	function(locObj) {
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(locObj.addr,assignLatLng);
		
		function assignLatLng(gPoint) {
			locObj.latLng = gPoint;
			if(locObj.latLng){
				NNMap.addMarker(locObj);
				NNMap.addToMapUl(locObj);
			}
		}
	}, //end getLatLng
	
	
	
	addToMapUl	: function(locObj) {
		var li = document.createElement('li');
		
		
		locObj.li = li; //save a reference to the DOM object
		li.location = locObj; //assign a reference back to the location object
		li.className = 'off';
		li.innerHTML = '<a href="#googleMap">' + locObj.short +  '</a>';
		li.onmouseover = function() {
			//this.className = this.className.replace('off', 'over');
			NNMap.changeToFullName(this);
		};
		li.onmouseout = function() {
			//this.className = this.className.replace('over', 'off');
			NNMap.changeToShortName(this);
		};
		li.onclick = function() {
			var m = this.location.marker;
			m.openInfoWindow(locObj.infoBox);
			//Add code for scrolling to show all the map
		}
		document.getElementById('mapUl').appendChild(li);
	},
	
	
	changeToFullName : function(liObj) {
			
	},
	
	changeToShortName : function(liObj) {
			
	},
	
	scrollToMap : function() {
			
	},
	
	addMarker	:	function(locObj) {
		var marker = null;
		var localIcon = new GIcon(G_DEFAULT_ICON);
		localIcon.image = '/images/GNNIcon.png';
		marker = new GMarker(locObj.geocode, {icon:localIcon});
		
		marker.location = locObj; //save the reference back to the location object
		NNMap.Map.addOverlay(marker);
		locObj.marker = marker; //save this reference in the location object
		GEvent.addListener(marker,'click', clickHandler);
		
		function clickHandler() {
			this.openInfoWindow(locObj.infoBox);
		}
		
	} //end addMarker
	
} //end NNMap

/*
	National Nurse List object:
	
	JavaScript object creates xhtml li tags using data from an external javascript database file.
		Object sorts the external data before using.
	
	File dependency =>  endorsersDB.js -- must be loaded before this file
	Object dependency => NNMap (in this file)

	Note: the initialize function also calls the initialization for the map object.

*/
NNList = {
		sortLocations2	:	function(locArry, prop) {
			locArry.sort(sorter);
			function sorter(obj1, obj2) {
				return obj1[prop].toLowerCase().localeCompare(obj2[prop].toLowerCase());
			}
		},
		
		makeList	:	function() {
			var divTag = document.getElementById('endorserDiv');
			var ulTag = document.createElement('ul');
			var liTag = null;
			var loc = null;
			var text =null;
			var aTag = null;
			var mapBtn = null;
			
			function makeMapBtn(locObj) {
				var mapBtn = document.createElement('a');
				var btnImg = new Image();
				btnImg.offSrc = 'images/googleMaps_off.png';
				btnImg.overSrc = 'images/googleMaps_over.png';
				btnImg.src = 'images/googleMaps_off.png';
				btnImg.alt = 'Our support is nation wide!';
				btnImg.title = btnImg.alt;
				btnImg.onmouseover = function() { this.src = this.overSrc; };
				btnImg.onmouseout = function() { this.src = this.offSrc; };
				mapBtn.href = '#googleMap';
				mapBtn.className = 'mapLink';
				mapBtn.onclick = function() { locObj.marker.openInfoWindow(locObj.infoBox); };
				mapBtn.appendChild(btnImg); 
				return mapBtn;
			}
			
			divTag.appendChild(ulTag);
			NNList.sortLocations2(NNDB,'sortName'); //sort the database
			for (var i = 0; i < NNDB.length; i++) {
				loc = NNDB[i];
				liTag = document.createElement('li');
				if (loc.name) text = document.createTextNode(loc.name);
				if(loc.web) {
					aTag = document.createElement('a');
					aTag.href = loc.web;
					aTag.appendChild(text);
					liTag.appendChild(aTag);
				}
				else liTag.appendChild(text);
				//liTag.appendChild(document.createTextNode(' '));
				liTag.appendChild(makeMapBtn(loc));
				ulTag.appendChild(liTag);
			}
		}, //end makeList
		
		
		initialize	:	function(){
	
			NNList.makeList();			//create and insert li tags
			NNMap.initialize();			//create and poplulate the Google Map object
		} //end initialize
}



// SET UP ONLOAD EVENT LISTENERS
if(document.getElementById){
  
	if(document.addEventListener){
		window.addEventListener('load',NNList.initialize,false);
		//window.addEventListener('load',NNMap.initialize,false);
		window.addEventListener('unload',GUnload,false);
	
	}
	
	else if (document.attachEvent){ 
		window.attachEvent('onload', NNList.initialize);
		//window.attachEvent('onload', NNMap.initialize);
		window.attachEvent('onunload', GUnload);
	}

}