$(function() {
  if (typeof jQuery.fn.pngFix != 'undefined') {
    $(document).pngFix()
  }
});
// Load google map
$(function() {
  if ($('#googlemap').get().length > 0) {
    var mapOptions = {
      zoom: 15,
      center: new google.maps.LatLng(51.53783404300503, 0.08178055286407471),
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      navigationControl: true,
      navigationControlOptions: {
        style: google.maps.NavigationControlStyle.SMALL
      }
    };
    var map = new google.maps.Map($('#googlemap').get(0), mapOptions);
    var logoImage = new google.maps.MarkerImage('/images/map-logo.png',
      new google.maps.Size(130, 52), // size
      new google.maps.Point(0, 0),    // origin
      new google.maps.Point(130, 48)     // anchor
    );
    var logo = new google.maps.Marker({
      position: new google.maps.LatLng(51.53783404300503, 0.08178055286407471),
      map: map,
      icon: logoImage,
      clickable: false,
      title: 'Vicarage Field'
    });
  }
});
// hide form labels on focus
$(function() {
  $('input[type=text], textarea').focus(function() {
    if ($(this).hasClass('inline-label')) {
      if (typeof $(this).attr('rel') == 'undefined') {
        $(this).attr('rel', $(this).attr('value'))
      }
      if ($(this).attr('value') == $(this).attr('rel')) {
        $(this).attr('value', '')
      }
    }
  })
  $('input[type=text], textarea').blur(function() {
    if ($(this).hasClass('inline-label')) {
      if ($(this).attr('value') == '') {
        $(this).attr('value', $(this).attr('rel'))
      }
    }
  })
});
// Google Analytics - track downloads and external links
$(function() {
  $('a').click(function() {
    if ($(this).get(0).protocol == 'mailto:') {
      pageTracker._trackPageview('/mailto/' + $(this).attr('href').substring(7))
    } else if ($(this).get(0).hostname == location.host) {
      var path = $(this).get(0).pathname + $(this).get(0).search;
      var isDoc = path.match(/\.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/);
      if (isDoc) {
        trackPageView($(this).get(0), 'downloads')
      }
    } else {
      trackPageView($(this).get(0), 'external')
    }
  });
});
trackPageView = function(link, tag) {
  var page = (link.pathname.charAt(0) == "/") ? link.pathname : "/" + link.pathname
  if (link.search && link.pathname.indexOf(link.search) == -1) page += link.search
  if (link.hostname != location.host) page = '/' + tag + '/' + link.hostname + page
  pageTracker._trackPageview(page)
}
// add margin between groups of letters in alphabetic list of shops on shops navigation
$(function() {
  if ($('#store-list').get().length > 0) {
    var lastFirstLetter = ''
    $('#store-list li a').each(function() {
      var currentFirstLetter = $(this).text().substr(0, 1)
      if (lastFirstLetter != '' && currentFirstLetter != lastFirstLetter) {
        $(this).parent().addClass('letter')
      }
      lastFirstLetter = currentFirstLetter
    })
  }
})
$(function() {
  if ($('#categories').get().length > 0) {
    // hide all stores within each category initially
    $('#categories ul ul:not(.selected)').hide()
    // toggle display of stores for each category when clicked
    $('#categories ul:first > li > a').click(function(e) {
      e.preventDefault()
      $(this).parent().find('ul').each(function() {
        $(this).slideToggle()
      });
    });
  }
});
$(function() {
  if (typeof Moo == 'undefined') {
    return false
  }
  slideshowId = Moo.$('slideshow-home') ? 'slideshow-home' : (Moo.$('slideshow') ? 'slideshow' : false)
  if (!slideshowId) {
    return false
  }
  var slideshow = new gallery(Moo.$(slideshowId), {
    elementSelector: 'div.slide',
    titleSelector: 'h2',
    timed: true,
    delay: 5000,
    embedLinks: false,
    showInfopane: true,
    showArrows: false
  });
});
$(function() {
  if ($('#features div.feature').get().length > 0) {
    $('#features div.feature').click(function(e) {
      window.location = $(this).find('h2 a').attr('href')
    })
  }
});