// --------------------------
// Helpers
// --------------------------

var bannerCurrentElement;
var bannerCurrentElId;
var bannerTime = 0;
var bannerInterval;

$.fn.innerfade_with_pagination = function(options) {
  var settings = {
    // Innerfade default config
    animationtype: 'fade',
    speed: 'normal',
    timeout: 2000,
    type: 'sequence',
    containerheight: 'auto',
    runningclass: 'innerfade',
    // Pagination default config
    pagination_container_id: 'images_pagination',
    pagination_item_class_base: 'image_',
    previous_link_class: 'previous',
    next_link_class: 'next'
  };

  if(options) {
    $.extend(settings, options);
  }

  var elements = $(this).children();

  if (elements.length > 1) {
    $(this).innerfade(settings);

    $('#' + settings.pagination_container_id + ' a').click(function(){

      last = 0;
      // Stooooooop everything
      clearTimeout($.innerfade_timeout);
      elements.each(function (i){
        $(this).stop(true, true);
        if( $(this).is(':visible') ) {
          //$(this).fadeOut(settings.speed);
          $(this).hide();
          last = i;
        }
      });

      if( $(this).hasClass(settings.previous_link_class) ) {
        if(last > 0) {
          current = last - 1;
        } else {
          current = last;
        }
      } else if( $(this).hasClass(settings.next_link_class) ) {
        if(last < elements.length-1) {
          current = last + 1;
        } else {
          current = last;
        }
      } else {
        current = parseFloat($(this).attr("class").split("_").pop());
      }
      $(elements[current]).show();
      //$.innerfade.next(elements, settings, current, last);
    });
    } // if
};


$.fn.autoclear = function() {
  return this.focus(function() {
    if( this.value == this.defaultValue ) {
      this.value = "";
    }
  }).blur(function() {
    if( !this.value.length ) {
      this.value = this.defaultValue;
    }
  });
};
// Helper for sidebar in menu page
$.show_category = function (elt)  {
  $("div.hack_js_glitch_top").slideUp();
  $("div.hack_js_glitch_bottom").slideUp();
  $("ul.foldable, ul.open").animate({opacity: 'hide', height: 'hide'});
  $("#sidebar h3 p").removeClass('saucySprite_current');

  $(elt).siblings('.hack_js_glitch_top').slideDown();
  $(elt).siblings('.hack_js_glitch_bottom').slideDown();
  $(elt).siblings('ul.foldable, ul.open').animate({opacity: 'show', height: 'show', duration: 1000});
  $(elt).children('p').addClass('saucySprite_current');
};
// --------------------------
// End of Helpers
// --------------------------

$(document).ready(function () {
  // --------------------------
  // Sidebar folding in food section
  // --------------------------
  $("#content ul.foldable").siblings('.hack_js_glitch_top').slideUp();
  $("#content ul.foldable").siblings('.hack_js_glitch_bottom').slideUp();
  $("#content ul.foldable").slideUp();

  $("#content ul.open").siblings('.hack_js_glitch_top').show();
  $("#content ul.open").siblings('.hack_js_glitch_bottom').show();

  $("#content #sidebar h3").click(function () {
    if( $(this).siblings('ul').size() > 0 ) {
      if( $(this).siblings('ul').is(':hidden') ) {
        $.show_category(this);
      }
      return false;
    }
  });
  // --------------------------
  // End of Sidebar folding in food section
  // --------------------------

  // --------------------------
  // Vacancies list animation
  // --------------------------
  // Init
  $('#content.vacancies .show_vacancies').each(function(i) {
    $(this).show();
    $(this).siblings('.vacancies_list').hide();
  });
  $('#content.vacancies .show_vacancies').click(function(i) {
    $(this).hide();
    $(this).siblings('.hide_vacancies').show();
    $(this).siblings('.vacancies_list').slideDown();
    return false;
  });
  $('#content.vacancies .hide_vacancies').click(function(i) {
    $(this).hide();
    $(this).siblings('.show_vacancies').show();
    $(this).siblings('.vacancies_list').slideUp();
    return false;
  });
  // --------------------------
  // End of Vacancies list animation
  // --------------------------


  // --------------------------
  // Home page
  // --------------------------
   $('#homepage_banner').innerfade({
     speed: 3000,
     timeout: 6000,
     type: 'sequence',
     containerheight: '348px'
   });
  // --------------------------
  // End of Home page
  // --------------------------

  // --------------------------
  // Facebox stuff
  // --------------------------
  $('a[rel*=facebox]').facebox({faceboxHtml:'<div id="facebox" style="display:none;"><div class="popup"><table><tbody><tr><td class="tl"/><td class="b"/><td class="tr"/></tr><tr><td class="b"/><td class="body"><div class="content"></div></td><td class="b"/></tr><tr><td class="bl"/><td class="b"/><td class="br"/></tr></tbody></table></div></div>' });

  $(document).bind('afterReveal.facebox', function() {
    $('#close a').click(function() {
      $.facebox.close();
      return false;
    });

    // --------------------------
    // Tell us what you think page
    // --------------------------
    $('.specific_restaurant_form').hide();
    $("#email_enquiry").change(function(){
      if($("#email_enquiry option:eq(1)").is(':selected')) {
        $('#email_ignore_restaurant_details').attr('value', 'false');
        $('.specific_restaurant_form').slideDown();
      } else {
        $('#email_ignore_restaurant_details').attr('value', 'true');
        $('.specific_restaurant_form').slideUp();
      }
    });

    for(var i=1;i<=6;i++){
      $("p.tr_child_"+i).css('display', 'none');
    }

  });
  // --------------------------
  // End of Facebox stuff
  // --------------------------

  // --------------------------
  // Work for us page
  // --------------------------
  $('.work-for-us-polaroid img').hover(function(){
    $(this).attr('src', function() {
      return this.src.replace(/\-off\.jpg/,'-on.jpg');
    });
  }, function() {
    $(this).attr('src', function() {
      return this.src.replace(/\-on\.jpg/,'-off.jpg');
    });
  });
  // --------------------------
  // End of Work for us page
  // --------------------------



  // --------------------------
  // Flash
  // --------------------------
  $(".asset_swf").each(function(i){
    var image = $(this).attr('meta_img');
    var meta_swf = $(this).attr('meta_swf');
    var flash_vars = "";
    if($(this).attr('meta_flash_vars') !== undefined) {
      flash_vars = $(this).attr('meta_flash_vars').split(',');
    }
    fv = {};
    for(i = 0; i< flash_vars.length; i++) {
      if(flash_vars[i] !== "") {
        f = flash_vars[i].split(':');
        fv[f[0]] = f[1].replace(/'/g, "");
      }
    }
    var dims = $(this).attr('meta_swf_dim').split(':');
    var width = dims[0];
    var height = dims[1];
    $(this).flash({
      id: 'asset_swf' + i,
      name: 'asset_swf' + i,
      src: meta_swf,
      width: width,
      height: height,
      wmode: 'transparent',
      menu: 'false',
      loop : 'true',
      allowScriptAccess: 'sameDomain',
      flashvars: fv
    },{
      update: false,
      expressInstall: true,
      version: 9
    });
    
    $(this).find('.alt').html("<img src='"+image+"'></img>").hide();
  });

  // --------------------------
  // End of Flash
  // --------------------------

  // --------------------------
  // Restaurant page
  // --------------------------
  // var map = new GMap2(document.getElementById('map'));
  // var lat = $('lat')
  $("#map_holder").each(function () {
    // setup map
    $('#map').each(function() {
      // build the map
      var lat_lng = $(this).attr("rel").split(' ');
      var restaurant_lat = Number(lat_lng[0]);
      var restaurant_lng = Number(lat_lng[1]);
      var location = new GLatLng(restaurant_lat, restaurant_lng);
      var map = new GMap2(this);
      map.setCenter(location, 16);
      var marker = new GMarker(location);
      map.addOverlay(marker);
      GEvent.addListener(marker, "click", function(){
        map.panTo(marker.getLatLng());
      });

      var map_div = $(this);
      $(this).siblings('.close_map').children("a").click(function() {
        map_div.slideUp();
        p = $(this).parent();
        p.siblings('.open_map').show();
        p.hide();
        return false;
      });

      $(this).siblings('.open_map').children("a").click(function() {
        map_div.slideDown();
        p = $(this).parent();
        p.siblings('.close_map').show();
        p.hide();
        return false;
      });
      // hide elements
      $(this).hide();
      $(this).siblings('.close_map').hide();
    });
  });

  $("ul.restaurant_image_links a").click(function() {
    $("img.restaurant_image").attr("src", $(this).attr("href"));
    $("ul.restaurant_image_links a").removeClass("selected");
    $(this).addClass("selected");
    return false;
  });
  $('#restaurant_splash_image #image_rotate').innerfade_with_pagination({
    speed: 3000,
    timeout: 6000,
    animationtype: 'fade',
    type: 'sequence',
    containerheight: '399px'
  });
  // --------------------------
  // End of Restaurant page
  // --------------------------

  // --------------------------
  // Input field content
  // --------------------------
  if($('#restaurant_search_input').attr('value') === '') {
    $('#restaurant_search_input').example(function() {
      return $(this).attr('title');
      }, {className: 'search_example'}
    );
  }
    
  // New design
  $('.box').hover(
    function() {
      $(this).find('span').stop(true,true).animate({'left':0},150);
    },
    function() {
      $(this).find('span').stop(true,true).animate({'left':'264px'},150,function() { $(this).css('left','-264px'); });
    }
  );
  
  Cufon.set('fontFamily', 'GeometricSlab');
  Cufon('#footer .social strong');
  Cufon('#footer p');
  Cufon('.entry-title');
  Cufon.set('hover', true);
  Cufon('#footer .menu a');
  Cufon('#aside h3');
  Cufon.set('textShadow','#000 1px 1px');
  Cufon('#header .menu a');
});

$(document.body).unload(function() {
  if (GBrowserIsCompatible()) {
    GUnload();
  }
});
