$(function(){

  // Set up background music for a page
  function setup_audio(file){

    $('body').append('<div id="music"></div>');

    $('#stop_play')
      .unbind()
      .show()
      .bind('mute',  mute)
      .bind('play',  play)
      .click(function(){
        $('#stop_play').data('muted') ? $('#stop_play').trigger('play') : $('#stop_play').trigger('mute');
      });

    $('#music').jPlayer({
      ready: function(){
        var audio_path = '/audio/' + file;
        $(this).setFile(audio_path + '.mp3', audio_path + '.ogg');
 
        $.cookie('muted') ? $('#stop_play').trigger('mute') : $('#stop_play').trigger('play');

      },
      swfPath: '/flash',
      oggSupport: true
    });

  }

  function mute(){
    $.cookie('muted', 'true');
    $('#stop_play')
      .addClass('muted')
      .data('muted', true);
    $('#music').stop();
  }

  function play(){
    $.cookie('muted', null);
    $('#stop_play')
      .removeClass('muted')
      .data('muted', false);
    $('#music').play();
  }

  // Set up some vars
  var restaurant = false;
  var audio_file = false;

  // Theme the site accordingly
  var restaurants = ['malones-banquets', 'harrys', 'malones', 'drakes', 'aqua', 'sals'];
  $.each(restaurants, function(i,r){
    // Check if we're on a restaurant page
    var search = new RegExp('(' + r + '$|' + r + '\/)');
    if (location.pathname.match(search)) {
      restaurant = r;
      return;
    }
  });

  if(restaurant) {
    $('#container')
      .addClass('restaurant')
      .addClass(restaurant);

    // Check if we're at the restaurant's "home page" and add the music stuff
    var search = new RegExp('(' + restaurant + '$|' + restaurant + '\/$)');
    if(location.pathname.match(search)) audio_file = restaurant;
  }

  // Homepage gets the bhg file
  if(location.pathname.match('(^$|^\/$)')) audio_file = 'bhg';

  // Finally, if there is audio on this page, set it up.
  if(audio_file) setup_audio(audio_file);

  // Give #subnav 100% height
  /*
  var height = $('#nav-and-text').height();
  $('#nav-and-text')
    .css('height', height)
    .find('#subnav')
      .css('height', '100%');
  */

  // Cycle slideshows
  $('#slides').cycle({timeout: 6000});

  // Make the gift card image animate
  $('#bhg-rewards').giftcard_animate();

  // Make the gallery navigation work
  $('#gallery-nav').gallery_nav();

  // All external links and documents need to open in a new window
  $('a').filter(function() {
    if(this.hostname !== location.hostname){ return true; }
    if(this.pathname.match('pdf')) { return true; }
    return false;
  }).click(function() {
    window.open($(this).attr('href'), '_target');
    return false;
  });

  // Set home page menu downloader
  $('#menu_downloader').menu_downloader();

  // Clicking "Live Chat" opens a chat window
  $('#buttons .livechat-link').click(function(){
    window.open('http://chat.livechatinc.net/licence/1024605/open_chat.cgi?groups=0'+'&s=1&lang=en&dc='+escape(document.cookie+';l='+document.location+';r='+document.referer+';s='+typeof lc_session),'Chat_1024605','width=604,height=247,resizable=no,scrollbars=no,status=1');
    return false;
  });

  // Set up multiple reserve-it selector for Malones
  $('#buttons .malones-link').unbind();
  $("#buttons .malones-link").click(function(){
    $.facebox({div: '#reserve-it-selector'}, 'reserve-it');
    $('#facebox form').submit(function(){
      var loc = $(this).find('select').val();
      window.open('https://www.reserveit.net/guest/dining/' + loc, '_target');
      return false;
    });
    return false;
  });

});

// Cufon text replacement
Cufon.replace('h2.title', {
  textShadow: '2px 2px 4px #828282',
  fontSize: '30px'
});

