$(document).ready(function() {
  //Flash Message
  setTimeout(hideFlashes, 10000);

  $('#tour').cycle({
    ftimeout: 10000,
    speed: 800,
    pager: '.tourNav',
    slideExpr: '.tour',
    pagerAnchorBuilder: function(idx, slide) { return '<a href="#"></a>'; },
    // activePagerClass: 'selected',
    cleartype: !$.support.opacity,
    cleartypeNoBg: true
  });
  
  var $active = $('.cycle');
  $active.cycle({
    timeout: 5000,
    speed: 800,
    height: '200px',
    cleartype: !$.support.opacity,
    cleartypeNoBg: true
   });

  // Delete
  $('a[data-confirm], input[data-confirm]').live('click', function() {
    if (!confirm($(this).attr('data-confirm'))) {
      return false;
    }
  });
  
  $('.delete').click(function() {
    if(confirm('Are you sure?')) {
      var f = document.createElement('form');
      $(this).after($(f).attr({
        method: 'post',
        action: $(this).attr('href')
      }).append('<input type="hidden" name="_method" value="DELETE" />'));
      $(f).submit();
    }
    return false;
  });

  $('.asubmit').click(function() {
    if(confirm('Are you sure?')) {
      $('#photos_delete').submit();
    }
    return false;
  });
  
  // Check/Uncheck all
  $('#check_all').live('click', function () {
    $(this).parents('form').find(':checkbox').attr('checked', this.checked);
  });

  // Sortable Listing Photos
  $(".photo_list").sortable({
    axis: 'x, y',
    dropOnEmpty: false,
    //handle: '.handle',
    cursor: 'crosshair',
    items: 'li',
    opacity: 0.9,
    scroll: true,
    update: function(){
      $.ajax({
        type: 'post',
        data: $(".photo_list").sortable('serialize'),
        dataType: 'script',
        complete: function(request){
        },
      url: '/admin/gallery/sort'})
    }
  });
});

var hideFlashes = function() {
  $('#messages_outer').slideUp(100);
}
