izistatsEntJsELog = '';
var qdt = new Date;

izistatsEnt = {
  counter: {}
};

window.onerror = function(m, u, l)
{
  if (izistatsEntJsELog.length > 1200)
  {
      izistatsEntJsELog += '>*';
      return true;
  };

  if (izistatsEntJsELog) izistatsEntJsELog += '|'
  var url = u.indexOf('http://') == 0 ? u.substring(7) : u;
  izistatsEntJsELog += escape(url) + ':' + escape(l) + ';' + escape(m);

  return true;
};

izistatsEnt.counter = {
  dPreload: (window.qdt ? qdt : new Date),
  dLoad: null,
  dUnload: null,

  actions: [],
  
  inview: 0,
  fromBookmark: 0,
  
  blockTimers: {},
  blockIds: [],
  blockPostAll: false,
  blockTime: 0,

  boot: function() {
    izistatsEnt.tools.attach(window, "load", izistatsEnt.counter.init);
  },

  init: function() {
    if (document.documentElement && document.documentElement.clientHeight && !window.opera) dx = document.documentElement;
    else if (document.body && document.body.clientHeight) dx = document.body;
    else dx = null;
    
    izistatsEnt.tools.attach(window, "beforeunload", izistatsEnt.counter.update);
    //izistatsEnt.tools.attach(window, "beforeprint", izistatsEnt.counter.logPrint);
    izistatsEnt.tools.attach(window, "scroll", izistatsEnt.counter.logScroll);
    izistatsEnt.tools.attach(document.body, "mouseup", izistatsEnt.counter.docClick);
    izistatsEnt.tools.attach(document.body, "keyup", izistatsEnt.counter.docKey);

    izistatsEnt.counter.logScroll();
    izistatsEnt.counter.scanBlocks();
    
    izistatsEnt.counter.dLoad = new Date;
  },
  
  action: function(vx) {
    izistatsEnt.counter.actions[izistatsEnt.counter.actions.length] = {v: vx ? vx : null};
  },
  
  docKey: function(e) {},

  docClick: function(e) {
    var evt = e ? e : (window.event ? event : null);
    if (!evt) return;

    // record href clicked
    var el = evt.srcElement ? evt.srcElement : evt.target;
    while (el.parentNode && (!izistatsEnt.tools.hasAttrib(el, "href") || el.nodeName != 'A')) el = el.parentNode;
    var href = izistatsEnt.tools.getHref(el);
    if (href) izistatsEnt.counter.action(escape(href));
  },


  logScroll: function() {
    // store highest scrollposition
    // improve for documentElement later (doctype switching)
    // and return netscape4 support

    var db = document.body, de = document.documentElement;
    var cinview = 0;
    if (de && de.clientHeight)
       cinview = Math.floor(100 * ((de.scrollTop + de.clientHeight) / de.scrollHeight));
    else if (db && db.clientHeight)
       cinview = Math.floor(100 * ((db.scrollTop + db.clientHeight) / db.scrollHeight));

    //if (cinview > 100) cinview = 100;
    izistatsEnt.counter.inview = Math.max(izistatsEnt.counter.inview, cinview);
  },

  scanBlocks: function() {
    var els = document.getElementsByTagName('div');
    var exMode = false;
    
    var clsName;
    
    for (var i = 0; i < els.length; i++)
        if (els[i].id)
        {
            clsName = ' ' + els[i].className + ' ';
            if (clsName.indexOf(' qsfInc ') != -1 && !exMode)
            {
                izistatsEnt.counter.blockIds = [];
                izistatsEnt.counter.blockTimers = {};
                izistatsEnt.counter.blockPostAll = true;
                exMode = true;
            };
            
            if ((exMode && clsName.indexOf(' qsfInc ') != -1) || (!exMode && clsName.indexOf(' qsfExc ') == -1))
            {
                izistatsEnt.counter.blockIds[izistatsEnt.counter.blockIds.length] = els[i].id;
                izistatsEnt.counter.blockTimers[els[i].id] = 0;
            };
        };
    
    setInterval(izistatsEnt.counter.scanBlocksVisibility, 1000);
  },
  
  scanBlocksVisibility: function() {
    for (var block, i = 0; i < izistatsEnt.counter.blockIds.length; i++)
    {
        block = izistatsEnt.counter.blockIds[i];
        izistatsEnt.counter.blockTimers[block] += izistatsEnt.tools.getVisibleSurface(document.getElementById(block));
    };
  },

  update: function(evt) {
    izistatsEnt.counter.logScroll();
    izistatsEnt.counter.dUnload = new Date;
    
    // serialize actions
    var acts = '';
    for (var i = 0; i < izistatsEnt.counter.actions.length; i++)
    {
        if (acts) acts += ';';
        acts += escape(izistatsEnt.counter.actions[i].v);
    };

    var get_start = document.URL.indexOf('?');
    
    // Get the site url and the possible get values
    var get_values;
    var site_url;
    if (get_start != -1) {
        get_values = document.URL.substring(get_start+1, document.URL.length);
        site_url = document.URL.substring(0, get_start);
    } else {
        get_values = "";
        site_url = document.URL.substring(0, document.URL.length);
    };
    
    // Some values needed to extract domain name and page from the url
    var domain_start = 2 + site_url.indexOf('/');
    var http_strip = site_url.substring(domain_start, site_url.length);
    var domain_end  = http_strip.indexOf('/');
    
    // Get the domain name and the requested page
    var domain_name;
    var page;
    if (domain_end != -1) {
        domain_name = http_strip.substring(0, domain_end);
        page = http_strip.substring(domain_end, http_strip.length);
    } else {
        domain_name = http_strip.substring(0, http_strip.length);
        page = "";
    };
    
    var color_depth;
    if (navigator.appName=="Netscape") {
      color_depth = screen.pixelDepth;
    } else {
      color_depth = screen.colordepth;
    };

    var r = '&loadtime=' + Math.round(izistatsEnt.counter.dLoad - izistatsEnt.counter.dPreload)
          + '&surftime=' + Math.round((izistatsEnt.counter.dUnload - izistatsEnt.counter.dLoad) / 1000)
          + '&actions=' + escape(acts)
          + '&referrer=' + escape(document.referrer)
          + '&inview=' + izistatsEnt.counter.inview
          + '&windowwidth=' + (document.documentElement.clientWidth + document.body.clientWidth)
          + '&windowheight=' + (document.documentElement.clientHeight + document.body.clientHeight)
          + '&screenwidth=' + screen.width
          + '&screenheight=' + screen.height
          + '&colordepth=' + escape(color_depth)
          + '&domain=' + escape(domain_name)
          + '&page=' + escape(page)
          + '&get_values=' + escape(get_values)
          + '&jserror=' + izistatsEntJsELog
          + '&action=update_stats';
    
    //if (typeof document.body.addBehavior == 'object')
        //r += '&printed=' + ((typeof izistatsEnt.counter.printed == 'boolean' && izistatsEnt.counter.printed) ? '1' : '0');
        
    window.qsu = new Image;
    qsu.src = '/index.php?module=stats' + r;
  }
};

izistatsEnt.tools = {
  attach: function(el, ev, hl) {
    if (el.addEventListener) {
        if (ev == 'beforeunload') ev = 'unload';
        el.addEventListener(ev, hl, false);
    } else if (el.attachEvent) {
      el.attachEvent('on' + ev, hl);
    } else {
        if (ev == 'beforeunload') ev = 'unload';
        if (typeof el[ev = 'on' + ev] == 'function') {
          var origFunc = el[ev];
          el[ev] = function(e) { origFunc(e); hl(e); };
        } else el[ev] = hl;
    };
  },

  hasAttrib: function(el, attrib) {
    if (!el.getAttribute) return null;
    return el.getAttribute(attrib) ? true : false;
  },

  getHref: function(el) {
    if (!el.getAttribute) return el.href;

    if (document.all && typeof el.outerHTML == 'string' && el.attachEvent) {
      if (!el.outerHTML) return el.href;

      var div = document.createElement("div");
      div.innerHTML = el.outerHTML.replace(/\shref=/i," izistatsref=");

      return div.firstChild.getAttribute("izistatsref");
    } else return el.getAttribute("href");
  },
  
  getVisibleSurface: function(el) {
    var br = izistatsEnt.tools.getBoundingClientRect(el);
    
    vh = (Math.min(window.innerHeight ? innerHeight : dx.clientHeight, br.bottom) - Math.max(0, br.top)) / Math.max(1, br.bottom - br.top);
    vw = (Math.min(window.innerWidth ? innerWidth : dx.clientWidth, br.right) - Math.max(0, br.left)) / Math.max(1, br.right - br.left);
    
    return Math.max(0, vh) * Math.max(0, vw);
  },
  
  getBoundingClientRect: function(el) {
    if (el.getBoundingClientRect)
        return el.getBoundingClientRect();
    else
    {
        var r = { top: 0, left: 0 };
        var tel = el;
        do
        {
            r.top += tel.offsetTop - tel.scrollTop;
            r.left += tel.offsetLeft - tel.scrollLeft;
        } while (tel = tel.offsetParent ? tel.offsetParent : tel == document.body && dx == document.documentElement ? dx : null);
        r.bottom = r.top + el.offsetHeight;
        r.right = r.left + el.offsetWidth;
        return r;
    };
  }
};

izistatsEnt.counter.boot();
