/**
 * UofC wide js code.
 */

// Immediately hide all elements of js-hidden class
var _uofc_js_hidden_style = $('<style type="text/css">.js-hidden {display:none;}</style>');
$('head').append(_uofc_js_hidden_style);

// Immediately hide all elements of js-wait class
var _uofc_js_wait_style = $('<style type="text/css">.js-wait {display:none;}</style>');
$('head').append(_uofc_js_wait_style);

// Immediately show all elements of js-until class
var _uofc_js_until_style = $('<style type="text/css">.js-until-block {display:block;} .js-until-inline {display:inline;}</style>');
$('head').append(_uofc_js_until_style);

$(document).ready(function() {
  // Show all elements of js-wait class upon document being ready
  _uofc_js_wait_style.remove();

  // Hide all elements of js-until class upon document being ready
  _uofc_js_until_style.remove();
});

/**
 * Open sections of the page containing a current anchor
 */
if (window.location.href.match('#')) {

  var split_location = window.location.href.split('#');
  var this_name = split_location.pop();
  var this_page = split_location.join('#');
  $().oninit('a[name='+this_name+']', function() {
    var anchor = $(this);
    setTimeout(function() {
      open_around_anchor(anchor);
      $.scrollTo(anchor, 300);
    }, 300);
  }, []);
}

/*
 * Smooth scrolling for anchors
 */
$().clickHandler('a[href*=#]', function(event) {

  // prevent detail row expanders from scrolling
  if ($(this).is('.uofc-row-expander')) {
    return false;
  }

  var href = this.href;
  var split_href = href.split('#');

  // Get the string after the last # in the URL
  var target_name = split_href.pop();

  // Get the string before the last # in the URL
  var target_page = split_href.join('#');

  // Get this page, with any target stripped off the end
  var this_name = "";
  var this_page = window.location.href;
  if (this_page.match("#")) {
    var split_location = this_page.split('#');
    this_name = split_location.pop();
    this_page = split_location.join('#');
  }

  // If the target page is not blank, or the current page, abort
  if (target_page.length != "" && target_page != this_page) {
    return true;
  }

  // Abort if no target
  var target = $('a[name='+target_name+']');
  if (target.length == 0) {
    return true;
  }

  // Open whatever it's in (i.e. collapsed list item)
  open_around_anchor($(target));

  // Scroll to it
  $.scrollTo(target, 300, function() {
    window.location = href;
  });
  return false;
});

/**
 * Opens all open-able things surrounding an anchor
 */
function open_around_anchor(anchor) {
  // Make sure the whole DOM up from the anchor is open
  var cursor = anchor;
  var parent = cursor.parent();
  while (parent.size() > 0) {
    parent.trigger('openchild', cursor);
    cursor = parent;
    parent = cursor.parent();
  }
}

/**
 * Concatenates the values of a variable into an easily readable string
 * by Matt Hackett [scriptnode.com]
 * @param {Object} x The variable to debug
 * @param {Number} max The maximum number of recursions allowed (keep low, around 5 for HTML elements to prevent errors) [default: 10]
 * @param {String} sep The separator to use between [default: a single space ' ']
 * @param {Number} l The current level deep (amount of recursion). Do not use this parameter: it's for the function's own use
 */
function print_r(x, max, sep, l) {

  l = l || 0;
  max = max || 5;
  sep = sep || ' ';

  if (l > max) {
      return "[WARNING: Too much recursion]\n";
  }

  var
    i,
    r = '',
    t = typeof x,
    tab = '';

  if (x === null) {
    r += "(null)\n";
  }
  else if (t == 'object') {

    l++;

    for (i = 0; i < l; i++) {
      tab += sep;
    }

    if (x && x.length) {
      t = 'array';
    }

    r += '(' + t + ") :\n";

    for (i in x) {
      try {
        r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
      } catch(e) {
        return "[ERROR: " + e + "]\n";
      }
    }
  }
  else {
    if (t == 'string') {
      if (x == '') {
        x = '(empty)';
      }
    }
    r += '(' + t + ') ' + x + "\n";
  }

  return r;
};

/**
 *
 */
function var_dump(input) {
  alert(print_r(input));
}

