$(document).ready(function(){
	var menuAnimationDuration = 500;
	
	$("#navlist a")
		.mouseover(function(){
			$(this)
				.stop()
				.animate({
					backgroundColor:"#007F40",
					borderLeftColor:"#00B55B"
					}, menuAnimationDuration);
			})
		.mouseout(function(){
			$(this)
				.stop()
				.animate({
					backgroundColor:"#FFE4BF",
					borderLeftColor:"#FFC880"
					}, menuAnimationDuration);
			})
		
	$("#navlist ul a")
		.mouseover(function(){
			$(this)
				.stop()
				.animate({
					backgroundColor:"#007F40",
					borderLeftColor:"#00B55B"
					}, menuAnimationDuration);
			})
		.mouseout(function(){
			$(this)
				.stop()
				.animate({
					backgroundColor:"#ffffff",
					borderLeftColor:"#ffe4bf"
					}, menuAnimationDuration);
			})
});


/* 
Gmaps script
------------
Script to include location maps and directions to contacts using the google maps api - JC
05/09/2008 - Now includes transport information using the bus via transport direct - jc
*/
/* 
Global Functions
*/

/* 
This function looks up a postcode returns the latitude and longitude
*/
function usePointFromPostcode(postcode, callbackFunction, address, name) {
    // Sets the action for when the local search has been completed
    localSearch.setSearchCompleteCallback(null,
    function() {
        if (localSearch.results[0]) {
            // Result is found
            // Latitude location of result
            var resultLat = localSearch.results[0].lat;
            // Longitude location of result
            var resultLng = localSearch.results[0].lng;
            // Puts the Longitude and Latitude into a format google prefers
            var point = new GLatLng(resultLat, resultLng);
            // calls placeMarkerAtPoint with information
            callbackFunction(point, postcode, address, name);
        } else {
            // No Results
            // Places alert box to inform user
            alert("Address not found!");
        }
    });
    // Starts geocoding search for postcode.
    localSearch.execute(postcode + ", UK");
}
/*
Puts location of address on map.
*/
function placeMarkerAtPoint(point, postcode, address, name) {
    // Centres map on address
    map[name].setCenter(point, 15);
    // Creates the marker object for address
    var marker = new GMarker(point);
    // Places marker on map
    map[name].addOverlay(marker);
    // Centres map on address
    map[name].setCenter(point, 15);
    // Adds information bubble to map to display marker information. Replaces commas with breaklines for display purposes.
    var PublicTransport;
    PublicTransport="<form method=\"post\" id=\"TransportDirectPlanningForm\" target=\"_blank\" action=\"http://www.transportdirect.info/web2/journeyplanning/jpLandingPage.aspx\">\n";
PublicTransport+="<table width=\"230px\" cellspacing=\"0\" style=\"margin: 1em;\">\n";
PublicTransport+="<tr bgcolor=\"#330099\">\n";
PublicTransport+="<td>\n";
PublicTransport+="<p style=\"margin: 2px 2px 2px 4px\">\n";
PublicTransport+="<img alt=\"Transport Direct (External link)\" src=\"http://www.transportdirect.info/Web2/App_Themes/TransportDirect/images/gifs/misc/TDLogo38.gif\" /></p>\n";
PublicTransport+="</td>\n";
PublicTransport+="</tr>\n";
PublicTransport+="<tr bgcolor=\"#99ccff\">\n";
PublicTransport+="<td>\n";
PublicTransport+="<p style=\"font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; text-align: left;\n";
PublicTransport+="margin: 5px 5px 5px 5px;\">\n";
PublicTransport+="Get directions by public transport and car with <a href=\"http://www.transportdirect.info/web2/Home.aspx\"\n";
PublicTransport+="target=\"_blank\">Transport Direct (External link)</a>.</p>\n";
PublicTransport+="<p style=\"font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; text-align: left;\n";
PublicTransport+="margin: 5px 5px 5px 5px;\">\n";
PublicTransport+="Enter your postcode</p>\n";
PublicTransport+="</td>\n";
PublicTransport+="</tr>\n";
PublicTransport+="<tr bgcolor=\"#99ccff\">\n";
PublicTransport+="<td>\n";
PublicTransport+="<p style=\"font-family: verdana, arial, helvetica, sans-serif; font-size: 12px; text-align: left;\n";
PublicTransport+="margin: 5px 5px 5px 5px;\">\n";
PublicTransport+="<input type=\"text\" id=\"txtOrigin\" name=\"o\" style=\"width: 80px; border-color: lightgrey;\n";
PublicTransport+="border-width: 1px; border-style: solid; font-size: 12px; font-family: verdana, arial, helvetica, sans-serif;\n";
PublicTransport+="background-color: white; height: 16px;\" />\n";
PublicTransport+="<input type=\"submit\" id=\"btnSubmit\" value=\"Go\" style=\"color: #264266; background-color: #ebebeb;\n";
PublicTransport+="font-family: verdana, arial, helvetica, sans-serif; font-weight: bold; font-size: 12px;\n";
PublicTransport+="text-align: center; text-decoration: none; border-bottom-color: #808080; border-top-color: #C0C0C0;\n";
PublicTransport+="border-right-color: #808080; border-left-color: #C0C0C0; border-width: 1px; border-style: solid;\n";
PublicTransport+="cursor: pointer; cursor: hand; width: auto; overflow: visible; padding: 0px 3px 1px 3px;\" />\n";
PublicTransport+="<input type=\"hidden\" name=\"oo\" value=\"p\" />\n";
PublicTransport+="<input type=\"hidden\" name=\"et\" value=\"jp\" />\n";
PublicTransport+="<input type=\"hidden\" name=\"m\" value=\"m\" />\n";
PublicTransport+="<input type=\"hidden\" name=\"do\" value=\"p\" />\n";
PublicTransport+="<input type=\"hidden\" name=\"d\" id=\"destinationData\" value=\"" + postcode + "\" />";
PublicTransport+="<input type=\"hidden\" name=\"dn\" value=\"" + postcode + "\" />";
PublicTransport+="<input type=\"hidden\" name=\"p\" value=\"1\" />\n";
PublicTransport+="<input type=\"hidden\" name=\"id\" value=\"BusinessLinks\" />\n";
PublicTransport+="</p>\n";
PublicTransport+="</td>\n";
PublicTransport+="</tr>\n";
PublicTransport+="</table>\n";
PublicTransport+="</form>\n";
    var NewAddress = marker.openInfoWindowTabsHtml([new GInfoWindowTab('Address',address.replace(/,/g, ",<br>") + postcode), new GInfoWindowTab("Transport",PublicTransport)]);
}
/*
This function is called from a button on the page, it will display directions from a specified postcode to the contact address.
*/
function doDirections(directionDiv, name, postcode) {
    // Select panel to place directions
    directionsPanel = document.getElementById(directionDiv);
    // Select the users entered postcode
    directionsStart = document.getElementById("from" + name).value;
    // Check if directions object for address exisits.
    if (!directions[name]) {
        // Directions object doesn't exisit
        // Create new directions object
        directions[name] = new GDirections(map[name], directionsPanel);
        // Add disclaimer text 
        directionsPanel.innerHTML = directionsPanel.innerHTML + "<p style=\"font-size: 75%\">These directions are for planning purposes only. You may find that construction projects, traffic, or other events may cause road conditions to differ from the map results.<\/p>";
        // Add print option
        directionsPanel.innerHTML = directionsPanel.innerHTML + "<a href=\"javascript:PrintDirections(" + name + ")\">Print directions to " + postcode + "<\/a>";
    }
    // Sets the action for when the local search for start has been completed
    localSearch2.setSearchCompleteCallback(null,
    function() {
        if (localSearch2.results[0]) {
            // Start address found
            // Sets the action for when the local search for contact has been completed
            localSearch.setSearchCompleteCallback(null,
            function() {
                if (localSearch.results[0]) {
                    // Both address found
                    // Clear exisiting directions if required.
                    directions[name].clear();
                    // Request directions, and load onto page
					if (postcode == "LA20 6EG") {
						
						var loc1 = new GLatLng(localSearch2.results[0].lat, localSearch2.results[0].lng);
		var loc2 = new GLatLng(54.283467,-3.229980);
		var loc3 = new GLatLng(localSearch.results[0].lat, localSearch.results[0].lng);
												directions[name].loadFromWaypoints([loc1, loc2, loc3]);
												//
						//alert("mo0");
					} else {
						
						directions[name].load(localSearch2.results[0].lat + "," + localSearch2.results[0].lng + " to " + localSearch.results[0].lat + "," + localSearch.results[0].lng + "");
					}
					
                
                } else {
                    // End address not found
                    alert("Address not found!");
                }
            });
            // Search end address
            localSearch.execute(postcode + ", UK");
        } else {
            // Start postcode not found
            alert("Start address not found!");
        }
    });
    // Search start address
    localSearch2.execute(directionsStart + ", UK");
}
	function createMarker(point, icon, text, title) {
  		var marker = new GMarker(point,{title:title, icon:icon});
  		GEvent.addListener(marker, "click", function() {
    		marker.openInfoWindowHtml(text);
  		});
  	return marker;
	}
/*
Function to initilise the map, run when user clicks "Location of X"
*/
function mapLoad(mapid, name) {
    //Checks browser is compatible
    if (GBrowserIsCompatible()) {

 
        // Creates the map object
        map[name] = new GMap2(document.getElementById(mapid));
        // Adds zoom and movement tools
        map[name].addControl(new GLargeMapControl());
        // Adds map type options
        map[name].addControl(new GMapTypeControl());
        // Zooms to townhall as a default location.
        map[name].setCenter(new GLatLng(53.54826350153167, -2.629680633544922), 13);
        // Looks up postcode for contact and places icon and bubble with address details on map.
      var loc1 = new GLatLng(54.389173, -3.171608);
		var loc2 = new GLatLng(54.363152,-3.05451);
		
		var aIcon = new GIcon(G_DEFAULT_ICON);
		aIcon.image = "http://maps.google.com/intl/en_de/mapfiles/icon_green.png";
		
		var bIcon = new GIcon(G_DEFAULT_ICON);
		bIcon.image = "http://maps.google.com/intl/en_de/mapfiles/icon_green.png";
		var marker = createMarker(
			loc1, bIcon,
			'<h2>Hinning House</h2><p>Seathwaite,<br>Broughton in Furness,<br>LA20 6EG</p>');
		map[name].addOverlay(marker);
		
		var marker = createMarker(
			loc2, aIcon,
			'<h2>Low Bank Ground </h2><p>Coniston,<br>Cumbria,<br>LA21 8AA</p>');
		map[name].addOverlay(marker);
		map[name].setCenter(loc1, 10);
    }
}
/*
Function to print the directions generated.
*/
function PrintDirections(name) {
    try {
        // Tries to put directions in the div and print.
        // Selects print iframe
        var oIframe = document.getElementById('ifrmPrint');
        // Selects directions div
        var oContent = document.getElementById("mapholder" + name + "directions").innerHTML;
        // puts div contents into iframe and prints.
        var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
        if (oDoc.document) oDoc = oDoc.document;
        oDoc.write("<html><head><title>title<\/title>");
        oDoc.write("<\/head><body onload='this.focus(); this.print();'>");
        oDoc.write(oContent + "<\/body><\/html>");
        oDoc.close();
    } catch(e) {
        // If all else fails prints all the page
        self.print();
		alert(e);
    }
}