﻿	currentHash = null;
	countyListLoaded = false;
	map=null;
	markers=null;
	//On load page, init the timer which check if the there are anchor changes each 300 ms
	$(document).ready(function(){
		toggleMap(document.getElementById("MapActuator").checked);
		setInterval("checkHash()", 300);
	});
	//Function which chek if there are anchor changes, if there are, sends the ajax petition
	function checkHash(){
		//Check if it has changes
		if(currentHash != location.hash){
			$("body").removeClass("ShowingSearchResults");
			currentHash = location.hash;
			
			if(!currentHash){
				arrayHash=["zip"];//default hash
			}else{
				arrayHash=currentHash.substring(1).split('|');
			}
			
			if(arrayHash[0] == "zip" && arrayHash.length == 3){
				if(!$("body").hasClass("SearchTypeZip")){
					$("body").removeClass("SearchTypeCounty").addClass("SearchTypeZip");
				}
				if(map){map.clearOverlays();}
				$("#CountyLookupCtr").hide();
				doZipSearch(arrayHash[1],arrayHash[2]);
				$("body").removeClass("CollapseMap");
			} else if(arrayHash[0] == "county" && arrayHash.length == 4){
				if(!$("body").hasClass("SearchTypeCounty")){
					$("body").removeClass("SearchTypeZip").addClass("SearchTypeCounty");
				}
				if(map){map.clearOverlays();}
				document.getElementById("ZipFormField").value="";
				document.getElementById("DistanceFormField").value=15;
				$("#CountyLookupCtr").hide();
				doCountySearch(arrayHash[1],arrayHash[2],arrayHash[3]);
				$("body").removeClass("CollapseMap");
			} else if(arrayHash[0] == "county"){
				$("body").removeClass("SearchTypeZip").addClass("SearchTypeCounty");
				if(map){map.clearOverlays();}
				$("body").addClass("CollapseMap");
				$("#DocIndexCtr").hide();
				if(countyListLoaded==false){
					$("#loading").show();
					$.get("/DocFinder/CountyIndex.cfm","metros=all", function(data){
						countyListLoaded=true;
						$("#CountyLookupCtr").html(data);
						$("#loading").hide();
						$("#CountyLookupCtr").show();
						/* $(".Regions").columnize({columns:4,buildOnce:true}); */
					});
				}else{
					$("#CountyLookupCtr").show();
				}
			} else {
				$("body").removeClass("SearchTypeCounty").addClass("SearchTypeZip");
				if(map){map.clearOverlays();}
				$("body").addClass("CollapseMap");
				$("#DocIndexCtr").hide();
				$("#CountyLookupCtr").hide();
			}
		}
	}
	function updateHash(NewHash){
		arrayOldHash=location.hash.substring(1).split('|');
		arrayNewHash=NewHash.split('|');
		//alert(arrayOldHash[0]);
		//copy from old to new where item == *
		for (ArrayIndex = 0; ArrayIndex <= 6; ArrayIndex++){
			if(arrayNewHash.length > ArrayIndex && arrayOldHash.length > ArrayIndex && arrayNewHash[ArrayIndex] == "*"){
				arrayNewHash[ArrayIndex]=arrayOldHash[ArrayIndex];
			}
		}
		//default
		if(arrayNewHash[0] == ""){
			arrayNewHash[0]="zip";
		}
		//alert(document.location.hash);
		location.hash=arrayNewHash.join("|");
		//alert(document.location.hash);
		checkHash();
	}
	function zipFormHandler(){
		updateHash("zip|"+document.getElementById("ZipFormField").value+"|"+document.getElementById("DistanceFormField").value);
		return false;
	}
	function doZipSearch(Zip,Distance){
		var URLParams=null;
		if(document.getElementById("ZipFormField").value != Zip){
			document.getElementById("ZipFormField").value=Zip;
		}
		if(document.getElementById("DistanceFormField").value != Distance){
			document.getElementById("DistanceFormField").value=Distance;
		}
		$("#DocIndexCtr").hide();
		$("#loading").show();
		URLParams="zip="+Zip+"&distance="+Distance;
		$.get("/DocFinder/DocIndex.cfm",URLParams, function(data){
			$("#DocIndexCtr").html(data);
			if($(".Location").length == 0 && Distance < 60){
				if(Distance==15){
					updateHash("zip|"+Zip+"|30");
				} else if(Distance==30){
					updateHash("zip|"+Zip+"|60");
				}
			}else{
				$("body").addClass("ShowingSearchResults");
				$("#loading").hide();
				$(".Location:odd").addClass("odd");
				$(".Location:even").addClass("even");
				$("#DocIndexCtr").show();
				updateMap();
			}
		});
		return false;
	}
	function addListenerToMarker(marker,i,LocationCtrID,LocationNum){
		GEvent.addListener(marker, "click", function(){doFocus(i,LocationCtrID,LocationNum);});
	}
	function updateMap(){
		
		if(document.getElementById("MapActuator").checked){
			if(map){map.clearOverlays();}
			var mapPointCount=0;
			var bounds=null;
			var marker=null;
			var i=null;
			var j=null;
			var aLatLon=null;
			var LocationCtrID=null;
			var LocationNum=null;
			var arrayOfLatLon=$(".LatLon");
			markers = [];
			for(i=0;i < arrayOfLatLon.length;i=i+1){
				markers[i]=null;
				aLatLon=arrayOfLatLon[i].innerHTML.split(",");
				LocationCtrID=arrayOfLatLon[i].id.split("|")[0];
				LocationNum=arrayOfLatLon[i].id.split("|")[1];
				if(aLatLon.length==2){
					mapPointCount++;
					point = new GLatLng(aLatLon[0],aLatLon[1]);
					if(!map){
						map = new GMap2($("#MapCanvasCtr")[0]);
						map.setUIToDefault();
					}
					//to initialize new maps and to set default center if there is only one pin
					if(mapPointCount==1) map.setCenter(point);
					marker = new GMarker(point);
					addListenerToMarker(marker,i,LocationCtrID,LocationNum);
					map.addOverlay(marker);
					markers[i]=marker;
				}
			}
			
			//get center and zoom
			if(mapPointCount > 1){
				bounds = new GLatLngBounds;
				for(j=0;j < markers.length;j=j+1){
					if(markers[j]!=null){
						bounds.extend(markers[j].getPoint());
					}
				}
				map.setZoom(map.getBoundsZoomLevel(bounds));
				map.setCenter(bounds.getCenter());
			} else {
				//hide map? not sure what to do here
			}
			
		}
	}
	function toggleSearchType(){
		if($("body").hasClass("SearchTypeZip")){
			$("body").removeClass("SearchTypeZip").addClass("SearchTypeCounty");
			updateHash('county');
		}else{
			$("body").removeClass("SearchTypeCounty").addClass("SearchTypeZip");
			updateHash('zip');
		}
	}
	function doFocus(markerPos,LocationCtrID,LocationNum){
		map.closeInfoWindow();
		if (LocationNum == undefined)
			LocationNum = "1";
		//markers[markerPos].openInfoWindowHtml('<div class="Location" style="height:200px; width:200px; overflow:auto;"><div class="TextCtr">'+document.getElementById(LocationCtrID+'-'+LocationNum+'-TextCtr').innerHTML+'</div><div class="AddressCtr">'+document.getElementById(LocationCtrID+'-'+LocationNum+'-AddressCtr').innerHTML+'</div><div class="MapMilCtr">'+document.getElementById(LocationCtrID+'-'+LocationNum+'-MapMilCtr').innerHTML+'</div></div>');
		markers[markerPos].openInfoWindowHtml('<div class="Location" style="height:200px; width:200px; overflow:auto;"><div class="TextCtr">'+document.getElementById(LocationCtrID+'-TextCtr').innerHTML+'</div><div class="AddressCtr">'+document.getElementById(LocationCtrID+'-AddressCtr').innerHTML+'</div><div class="MapMilCtr">'+document.getElementById(LocationCtrID+'-MapMilCtr').innerHTML+'</div></div>');
		document.getElementById("DistanceFormField").focus();
	}
	function doCountySearch(MetroName,MetroID,Distance){
		$("#DocIndexCtr").hide();
		$("#loading").show();
		URLParams="metroname="+MetroName+"&metroid="+MetroID+"&distance="+Distance;
		$.get("/DocFinder/DocIndex.cfm",URLParams, function(data){
			$("#DocIndexCtr").html(data);
			if($(".Location").length == 0 && Distance < 60){
				if(Distance==15){
					updateHash("county|"+MetroName+"|"+MetroID+"|30");
				} else if(Distance==30){
					updateHash("county|"+MetroName+"|"+MetroID+"|60");
				}
			}else{
				$("body").addClass("ShowingSearchResults");
				$(".Location:odd").addClass("odd");
				$(".Location:even").addClass("even");
				$("#loading").hide();
				$("#DocIndexCtr").show();
				updateMap();
			}
		});
		return false;
	}
	function toggleDetails(LocationCtrID){
		if($("#"+LocationCtrID).hasClass("DetailsOn")){
			$("#"+LocationCtrID).removeClass("DetailsOn").addClass("DetailsOff");
		}else{
			$("#"+LocationCtrID).removeClass("DetailsOff").addClass("DetailsOn");
		}
	}
	function showDetails(LocationCtrID){
		$("#"+LocationCtrID).removeClass("DetailsOff").addClass("DetailsOn");
	}
	function hideDetails(LocationCtrID){
		$("#"+LocationCtrID).removeClass("DetailsOn").addClass("DetailsOff");
	}
	function toggleMap(var001){
		if(var001){
			$("body").removeClass("MapStateOff").addClass("MapStateOn");
		}else{
			$("body").removeClass("MapStateOn").addClass("MapStateOff");
		}
	}
	
	
//Functions for anchors
function ObjectPosition(obj) {
	objID = document.getElementById(obj);
	var curleft = 0;
	var curtop = 0;
	if (objID.offsetParent) {
		do {
			curleft += objID.offsetLeft;
			curtop += objID.offsetTop;
		}
		while (objID = objID.offsetParent);
	}
	  return [curleft,curtop];
}
function ScrollTo(obj){
    try{
        var objpos = ObjectPosition(obj);
    }catch(e){}
    try{
        scroll(0,objpos[1]);
    }catch(e){}
    try{
        window.scrollTo(0,objpos[1]);
    }catch(e){}
}

