var drafts = true;
var editing = false;

// Fire AJAX save events for revisions
function saveDraft(type, id, content) {
  $.ajax({
    type: 'POST',
    url: 'drafts.php',
    data: {action: 'create', type: type, id: id, content: content},
    dataTypeString: 'text',
    success: function(data) {
      if (data != 'true') {
        $('.system-message').show();
        $('.message').find('li:first').html('Erroring saving draft.');
        fadeMessage();
      }
    }
  });

  return true;
}

function cellLinks() {
  $('table tbody td a.icon').each(function() {
    $(this).parent('td').css('padding', '0');
    $(this).css({
      'height': ($(this).parent('td').height()-14),
      'padding-top': '10px'
    });
  })
}

function fadeMessage() {
  $('.fade:visible').delay(4000).fadeOut('slow', function() {});
}

function toolTips() {
  if ($('.tooltip').length > 0) {
    $('.tooltip').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fixPNG: true,
        opacity: 0.95,
        top: -180
    });
  }
}

function sidebar() {
  var sidebar_visible = true;
  $('.sidebar-toggle').click(function() {
    if ($('.sidebar-toggle').length == 0) {

    }
    else {
      if (sidebar_visible == false) {
        $('.page .article').animate({'width': '-=230', 'padding-left': '+=20'}, 500);
        sidebar_visible = true;
        $('.sidebar').animate({'width': '+=220', 'height': 'toggle', 'opacity': '1'}, 500);
      }
      else {
        $('.sidebar').animate({'width': '-=220', 'height': 'toggle', 'opacity': '0'}, 500);
        $('.page .article').animate({'width': '+=230', 'padding-left': '-=20'}, 500).delay(800);
        sidebar_visible = false;
      }
    }
    $.cookie('showsidebar', sidebar_visible);
  });
  if ($.cookie('showsidebar') == 'false') {
    if ($('.sidebar-toggle').length == 0) {

    }
    else {
      //$('.sidebar').css({'width': '0px', 'opacity': '0'});
      $('.sidebar').animate({'width': '-=220', 'height': 'toggle', 'opacity': '0'}, 0);
      $('.page .article').css({'width': '920px', 'padding-left': '0px'});
      sidebar_visible = false;
    }
  }
}


function ready() {
  // Validate forms
  if ($('form.validate').length > 0) {
    $('form.validate').each(function() {
      $(this).validate({
        highlight: function(element, errorClass, validClass) {
          /*var message = $(element).parents('form.validate').prev('.system-message');
          message.find('dd.error ul li').html('There was an error with one or more of your fields.');
          fadeMessage();
          message.show();*/
          $(element).addClass(errorClass).removeClass(validClass);
          $(element).parent().addClass('error');
        },
        unhighlight: function(element, errorClass, validClass) {
          $(element).parent().removeClass('error');
        },
        errorPlacement: function(error, element) {
          error.appendTo(element.parent());
          /*if (element.hasClass('select')) {
            console.log($(element).next());
            if ($(element).next().hasClass('customStyleSelectBox')) {
              var label = $(element.form).find("label[for="+element.id+"]");
              console.log(label);
            }
          }*/
        }
      });
    });
  }

  // Hide any messages
  fadeMessage();

  // Initialize tooltips
  toolTips();

  sidebar();

  // Make table icon links cover the whole cell
  cellLinks();
}

$(document).ready(function() {
  ready();
});

