// JQuery function for removing empty "children" ul as it shows in silly IE
function removeEmptyChildren() {
  $("#children:empty").remove();
}

// JQuery function for removing duplicate navigation items
function removeDuplicateItems() {
  var cursib = $("a.current").text();
  $("a.sibling").each(function(){
    if ($(this).text() === cursib) {
      $(this).parent().remove();
    }
  });
  var curpar = $("a.c-p-inactive").text();
  $("a.top-level").each(function(){
    if ($(this).text() === curpar) {
      $(this).parent().remove();
    }
  });
}
// JQuery function finds all links to PDF, DOC, XLS and PPT files, and adds a JS
// event which fires when the link is followed (works with mouse clicks *and* keyboard presses)
// and tracks the download link activation in Google Analytics, showing it in the
// Content > Events section.
function addGoogleAnalyticsToDownloadLinks() {
  var downloadLinks = $('a[href$=".pdf"],a[href$=".doc"],a[href$=".xls"],a[href$=".ppt"]');
  downloadLinks.each(function(){
    if (!_gaq) { var _gaq = []; }
    $(this).bind('click', function(){
      _gaq.push(['_trackEvent', 'Documents', 'Download', this.pathname]);
    });
  });
}

// JQuery function for restyling byte-based file sizes to be readable
function createHumanReadableFileSizes() {
  $(".assetsize").each(function(){
    var spancontents = $(this).text();
    var filesize = spancontents.replace(/ bytes/i, '') / 1024; // KB filesize
    filesize = Math.round(filesize * 10) / 10; // round
    if (filesize > 1000) {
      filesize = filesize / 1024; // MB filesize
      filesize = Math.round(filesize * 10) / 10; // round
      $(this).empty().text(filesize + " MB"); // or could be in MiB to be correct
    } else {
    $(this).empty().text(filesize + " KB"); // or could be in KiB to be correct
    }
  });
}

// JQuery function which uses two other JQuery plugins to find links to external sites,
// give them a special CSS class, and then create a tooltip with a warning next
// to each one. 
function addExternalLinkTooltips() {
  $.getScript("http://www.commerce.wa.gov.au/global/scripts/jq-external-links.js", function() {
    $('#page-content').externallink({
      localhosts: ['commerce.wa.gov.au','fuelwatch.wa.gov.au','docep.wa.gov.au','worksafe.wa.gov.au','reba.wa.gov.au','sasb.wa.gov.au','plumbers.wa.gov.au','publicsectorsafety.wa.gov.au','buildingcommission.wa.gov.au','builders.wa.gov.au','painters.wa.gov.au','safetyline.wa.gov.au','anodeconsulting.com.au','energysafety.wa.gov.au','mediastatements.wa.gov.au'],
      classname: 'extlink'
    });
    $.getScript("http://www.commerce.wa.gov.au/global/scripts/jq-qtip-patched.js", function() {
      $("#page-content a.extlink").qtip({
        content: { url: 'http://www.commerce.wa.gov.au/global/scripts/message.html', prerender: false },
        show: { delay: 250 },
        position: {
          corner: { target: 'rightMiddle', tooltip: 'leftMiddle' }
        },
        style: {
          name: 'light',
          tip: { corner: 'leftMiddle' },
          width: { min: 380, max: 420 },
          border: { width: 2, color: '#5D8EA3' }
        },
        hide: { fixed: true, delay: 700 }
      });
    });
  });
}

// Finally, call all of the above functions on document.ready()
$(document).ready(function(){
  removeEmptyChildren();
  removeDuplicateItems();
  createHumanReadableFileSizes();
  addExternalLinkTooltips();
  addGoogleAnalyticsToDownloadLinks();
});
