$(function(){
  
  /* returns the parent menu div of any element within one */
  $.fn.parentMenu = function() {
    return $(this).parents(".wrap:first");
  };

  /* collapse this menu */
  $.fn.collapseMenu = function() {
    return this.each(function() {
      var menu = $(this);
      menu.children(".content").slideUp('slow',function(){
        menu.removeClass('active');
        menu.resetMenu();
      });
    });
  };

  /* expand this menu */
  $.fn.expandMenu = function() {
    return this.each(function(){
      var menu = $(this);
      menu.addClass('active');
      menu.find(".wrap_image").hide();
      $("#content-wrapper").width('960px');
      $("#slideshow").fadeOut(0,function(){
        menu.children(".content").slideDown('slow');
      });
    });
  };

  /* reset menu state */
  $.fn.resetMenu = function() {
    return this.each(function(){
      var menu = $(this);
      menu.find(".sections").css('left','0px');
      menu.find(".sections_wrap").height(menu.data('initHeight')+'px');
      menu.find(".galleryfied").find(".slider a:first").trigger('click');
    });
  };

  /* bound to menu links this will perform menu logic and expand/collapse required menus */
  toggleMenu = function(event) {
    if(!$(this).parentMenu().hasClass('active')) {
      $(".active").collapseMenu();
      $(this).parentMenu().expandMenu();
    } else {
      $(this).parentMenu().collapseMenu();
      $("#content-wrapper").delay('slow').animate({width:'350px'},0);
      $("#slideshow").delay('slow').fadeIn(0);
    };
    return false;
  };

  /* slide content to correct position to show relevant content div */
  slideContent = function(event) {
    var w = $(this).parentMenu().find(".sections_wrap");
    var c = w.find(".sections");
    var l = (event.data.index * 710) * -1;
    var ch = c.find(".section").eq(event.data.index).height();
    c.animate({left: l+'px'},'slow');
    w.animate({height: ch+'px'},'slow');
    return false;
  };

  $(".wrap").each(function(){    
    if($(this).find(".sections").length > 0) {
      var sections_width = $(this).find(".section").length * 710;
      var first_section_height = $(this).find(".section:first").height();
      $(this).find(".sections").css('width',sections_width+'px').wrap('<div class="sections_wrap"></div>');
      $(this).data('initHeight',first_section_height);
      $(this).find(".sections_wrap").height(first_section_height);
    }
    $(this).find(".menu a").each(function(i){
      $(this).bind('click',{index:i},slideContent);
    });
  });

  $("#logo a").click(function(){
    $(".wrap").collapseMenu();
    $(".active").removeClass('active');
    $("#slideshow").delay('slow').fadeIn(0);
    $("#content-wrapper").delay('slow').animate({width:'350px'},0);
    return false;
  });

  $("#header li.address a").fancybox({
    overlayColor: '#000',
    opacity: 0.8
  });

  $("#gallery_wrapper").epicGalleryify();
  $("#team_wrapper").epicGalleryify({scrollAmount: 960});
  $("#gallery_images li a").fancybox({
    overlayColor: '#000',
    opacity: 0.8
  });

  $('#latest_tweet').tweetable({username: 'epicespresso', time: true, limit: 1});

  $(document).pngFix();

  // Wrap epic espresso team images with div for borders
  $("#team_members li img").wrap('<div class="team_photo">');

  // Create slideshow
  $("#slideshow ul").cycle();

  // Show menu associated image in slieshow area on hover
  $(".wrap_image").hide();
  $("h2 a").hover(function(){
    if($(".active").size() == 0) $(this).parentMenu().children(".wrap_image").show();
  },function() {
    if($(".active").size() == 0) $(this).parentMenu().children(".wrap_image").hide();
  });

  $("h2 a").bind('click',toggleMenu);
  $(".content").hide();
  $("body").addClass('javascript-enabled');
});
