var msie = ((navigator.appVersion.indexOf("MSIE")!= -1)&&!window.opera)? true : false;
var msie6 = ((navigator.appVersion.indexOf("MSIE 6")!= -1)&&!window.opera)? true : false;
var opera = (navigator.userAgent.indexOf('Opera')!=-1) ? true : false;
var autoTimeOut;

function f_aev(obj, type, fn) { try { if (obj.addEventListener) { obj.addEventListener(type, fn, false); } else if (obj.attachEvent) { obj["e"+type+fn] = fn; obj[type+fn] = function() { obj["e"+type+fn](window.event); }; obj.attachEvent("on"+type, obj[type+fn]); }} catch (e) {}}
function f_gob(n) { return document.getElementById(n); }
function f_trm(x) { return x.replace(/^\s+|\s+$/g,''); }
function f_hcl(obj, clName) { var clN = ' ' + obj.className + ' '; return (clN.match(' ' + clName + ' ') != null); }
function f_acl(obj, clName) { if (!f_hcl(obj, clName)) obj.className = f_trm(obj.className + ' ' + clName); }
function f_rcl(obj, clName) { if (!f_hcl(obj, clName)) return; var clN = ' ' + obj.className + ' '; eval("obj.className = f_trm(clN.replace(/ " + clName + " /g,''));"); }
function f_scl(obj, oldC, newC) { f_rcl(obj, oldC); f_acl(obj, newC); }
function f_fpo(obj) { var curleft = curtop = 0; if (obj.offsetParent) { curleft = obj.offsetLeft; curtop = obj.offsetTop; while (obj = obj.offsetParent) { curleft += obj.offsetLeft; curtop += obj.offsetTop; }}; return [curleft,curtop]; }
function f_gbtc(obj, tag, clName) { var objs = obj.getElementsByTagName(tag); var robj = new Array(); var j = 0; for (var i = 0; i < objs.length; i++) { if (f_hcl(objs[i], clName)) robj[j++] = objs[i]; }; return robj; }
function f_cookie(name, value, days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; }
function f_hide(n) { f_gob(n).style.display = 'none'; }
function f_show(n) { f_gob(n).style.display = 'block'; }
function f_swap(m, n) { f_hide(m); f_show(n); }
function f_bit(a, b) { return ((a & b) == b); }
function f_createCookie(name, value, days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; }
function f_readCookie(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; }
function f_eraseCookie(name) { f_createCookie(name,"",-1); }
function f_gpbn(obj, tgName) {
 var par = obj.parentNode;
 if(par){
  var tn = f_trm(tgName.toLowerCase());
  while( f_trm(par.tagName.toLowerCase()) != tn ) {
   par = par.parentNode;
   if(par.tagName == undefined){
    par = null;
    break;
   }
  }
 }
 return par;
}

function LiveArticle() {
  this.lastEntry = 0;
  this.lastUpdatedEntry = null;
  this.itemsForUpdate = 3;
  this.container = false;
  this.init_done = false;
  this.active = false;
  this.lastDate = '0000-00-00 00:00:00';

  this.init = function() {
    this.container = f_gob('artykul_live');
    if (!this.container) return;
    this.init_done = true;
    if (f_gob('last_position')) {
      var pos = parseInt(f_gob('last_position').innerHTML);
      this.setLastEntry(pos);
    }
  }

  this.initialized = function() {
    return this.init_done;
  }

  this.cleanUpEntries = function(){
   for(var i = this.getEntryToUpdateFrom() + 1; i <= this.getLastEntry(); i++)
     this.removeEntry(i);
  }

  this.makeEntry = function(json_obj) {
   if(this.lastUpdatedEntry == null)
    this.cleanUpEntries();

   var _entry_div = f_gbtc(this.container, 'div', 'e'+json_obj.pos)[0];
   if(_entry_div) {
    this.updateEntry(json_obj);
   }else{
    this.addEntry(json_obj);
   }
   this.lastUpdatedEntry = json_obj.pos;
  }

  this.addEntry = function(json_obj) {
    var _o_div = document.createElement('div');
    if(json_obj.time) var _o_strong_i = document.createElement('strong');
    var _o_div_i = document.createElement('div');
    f_acl(_o_div, 'entry e'+json_obj.pos);
    if(json_obj.time) f_acl(_o_strong_i, 'entry_time');
    f_acl(_o_div_i, 'entry_content');

    if(json_obj.time){
     if( json_obj.time.search(":") == -1 ) json_obj.time = json_obj.time + ". min.";

     if (json_obj.bonusTime) {
       _o_strong_i.innerHTML = '' + json_obj.time + ' + ' + json_obj.bonusTime + '.';
     } else {
       _o_strong_i.innerHTML = (!json_obj.time || json_obj.time == '') ? '&nbsp;' : json_obj.time;
     }
    }

    _o_div_i.innerHTML = json_obj.msg;
    if(json_obj.time) _o_div.appendChild(_o_strong_i);
    _o_div.appendChild(_o_div_i);

    if (f_gbtc(this.container, 'div', 'entry') == 0) {
      this.container.appendChild(_o_div);
    } else {
      this.container.insertBefore(_o_div, f_gbtc(this.container, 'div', 'entry')[0]);
    }
    this.lastEntry = json_obj.pos;
  }

  this.updateEntry = function(json_obj) {
   var _entry_div = f_gbtc(this.container, 'div', 'e'+json_obj.pos)[0];
   if(json_obj.time) var _o_strong_i = document.createElement('strong');
   var _o_div_i = document.createElement('div');

   if(json_obj.time) f_acl(_o_strong_i, 'entry_time');
   f_acl(_o_div_i, 'entry_content');

   if(json_obj.time) {
    if (json_obj.bonusTime) {
      _o_strong_i.innerHTML = json_obj.time.substring(0, json_obj.time.length) + ' + ' + json_obj.bonusTime + '.';
    } else {
      _o_strong_i.innerHTML = (!json_obj.time || json_obj.time == '') ? '&nbsp;' : json_obj.time;
    }
   }

   _entry_div.innerHTML = '';

   _o_div_i.innerHTML = json_obj.msg;
   if(json_obj.time) _entry_div.appendChild(_o_strong_i);
   _entry_div.appendChild(_o_div_i);

  }

  this.removeEntry = function(no){
   var _entry_div = f_gbtc(this.container, 'div', 'e'+no)[0];
   var toRemove = [];
   if(_entry_div){

    var el = _entry_div.nextSibling;
    while(el){
     if(el.nodeType==1 && el.nodeName.toUpperCase() == "BR"){
      toRemove.push(el);
      el = el.nextSibling;
     }else if(el.nodeType==1){
      break;
     }else{
      el = el.nextSibling;
     }
    }

    for(var i = 0; i < toRemove.length; i++){
     _entry_div.parentNode.removeChild(toRemove[i]);
    }
    _entry_div.parentNode.removeChild(_entry_div);

   }
  }

  this.getEntryToUpdateFrom = function(){
   var index = this.itemsForUpdate - 1;
   while(!f_gbtc(this.container, 'div', 'entry')[index]){
    index--;
    if(index == -1) break;
   }
   index > -1 ? index = parseInt(f_gbtc(this.container, 'div', 'entry')[index].className.replace("entry e",""),10)-1 : index = 0;

   if(index > (livearticle.getLastEntry() - livearticle.itemsForUpdate)) index = livearticle.getLastEntry() - livearticle.itemsForUpdate;
   return index;
  }

  this.getLastEntry = function() {
    return this.lastEntry;
  }

  this.setLastEntry = function(pos) {
    this.lastEntry = pos;
  }

  this.isActive = function() {
    return this.active;
  }

  this.activate = function() {
    this.active = true;
  }

  this.deactivate = function() {
    this.active = false;
  }

  this.getDate = function() {
    return this.lastDate;
  }

  this.setDate = function(newDate) {
    this.lastDate = newDate;
  }

  this.getXX = function() {
    var _p = document.location.href.split(",");
    if(_p.length > 2) return _p[2];
    return null;
  }

}

var livearticle = new LiveArticle();

function la_init() {
  livearticle.init();
  livearticle.activate();
  f_createCookie("la_autorefresh"+livearticle.getXX(), true, 0);
  updateAutoRefreshJSON();
}

function updateAutoRefreshJSON() {
  if(autorefresh){
   updateAutoRefresh(livearticle.getEntryToUpdateFrom(), livearticle.isActive(), livearticle.getDate());
   window.setTimeout(updateAutoRefreshJSON, 45*1000);
  }
}

function la_add_entries(json_query) {
  try {
    livearticle.lastUpdatedEntry = null;
    var json_obj = typeof json_query == 'object' ? json_query : eval('('+json_query+')');
    json_obj_l = json_obj.entries.length;
    for (var i = 0; i < json_obj_l; i++) {
      var _co = json_obj.entries[i];
      switch (_co.entryType) {
       case 'AccountEntry': if (livearticle.isActive()) livearticle.makeEntry(_co); break;
       case 'AccountDate': if (livearticle.isReady()) livearticle.setDate(_co.content); break;
      }
    }
    if(livearticle.lastUpdatedEntry == null) livearticle.cleanUpEntries();
  } catch (e) {}
}

function la_start() {
 if(autorefresh == true){
  if(f_gob('autoOdsniez')){
   f_gob('autoOdsniez').checked = 'true';
   autoTimeOut = window.setTimeout(function() { document.location.reload(); }, 15*60*1000);
  }
  la_init();
 }else if(autorefresh == false){
  if(f_gob('autoOdsniez') && f_gob('autoOdsniez').checked == true){
   autorefresh = true;
   la_init();
   autoTimeOut = window.setTimeout(function() { document.location.reload(); }, 15*60*1000);
  }else{
   if(f_readCookie("la_autorefresh"+livearticle.getXX()) && f_readCookie("la_autorefresh"+livearticle.getXX()) == "true"){
    if(f_gob('autoOdsniez')){
     f_gob('autoOdsniez').checked = 'true';
    }
    autorefresh = true;
    la_init();
    autoTimeOut = window.setTimeout(function() { document.location.reload(); }, 15*60*1000);
   }
  }
 }

 if(f_gob('autoOdsniez')){
  f_aev(f_gob('autoOdsniez'), 'click', checkAutoOdsniez);
 }

}

function checkAutoOdsniez(){
  if(f_gob('autoOdsniez').checked == true){
      autorefresh = true;
      autoTimeOut = window.setTimeout(function() { document.location.reload(); }, 15*60*1000);
      f_createCookie("la_autorefresh"+livearticle.getXX(), true, 0);
      if(!livearticle.isActive()){
         la_init();
      }else{
       updateAutoRefreshJSON();
      }
  }else{
      autorefresh = false;
      window.clearTimeout(autoTimeOut);
      f_eraseCookie("la_autorefresh");
  }
}

var la_tags = {
 root: null,
 init: function(){
  me = la_tags;
  var _p = f_gob('sS');
  if(_p){
   me.root = f_gbtc(_p, 'div', 'artykul_live_tagi')[0];
   if(me.root){
    var _l = f_gbtc(_p, 'a', 'allTags')[0];
    if(_l){
     f_aev(_l, 'click', la_tags.toogle);
    }
   }
  }
 },
 toogle: function(e){
  var el; if (window.event && window.event.srcElement) { el = window.event.srcElement; }; if (e && e.target) { el = e.target; }; if (!el) { return; }; (e.preventDefault) ? e.preventDefault() : (e.returnValue = false);
  f_hcl(la_tags.root, 'artykul_live_tagi_open') ? f_rcl(la_tags.root, 'artykul_live_tagi_open') : f_acl(la_tags.root, 'artykul_live_tagi_open');
 }
}

f_aev(window, 'load', la_start);
f_aev(window, 'load', la_tags.init);
f_aev(window, 'load', function() {
  var _ls = f_gbtc(document, 'div', 'mod-live-scores');
  for(var i = 0; i < _ls.length; i++) {
   init_ls(_ls[i]).init();
 }
});


function init_ls(elem){
 return ls_mod = {
  root: elem,
  init: function(){
   var me = ls_mod;
   if(!me.root) return;

   var _e = f_gbtc(me.root, 'div', 'entry');
   for(var i = 0; i < _e.length; i++){
     _li = _e[i].getElementsByTagName('li');
     for(var j = 0; j < _li.length; j++){
       /* if(!f_hcl(f_gpbn(_li[j], 'ul'),'scores')){  */
         _a = _li[j].getElementsByTagName('a');
             for(var k = 0; k < _a.length; k++){
               f_aev(_a[k], 'click', me.blockLink );
             }
         f_aev(_li[j], 'mouseover', me.hover );
             f_aev(_li[j], 'mouseout', me.unhover );
             f_aev(_li[j], 'click', me.gotoPage );
       /* } */
     }
   }
  },
  blockLink: function(e){
   var el; if (window.event && window.event.srcElement) { el = window.event.srcElement; }; if (e && e.target) { el = e.target; }; if (!el) { return; }; (e.preventDefault) ? e.preventDefault() : (e.returnValue = false);
  },
  hover: function(e){
   var el; if (window.event && window.event.srcElement) { el = window.event.srcElement; }; if (e && e.target) { el = e.target; }; if (!el) { return; }; (e.preventDefault) ? e.preventDefault() : (e.returnValue = false);
   if(f_trm(el.tagName.toLowerCase()) != 'li') el = f_gpbn(el, 'li');
   var _ul = f_gpbn(el, 'ul');
   if(_ul && f_hcl(_ul,'scores')) el = f_gpbn(_ul, 'li');
   f_acl(el, 'hovered');
  },
  unhover: function(e){
   var el; if (window.event && window.event.srcElement) { el = window.event.srcElement; }; if (e && e.target) { el = e.target; }; if (!el) { return; }; (e.preventDefault) ? e.preventDefault() : (e.returnValue = false);
   if(f_trm(el.tagName.toLowerCase()) != 'li') el = f_gpbn(el, 'li');
   var _ul = f_gpbn(el, 'ul');
   if(_ul && f_hcl(_ul,'scores')) el = f_gpbn(_ul, 'li');
   f_rcl(el, 'hovered');
  },
  gotoPage: function(e){
   var el; if (window.event && window.event.srcElement) { el = window.event.srcElement; }; if (e && e.target) { el = e.target; }; if (!el) { return; }; (e.preventDefault) ? e.preventDefault() : (e.returnValue = false);
   if(f_trm(el.tagName.toLowerCase()) != 'li') el = f_gpbn(el, 'li');
   var _ul = f_gpbn(el, 'ul');
   if(_ul && f_hcl(_ul,'scores')) el = f_gpbn(_ul, 'li');
   var _score = f_gbtc(el, 'span', 'score')[0];
   if(_score){
        _link =  _score.getElementsByTagName('a')[0];
        if(_link) document.location.href = _link.href;
   }else{
     _score = f_gbtc(el, 'ul', 'scores')[0];
     if(_score){
       _link =  _score.getElementsByTagName('a')[0];
       if(_link) document.location.href = _link.href;
     }
   }
  }
 }
}



