function GetAbsoluteTop(elem) {
    var top = 0;
    while (elem.offsetParent) {
        top += elem.offsetTop;
        elem = elem.offsetParent;
    }
    return top;
}

function GetAbsoluteLeft(elem) {
    var left = 0;
    while (elem.offsetParent) {
        left += elem.offsetLeft;
        elem = elem.offsetParent;
    }
    return left;
}

function ElemsOverlap(elemA, elemB) {
    var la,lb,ta,tb,wa,wb,ha,hb;
    la = GetAbsoluteLeft(elemA);
    lb = GetAbsoluteLeft(elemB);
    ta = GetAbsoluteTop(elemA);
    tb = GetAbsoluteTop(elemB);
    wa = elemA.offsetWidth;
    wb = elemB.offsetWidth;
    ha = elemA.offsetHeight;
    hb = elemB.offsetHeight;

    if (wa == 0 || wb == 0 || ha == 0 || hb == 0) return false;

    if (lb > la) {
        if ((lb - la) >= wa) return false;
    } else if (lb < la) {
        if ((la - lb) >= wb) return false;
    }

    if (tb > ta) {
        if ((tb - ta) >= ha) return false;
    } else if (lb < la) {
        if ((ta - tb) >= hb) return false;
    }

    return true;
}

function OpenWindow(url, args, target) {
    if (null == target) target = "_blank";
    var w = window.open(url, target, args);
    ABLFrame.Control.Focus();
    w.focus();
    return w;
}

function EscapeQueryTerms(qts) {
    return qts.replace(/"/g, "\\\"");
}

function SetPluginElementVisible(elem, visible) {
    elem = D_Elem(elem);
    if (UA_SAFARI) {
        elem.style.left = visible ? "0" : "8000px";
    } else if (UA_MSIE) {

        elem.style.display = visible ? "block" : "none";


    } else {
        elem.style.visibility = visible ? "visible" : "hidden";
    }
}


/**
 * Simple crossbrowser onDOMReady functionality
 *
 * usage:
 * onDOMReady(function() {
 *     myFunction();
 * });
 */
var onDOMReadyQueue = [];

var onDOMReady = function(fn) {
    onDOMReadyQueue.push(fn);

    var init = function() {
        if (arguments.fired) return;
        arguments.fired = true;

        for (var i = 0; i < onDOMReadyQueue.length; i++) {
            var cb = onDOMReadyQueue.pop();
            cb();
        }

    };

    if (document.addEventListener) {
        document.addEventListener("DOMContentLoaded", init, false);
    }

    /*@cc_on @*/
    /*@if (@_win32)
     document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
     var script = document.getElementById("__ie_onload");
     script.onreadystatechange = function() {
     if (this.readyState == "complete")
     init();
     }
     /*@end @*/

    if (/WebKit/i.test(navigator.userAgent)) {
        var _timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState))
                clearInterval(_timer);
            init();
        }, 10);
    }

    window.onload = init;
}


/////////////////////////////////////////////////
function syndeticsPlaceTOC()
{
    // See if there is a syndetics toc placeholder in sideinfo, if there is use it.
    // Otherwise, shove it at the end of the sideinfo div.
    if (D_ElemUnsafe('syndetics_toc'))
    {
        if (D_ElemUnsafe('syndetics_sideinfo'))
        {
            D_Elem('syndetics_sideinfo').appendChild(D_ElemUnsafe('syndetics_toc'));
        }
        else if (D_ElemUnsafe('sideinfo'))
        {
            D_Elem('sideinfo').appendChild(D_ElemUnsafe('syndetics_toc'));
        }

        // Make it visible.
        D_ElemUnsafe('syndetics_toc').style.display = "block";
    }
}

function xIsbnPlaceElements()
{
    // 1. Attempt to place any toc entry.
    if (D_ElemUnsafe('sideinfo') && D_ElemUnsafe('xisbn_toc'))
    {
        D_Elem('sideinfo').appendChild(D_ElemUnsafe('xisbn_toc'));
    }

    // Make it visible.
    if (D_ElemUnsafe('xisbn_toc'))
        D_ElemUnsafe('xisbn_toc').style.display = "block";

    // 2. Attempt to place the mini refine.
    if (D_ElemUnsafe('sideinfo'))
    {
        D_Elem('sideinfo').appendChild(D_ElemUnsafe('xisbn_minirefine'));
    }

    // Make it visible.
    D_ElemUnsafe('xisbn_minirefine').style.display = "block";
}

/*
function forceReflow() {
    if (UA_MOZ) {
        try {
            var res = ABLFrame.Content.GetResultDocument();
            if (res) {
                res.body.style.paddingBottom = "1px solid white";
                res.body.style.paddingBottom = "";
            }
        }
        catch (e) {} // don't crash if page isn't fully available yet
    }
}
*/

function forceReflow () {
    try {
    	var res = ABLFrame.Content.GetResultDocument();
        if (res && UA_MOZ) {
                res.body.style.paddingBottom = "1px solid white";
                res.body.style.paddingBottom = "";
        }
    }
    catch(e) {}

	if (UA_MSIE) {
		var s = ABLFrame.document.body.style, p = s.paddingBottom;
		s.paddingBottom = '10px';
		setTimeout(function() { s.paddingBottom = p; }, 100);
	}
}


var Util = {};
Util.Cookie = { // ripped part of md cookie code
  read: function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ')
        c = c.substring(1, c.length);
      if (c.indexOf(nameEQ) == 0)
        return c.substring(nameEQ.length, c.length);
    }
    return null;
  },
  write: function(name, value, lifetime) {
      var expires = '';
      if (lifetime) {
          var date = new Date();
          date.setTime(date.getTime()+lifetime);
          expires = '; expires=' + date.toGMTString();
      }
      document.cookie = name + '=' + value + expires + '; path=/';
  },
  remove: function(name) {
      Util.Cookie.write(name, '', -1);
  }
};
// legacy method names
Util.Cookie.Read = Util.Cookie.read;

Util.Array = {
  includes: function(a, s) {
    if (!jQuery.isArray(s))
      throw "second argument needs to be an array or array-like object";
    // a = array, s = subset to check
    for (var i = 0; i < s.length; i++) {
      if (indexOf(a, s[i]) == -1) {
        return false;
      }
    }
    return true;
  },
  iterate: function(a) {
    var i = 0;
    return function() {
      return a[i++];
    }
  },
  indexOf: function(a, s) {
    return indexOf(a, s); // calls old function for now
  },
  // based on Array.remove by John Resig: http://ejohn.org/blog/javascript-array-remove/
  remove: function(a, from, to) {
    var rest = a.slice((to || from) + 1 || a.length);
    a.length = from < 0 ? a.length + from : from;
    return a.push.apply(a, rest);
  }
};
// legacy method names
Util.Array.Includes = Util.Array.includes;
Util.Array.Iterate = Util.Array.iterate;


//DB: stole this from Taco's federate thingy on okstate.. He probably stole it too ;)
Util.formatNumber = function(a, b, c, d) {
  a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
  var i, j;
  var e = a + '';
  var f = e.split('.');
  if (!f[0]) {
    f[0] = '0';
  }
  if (!f[1]) {
    f[1] = '';
  }
  if (f[1].length < b) {
    var g = f[1];
    for (i = f[1].length + 1; i <= b; i++) {
      g += '0';
    }
    f[1] = g;
  }
  if (d != '' && f[0].length > 3) {
    var h = f[0];
    f[0] = '';
    for (j = 3; j < h.length; j += 3) {
      i = h.slice(h.length - j, h.length - j + 3);
      f[0] = d + i + f[0] + '';
    }
    j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
    f[0] = j + f[0];
  }
  c = (b <= 0) ? '' : c;
  return f[0] + c + f[1];
};
Util.FormatNumber = Util.formatNumber;

Util.decodeComponent = function(s) {
  var d = s.split('+').join('%20');
  try {
    return decodeURIComponent(d);
  }
  catch (e) {
    return NonUTF8URLDecode(d);
  }
};

Util.Object = {

  merge: function(/* obj, obj, ... */) {
    var a = jQuery.makeArray(arguments),
      o = a[0],
      k, i, c;
    for (i = 1; i < a.length; i++) {
      c = a[i];
      for (k in c) {
        if (!jQuery.isFunction(c[k]))
          o[k] = c[k];
      }
    }
    return o;
  },

  toUrl: function(o) {
    var a = [], i;
    jQuery.each(o, function(k, v) {
      if (v != null) {
        if (jQuery.isArray(v)) {
          for (i = 0; i < v.length; i++) {
            a.push(encodeURIComponent(k) + '=' + encodeURIComponent(v[i]));
          }
        }
        else {
          a.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
        }
      }
    });
    return a.join('&');
  },

  fromString: function(s) {
    if (!s || !s.length)
      return {};

    var parts = s.split('&'),
        o = {},
        partCount = {},
        part, partName, partValue, i;

    for (i = 0; i < parts.length; i++) {
      part = parts[i].split('=');
      partName = Util.decodeComponent(part[0]);
      if (partCount[partName])
        partCount[partName]++;
      else
        partCount[partName] = 1;
    }
    for (i = 0; i < parts.length; i++) {
      part = parts[i].split('=');
      partName = Util.decodeComponent(part[0]);
      partValue = Util.decodeComponent(part[1]);
      if (partCount[partName] > 1) {
        if (o[partName])
          o[partName].push(partValue);
        else
          o[partName] = [partValue];
      }
      else {
        o[partName] = partValue;
      }
    }
    return o;

//    var parts = args.split('&'), part, partCount = {}, partName, partValue, i, s = {};
//    for (i = 0; i < parts.length; i++) {
//      part = parts[i].split('=');
//      partName = Util.decodeComponent(part[0]);
//      if (partCount[partName])
//        partCount[partName]++;
//      else
//        partCount[partName] = 1;
//    }
//    for (i = 0; i < parts.length; i++) {
//      part = parts[i].split('=');
//      partName = Util.decodeComponent(part[0]);
//      partValue = Util.decodeComponent(part[1]);
//      if (partCount[partName] > 1) {
//       if (s[partName])
//          s[partName].push(partValue);
//        else
//          s[partName] = [partValue];
//      }
//      else {
//        s[partName] = partValue;
//      }
//    }
//    for (i in s) {
//      this.Set(i, s[i]);
//    }
//
//    if (!s || !s.length)
//      return {};
//    var o = {}, i, part,
//        parts = s.split('&');
//    if (parts && parts.length) {
//      for (i = 0; i < parts.length; i++) {
//        part = parts[i].split('=');
//        if (part && part.length == 2)
//          o[Util.decodeComponent(part[0])] = Util.decodeComponent(part[1]);
//      }
//    }
//    return o;
  }
};

document.PrintWindow = function() {
    if (OS_WIN && UA_MSIE)
        document.execCommand("Print");
    else
        window.print();
}

