document.createElement('header');
document.createElement('hgroup');
document.createElement('nav');
document.createElement('menu');
document.createElement('section');
document.createElement('article');
document.createElement('aside');
document.createElement('footer');

jQuery(document).ready(function () {
    jQuery("#search .postcode").focus(function () {
        var thisEl = jQuery(this);
        if (thisEl.val() === "Postcode") {
            thisEl.val("");
        }
    });
    jQuery("#search .postcode").blur(function () {
        var thisEl = jQuery(this);
        if (thisEl.val() === "") {
            thisEl.val("Postcode");
        }
    });
  
    jQuery("#getDirections").click(function () {
        calcRoute()
    });
});


function sortHeight() {
  var $leftCol = jQuery("#navColumn");
  var newHeight = $leftCol.height();
  var $slides = jQuery(".slides");
  var $mapCanvas = jQuery("#mapCanvas");
  
  if ($slides.length > 0)
    newHeight -= $slides.height();
  if ($mapCanvas.length > 0)
    newHeight -= $mapCanvas.height();
  jQuery("#heightFix").css({"height": newHeight + "px"});
} 

jQuery(window).load(function () {
    if (jQuery("#mapCanvas").length > 0)
        loadMapScript();

    var $pagination = jQuery("#banner .pagination");
    $pagination.css({
        "left": 460 - $pagination.width() / 2 + "px"
    })
      
    sortHeight();
});

var directionsDisplay;
var directionsService;
var map;

function loadMap() {
    var myLatlng = new google.maps.LatLng(52.46354,-1.883469);
    var myOptions = {
        zoom: 13,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    directionsDisplay = new google.maps.DirectionsRenderer(); ;
    directionsService = new google.maps.DirectionsService();
    map = new google.maps.Map(document.getElementById("mapCanvas"), myOptions);

    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directionResult"));

    var info = "<p>Birmingham Car Auctions<br />302-312 Moseley Road<br />Birmingham<br />B12 0BS</p>";

    var infowindow = new google.maps.InfoWindow({
        content: info
    });

    var marker = new google.maps.Marker({
        position: myLatlng,
        map: map,
        title: 'Birmingham Car Auctions'
    });

    google.maps.event.addListener(marker, 'click', function () {
      infowindow.open(map, marker);
    });
}

function loadMapScript() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=loadMap";
  document.body.appendChild(script);
}

function calcRoute() {
    google.maps.event.addListener(directionsDisplay, 'directions_changed', function () {
        //computeTotalDistance(directionsDisplay.directions);
    });

    var request = {
        origin: jQuery("#origin").val(),
        destination: "Moseley Road+Birmingham+B12+0BS",
        travelMode: google.maps.DirectionsTravelMode.DRIVING,
        unitSystem: google.maps.UnitSystem.IMPERIAL
    };
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });
}

function computeTotalDistance(result) {
    var total = 0;
    var myroute = result.routes[0];
    for (i = 0; i < myroute.legs.length; i++) {
        total += myroute.legs[i].distance.value;
    }
    total = total / 1000.
    //document.getElementById("total").innerHTML = total + " km";
}

