var getClientLocation;
if (google.loader.ClientLocation) {
  getClientLocation = function() {
    return google.loader.ClientLocation;
  }
} else {
  document.write('<script type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>');
  getClientLocation = function() {
    return {
      latitude: geoip_latitude(),
      longitude: geoip_longitude(),
      address: {
        city: geoip_city(),
        country: geoip_country_name(),
        country_code: geoip_country_code(),
        region: geoip_region_name()
      }
    };
  }
}

google.load('jquery', '1');
google.load('maps', '3', {'other_params' : 'sensor=false'});

google.setOnLoadCallback(function() {
    initSyntaxHighlighter();
    initGallery();
    initMap();
});

var initSyntaxHighlighter = function() {
    /* 
     * To avoid a runtime error in IE, replace:
     * cs.innerHTML = newContent;
     * with
     * $(cs).html(newContent);
     * in prettify.js (unminified) or
     * f.innerHTML=a
     * with
     * $(f).html(a)
     * in the minified version of prettify.js.
     */
    var codeElements = $('code');
    if(codeElements.length) {
        codeElements.replaceWith(function() {
            var htmlContent = $(this).html();
            if(!htmlContent) {
                return '';
            }
            return  '<ol class="code"><li><code class="prettyprint">'+
                    $(this).html().replace(/<br>\s*\n|<BR>/g,'</code></li><li><code class="prettyprint">')+
                    '</code></li></ol>';
        });
        $.getScript('/wp-content/themes/aquantum-2.0/prettify/prettify.js', function() {
            prettyPrint();
        });
    }
}

var initGallery = function() {
    /*
     * To avoid loading problems in IE, adjust the src paths for the AlphaImageLoader.
     * In jquery.fancybox-1.3.1.css
     * replace
     * fancybox/
     * with
     * /wp-content/themes/aquantum-2.0/fancybox/
     */
    if($('.gallery a, a.lightbox').length) {
        $.getScript('/wp-content/themes/aquantum-2.0/fancybox/jquery.fancybox-1.3.1.pack.js', function() {
            $('.gallery a').attr('rel', 'gallery').fancybox({
                'overlayOpacity' : 0.8,
                'overlayColor' : '#000'
            });
        });
    }
}

var initMap = function() {
  $('#aquantum_map').each(function(index, mapCanvas) {
    var clientLocation = getClientLocation();
    var clientLatLng = new google.maps.LatLng(
      clientLocation.latitude,
      clientLocation.longitude
    );
    var aquantumLatLng = new google.maps.LatLng(
      47.975218612441594,
      7.829093188047409
    );
    var directionsDisplay = new google.maps.DirectionsRenderer();
    var directionsService = new google.maps.DirectionsService();
    var mapOptions = {
      zoom:7,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: aquantumLatLng
    }
    var map = new google.maps.Map(mapCanvas, mapOptions);
    var marker = new google.maps.Marker({
        position: aquantumLatLng, 
        map: map,
        title: 'AQUANTUM'
    });
    var infoWindow = new google.maps.InfoWindow({
      content: $('#contact_address').get(0).cloneNode(true)
    });
    google.maps.event.addListener(marker, 'click', function() {
      infoWindow.open(map,marker);
    });
    directionsDisplay.setMap(map);
    var directionRequest = {
        origin: clientLatLng, 
        destination: aquantumLatLng,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(directionRequest, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
  });
}