/**
 * Hacks for non-custom pages.
 */

$(function() {
  if (typeof(window.javascript_hacked_format) == 'undefined') {
    var type = typeof(window.page_type) == 'undefined' ? null : window.page_type;
    noncustom_perform_default_format_changes(type);
  }
  replace_copyright_year();
});


/**
 * Perform formatting changes for built-in pages.
 */
function noncustom_perform_default_format_changes(type) {
  if (type) {
    noncustom_move_things_around(type);
    noncustom_move_footer();
    noncustom_remove_empty_p_tags();
  }
  else if (type == null) {
    // This is a non-editable page.. we'll have to be tricky here...
    noncustom_move_things_around('a'); // type 'a' - arbitrary name for this condition
  }
  noncustom_change_empty_td_padding();
}


/**
 *
 */
function noncustom_move_things_around(type) {
  var contentWrap = $('<div id="custom-page-content-content" />');
  if (type == 'form') {
    // $('[name="autoNamedForm0"]').wrap(contentWrap);
    $('[name="autoNamedForm0"]').addClass('custom-form');
    $('#contentArea').addClass('custom-form-page');
    var tables = $.makeArray($('#contentArea').children('table'));
    $(tables[0]).wrap(contentWrap);
    for (var i = 1; i < tables.length; i++) {
      $('#custom-page-content-content').append($(tables[i]));
    }
  }
  else if (type == 'news') {
    var tables = $.makeArray($('#contentArea').children('table'));
    $(tables[0]).wrap(contentWrap);
    $('#custom-page-content-content').append($(tables[1]));
    tables = $.makeArray($('#custom-breadcrumb').children('table'));
    $(tables[0]).hide();
  }
  else if (type == 'news-article') {
    var tables = $.makeArray($('#contentArea').children('table'));
    $(tables[0]).wrap(contentWrap);
    $('#custom-page-content-content p').css('font-size', '110%');
    $('#contentArea > table').hide();
  }
  else if (type == 'a') {
    // $('tr.pageRuleB td.pageHeader').parent().parent().parent().wrap(contentWrap);
    // $('#custom-page-content-content').append($('#contentArea').children('table'));
    var elements = $.makeArray($('#contentArea').children());
    var numElements = elements.length;
    $(elements[0]).wrap(contentWrap);
    if (elements.length > 1) {
      for (var i = 1; i < numElements; i++) {
        $('#custom-page-content-content').append($(elements[i]));
      }
    }
  }
  else if (type == 'tables') {
    var tables = $.makeArray($('#contentArea').children('table'));
    $(tables[0]).wrap(contentWrap);
    if (tables.length > 1) {
      for (var i in tables) {
        $('#custom-page-content-content').append($(tables[i]));
      }
    }
  }
  $('#custom-page-content-content').wrap($('<div id="custom-page-content" class="subpage no-sidebar"></div>'));
  $('#custom-page-content').wrap('<div id="custom-page-inner" class="clearfix"></div>');
  $('#custom-page-inner').prepend($('#custom-header'));
}


/**
 * Move footer to bottom.
 */
function noncustom_move_footer() {
  var cloned = $('#custom-page-footer').clone();
  $('#custom-page-footer').remove();
  $('#contentArea').after(cloned);
  $('#contentArea').append('<div class="clearfix"></div>');
}


/**
 * Remove empty p tags.
 */
function noncustom_remove_empty_p_tags() {
  $('p').each(function() { if ($(this).html() == '') { $(this).remove(); } });
}


/**
 * Change padding for empty td.
 */
function noncustom_change_empty_td_padding() {
  $('div.zero').parent().css({
    padding: 0
  });
  $('img[alt="Edit Mode"]').parent().parent().parent().parent().parent().css({
    paddingTop: 50
  });
}

/**
 * Replace copyright year (span class="copyright-year-replace")
 */
function replace_copyright_year() {
  var d = new Date();
  $('.copyright-year-replace').html(d.getFullYear());
}

