var _count_click_track = 1; // Track a percentage of the pages click
var _count_click_pause = 500; // delay wainting for server
var _count_percentage = 1;  // Track a percentage of the pages click
var _count_onload = true;  // Wait for onload to send count request

// Track a percentage of the pages
if (Math.random()<_count_percentage) {
  function _count_escape(_count_str){
    if(typeof(encodeURIComponent) == 'function') {
      return encodeURIComponent(_count_str);
    } else {
      return escape(_count_str);
    }
  }

  function _count_rpc(url){
    // create SCRIPT tag
    var scrptElement = document.createElement("script");

    // set its attributes
    scrptElement.setAttribute("type", "text/javascript");
    scrptElement.setAttribute("language", "JavaScript");
    scrptElement.setAttribute("src", url);
    //scrptElement.setAttribute("defer", "defer");

    // add this new element to the head tag
    document.getElementsByTagName("head")[0].appendChild(scrptElement);
  }

  function _count_pause(_count_time_msec) {
    var _count_now = new Date();
    var _count_expire = _count_now.getTime() + _count_time_msec;
    while(_count_now.getTime() < _count_expire)
    _count_now = new Date();
  }

  function _count_click(event) {
    var element = Event.element(event);

    // Find fist parent DOM element with id
    var _count_dom = '';
    var _count_click = '';
    obj = element;
    do {
      if (_count_click == '' && obj.tagName == 'A') {
        _count_click = obj.href;
      }
      if (obj.tagName == 'BODY' || obj.tagName == 'HTML') {
        break;
      }
      if (obj.id!='' && obj.id!=undefined) {
        _count_dom = obj.id;
        break;
      }
    } while (obj = obj.up());

    if (_count_click!='') {
      _count_x = event.pointerX();
      _count_y = event.pointerY();

      // Find xy relative to DOM element
      if (_count_dom!='') {
        tmp = $(_count_dom).cumulativeOffset();
        _count_x = _count_x - tmp[0];
        _count_y = _count_y - tmp[1];
      }

      var _count_src = _count_url
          +'?site='+_count_escape(_count_site)
          +'&cat='+_count_escape(_count_cat)
          +'&type='+_count_escape(_count_type)
          +'&id='+_count_escape(_count_pageid)
          +'&url='+_count_escape(document.location.href)
          +'&url_click='+_count_escape(_count_click)
          +'&x='+_count_x
          +'&y='+_count_y
          +'&dom='+_count_dom
          +'&v=' + _count_version
          +'&r=' + Math.random();

      // count click
      //alert(_count_src);
      _count_rpc(_count_src);
      _count_pause(_count_click_pause);
    }
    return true;
  }

  function _count_count() {
    //alert('count_count');
    var _count_cookie = (navigator.cookieEnabled)? '1' : '0';
    if((typeof (navigator.cookieEnabled) == "undefined") && (_count_cookie == '0')) {
      document.cookie="_count_testcookie"
      _count_cookie=(document.cookie.indexOf("_count_testcookie")!=-1)? '1' : '0';
    }

    var _count_ref = '';
    try {
      _count_ref = top.document.referrer;
    } catch(e1) {
      if(parent){
        try{ _count_ref = parent.document.referrer; } catch(e2) { _count_ref=''; }
      }
    }
    if(_count_ref == '') {
      _count_ref = document.referrer;
    }

    var _count_src = _count_url
        +'?site='+_count_escape(_count_site)
        +'&cat='+_count_escape(_count_cat)
        +'&type='+_count_escape(_count_type)
        +'&id='+_count_escape(_count_pageid)
        +'&url='+_count_escape(document.location.href)
        +'&ref='+_count_escape(_count_ref)
        +'&c=' + _count_cookie
        +'&v=' + _count_version
        +'&r=' + Math.random();

    if (_count_onload) {
      _count_now = new Date();
      _count_src += '&lt='+ (_count_now.valueOf()-_count_load_date.valueOf());
      _count_src += '&dlt='+ (_count_now.valueOf()-_count_dom_loaded.valueOf());
    }
    _count_rpc(_count_src);
  }

  // Main
  _count_load_date = new Date()

  if (_count_onload) {
    // Set timestamp
    document.observe("dom:loaded", function() { _count_dom_loaded = new Date(); });
    // Count on window load
    Event.observe(window, 'load', _count_count);

  } else {
    // Count
    _count_count();
  }

  // init click count
  Event.observe (document, 'click', function(event) {_count_click(event); return false});

}
