liteup_riskies = function() {
  $("div.why").click(function() {
    $("div.why").hide();
    $("div.rest").slideToggle();
  });
};

edit_button = function(e, id, dialog) { 
  e.prepend('<button class="edit_button hoverable ui-state-default ui-corner-all" id="edit_' + id + '">' + id + '</button>');
  $("#edit_" + id).click(function() { 
    $(dialog).dialog('open');
  });
} 

liteup_admin_editor = function() { 
  $(".dialog").dialog({autoOpen: false});

  $("#container.admin").each(function() { 
    // header left center right content

    $("#left", $(this)).hover(function() { 
      edit_button($(this), 'add-sidebar', '#add_left_sidebar');
      edit_button($(this), 'modify-sidebar', '#modify_left_sidebar');
    }, function() { 
      $(".edit_button", $(this)).remove();
    });

    $("#center", $(this)).hover(function() { 
      edit_button($(this), 'edit-headline', '#edit_headline');
      edit_button($(this), 'edit-overview', '#edit_overview');
      edit_button($(this), 'edit-content', '#edit_content');
    }, function() { 
      $(".edit_button", $(this)).remove();
    });

  });

  $(".tidbit_form").submit(function() { 
    var base = $(this).attr('action');
    $(this).attr('action', base + $("#tidbit_marker").attr('data-tidbit-id') + "/edit");
    return true;
  });

  $("#edit_edit-content").live('click', function() { 
    $.get("/a/retail/tidbit/" + $("#tidbit_marker").attr("data-tidbit-id") + "/content", function(d) { 
      $("#tidbit_content").val(d);
    }, "text");
    return true;
  });
};

liteup_hovers = function() { 
  $(".hoverable").live('mouseover', function() { 
      $(this).addClass("ui-state-hover"); 
  }).live('mouseout', function() { 
      $(this).removeClass("ui-state-hover"); 
  });
};

$(document).ready(function() {
  liteup_hovers();
  liteup_admin_editor();

  $("#left").find("a").click(function(){
      $("#left li").removeClass("active");
      $(this).parent().addClass("active");
      var no = $(this).attr("id");
      $.getJSON("/retail/tidbit/" + no, function(d){
          // these are the codes that made the full-on cms work.
          // var oView = $("#overview > div")
          // oView.empty();
          // oView.append(d["overview"]).css("display", "block");
          // these are the codes that make the partly-cms work.
          $("#overview").empty().append(d["overview"]);
          $("#overview > div").css("display", "block");
          $("#tidbit_marker").attr('data-tidbit-id', no);
          $("#content").empty();
          $("#content").append(d["content"]);
          document.title = "Playbook | " + d["title"]
        });
  });

  $("a#check_all").click(function() {
   $("input.input_checkbox", $(this).closest("form")).each(function() {
     $(this).attr('checked', true);
   });
    return false;
  });

  $("a#uncheck_all").click(function() {
   $("input.input_checkbox", $(this).closest("form")).each(function() {
     $(this).attr('checked', false);
   });
    return false;
  });

  // $(".matrix tr:even").css("background-color", "#ddd");
  $(".matrix tr:even").find("input").css("background-color", "#eee");

  $(".matrix input").keypress(function(e){
      var thisAction = $(".matrix form").attr("action");
      if(e.keyCode == 13) {
          var thisThing = $(this)
          var protoName = thisThing.attr("id");
          var fieldName = thisThing.attr("name");
          var fieldValue = thisThing.attr("value");
          var url = thisAction + protoName + "/update/" + fieldName + "?new_value=" + fieldValue
          $.post(url, "", function(d){
              if(d == "success") {
                  origColor = thisThing.css("background-color");
                  thisThing.animate({backgroundColor: "#CAFF70"}, 100).animate({backgroundColor: origColor}, 3000);
                  // alert("helu");
              } else {
                  alert(d);
              }
          });

      } else {
          return true;
      }
  });

  $(".delete_me").click(function(){
      var action = $(this).attr("href");
      var goAhead = confirm("Are you sure you want to delete this?")
      if(goAhead){
          $.post(action, function(d){
              window.location.reload( false );
          });
      }
      return false;
  });

  $(".delete_page").click(function(){
      var action = $(this).attr("href");
      var goAhead = confirm("Are you sure you want to delete this page?")
  });


  $.fn.fancyzoom.defaultsOptions.imgDir='/ui/script/fancyzoom/ressources/';

  $(".zoomable").fancyzoom();

  $(".screenshot").each(function() {
    var div = $(this);
    var caption_start = '<div class="img_caption" id="cap';
    var caption_mid = '"><p>';
    var caption_stop  = '</p></div>';
    var scrid = parseInt($(this).attr('id').substr(1));
    $.each(scrshots[scrid], function() {
      var cap = this;
      div.append(caption_start + cap['y'] + caption_mid + cap['caption'] + caption_stop);
      if(cap['left']) {
        $("#cap" + cap['y']).css('top', cap['y']).addClass('left');
      } else {
        $("#cap" + cap['y']).css('top', cap['y']).addClass('right');
      }
    });
  });
});

var scrshots = {};

scrshot = function() {
  $("#target").hover(function() {
    var div = $(this);
    var img = $("img", $(this));
    var off = img.offset();

    var tmpdiv = '<div class="tmpline img_caption"><p>' + $("#caption").val() + '</p></div>';

    div.append(tmpdiv);

    img.mousemove(function(e) {
      var image_yoffset = e.pageY - off.top;
      var image_xoffset = e.clientX - off.left;

      $("#smurfette p").text(image_xoffset + ' ' + image_yoffset);

      if(image_xoffset > 250) {
          $(".tmpline", div).removeClass("left").addClass("right").css('top', image_yoffset);
      } else {
          $(".tmpline", div).removeClass("right").addClass("left").css('top', image_yoffset);
      }
    }).click(function(e) {
      var image_yoffset = e.pageY - off.top;
      var image_xoffset = e.clientX - off.left;
      var left = 1;

      if(image_xoffset > 250) {
        left = 0;
      }

      $.post("/a/screenshots/" + $("a#screenshot_id").attr('href') + "/captions/new",
	     { caption: $("#caption").val(), left: left, y: image_yoffset },
             function(d) {
      }, 'json');

      return false;
    })
  }, function() {
    $("div.tmpline", $(this)).remove();
  });

};

