// BEHAVIOUR
$(document).ready(function() {

  // unobfuscate email addresses
  $('span.email').each(function() {
    // convert text inside the element to a valid email address
    email = $(this).text().replace(/ AT /i, '@').replace(/ \. /g, '.').replace(/ DOT /ig, '.')
    // wrap link tag around the element
    $(this).wrap('<a href="mailto:' + email + '"></a>')
    // put the email address as text inside the link tag (old class="email" element is automatically removed)
    $(this).parent().text(email)
  })

  // add 'rel' attributes to images to make them lightboxed
  $('.column1 a[href$=\.jpg] img, .column1 a[href$=\.png] img, .gallery a[href$=\.jpg] img').each(function() {
    $(this).parent().attr('rel', 'lightbox[gallery]')
  })

  // add zoom icon for some lightboxed images
  $('.column1 a[href$=\.jpg] img, .column1 a[href$=\.png] img').each(function() {
    var zoom = $('<img src="/images/zoom-icon.png" class="zoom" />')
    $(this).parent().append(zoom)
  })

  // lightbox images
  $("a[rel=lightbox[gallery]]").lightbox({
    fileLoadingImage: '/images/lightbox/loading.gif',
    fileBottomNavCloseImage: '/images/lightbox/close.gif'
  })

  // Google maps
  if ($('#map').length > 0) {
    if (!GBrowserIsCompatible()) return

    var map = new GMap2(document.getElementById("map"))
    var kaniaCoords = new GLatLng(54.377649, 18.183226)
    map.setMapType(G_NORMAL_MAP)
    map.setUIToDefault()

    var marker = new GMarker(kaniaCoords)
    var info = "<strong>Kania Lodge</strong><br />Sytna Góra 10<br />83-300 Kartuzy"

    GEvent.addListener(marker, 'click', function() {
      map.openInfoWindowHtml(kaniaCoords, info)
      map.setCenter(kaniaCoords)
    })

    map.addOverlay(marker)

    // location page
    if ($('#location').length > 0) {
      map.setCenter(kaniaCoords, 8)
      map.openInfoWindowHtml(kaniaCoords, info)
    }

    // driving directions
    if ($('#driving-directions').length > 0) {
      var polyline = new GPolyline([
        new GLatLng(54.335025, 18.20161),  // majkowskiego
        new GLatLng(54.336213, 18.200859),
        new GLatLng(54.337702, 18.199486),
        new GLatLng(54.339403, 18.198563),
        new GLatLng(54.340961, 18.198112),
        new GLatLng(54.341749, 18.197318),
        new GLatLng(54.342818, 18.196084), // klasztorne (most)
        new GLatLng(54.343118, 18.195816),
        new GLatLng(54.343994, 18.195763),
        new GLatLng(54.344063, 18.195784),
        new GLatLng(54.344432, 18.195966),
        new GLatLng(54.345045, 18.196621),
        new GLatLng(54.345707, 18.196836),
        new GLatLng(54.347383, 18.196331),
        new GLatLng(54.348453, 18.197447),
        new GLatLng(54.349384, 18.199732),
        new GLatLng(54.349985, 18.199893),
        new GLatLng(54.350316, 18.199636),
        new GLatLng(54.351692, 18.197608),
        new GLatLng(54.352836, 18.197147),
        new GLatLng(54.354612, 18.192512),
        new GLatLng(54.354249, 18.190463),
        new GLatLng(54.354618, 18.185946),
        new GLatLng(54.354662, 18.182051),
        new GLatLng(54.355437, 18.17806),
        new GLatLng(54.355293, 18.172975),
        new GLatLng(54.355762, 18.17041),
        new GLatLng(54.356212, 18.169745),
        new GLatLng(54.356294, 18.168812),
        new GLatLng(54.356119, 18.167213),
        new GLatLng(54.356431, 18.166527),
        new GLatLng(54.356481, 18.166494),
        new GLatLng(54.357432, 18.166205),
        new GLatLng(54.358294, 18.166763),
        new GLatLng(54.358801, 18.166741), // kosciół
        new GLatLng(54.360295, 18.169692),
        new GLatLng(54.363658, 18.172545), // tory kolejowe
        new GLatLng(54.365721, 18.171644),
        new GLatLng(54.366618, 18.171333),
        new GLatLng(54.367705, 18.170861),
        new GLatLng(54.368521, 18.170367),
        new GLatLng(54.368696, 18.170153),
        new GLatLng(54.369234, 18.169439),
        new GLatLng(54.369524, 18.16938),
        new GLatLng(54.371471, 18.170239),
        new GLatLng(54.371877, 18.170432),
        new GLatLng(54.372499, 18.171097),
        new GLatLng(54.372905, 18.170925),
        new GLatLng(54.373155, 18.170577),
        new GLatLng(54.373977, 18.169627),
        new GLatLng(54.375146, 18.169193),
        new GLatLng(54.375511, 18.16915),
        new GLatLng(54.375783, 18.169295),
        new GLatLng(54.377249, 18.170899),
        new GLatLng(54.377327, 18.171017),
        new GLatLng(54.377542, 18.171537),
        new GLatLng(54.37783,  18.171853),
        new GLatLng(54.378658, 18.171977),
        new GLatLng(54.379061, 18.172245),
        new GLatLng(54.37957,  18.173195),
        new GLatLng(54.381154, 18.173559),
        new GLatLng(54.381979, 18.174219),
        new GLatLng(54.382825, 18.175303),
        new GLatLng(54.3831,   18.175582), // krzyż
        new GLatLng(54.381932, 18.17733),
        new GLatLng(54.381057, 18.179063),
        new GLatLng(54.380635, 18.180715),
        new GLatLng(54.379745, 18.180833),
        new GLatLng(54.379001, 18.180351),
        new GLatLng(54.378645, 18.1803882),
        new GLatLng(54.377802, 18.1832582),
        kaniaCoords], "#AA0000", 6)

      map.addOverlay(polyline)
      map.setCenter(new GLatLng(54.364258, 18.191986), 12)
    }
  }

})

// Execute only AFTER all images have been loaded
$(window).load(function() {

  // make frontpage columns equal height
  var column1 = $("#welcome #document .column1")
  var column2 = $("#welcome #document .column2")
  if ((column1.length > 0) && (column2.length > 0)) {
    if (column1.height() > column2.height()) {
      column2.css('padding-bottom', column1.height() - column2.height())
    } else {
      column1.css('padding-bottom', column2.height() - column1.height())
    }
  }

})
