$(function(){
  
  var animation_speed = 500;
  
  // ===============
  // = Bottom List =
  // ===============
  var active_button_lists = $(".button_list").not(".inactive");
  
  active_button_lists.children('li').not('.active').children("ul").hide();
  
  active_button_lists.find('h4').click(function(){
    if($(this).parent('li').children('ul').css('display') == 'block'){
      $(this).parent('li').children('ul').slideUp('slow');
    }
    else{
      $(this).parent('li').siblings('li').children('ul').slideUp('slow');
      $(this).parent('li').children('ul').slideDown('slow');
    }
  });
  
  
  // ============
  // = Sortable =
  // ============
  $(".sortable").sortable({ 
      handle: '.handle',
      update: function () { 
        $(this).siblings(".order").val($(this).sortable('serialize').replace(/[^\d\&]*/g, "").replace(/[\&]/g, ",")); 
      } 
    }); 
    
  $(".sortable").disableSelection();
  
  
  // =================
  // = Flash Message =
  // =================
  $('.flash .close').show();
  
  $('.flash').animate({opacity: 1}, 2000).fadeOut(6000);
  $('.flash').toggle(function(){
    $(this).stop();
    $(this).css('opacity', 1);
  },function(){
    $(this).fadeOut(2000);
  });
  
  
  // ======================
  // = Primary Image Swap =
  // ======================
  $('.primary_image img:last-of-type').addClass('active').addClass('primary');
  $('.primary_image img').not('.active').hide();
  
  $('.thumb_image').click(function(){
    return false;
  });
  
  $('.thumb_image').hoverIntent(function(){
    image_id = $(this).attr('id').match(/\d+/);
    if(!$('#primary_' + image_id).hasClass('primary')){
      $('.primary_image img.active').fadeOut(animation_speed).removeClass('active');
      $('#primary_' + image_id).fadeIn(animation_speed).addClass('active');
    }
  }, function(){
    if(!$('.primary_image img.active').hasClass('primary')){
      old = $('.primary_image img.active').removeClass('acitve');
      if($('.primary_image img.active')){
        $('.primary_image img.active').fadeOut(animation_speed).removeClass('active');
        $('.primary_image img.primary').addClass('active');
        $('.primary_image img.active').fadeIn(animation_speed);
      }
    }
  });

});