$(document).ready(function() {
  $(document).bind('reveal.facebox', function(){
    $("#sortable").sortable();
    $("#sortable_submit").click(function(){
      new_order = $("#sortable").sortable("serialize");
      form_action = $(this).parent().attr("action");
      form_action_with_params = form_action + "?" + new_order;
      /* haha, lame, I didn't want the form to submit using ajax */
      var form = document.createElement("form");
      form.setAttribute("method", "post");
      form.setAttribute("action", form_action_with_params);
      document.body.appendChild(form);
      form.submit();
      return false;
    });

    $('form').validate()
  });

  $('ul.gallery').galleria({
    history   : false, // activates the history object for bookmarking, back-button etc.
    clickNext : true, // helper for making the image clickable
    insert    : '#main_image', // the containing selector for our main image
    onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

      $(".gallery").hide(300);
    
      registerConfirm();

      /*
      $('img.replaced').maxImage({
        maxFollows: 'height',
        position: 'relative',
        leftSpace: 'auto',
        rightSpace: 'auto'
      }); */

      $('#main_image').vAlign();
      
      // fade in the image & caption
      if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
        image.css('display','none').fadeIn(400);
      }
      caption.css('display','none').fadeIn(400);
      
      // fetch the thumbnail container
      var _li = thumb.parents('li');
      
      // fade out inactive thumbnail
      _li.siblings().children('img.selected').fadeTo(500,0.3);
      
      // fade in active thumbnail
      thumb.fadeTo('fast',1).addClass('selected');
      
      // add a title for the clickable image
      image.attr('title','Next image >>');
      $(".caption a[rel*=facebox]").facebox();
    },
    onThumb : function(thumb) { // thumbnail effects goes here
      
      // fetch the thumbnail container
      var _li = thumb.parents('li');
      
      // if thumbnail is active, fade all the way.
      var _fadeTo = _li.is('.active') ? '1' : '0.3';
      
      // fade in the thumbnail when finnished loading
      thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
      
      // hover effects
      thumb.hover(
        function() { thumb.fadeTo('fast',1); },
        function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
      )
    }
  });

  $(".still img").css("display", "none");
  $(".still img").map(function(){
    /* force the load event to fire */
    $(this).attr("src", $(this).attr("src"));
  })
  $(".still img").load(function(){
    $(this).fadeIn(1500);
  });

  /* init facebox links in caption */
  $("a[rel*=facebox]").facebox();

  $("#current_gallery").toggle(function(){
    $(this).css('background-image', 'url(/images/small_left_arrow.png)')
    $("#gallery_list").slideDown(300);
  }, 
  function(){
    $(this).css('background-image', 'url(/images/small_right_arrow.png)')
    $("#gallery_list").slideUp(300);
  });

  $("#thumbnails_link").click(function(){
    if ($(".gallery").css("display") == "none") {
      $(".gallery").show(300);
    }
    else {
      $(".gallery").hide(300);
    }
    return false;
  });

  $(".gallery_prev").click(function(){
    $.galleria.prev(); return false;
  });

  $(".gallery_next").click(function(){
    $.galleria.next(); return false;
  });

  registerConfirm();
});

function registerConfirm(){
  $(".confirm").click(function(){
    var answer = confirm("are you sure?");
    if (answer) {
      return true;
    }
    else {
      return false;
    }
  });
}
