// loading ... layer
if (navigator.userAgent.indexOf("Opera")!=-1
    && document.getElementById) type="OP"; 
    
if (document.all) type="IE"; 
if (document.layers) type="NN"; 
if (!document.all && document.getElementById) type="MO";
	
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function writit(text,id) {
	if (document.getElementById) 	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all) 	{
		x = document.all[id];
		x.innerHTML = text;
	}
	else if (document.layers) 	{
		x = document.layers[id];
		text2 = '<P>' + text + '</P>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}

// Creates a small marker 
function createMyMarker(index1, index2, sptype, point) {
  if (sptype=='CE') {
  	var marker = new GMarker(point, baseIcon);
  	GEvent.addListener(marker, "click", function() { 
 		var center = map.getCenterLatLng();
		markerEvent=1;
		var request = GXmlHttp.create(); 
		request.open("GET", "http://www.libraries411.com/get_library_info.php?sp=" + index1 + "&ls=" + index2, true); 
		request.onreadystatechange = function() { 
			if (request.readyState == 4) { 
      			html = request.responseText; 
            	marker.openInfoWindowHtml(html); 
        	} 
      	}; 
      	request.send(null); 
	}); 
		GEvent.addListener(marker, "infowindowclose", function() { 
		map.centerAtLatLng(center);
		markerEvent=0;
		//map.recenterOrPanToLatLng(point);
	});	
   	return marker; 
  }
  if (sptype=='BR') {
  var marker1 = new GMarker(point, bicon);
  GEvent.addListener(marker1, "click", function() { 
 	var center = map.getCenterLatLng();
	markerEvent=1;
	var request = GXmlHttp.create(); 
	request.open("GET", "http://www.libraries411.com/get_library_info.php?sp=" + index1 + "&ls=" + index2, true); 
	request.onreadystatechange = function() { 
		if (request.readyState == 4) { 
      			html = request.responseText; 
            	marker1.openInfoWindowHtml(html); 
        	} 
      	}; 
      	request.send(null); 
	}); 
	GEvent.addListener(marker1, "infowindowclose", function() { 
		map.centerAtLatLng(center);
		markerEvent=0;
	});	
   	return marker1; 
  }
  if (sptype=='BS') {
  	var marker2 = new GMarker(point, wicon);
  	GEvent.addListener(marker2, "click", function() { 
 		var center = map.getCenterLatLng();
		markerEvent=1;
		var request = GXmlHttp.create(); 
		request.open("GET", "http://www.libraries411.com/get_library_info.php?sp=" + index1 + "&ls=" + index2, true); 
		request.onreadystatechange = function() { 
			if (request.readyState == 4) { 
      			html = request.responseText; 
            	marker2.openInfoWindowHtml(html); 
        	} 
      	}; 
      	request.send(null); 
	}); 
	GEvent.addListener(marker2, "infowindowclose", function() { 
		map.centerAtLatLng(center);
		markerEvent=0;
	});	
   	return marker2; 
  }
}

function spMapper(x) {
	for (i=0; i<CEresults; i++) {
		var point = new GPoint(parseFloat(CElngs[i]), parseFloat(CElats[i]));
		var marker = createMyMarker(CEsps[i], CElss[i], 'CE', point);
//  add marker overlay
map.addOverlay(marker);
		markersCE[markersCE.length] = marker;	
	}
	writit(" 0 of " + CEresults, 'CEme'); 
	
	for (i=0; i<BRresults; i++) {
		var point = new GPoint(parseFloat(BRlngs[i]), parseFloat(BRlats[i]));
		var marker = createMyMarker(BRsps[i], BRlss[i], 'BR', point);	
//  add marker overlay
map.addOverlay(marker);
		markersBR[markersBR.length] = marker;	
	}
	writit(" 0 of " + BRresults, 'BRme'); 
	if (BRresults>99) waitOn();

	for (i=0; i<BSresults; i++) {
		var point = new GPoint(parseFloat(BSlngs[i]), parseFloat(BSlats[i]));
		var marker = createMyMarker(BSsps[i], BSlss[i], 'BS', point);	
//  add marker overlay
map.addOverlay(marker);
		markersBS[markersBS.length] = marker;	
	}
	writit(" 0 of " + BSresults, 'BSme'); 
	writit("Total: " + numresults, 'TOTme'); 

document.toggler.dCE.checked=true;
document.toggler.dBR.checked=true;
document.toggler.dBS.checked=true;

	//if (numresults<200 && numresults>0 && CEresults>0 && CEresults<50) {
	//	document.toggler.dCE.checked=true;
	//	if (BRresults>0 && BRresults<100) document.toggler.dBR.checked=true;
	//	else document.toggler.dBR.checked=false;
	//	if (BSresults>0 && BSresults<50) document.toggler.dBS.checked=true;
	//	else document.toggler.dBS.checked=false;
	//}
	//toggleChk(0);
	if (x!=0) dMap(0);
}

function toggleChk(x){
	return;
	var CE1 = markersCE.length;
	if (CE1>0) {
		if (document.toggler.dCE.checked==false && ceStatus>0) {
			for (z1=0;z1<CE1;z1++) {
				if (markersCE[z1].icon.image=="http://www.libraries411.com/images/logo_pin_green.gif")  {
					map.removeOverlay(markersCE[z1]);
				}
			}
			ceStatus = 0;
			writit(" 0 of " + CE1,'CEme');
		}
		if (document.toggler.dCE.checked==true && ceStatus<1) {
			for (z1=0;z1<CE1;z1++) {
				if (markersCE[z1].icon.image=="http://www.libraries411.com/images/logo_pin_green.gif")  {
					map.addOverlay(markersCE[z1]);
 					var topElement = markersCE[z1].iconImage;
  					if (markersCE[z1].transparentIcon) {topElement = markersCE[z1].transparentIcon;}
  					if (markersCE[z1].imageMap) {topElement = markersCE[z1].imageMap;}    
  					//topElement.setAttribute( "title", CEnames[z1]);
				}
			}
			ceStatus = 1;
			writit(" " + CE1 + " of " + CE1,'CEme');
		}
		if (showLS!=showSP) {
			for (z1=0;z1<CE1;z1++) {
				if (CElss[z1]==showLS && CEsps[z1]==showSP && spMarker==0) {
					map.addOverlay(markersCE[z1]);
 					var topElement = markersCE[z1].iconImage;
  					if (markersCE[z1].transparentIcon) {topElement = markersCE[z1].transparentIcon;}
  					if (markersCE[z1].imageMap) {topElement = markersCE[z1].imageMap;}    
  					//topElement.setAttribute( "title", CEnames[z1]);
  					spMarker=1;
  					// map.centerAndZoom(new GPoint(CElngs[z1], CElats[z1]), 5);
				}
			}		
		}
	} else document.toggler.dCE.checked=false;

	var BR1 = markersBR.length;
	if (BR1>0) {
		if (document.toggler.dBR.checked==false && brStatus>0) {
			for (z1=0;z1<BR1;z1++) {
				if (markersBR[z1].icon.image=="http://www.libraries411.com/images/stick_red.gif")  {
					map.removeOverlay(markersBR[z1]);
				}
			}
			brStatus = 0;
			writit(" 0 of " + BR1,'BRme');
		}
		if (document.toggler.dBR.checked==true && brStatus<1) {
			waitOff();
			for (z1=0;z1<BR1;z1++) {
				if (markersBR[z1].icon.image=="http://www.libraries411.com/images/stick_red.gif")  {
					map.addOverlay(markersBR[z1]);
 					var topElement = markersBR[z1].iconImage;
  					if (markersBR[z1].transparentIcon) {topElement = markersBR[z1].transparentIcon;}
  					if (markersBR[z1].imageMap) {topElement = markersBR[z1].imageMap;}    
  					//topElement.setAttribute( "title", BRnames[z1]);
				}
			}
			brStatus = 1;
			writit(" " + BR1 + " of " + BR1,'BRme');
		}
		if (showLS!="") {
			for (z1=0;z1<BR1;z1++) {
				if (BRlss[z1]==showLS && BRsps[z1]==showSP && spMarker==0) {
					map.addOverlay(markersBR[z1]);
 					var topElement = markersBR[z1].iconImage;
  					if (markersBR[z1].transparentIcon) {topElement = markersBR[z1].transparentIcon;}
  					if (markersBR[z1].imageMap) {topElement = markersBR[z1].imageMap;}    
  					//topElement.setAttribute( "title", BRnames[z1]);
  					spMarker=1;
  					//map.centerAndZoom(new GPoint(BRlngs[z1], BRlats[z1]), 5);
				}
			}		
		}
	} else document.toggler.dBR.checked=false;

	var BS1 = markersBS.length;
	if (BS1>0) {
		if (document.toggler.dBS.checked==false && bsStatus>0) {
			for (z1=0;z1<BS1;z1++) {
				if (markersBS[z1].icon.image=="http://www.libraries411.com/images/stick_grey.gif")  {
					map.removeOverlay(markersBS[z1]);
				}
			}
			bsStatus = 0;
			writit(" 0 of " + BS1,'BSme');
		}
		if (document.toggler.dBS.checked==true && bsStatus<1) {
			for (z1=0;z1<BS1;z1++) {
				if (markersBS[z1].icon.image=="http://www.libraries411.com/images/stick_grey.gif")  {
					map.addOverlay(markersBS[z1]);
 					var topElement = markersBS[z1].iconImage;
  					if (markersBS[z1].transparentIcon) {topElement = markersBS[z1].transparentIcon;}
  					if (markersBS[z1].imageMap) {topElement = markersBS[z1].imageMap;}    
  					//topElement.setAttribute( "title", BSnames[z1]);
				}
			}
			//	map.addOverlays(markersBS);
			bsStatus = 1;
			writit(" " + BS1 + " of " + BS1,'BSme');
		}
		if (showLS!=showSP) {
			for (z1=0;z1<BS1;z1++) {
				if (BSlss[z1]==showLS && BSsps[z1]==showSP && spMarker==0) {
					map.addOverlay(markersBS[z1]);
 					var topElement = markersBS[z1].iconImage;
  					if (markersBS[z1].transparentIcon) {topElement = markersBS[z1].transparentIcon;}
  					if (markersBS[z1].imageMap) {topElement = markersBS[z1].imageMap;}    
  					//topElement.setAttribute( "title", BSnames[z1]);
  					spMarker=1;
  					//map.centerAndZoom(new GPoint(BSlngs[z1], BSlats[z1]), 5);
				}
			}
		}
	} else document.toggler.dBS.checked=false;
	if (x>0) { // clear the array
		markersCE = [];
		markersBR = [];
		markersBS = [];
	}	
} //closes function loop

// Extract content from specified XML tag
function getDetails(xml, start, end) {
   	startpos = xml.indexOf(start);
   	if (startpos <0) {
       return false;
   	}
   	startpos = startpos + start.length;
   	endpos = xml.indexOf(end);
   	tag = xml.substring(startpos, endpos);
   	return tag;
}

function gTags(h,x,y) {
	var	myh =getDetails(h, "<" + x + ">", "</" + x + ">");
	if (x=='CE') {
		for (i=0; i<y; i++) {	
			CElngs[i] = getDetails(myh, "<ln" + i + ">", "</ln" + i + ">");
			CElats[i]  = getDetails(myh,  "<lt" + i + ">", "</lt" + i + ">");
			CEnames[i]  = getDetails(myh,  "<lm" + i + ">", "</lm" + i + ">");
			CElss[i]  = getDetails(myh,  "<li" + i + ">", "</li" + i + ">");
			CEsps[i]  = getDetails(myh,  "<si" + i + ">", "</si" + i + ">");
			CEtype[i]  = getDetails(myh,  "<tp" + i + ">", "</tp" + i + ">");
			// alert(i + ":" + CElats[i]);
		}
	}
	if (x=='BR') {
		for (i=0; i<y; i++) {	
			BRlngs[i] = getDetails(myh, "<ln" + i + ">", "</ln" + i + ">");
			BRlats[i]  = getDetails(myh,  "<lt" + i + ">", "</lt" + i + ">");
			BRnames[i]  = getDetails(myh,  "<lm" + i + ">", "</lm" + i + ">");
			BRlss[i]  = getDetails(myh,  "<li" + i + ">", "</li" + i + ">");
			BRsps[i]  = getDetails(myh,  "<si" + i + ">", "</si" + i + ">");
			BRtype[i]  = getDetails(myh,  "<tp" + i + ">", "</tp" + i + ">");
		}
	}
	if (x=='BS') {
		for (i=0; i<y; i++) {	
			BSlngs[i] = getDetails(myh, "<ln" + i + ">", "</ln" + i + ">");
			BSlats[i]  = getDetails(myh,  "<lt" + i + ">", "</lt" + i + ">");
			BSnames[i]  = getDetails(myh,  "<lm" + i + ">", "</lm" + i + ">");
			BSlss[i]  = getDetails(myh,  "<li" + i + ">", "</li" + i + ">");
			BSsps[i]  = getDetails(myh,  "<si" + i + ">", "</si" + i + ">");
			BStype[i]  = getDetails(myh,  "<tp" + i + ">", "</tp" + i + ">");
		}
	}
}

function gLibs(x) {
	// alert ("Fetch Library Details");
	numresults = parseInt('0');
	CEresults = parseInt('0');
	BRresults = parseInt('0');
	BSresults = parseInt('0');
	var request = GXmlHttp.create(); 
	// alert("http://www.libraries411.com/get_lib_details.php?lat=" + mylat + "&lng=" + mylon + "&cnt=" + cntry + "&reg=" + stprov + "&locid=" + city + "&loc=" + cityname + "&rad=" + radius + "&pz=" + pzip);
	request.open("GET", "http://www.libraries411.com/get_lib_details.php?lat=" + mylat + "&lng=" + mylon + "&cnt=" + cntry + "&reg=" + stprov + "&locid=" + city + "&loc=" + cityname + "&rad=" + radius + "&pz=" + pzip, true); 
	request.onreadystatechange = function() { 
		if (request.readyState == 4) { 
      		html = request.responseText; 	
			// alert(html);
			CEresults = getDetails(html, "<CERES>", "</CERES>");
			BRresults = getDetails(html, "<BRRES>", "</BRRES>");
			BSresults = getDetails(html, "<BSRES>", "</BSRES>");
			numresults = parseInt(CEresults) + parseInt(BRresults) + parseInt(BSresults);
			//alert(numresults);

			var	CEh=getDetails(html, "<CE>", "</CE>");
			for (i=0; i<CEresults; i++) {	
				CElngs[i] = getDetails(CEh, "<ln" + i + ">", "</ln" + i + ">");
				CElats[i]  = getDetails(CEh,  "<lt" + i + ">", "</lt" + i + ">");
				CEnames[i]  = getDetails(CEh,  "<lm" + i + ">", "</lm" + i + ">");
				CElss[i]  = getDetails(CEh,  "<li" + i + ">", "</li" + i + ">");
				CEsps[i]  = getDetails(CEh,  "<si" + i + ">", "</si" + i + ">");
				CEtype[i]  = getDetails(CEh,  "<tp" + i + ">", "</tp" + i + ">");
			}
			var	BRh=getDetails(html, "<BR>", "</BR>");
			for (i=0; i<BRresults; i++) {	
				BRlngs[i] = getDetails(BRh, "<ln" + i + ">", "</ln" + i + ">");
				BRlats[i]  = getDetails(BRh,  "<lt" + i + ">", "</lt" + i + ">");
				BRnames[i]  = getDetails(BRh,  "<lm" + i + ">", "</lm" + i + ">");
				BRlss[i]  = getDetails(BRh,  "<li" + i + ">", "</li" + i + ">");
				BRsps[i]  = getDetails(BRh,  "<si" + i + ">", "</si" + i + ">");
				BRtype[i]  = getDetails(BRh,  "<tp" + i + ">", "</tp" + i + ">");
			}
			var	BSh=getDetails(html, "<BS>", "</BS>");
			for (i=0; i<BSresults; i++) {	
				BSlngs[i] = getDetails(BSh, "<ln" + i + ">", "</ln" + i + ">");
				BSlats[i]  = getDetails(BSh,  "<lt" + i + ">", "</lt" + i + ">");
				BSnames[i]  = getDetails(BSh,  "<lm" + i + ">", "</lm" + i + ">");
				BSlss[i]  = getDetails(BSh,  "<li" + i + ">", "</li" + i + ">");
				BSsps[i]  = getDetails(BSh,  "<si" + i + ">", "</si" + i + ">");
				BStype[i]  = getDetails(BSh,  "<tp" + i + ">", "</tp" + i + ">");
			}
			spMapper(x);
        } 
    }
    request.send(null); 	
}

function clrDmrkers(x) {
	// clear the current markers from the screen and array
	waitOff();
	document.toggler.dCE.checked=false;
	document.toggler.dBR.checked=false;
	document.toggler.dBS.checked=false;
	if ((ceStatus + brStatus + bsStatus)>0) {
		map.clearOverlays();
		ceStatus = 0;
		brStatus = 0;
		bsStatus = 0;
	}
	markersCE = [];
	markersBR = [];
	markersBS = [];
	// fetch the libraries for this location
	gLibs(x);
	// map the service points
}

function initMap(x) {
	window.name='lib411Win';
	spMarker=0;
	document.toggler.dCE.checked=false;
	document.toggler.dBR.checked=false;
	document.toggler.dBS.checked=false;
	GMap.prototype.centerAndZoomOnBounds = function(bounds) { 
    	var span = new GSize(bounds.maxX - bounds.minX, bounds.maxY - bounds.minY); 
    	var center = new GPoint(bounds.minX + span.width / 2.0, bounds.minY + span.height / 2.0); 
    	var newZoom = this.spec.getLowestZoomLevel(center, span, this.viewSize); 
    	if (this.getZoomLevel() != newZoom) { 
        	this.centerAndZoom(center, newZoom); 
    	} else { 
        	this.recenterOrPanToLatLng(center); 
    	} 
	} 
	//map.centerAndZoomOnBounds(new GBounds(parseFloat(minlon), parseFloat(minlat), parseFloat(maxlon), parseFloat(maxlat)));	
	
	if(pzip && pzip!='') {
		//var point = new GPoint(parseFloat(zplng), parseFloat(zplat));
  		//var marker = new GMarker(point);
  		//GEvent.addListener(marker, "click", function() { 
       	//	marker.openInfoWindowHtml(pzip); 
		//}); 
 		//map.addOverlay(marker);
 		//var topElement = marker.iconImage;
  		//if (marker.transparentIcon) {topElement = marker.transparentIcon;}
  		//if (marker.imageMap) {topElement = marker.imageMap;}    
  		//topElement.setAttribute( "title", pzip);
  		var zoomer = 3 + parseInt(zmfactor);
  		// map.centerAndZoom(new GPoint(parseFloat(slon), parseFloat(slat)), zoomer);
   		map.centerAndZoom(new GPoint(parseFloat(lng), parseFloat(lat)), zoomer);
  	} else {
  		//	if (slon && slon!='' && gotFromIP<1) 
  		map.centerAndZoom(new GPoint(parseFloat(lng), parseFloat(lat)), 7);
  	 }

	// if (showLS!=showSP && BRresults>24) waitOn();
	spMapper(x);
}

function dMap(d) {
	// center point for Map
	var latLng = new GPoint(parseFloat(lng), parseFloat(lat));

	// for determining the appropriate zoom level for this map
	GMap.prototype.centerAndZoomOnBounds = function(bounds) { 
    	var span = new GSize(bounds.maxX - bounds.minX, bounds.maxY - bounds.minY); 
    	var center = new GPoint(bounds.minX + span.width / 2.0, bounds.minY + span.height / 2.0); 
    	var newZoom = this.spec.getLowestZoomLevel(center, span, this.viewSize); 
    	if (this.getZoomLevel() != newZoom) { 
        	this.centerAndZoom(latLng, newZoom); 
    	} else { 
        	this.recenterOrPanToLatLng(latLng); 
    	} 
	} 
	var boundz = new GBounds(parseFloat(minlon), parseFloat(minlat), parseFloat(maxlon), parseFloat(maxlat));

	// auto zoom to Bounds unless only one location (distance=0) 
	//if (d>0) map.centerAndZoomOnBounds(boundz); 
	// else 
	
	map.recenterOrPanToLatLng(new GPoint(parseFloat(lng), parseFloat(lat)));
}

function doCrc(x) { 
	if (pzip!='') {                 
  		// Create a circle by specifying a radius.
  		var center = new GPoint(parseFloat(mylon), parseFloat(mylat));
  		if (cntry=='US') { 
    		var radius = new XDistance(radius, XDistance.MI);
    		var radtext="* " + radius + " Miles *";
    		var outlineStyle = {text: radtext, color: '#7777ff', textFgStyle: { color: '#ffffff' }};
  		} else {
  			var radius = new XDistance(radius, XDistance.KM);
    		var radtext="* " + radius + " KMs *";
    		var outlineStyle = {text: radtext, color: '#7777ff', textFgStyle: { color: '#ffffff' }};
  		}
		map.recenterOrPanToLatLng(center);
	}   		
} // end function

function windowWidth() {
  return window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
}

function windowHeight() {
  return window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
}

var lastclick = "" ;				// Last Clicked Point
// Check for a double click...
function checkclick ( point ) {
 if (markerEvent>0) return;
 if ( lastclick != point ) {
 	lastclick = point ;
 	lng = point.x;
 	lat = point.y;
  	mylon = point.x;
 	mylat = point.y;
	//document.zipper.lat.value=lat;
	//document.zipper.lng.value=lng;
	cntry="";
	stprov="";
	city="";
	cityname="";
 	var zoomlvl = map.getZoomLevel();
 	if (zoomlvl > 7) { 
 		zoomlvl = 7;
 		zoom = zoomlvl;
 	}
 	clrDmrkers(0);
 }
}

function jt_ShowHideElm(elm, showIt) {
  if (elm) elm.style.visibility = (showIt) ? "visible" : "hidden";
}
  

function alterLcontent(h) {
	var y=document.getElementById('message');
	if (y.firstChild) y.innerHTML= h;
	else {
		div2=document.createElement('div'); div2.id='div2';
		div2.innerHTML=  h ;
		y.appendChild(div2);
	}
}

function jt_Point(x, y) { 
  this.x = x; 
  this.y = y; 
} 

function jt_getOffsetXY(obj) { 
  var xPos = obj.offsetLeft; 
  var yPos = obj.offsetTop; 
  var parent = obj.offsetParent; 
  while (parent != null) { 
    xPos += parent.offsetLeft; 
    yPos += parent.offsetTop; 
    parent = parent.offsetParent; 
    } 
  return new jt_Point(xPos, yPos); 
} 

function jt_moveTo(obj, x, y) { 
  obj.style.left = x + "px"; 
  obj.style.top = y + "px"; 
} 

function ShowLayer(id, action){
  if (type=="IE") eval("document.all." + id + ".style.visibility='" + action + "'");
  if (type=="NN") eval("document." + id + ".visibility='" + action + "'");
  if (type=="MO" || type=="OP") 
    eval("document.getElementById('" + id + "').style.visibility='" + action + "'");
}

// wait message while markers are displayed
function waitOff()   {
	ShowLayer('cache','hidden');
}
function waitOn() {
	ShowLayer('cache','visible');
}