$(document).ready(function() {
	$("#day_photo").append("\
		<div id=\"full_map\"></div>\
		<img src=\"/lib/images/entries/thumbs/day40_1238262929.jpg\" alt=\"Next Day Thumbnail\" id=\"photo_next_day\" />\		<img src=\"/lib/images/entries/thumbs/day38_1238262819.jpg\" alt=\"Previous Day Thumbnail\" id=\"photo_prev_day\" />\		");
	$("#preview_map").append("\
		<img src=\"/lib/images/entries/thumbs/day39_1238262827.jpg\" alt=\"Photo Thumbnail\" id=\"day_photo_thumb\"/>\
		");
		
	$("#prev a").hover(
		function(){
			$("#photo_prev_day").show();
		},
		function(){
			$("#photo_prev_day").hide();
		});
	$("#next a").hover(
		function(){
			$("#photo_next_day").show();
		},
		function(){
			$("#photo_next_day").hide();
		});
		
	$init = false;
	$("#preview_map,#location_human,.show-map-on-click").click(function(){
		$init = showMap($init);
		return false;
		}).addClass("jsanchor");
	
	$("#timeline #background").append("\
		<div id=\"timepassed\"></div>\
		<div id=\"marker\"></div>\
		");
	
	$def = $("h1").html();
	$c = 39;
	$max = 516;
	$highlight = 0;
	$clicked = false;
	$hoverIntended = false;
	$("#timeline")
		.ready(function(){
			$("#background #marker").css("left", ($c*100)/516+"%");
			$("#background #timepassed").css("width", ($max*100)/516+"%");
		})
		.hoverIntent(
			function(){
				$hoverIntended = true;
			},
			function(){
				$hoverIntended = false;
			})
		.mousemove(function(e){
			if (!$clicked && $hoverIntended) {
				$padding = 20;
		    	$i = Math.ceil(((e.pageX - $("#timeline #background").offset().left)/960)*516);
				$highlight = ($i <= $max+$padding && $i > -$padding	 && $i != $c) ? (($i>$max)?(($c!=$max)?$max:0):($i<1?(($c!=1)?1:0):$i)) : 0;
				$highlight ? $("h1").text("Show Day " + $highlight) : $("h1").html($def);
				$("#background #marker").css("left", (($highlight ? $highlight : $c)*100)/516+"%");
				$(this).css("cursor", ($highlight ? "pointer" : "auto"));
			}
	    })
		.mouseout(function(){
			if (!$clicked) {
				$highlight = 0;
				$("h1").html($def);
				$("#background #marker").css("left", ($c*100)/516+"%");
			}
		})
		.click(function(){
			//bug currently makes h1 tag clear once following line is called
			//only occuress once html content of h1 has been changed using jquery
			if ($highlight) {
				$clicked = true;
				$("h1").text("Loading Day " + $highlight);
				document.location.href = "/"+ $highlight;
			}
			
		});
	
	$(document).konami(function(){
		$("link[title='color']").attr({href : "/lib/css/INLAND-EMPIRE.css"});
		$("body").addClass("empired").translate("polish");
	});
});

function showMap($init) {
	$("#full_map").show();
	if (!$init) GLoad();
	else {
		$l = $("#full_map").css("left");
		$("#full_map").css("left",$l=="1000px"?"0px":"1000px");
	}
	$("#day_photo_thumb").toggle();
	$('html,body').animate({scrollTop: $("#timeline").offset().top}, 500, "easeOutSine");
	return true;
}

window.onunload= GUnload;

function GLoad() {
	//alert("GLoad");
	if (GBrowserIsCompatible()) {	
		var map = new GMap2(document.getElementById("full_map"));
		//map.setMapType(G_HYBRID_MAP);
		map.setMapType(G_SATELLITE_MAP);
		
		var center = new GLatLng(40.71041,-74.009506);
		map.setCenter(center, 18);
		
		var whiteIcon = new GIcon(G_DEFAULT_ICON);
		whiteIcon.image = "http://www.google.com/intl/en_ALL/mapfiles/marker_white.png";
		
		var marker = new GMarker(center, {clickable: false, icon:whiteIcon});
		map.addOverlay(marker);
		map.addControl(new GMapTypeControl());
		map.addControl(new GLargeMapControl());
	 }	
}
