
$(document).keyup(function(e) {
	if(e.keyCode == 27) {
		$("div#pop2012Shell").fadeOut("slow");
		$("div#poppins").fadeOut("slow");
		$("div#blowHole").fadeOut("slow");
		unloadCSS("/_css/pop.css");
	}
});



$(document).ready(function() {

	$(".topClose").click(function(e) { 
		e.preventDefault();	
		$("div#pop2012Shell").fadeOut("slow");
		$("div#blowHole").fadeOut("slow");
		unloadCSS("/_css/pop.css");
	});

	$(".popClose").click(function(e) { 
		e.preventDefault();	
		$("div#pop2012Shell").fadeOut("slow");
		$("div#blowHole").fadeOut("slow");
		unloadCSS("/_css/pop.css");
	});
	

	$(".loadPage2012").click(function(e) {  
	
        /* cancel the href call */ 
        e.preventDefault();

		$("div#blowHole").css("filter", "alpha(opacity=40)"); 
		$("div#blowHole").fadeIn("slow");
			
		loadCSS("/_css/pop.css");

		var viewPortWidth = $(window).width();
		var viewPortHeight = $(window).height();
		var viewPortOffset = $(window).scrollTop();		

		$("#pop2012Shell").fadeIn("slow");
		
		$("#pop2012Shell").width(viewPortWidth - 50);		
		$("#pop2012Shell").height(viewPortHeight - 50);		

		$("#pop2012Shell").css("left", viewPortWidth/2-$("#pop2012Shell").width()/2);
		$("#pop2012Shell").css("top",  ((viewPortHeight / 2) - ($("#pop2012Shell").height() / 2 )) + viewPortOffset);


/*		$("#btnPrevious").hide();
		$("#btnNext").hide();*/		
		
		var page = $(this).attr("id");
		var title = $(this).attr("title");
		var pageVars = $(this).attr("pageVars");
		
		$.ajax({
			url: "/_code/page_loader.php",
			data: {"mode": "load_page", "page": page, "page_vars": pageVars, "title": title},
			context: $("#pop2012Window"),
			success: function(data) {
				$("#pop2012Window").html(data);
			}
		});		

	});





/* 2011 */


	$(".btnClose").click(function() {  
		$("div#poppins").fadeOut("slow");
		$("div#blowHole").fadeOut("slow");
		unloadCSS("/_css/pop.css");
	}); 


	$(".loadPage").click(function() {  

		$("div#blowHole").css("filter", "alpha(opacity=40)"); 
		$("div#blowHole").fadeIn("slow");
			
		$("div#poppins").fadeIn("slow");

		loadCSS("/_css/pop.css");

		var viewPortOffset = $(window).scrollTop();
		var viewPortHeight = $(window).height();
			
		if (viewPortHeight < 575) {
			$("#poppins").css("top", (viewPortOffset + 0));
		} else {
			$("#poppins").css("top", (viewPortOffset + 25));
		  }			
		
		$("#btnPrevious").hide();
		$("#btnNext").hide();		
		
		var pageVars = $(this).attr("pageVars");
		var page = $(this).attr("id");
		
		$.ajax({
			url: "/_code/page_loader.php",
			data: {"mode": "load_page", "page": page, "page_vars": pageVars},
			context: $("#popDisplayArea"),
			success: function(data) {
				$("#popDisplayArea").html(data);
			}
		});		

	});



	$(".galleryBtn").click(function() {
			
		$("div#blowHole").css("filter", "alpha(opacity=40)"); 
		$("div#blowHole").fadeIn("slow");
			
		$("div#poppins").fadeIn("slow");

		loadCSS("/_css/pop.css");

		var viewPortOffset = $(window).scrollTop();
		var viewPortHeight = $(window).height();
			
		if (viewPortHeight < 575) {
			$("#poppins").css("top", (viewPortOffset + 0));
		} else {
			$("#poppins").css("top", (viewPortOffset + 25));
			}			

	});



/* 
	standalone pop map
*/	
		$(".popMapBtn").click(function() {
		
			var addr = this.attributes.id.value;
			var address = addr + " Savannah, GA 31411";
			
			$("div#blowHole").css('filter', 'alpha(opacity=40)'); 
			$("div#blowHole").fadeIn("slow");
			
			$("div#popMapShell").fadeIn("slow");

			loadCSS('/_css/pop.css');
			
			var mapTab1 = document.getElementById("mapTab1");
			mapTab1.style.display = "inline-block";	
			mapTab1.innerHTML = addr;			
			
			var popMapDisplayArea = document.getElementById("popMapDisplayArea"); 

			var div = document.createElement("div");

			popMapDisplayArea.appendChild(div);

			div.innerHTML = "<div id=\"mapCanvas\"></div>";		

			function getClientY() {
				return window.pageYOffset ? window.pageYOffset : document.body.scrollTop || document.documentElement.scrollTop;
			} 

			function getClientHeight() {
				return window.innerHeight ? window.innerHeight : document.body.clientHeight || document.documentElement.clientHeight;
			}

			winY = getClientY();
			winH = getClientHeight();

			/* pop window shell */
			var popMapShell = document.getElementById("popMapShell");
			popMapShell.style.top = (winY + 0) + "px";
			
			var mapCanvas = document.getElementById("mapCanvas");
			mapCanvas.style.height = (winH - 38) + "px";			



			function initialize(address) {
		
				var latlng = new google.maps.LatLng(32.0783933456931, -81.1255445380571);
		
				var myOptions = {
					zoom: 15,
					center: latlng,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				};

				var map = new google.maps.Map(document.getElementById("mapCanvas"), myOptions);

				geocoder = new google.maps.Geocoder();
		
				codeAddress(address);

				function codeAddress(address) {

					geocoder.geocode({'address': address}, function(results, status) {
						if (status == google.maps.GeocoderStatus.OK) {
							map.setCenter(results[0].geometry.location);
							var marker = new google.maps.Marker({
								map: map, 
								position: results[0].geometry.location
							});
      					} else {
							alert("Geocode was not successful for the following reason: " + status);
				  		}
					});
				}

			}	


			initialize(address);

		});	




/*
	standalone pop map with directions
*/
		$(".popMapBtnD").click(function() {

			var mainGateGeo = "31.934916935154444, -81.04978980125907";

			var addr = this.attributes.id.value;
			var address = addr + " Savannah, GA 31411";
			
			$("div#blowHole").css('filter', 'alpha(opacity=40)'); 
			$("div#blowHole").fadeIn("slow");
			
			$("div#popMapShell").fadeIn("slow");

			loadCSS('/_css/pop.css');


			var mapTab1 = document.getElementById("mapTab1");
			mapTab1.style.display = "inline-block";	
			mapTab1.innerHTML = "Directions to " + addr + " from the Main Gate";			


			/* create the map canvas on-the-fly */
			var popMapDisplayArea = document.getElementById("popMapDisplayArea"); 

			var div = document.createElement("div");

			popMapDisplayArea.appendChild(div);

			div.innerHTML = "<div id=\"directionsMap\"></div><div id=\"popMapDirections\"></div>";		

			function getClientY() {
				return window.pageYOffset ? window.pageYOffset : document.body.scrollTop || document.documentElement.scrollTop;
			} 

			function getClientHeight() {
				return window.innerHeight ? window.innerHeight : document.body.clientHeight || document.documentElement.clientHeight;
			}

			winY = getClientY();
			winH = getClientHeight();
			
			/* pop window shell */
			var popMapShell = document.getElementById("popMapShell");
			popMapShell.style.top = (winY + 0) + "px";
			
			var mapCanvas = document.getElementById("directionsMap");
			mapCanvas.style.height = (winH - 38) + "px";			


			/* G maps directions stuff */
			var directionsDisplay;
			var directionsService = new google.maps.DirectionsService();
			var map;

			function initialize() {

				directionsDisplay = new google.maps.DirectionsRenderer();

				var myOptions = {
					zoom: 15,
					mapTypeId: google.maps.MapTypeId.ROADMAP,
					center: new google.maps.LatLng(32.0783933456931, -81.1255445380571)
				}
				
				map = new google.maps.Map(document.getElementById("directionsMap"), myOptions);
				directionsDisplay.setMap(map);
				directionsDisplay.setPanel(document.getElementById("popMapDirections"));
				
			}


			function computePath(start, finish) {

				var request = {
					origin: start,
					destination: finish,
					travelMode: google.maps.DirectionsTravelMode.DRIVING
				};
	
				directionsService.route(request, function(response, status) {
					// alert("Result: " + status);
					if (status == google.maps.DirectionsStatus.OK) {
						directionsDisplay.setDirections(response);
					// alert("Distance: " + response.routes[0].legs[0].distance.value / 1000 + " kilometer(s)");
					// alert("Travel time: " + response.routes[0].legs[0].duration.value /60 /60 + " hour(s)");
					}
				});
	
			}	

			initialize();
			computePath(mainGateGeo, address);

		});



	$(".popMapClose").click(function() {  
		
		var popMapDisplayArea = document.getElementById("popMapDisplayArea");
		popMapDisplayArea.innerHTML = "";
		
		$("div#popMapShell").fadeOut("slow");
		$("div#blowHole").fadeOut("slow");
			
		unloadCSS("_css/pop.css");

	}); 


	var meOn = false;

	$("div#MLquickTab").click(function() {  

		if (meOn == false) {
			$("div#myLandings").animate({right: '0'}, "slow");
			meOn = true;
		} else {
			$("div#myLandings").animate({right: '-302'}, "slow");
			meOn = false;			
			}
			
	}); 




/*
		working stuff - 2011-04-29 - remove	
*/		
		$(".poop").click(function() {  
		
			myName = this.attributes.name.value;
			
			poop(myName);
			
			myItem = document.getElementById(myName); 

			$(myItem).fadeOut("slow");			
			
		}); 			


}); /* end, listener */	
	
	
	
	
/*
		core functions	
*/

/*
	set and reset state when gallery pop gets opened or reopened
*/
	function loadGallery() {

		var this_idx = 1;

		popDisplayArea.innerHTML = "";
		
		$("#btnPrevious").show();		
		$("#btnNext").show();		
	
		if(!previous_idx) {
			btnPreviousA.style.display = "none";		
			btnPreviousAoff.style.display = "inline-block";			
		} else {
			btnPreviousA.style.display = "inline-block";	
			btnPreviousAoff.style.display = "none";
			btnPreviousA.href = "javascript: move_gallery(previous_idx);";
			
			this_idx = (previous_idx + 1);
			
		  }

		if(!next_idx) {
			btnNextA.style.display = "none";		
			btnNextAoff.style.display = "inline-block";			
		} else {
			btnNextA.style.display = "inline-block";	
			btnNextAoff.style.display = "none";
			btnNextA.href = "javascript: move_gallery(next_idx);";
			
			this_idx = (next_idx - 1);
			
		  }

		popDisplayArea.innerHTML = jsGallery[this_idx];
				
	}


	function loadCSS(filename) {
		var headID = document.getElementsByTagName("head")[0];
		var cssNode = document.createElement('link');
		cssNode.type = 'text/css';
		cssNode.rel = 'stylesheet';
		cssNode.href = filename;
		cssNode.media = 'screen';
		headID.appendChild(cssNode);	
	}


	function unloadCSS(filename) {

		var targetElement = "link";
		var targetAttr = "href";
		var allCtrl = document.getElementsByTagName(targetElement);
	
		/* search backwards within nodelist for matching elements to remove */
	
		for (var i=allCtrl.length; i>=0; i--) { 
			if (allCtrl[i] && allCtrl[i].getAttribute(targetAttr)!=null && allCtrl[i].getAttribute(targetAttr).indexOf(filename)!=-1) {
				allCtrl[i].parentNode.removeChild(allCtrl[i]);
			}	
		}

	}		
	
