/*
$(function() {
  if (typeof jQuery.fn.pngFix != 'undefined') {
    $(document).pngFix()
  }
});
*/

$(function() {
  if ($('#googlemap').get().length > 0) {
    var mapOptions = {
      zoom: 15,
      center: new google.maps.LatLng(53.42409800194218, -2.3227500915527343),
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      navigationControl: true
    };
    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(53.42409800194218, -2.3227500915527343),
      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'))
      }
    }
  })
});
$(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 ($('#categories').get().length > 0) {
    // hide all stores within each category initially
    $('#categories ul:not(.selected)').hide()
    // toggle display of stores for each category when clicked
    $('#categories > li > a').click(function(e) {
      e.preventDefault()
      $(this).parent().find('ul').each(function() {
        $(this).slideToggle()
      });
    });
  }
});
$(function() {
  $('#tab-content-categories').height() < 290 ? $('#tab-content-categories').css('height', '290px') : null
  $('#tab-content-news').height() < 290 ? $('#tab-content-news').css('height', '290px') : null
  $('#tab-content-offers').height() < 290 ? $('#tab-content-offers').css('height', '290px') : null
  $('li#tab-categories > a').click(
    function(e) {
      e.preventDefault()
      if ($('div#tab-content-categories').css('display') == 'none') {
        var offset = $(this).offset()
        $('div#tab-content-categories').css({
          'left' : offset.left - 22 + 'px',
          'top' : offset.top + 30 + 'px'
        })
        $(this).parent().parent().parent().attr('id', 'tab1')
        $(this).parent().parent().find('a').each(function() {
          $(this).css({'background-position' : 'top left'})
        })
        $('div#tab-content-news').slideUp()
        $('div#tab-content-offers').slideUp()
        $('div#tab-content-categories').slideDown()
        $(this).css({'background-position' : 'bottom left'})
        // make borders all same height
        var colHeight = Math.max(290, $('div#category-tab-1').height(), $('div#category-tab-2').height(), $('div#category-tab-3').height())
        $('div#category-tab-2').css('height', colHeight + 'px')
      } else {
        $('div#tab-content-categories').slideUp()
        $(this).css({'background-position' : 'top left'})
      }
    }
  )
  $('li#tab-news > a').click(
    function(e) {
      e.preventDefault()
      if ($('div#tab-content-news').css('display') == 'none') {
        var offset = $(this).offset()
        $('div#tab-content-news').css({
          'left' : offset.left - 349 + 'px',
          'top' : offset.top + 30 + 'px'
        })
        $(this).parent().parent().parent().attr('id', 'tab2')
        $(this).parent().parent().find('a').each(function() {
          $(this).css({'background-position' : 'top left'})
        })
        $('div#tab-content-categories').slideUp()
        $('div#tab-content-offers').slideUp()
        $('div#tab-content-news').slideDown()
        $(this).css({'background-position' : 'bottom left'})
        // make borders all same height
        var colHeight = Math.max(290, $('div#news-tab-1').height(), $('div#news-tab-2').height(), $('div#news-tab-3').height())
        $('div#news-tab-2').css('height', colHeight + 'px')
      } else {
        $('div#tab-content-news').slideUp()
        $(this).css({'background-position' : 'top left'})
      }
    }
  )
  $('li#tab-offers > a').click(
    function(e) {
      e.preventDefault()
      if ($('div#tab-content-offers').css('display') == 'none') {
        var offset = $(this).offset()
        $('div#tab-content-offers').css({
          'left' : offset.left - 676 + 'px',
          'top' : offset.top + 30 + 'px'
        })
        $(this).parent().parent().parent().attr('id', 'tab3')
        $(this).parent().parent().find('a').each(function() {
          $(this).css({'background-position' : 'top left'})
        })
        $('div#tab-content-categories').slideUp()
        $('div#tab-content-news').slideUp()
        $('div#tab-content-offers').slideDown()
        $(this).css({'background-position' : 'bottom left'})
        // make borders all same height
        var colHeight = Math.max(290, $('div#offer-tab-1').height(), $('div#offer-tab-2').height(), $('div#offer-tab-3').height())
        $('div#offer-tab-2').css('height', colHeight + 'px')
      } else {
        $('div#tab-content-offers').slideUp()
        $(this).css({'background-position' : 'top left'})
      }
    }
  )
})

// Google Analytics - track downloads and external links
if (typeof pageTracker != 'undefined') {
  $(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)
  }
}

$(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 ($('a').hasClass('fancybox')) {
    $('a.fancybox').fancybox({
      overlayShow  :  true
    });
  }
})
// manage email a friend box
$(function(){
  $('#send-friend').click(function(event) {
    event.preventDefault();
    var buttonPosition = $(this).position()
    $('#email-friends-box').css({'left': buttonPosition.left + 'px', 'top': (buttonPosition.top - 100) + 'px', 'display': 'block'})
    $('#email-friends-box').animate({ width: '250px', height: '200px'})
  });
  $('#email-friends-box input#cancel').click(function(){
    $('#email-friends-box').css({'width': '50px', 'height': '50px', 'display': 'none'})
  });
});
// cross browser bookmark
$(function(){
  // add a "rel" attrib if Opera 7+
  if(window.opera) {
    if ($('a.bookmark').attr('rel') != '') { // don't overwrite the rel attrib if already set
      $('a.bookmark').attr('rel','sidebar')
    }
  }
  $('a.bookmark').click(function(e) {
    e.preventDefault()
    var url = window.location.href
    var title = $(document).attr('title');
    if (window.sidebar) { // Mozilla Firefox Bookmark
      window.sidebar.addPanel(title, url,'')
    } else if (window.external) { // IE Favorite
      window.external.AddFavorite(url, title)
    } else if (window.opera) { // Opera 7+
      return false // do nothing - the rel="sidebar" should do the trick
    } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
      alert('Unfortunately, this browser does not support the requested action,' + ' please bookmark this page manually.')
    }
  });
});
$(function() {
  $('a.print').click(function(e) {
    e.preventDefault()
    window.print()
  })
})
$(function() {
  $('div.feature').css('cursor', 'pointer')
  $('div.feature').click(function() {
    window.location = $(this).find('a').attr('href')
  })
})
// login panel
$(function() {
  // Expand Panel
  $('#occupiers-info-button').click(function(e) {
    if ($(this).find('ul').attr('class') == 'logged-in') {
      return
    }
    e.preventDefault()
    if ($('div#panel').css('display') == 'none') {
      $('div#panel').slideDown('slow')
    } else {
      $('div#panel').slideUp('slow')
    }

  });
});
// Cufon config
Cufon.replace([
    'h1',
    'h2',
    'h3:not(#send-friend-title)',
    'div.tabs > ul > li > a',
    'ul#nav a',
    'div.categories ol a',
    'div#occupiers-info-button a'
  ], {
    hover: true
  }
)
