// IE HTML5 hack
$("<nav>");
$("<header>");
$("<footer>");

if( typeof localStorage == "undefined" ) {
	localStorage = [];
}

// Google analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-15732690-3']);
_gaq.push(['_trackPageview']);
$.getScript("http://www.google-analytics.com/ga.js");

// Google maps
var directionsService,directionDisplay,classLocation,map,errCode = [];
var watermark = "Address, town or postcode";
var from;

function computeDirections() {
	from = $("#from").val();
	
	if( from == watermark ) return false;
	
	directionsService.route({
		origin: from,
		destination: classLocation,
		travelMode: google.maps.DirectionsTravelMode.DRIVING,
		region: "uk",
		unitSystem: google.maps.DirectionsUnitSystem.IMPERIAL		
	}, displayDirections);
	
	return false;
}
	
function displayDirections(reply,status){
	$("#error").hide();
	if( status == google.maps.DirectionsStatus.OK ){
		$("#directions_panel").html("<p>These directions are for planning purposes only. You may find that construction projects, traffic, weather, or other events may cause conditions to differ from the map results, and you should plan your route accordingly. You must obey all signs or notices regarding your route.</p>");
	
		directionsDisplay.setMap(map);
		directionsDisplay.setPanel(document.getElementById('directions_panel'));
		directionsDisplay.setDirections(reply);
		
		$("html, body").animate({scrollTop:$("#map_container").offset().top},600);
		localStorage.location = from;
	}else{
		$("#error").html(errCode[status]).fadeIn(300);
		directionsDisplay.setMap(null);
		directionsDisplay.setPanel(null);
	}
}

function showMap(lat,lng,address){
	classLocation = new google.maps.LatLng(lat,lng);
	map = new google.maps.Map(document.getElementById("map"),{
		zoom: 13,
		center: classLocation,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		scrollwheel: false,
		styles:[{
			featureType: "poi.business",
			elementType: "labels",
			stylers: [{visibility: "off"}]
		}]
	});
	var addressWindow = new google.maps.InfoWindow({content:address});
	var marker = new google.maps.Marker({position:classLocation,map:map});
	addressWindow.open(map,marker);
	
	with( google.maps.DirectionsStatus ){
		errCode[INVALID_REQUEST] = "Invalid request.";
		errCode[OVER_QUERY_LIMIT] = "Cannot obtain directions: Too many queries.";
		errCode[REQUEST_DENIED] = "Request denied.";
		errCode[UNKNOWN_ERROR] = "A connection error occurred while trying to obtain directions. Please try again.";
		errCode[ZERO_RESULTS] = "The route could not be calculated for the specified address.";
		errCode[NOT_FOUND] = "Address not found.";
	}
	
//	$("#location_info").append();

	$("#from").val(localStorage.location || "").focus(function(){
		if($(this).val() == watermark){
			$(this).val("");
		}
		$(this).removeClass("watermark")
	}).blur(function(){
		var isEmpty = $(this).val() == "";

		if( isEmpty ){
			$(this).val(watermark);
		}
			
		$(this).toggleClass("watermark", isEmpty);
	}).blur();
		
	google.maps.event.addListener(marker,'click',function(){addressWindow.open(map,marker)});
	google.maps.event.addListener(map,'click',function(){addressWindow.close()});
	directionsDisplay = new google.maps.DirectionsRenderer();
	directionsService = new google.maps.DirectionsService();
}

$.fn.center = function(){
	var w = $(window);
	return this.css({
		top: (w.height() - this.height()) / 2,
		left: (w.width() - this.width()) / 2
	});
}

$(function(){
	if($("#map").length){
		$("#directionsForm").submit(computeDirections);
		showMap(lat,lng,address);
	}

	var modalBlocker = $("#modalBlocker");
	var imageViewer = $("#imageViewer");
	
	$("a[href$='jpg']").click(function(e) {
		e.stopPropagation();
		
		modalBlocker.fadeTo(200, 0.75).addClass("loading")
		
		imageViewer.find("img").attr("src", $(this).attr("href")+"?"+new Date().getTime()).one("load", function() {

			imageViewer.center().fadeIn(200);
			
			modalBlocker.removeClass("loading");

			$("body").one("click keypress", function(){
				imageViewer.fadeOut(200);
				modalBlocker.fadeOut(200);
				return false;
			});
		});

		e.preventDefault();
	});
});
