/**
 * Interface Elements for jQuery
 * utility function
 *
 * http://interface.eyecon.ro
 *
 * Copyright (c) 2006 Stefan Petre
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 *
 */

jQuery.iUtil = {
  getPosition : function(e, forceIt)
  {
    var x = 0;
    var y = 0;
    var es = e.style;
    var restoreStyles = false;
    if (forceIt && jQuery.curCSS(e,'display') == 'none') {
      var oldVisibility = es.visibility;
      var oldPosition = es.position;
      restoreStyles = true;
      es.visibility = 'hidden';
      es.display = 'block';
      es.position = 'absolute';
    }
    var el = e;
    if (el.getBoundingClientRect) { // IE
      var box = el.getBoundingClientRect();
      x = box.left + Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) - 2;
      y = box.top + Math.max(document.documentElement.scrollTop, document.body.scrollTop) - 2;
    } else {
      x = el.offsetLeft;
      y = el.offsetTop;
      el = el.offsetParent;
      if (e != el) {
        while (el) {
          x += el.offsetLeft;
          y += el.offsetTop;
          el = el.offsetParent;
        }
      }
      if (jQuery.browser.safari && jQuery.curCSS(e, 'position') == 'absolute' ) {
        x -= document.body.offsetLeft;
        y -= document.body.offsetTop;
      }
      el = e.parentNode;
      while (el && el.tagName.toUpperCase() != 'BODY' && el.tagName.toUpperCase() != 'HTML') 
      {
        if (jQuery.curCSS(el, 'display') != 'inline') {
          x -= el.scrollLeft;
          y -= el.scrollTop;
        }
        el = el.parentNode;
      }
    }
    if (restoreStyles == true) {
      es.display = 'none';
      es.position = oldPosition;
      es.visibility = oldVisibility;
    }
    return {x:x, y:y};
  },
  getPositionLite : function(el)
  {
    var x = 0, y = 0;
    while(el) {
      x += el.offsetLeft || 0;
      y += el.offsetTop || 0;
      el = el.offsetParent;
    }
    return {x:x, y:y};
  },
  getSize : function(e)
  {
    var w = 0;
    var h = 0;
    var wb = 0;
    var hb = 0;
    if (jQuery.curCSS(e, 'display') != 'none') {
      wb = e.offsetWidth;
      hb = e.offsetHeight;
      w = parseInt(jQuery.curCSS(e,'width'), 10);
      h = parseInt(jQuery.curCSS(e,'height'), 10);
    } else {
      var es = e.style;
      var oldStyle = $(e).attr('style');
      es.visibility = 'hidden';
      es.display = 'block';
      es.position = 'absolute';
      wb = e.offsetWidth;
      hb = e.offsetHeight;
      w = parseInt(jQuery.curCSS(e,'width'), 10);
      h = parseInt(jQuery.curCSS(e,'height'), 10);
      $(e).attr('style', oldStyle||'');
    }
    return {w:w, h:h, wb:wb, hb:hb};
  },
  getSizeLite : function(el)
  {
    return {
      wb:el.offsetWidth||0,
      hb:el.offsetHeight||0
    };
  },
  getClient : function(e)
  {
    var h, w;
    if (e) {
      w = e.clientWidth;
      h = e.clientHeight;
    } else {
      var de = document.documentElement;
      w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
      h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
    }
    return {w:w,h:h};
  },
  getScroll : function (e)
  {
    var t=0, l=0, w=0, h=0, iw=0, ih=0;
    if (e && e.nodeName.toLowerCase() != 'body') {
      t = e.scrollTop;
      l = e.scrollLeft;
      w = e.scrollWidth;
      h = e.scrollHeight;
    } else  {
      if (document.documentElement) {
        t = document.documentElement.scrollTop;
        l = document.documentElement.scrollLeft;
        w = document.documentElement.scrollWidth;
        h = document.documentElement.scrollHeight;
      } else if (document.body) {
        t = document.body.scrollTop;
        l = document.body.scrollLeft;
        w = document.body.scrollWidth;
        h = document.body.scrollHeight;
      }
      if (typeof pageYOffset != 'undefined') {
        t = pageYOffset;
        l = pageXOffset;
      }
      iw = self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0;
      ih = self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0;
    }
    return { t: t, l: l, w: w, h: h, iw: iw, ih: ih };
  },
  getMargins : function(e, toInteger)
  {
    var t = jQuery.curCSS(e,'marginTop') || '';
    var r = jQuery.curCSS(e,'marginRight') || '';
    var b = jQuery.curCSS(e,'marginBottom') || '';
    var l = jQuery.curCSS(e,'marginLeft') || '';
    if (toInteger)
      return {
        t: parseInt(t, 10)||0,
        r: parseInt(r, 10)||0,
        b: parseInt(b, 10)||0,
        l: parseInt(l, 10)
      };
    else
      return {t: t, r: r, b: b, l: l};
  },
  getPadding : function(e, toInteger)
  {
    var t = jQuery.curCSS(e,'paddingTop') || '';
    var r = jQuery.curCSS(e,'paddingRight') || '';
    var b = jQuery.curCSS(e,'paddingBottom') || '';
    var l = jQuery.curCSS(e,'paddingLeft') || '';
    if (toInteger)
      return {
        t: parseInt(t, 10)||0,
        r: parseInt(r, 10)||0,
        b: parseInt(b, 10)||0,
        l: parseInt(l, 10)
      };
    else
      return {t: t, r: r, b: b, l: l};
  },
  getBorder : function(e, toInteger)
  {
    var t = jQuery.curCSS(e,'borderTopWidth') || '';
    var r = jQuery.curCSS(e,'borderRightWidth') || '';
    var b = jQuery.curCSS(e,'borderBottomWidth') || '';
    var l = jQuery.curCSS(e,'borderLeftWidth') || '';
    if (toInteger)
      return {
        t: parseInt(t, 10)||0,
        r: parseInt(r, 10)||0,
        b: parseInt(b, 10)||0,
        l: parseInt(l, 10)||0
      };
    else
      return {t: t, r: r, b: b, l: l};
  },
  getPointer : function(event)
  {
    var x = event.pageX || (event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)) || 0;
    var y = event.pageY || (event.clientY + (document.documentElement.scrollTop || document.body.scrollTop)) || 0;
    return {x:x, y:y};
  },
  traverseDOM : function(nodeEl, func)
  {
    func(nodeEl);
    nodeEl = nodeEl.firstChild;
    while(nodeEl){
      jQuery.iUtil.traverseDOM(nodeEl, func);
      nodeEl = nodeEl.nextSibling;
    }
  },
  purgeEvents : function(nodeEl)
  {
    jQuery.iUtil.traverseDOM(
      nodeEl,
      function(el)
      {
        for(var attr in el){
          if(typeof el[attr] === 'function') {
            el[attr] = null;
          }
        }
      }
    );
  },
  centerEl : function(el, axis)
  {
    var clientScroll = $.iUtil.getScroll();
    var windowSize = $.iUtil.getSize(el);
    if (!axis || axis == 'vertically')
      $(el).css(
        {
          top: clientScroll.t + ((Math.max(clientScroll.h,clientScroll.ih) - clientScroll.t - windowSize.hb)/2) + 'px'
        }
      );
    if (!axis || axis == 'horizontally')
      $(el).css(
        {
          left: clientScroll.l + ((Math.max(clientScroll.w,clientScroll.iw) - clientScroll.l - windowSize.wb)/2) + 'px'
        }
      );
  },
  fixPNG : function (el, emptyGIF) {
    var images = $('img[@src*="png"]', el||document), png;
    images.each( function() {
      png = this.src;       
      this.src = emptyGIF;
      this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + png + "')";
    });
  }
};

// Helper function to support older browsers!
[].indexOf || (Array.prototype.indexOf = function(v, n){
  n = (n == null) ? 0 : n;
  var m = this.length;
  for (var i=n; i<m; i++)
    if (this[i] == v)
      return i;
  return -1;
});
