/*jslint browser: true */
/*global jQuery, $, FB, AnyTime */

// fb
window.fbAsyncInit = function () {
  FB.init({
    appId  : '49bbbeafe6934e4122fde3534113150d',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true  // parse XFBML
  });
};

// jquery extensions
(function($) {
  // http://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery
  // checks if a element exists
  $.fn.exists = function(){return jQuery(this).length>0;};
})(jQuery);

// map toggle
$(document).ready(function(){
  if ($(".map-toggle").exists()) {
    $(".map-toggle").toggle(function(){
      $(".google-map").show('slow');
    },function(){
      $(".google-map").hide('slow');
    });
  }

  if ($("#event-form").exists()) {
    AnyTime.picker( "id_start_at", { firstDOW: 1 } );
    AnyTime.picker( "id_end_at", { firstDOW: 1 } );


    // TODO refactor to a jquery plugin
    $("#event-form input[type='radio']:checked").siblings("img").css("background", "#23BFB8");
    $.data(document.body , "checked", $("#event-form input[type='radio']:checked").val());

    $("#event-form label img").hover(function () {
        $(this).css("background-color", "#cccccc");
      }, function () {
        if ($.data(document.body , "checked") === $(this).siblings("input").val() ) {
          $(this).css("background-color", "#23BFB8");
        } else {
          $(this).css("background-color", "#ffffff");
        }
      }
    );

    $("#event-form input[type='radio']").change(function() {
        $("#event-form input[type='radio']").siblings("img").css("background-color", "#ffffff");
        $(this).siblings("img").css("background-color", "#23BFB8");
        $.data(document.body , "checked", $("#event-form input[type='radio']:checked").val());
    });
  }

  $(".fancy-box a").fancybox({
    'titleShow': false,
    'overlayOpacity': 0.6,
    'overlayColor': "#000000"
  });

  // sorting photosets
  $(".sortable").sortable();
  $(".sortable").disableSelection();

  $("#update-album-order").click(function() {
    var key_name_list = $(".sortable li").map( function() {
      return $(this).attr("data-id");
    }).get().join(',');
    $.post( "/admin/photosets/update-order/", { 'photosets': key_name_list });
  });
});

