
/**
 * 
 */ 
XarLib = {
    
    isStrict: document.compatMode == "CSS1Compat",
    isOpera: navigator.userAgent.toLowerCase().indexOf("opera") > -1,
    isChrome: navigator.userAgent.toLowerCase().indexOf("chrome") > -1,
    isSafari: !this.isChrome && (/webkit|khtml/).test(navigator.userAgent.toLowerCase()),
    isSafari3: this.isSafari && navigator.userAgent.toLowerCase().indexOf("webkit/5") != -1,
    isIE: !this.isOpera && navigator.userAgent.toLowerCase().indexOf("msie") > -1,
    isIE7: !this.isOpera && navigator.userAgent.toLowerCase().indexOf("msie 7") > -1,
    isIE8: !this.isOpera && navigator.userAgent.toLowerCase().indexOf("msie 8") > -1,
    isGecko: !this.isSafari && !this.isChrome && navigator.userAgent.toLowerCase().indexOf("gecko") > -1,
    isGecko3: this.isGecko && navigator.userAgent.toLowerCase().indexOf("rv:1.9") > -1,
    isWindows: (navigator.userAgent.toLowerCase().indexOf("windows") != -1 || navigator.userAgent.toLowerCase().indexOf("win32") != -1),
    isMac: (navigator.userAgent.toLowerCase().indexOf("macintosh") != -1 || navigator.userAgent.toLowerCase().indexOf("mac os x") != -1),
    isAir: (navigator.userAgent.toLowerCase().indexOf("adobeair") != -1),
    isLinux: (navigator.userAgent.toLowerCase().indexOf("linux") != -1),
    isSecure: window.location.href.toLowerCase().indexOf("https") === 0,
    
    
    
    /**
     * @v - variable to check
     */ 
    isArray : function(v){
        return v && typeof v.length=="number" && typeof v.splice=="function";
    },
    
    /**
     * @el - DOM object
     */ 
    getEl : function(el){
      return new XarLib.Element(el);
    }

}//END: XarLib = {




/**
 * @el - DOM object or string id of DOM element
 */ 
XarLib.Element = function(el) {
  this.el = typeof el == "string" ? document.getElementById(el) : el;

};

XarLib.Element.prototype = {

    getY : function() {
        return this.getXY(this.el)[1];
    },

    getX : function() {
        return this.getXY(this.el)[0];
    },


    getXY : function() {
        var p, pe, scroll, bd = (document.body || document.documentElement);

        if(this.el == bd){
            return [0, 0];
        }

        if (this.el.getBoundingClientRect) {
            /* -- modified by kbalcerek -- */
            var wasDisplayNone = false;
            if(this.getStyle("display") == "none"){
              wasDisplayNone = true;
              this.el.style['display'] = 'block';
            }
            /* -- END: modified by kbalcerek -- */
            
            var rectObj = this.el.getBoundingClientRect();
            
            /* -- modified by kbalcerek -- */
            if(wasDisplayNone){
              this.el.style['display'] = 'none';
            }
            /* -- END: modified by kbalcerek -- */
            
            return [rectObj.left + this.getScroll().left, rectObj.top + this.getScroll().top];
        }
        var x = 0, y = 0;

        p = this.el;

        var hasAbsolute = this.getStyle("position") == "absolute";


        /* -- modified by kbalcerek -- */
        var wasDisplayNone = false;
        if(this.getStyle("display") == "none"){
          wasDisplayNone = true;
          this.el.style['display'] = 'block';
        }
        /* -- END: modified by kbalcerek -- */
        
        
        while (p) {

            x += p.offsetLeft;
            y += p.offsetTop;

            if (!hasAbsolute && XarLib.getEl(p).getStyle("position") == "absolute") {
                hasAbsolute = true;
            }

            if (XarLib.isGecko) {
                pe = XarLib.getEl(p);

                var bt = parseInt(pe.getStyle("borderTopWidth"), 10) || 0;
                var bl = parseInt(pe.getStyle("borderLeftWidth"), 10) || 0;


                x += bl;
                y += bt;


                if (p != this.el && pe.getStyle('overflow') != 'visible') {
                    x += bl;
                    y += bt;
                }
            }
            p = p.offsetParent;
        }

        if (XarLib.isSafari && hasAbsolute) {
            x -= bd.offsetLeft;
            y -= bd.offsetTop;
        }

        if (XarLib.isGecko && !hasAbsolute) {
            var dbd = XarLib.getEl(bd);
            x += parseInt(dbd.getStyle("borderLeftWidth"), 10) || 0;
            y += parseInt(dbd.getStyle("borderTopWidth"), 10) || 0;
        }

        p = this.el.parentNode;
        while (p && p != bd) {
            if (!XarLib.isOpera || (p.tagName != 'TR' && XarLib.getEl(p).getStyle("display") != "inline")) {
                x -= p.scrollLeft;
                y -= p.scrollTop;
            }
            p = p.parentNode;
        }
        
        
        /* -- modified by kbalcerek -- */
        if(wasDisplayNone){
          this.el.style['display'] = 'none';
        }
        /* -- END: modified by kbalcerek -- */
        
        return [x, y];
    },

    setXY : function(xy) {
        /* -- modified by kbalcerek -- */
        if(XarLib.isOpera){
          if(this.el.style.left == ''){
            this.el.style.left = '0px';
          }
          if(this.el.style.top == ''){
            this.el.style.top = '0px';
          }
        }
        /* -- END: modified by kbalcerek -- */
        
        this.position();
        var pts = this.translatePoints(xy);
        if (xy[0] !== false) {
            this.el.style.left = pts.left + "px";
        }
        if (xy[1] !== false) {
            this.el.style.top = pts.top + "px";
        }
    },

    setX : function(x) {
        this.setXY([x, false]);
    },

    setY : function(y) {
        this.setXY([false, y]);
    },


    getScroll : function(){
        var d = this.el, doc = document;
        if(d == doc || d == doc.body){
            var l, t;
            if(XarLib.isIE && XarLib.isStrict){
                l = doc.documentElement.scrollLeft || (doc.body.scrollLeft || 0);
                t = doc.documentElement.scrollTop || (doc.body.scrollTop || 0);
            }else{
                l = window.pageXOffset || (doc.body.scrollLeft || 0);
                t = window.pageYOffset || (doc.body.scrollTop || 0);
            }
            return {left: l, top: t};
        }else{
            return {left: d.scrollLeft, top: d.scrollTop};
        }
    },

    
    getStyle : function(){
        return document.defaultView && document.defaultView.getComputedStyle ?
            function(prop){
                var v, cs, camel;
                if(prop == 'float'){
                    prop = "cssFloat";
                }
                if(v = this.el.style[prop]){
                    return v;
                }
                if(cs = document.defaultView.getComputedStyle(this.el, "")){
                    camel = prop.replace(/(-[a-z])/gi, function(m, a){ return a.charAt(1).toUpperCase(); });
                    return cs[camel];
                }
                return null;
            } :
            function(prop){
                var v, cs, camel;
                if(prop == 'opacity'){
                    if(typeof this.el.style.filter == 'string'){
                        var m = this.el.style.filter.match(/alpha\(opacity=(.*)\)/i);
                        if(m){
                            var fv = parseFloat(m[1]);
                            if(!isNaN(fv)){
                                return fv ? fv / 100 : 0;
                            }
                        }
                    }
                    return 1;
                }else if(prop == 'float'){
                    prop = "styleFloat";
                }
                camel = prop.replace(/(-[a-z])/gi, function(m, a){ return a.charAt(1).toUpperCase(); });
                if(v = this.el.style[camel]){
                    return v;
                }
                if(cs = this.el.currentStyle){
                    return cs[camel];
                }
                return null;
            };
    }(), //END: getStyle : function(){

    
    position : function(pos, zIndex, x, y){
        if(!pos){
           if(this.getStyle('position') == 'static'){
               //this.setStyle('position', 'relative');
               this.el.style['position'] = 'relative';
           }
        }
    },

    
    translatePoints : function(x, y){
        if(typeof x == 'object' || XarLib.isArray(x)){
            y = x[1]; x = x[0];
        }
        var p = this.getStyle('position');
        var o = this.getXY();

        var l = parseInt(this.getStyle('left'), 10);
        var t = parseInt(this.getStyle('top'), 10);

        if(isNaN(l)){
            l = (p == "relative") ? 0 : this.el.offsetLeft;
        }
        if(isNaN(t)){
            t = (p == "relative") ? 0 : this.el.offsetTop;
        }
        
        /*
        alert(x +    ', a' + y);
        alert(o[0] + ', b' + o[1]);
        alert(l +    ', c' + t);
        alert((x - o[0] + l) + ', suma: ' + (y - o[1] + t));
        */
        
        return {left: (x - o[0] + l), top: (y - o[1] + t)};
    }
    
    
}//END: XarLib.Element.prototype = {

