var IE = false;

function callMe(url, callback, urllock){
  ajaxObject = function() {
  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e){
  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e){
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){
  try { return new XMLHttpRequest(); } catch(e){
  throw new Error( "This browser does not support XMLHttpRequest." );
  }}}}}};
  ajax = new ajaxObject();
  ajax.onreadystatechange = function(){
    if(ajax.readyState == 4){
      if(ajax.status == 200){
        callHandle(url,ajax.responseText,callback,urllock);
      }
    }
  };
  if(url.indexOf('time') == -1){
    url += '?time='+Number(new Date());
  }
  ajax.open('GET', url, true);
  ajax.send(null);
}
function callMePost(url, params, callback, urllock){
  ajaxObject = function() {
  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e){
  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e){
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){
  try { return new XMLHttpRequest(); } catch(e){
  throw new Error( "This browser does not support XMLHttpRequest." );
  }}}}}};
  ajax = new ajaxObject();
  ajax.onreadystatechange = function(){
    if(ajax.readyState == 4){
      if(ajax.status == 200){
        callHandle(url.replace('ajax/',''),ajax.responseText,callback,urllock);
      }
    }
  };
  ajax.open('POST', url, true);
  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ajax.setRequestHeader("Content-length", params.length);
  ajax.setRequestHeader("Connection", "close");
  ajax.send(params);
}
function callHandle(url,json, callback, urllock){
//   if(!urllock) location.href = "#"+url.replace(/,small_1/,'').split('?')[0];
  var obj;
  try{
    if(typeof(JSON) === "object" && typeof(JSON.parse) === "function")
      obj = JSON.parse(json);
    else
      eval("obj = "+json+";");
    if(obj.title) document.title = obj.title;
    if(obj.description){
      document.getElementById("sitedescription").content = obj.description.replace(/\<[^\>]*\>/g,'').substring(0,250);
    }
  }catch(e){
   obj = json;
  }
  if(typeof(callback) === "function"){
    callback(obj);
  }
}
function qqUploaded(id, filename, responseJSON){
  if(responseJSON && responseJSON.success){
    var el = document.getElementById(id);
    el.parentNode.innerHTML = (responseJSON.msg ? responseJSON.msg : filename).replace('&lt;','<').replace('&gt;','>');
  }
}

function trim(str){
  s = str.replace(/^(\s)*/, '');
  s = s.replace(/(\s)*$/, '');
  return s;
}

function EmitSignal(signal, value){
  var els = document.getElementsByName("show["+signal+"_"+"0]"), i;
  for(i=0;i<els.length;i++){
    els[i].style.display = value?'none':'';
  }
  els = document.getElementsByName("show["+signal+"_"+"1]");
  for(i=0;i<els.length;i++){
    els[i].style.display = value?'':'none';
  }
}

var LoaderFunctions = [];
var LoaderLoaded = false;
var LoaderLastOnLoad = window.onload;
if(typeof(LoaderLastOnLoad) !== 'function')
  window.onload = FireLoader;
else
  window.onload = function(){LoaderLastOnLoad(); FireLoader();};
var LoaderLastContentLoaded = window.DOMContentLoaded;
if(typeof(LoaderLastContentLoaded) !== 'function')
  window.DOMContentLoaded = FireLoader;
else
  window.DOMContentLoaded = function(){LoaderLastContentLoaded(); FireLoader();};
function AddLoad(func){
  LoaderFunctions.push(func);
}
function FireLoader(){
  for(var i=0; i<LoaderFunctions.length; i++){
    LoaderFunctions[i]();
  }
  LoaderFunctions = [];
}

function ShowHide(id){
  var el = document.getElementById(id);
  if(el){
    if(el.style.display !== 'block'){
      el.style.display = 'block';
    }else{
      el.style.display = 'none';
    }
  }
}

function stop(e){
  if (!e) var e = window.event;
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
}
function RegDebug(){
  el = document.getElementById("debugger");
  if(!el) return;
  el.onclick = function(){ShowHide('debugger')};
  el.firstChild.onclick = stop;
}
AddLoad(RegDebug);

function Trans(id, text, lang){
  var ret = prompt("ID: "+id, text);
  if(ret){
    callMePost("trans.php", "id="+id+"&lang="+lang+"&text="+ret, function(obj){ alert(obj) }, false);
  }
}

function Url(val){
  return val.replace(/,/g,'$przec;').replace(/_/g,'$under;');
}

function PagerGo(el, url){
  var val = el.value;
  if(!val) url = url.replace(",page_X",'');
  else url = url.replace(",page_X",",page_"+val);
  location.href = url;
}
function PagerSearch(el, url){
  var val = el.value;
  if(!val.length) url = url.replace(",search_","");
  else url = url.replace(",search_",",search_"+Url(val));
  location.href = url;
}
function PagerSearchFocus(el, url){
  el.onkeypress = function(e){
    if(!e) e = window.event;
    var keynum;
    if(window.event)
      keynum = e.keyCode;
    else if(e.which)
      keynum = e.which;
    if(keynum == 13){
      e.returnValue = false;
      if(e.preventDefault) e.preventDefault();
      PagerSearch(this, url);
    }
  };
  el.onblur = function(e){
    this.onkeypress = null;
    this.onblur = null;
  }
}

function RowHover(el, url){
  el.onclick = function(e){
    if(!e) e = window.event;
    if(!e.target){
      e.target = e.srcElement;
    }
    if(e.target.tagName.toLowerCase() == 'td'){
      location.href = url;
    }
  };
  el.mouseout = function(e){
    this.onclick = null;
    this.mouseout = null;
  }
}

function FavChange(el){
  var ell = el.nextSibling;
  if(el.value == "0"){
    ell.style.display = '';
  }else{
    ell.style.display = 'none';
  }
}

function FavSubmit(el, none, noname){
  var el = el.previousSibling.previousSibling;
  if(el.value == "##"){
    alert(none);
    return false;
  }
  if(el.value == "0"){
    el = el.nextSibling.lastChild;
    if(!el.value){
      alert(noname);
      return false;
    }
  }
  return true;
}

function ChBxChng(el){
  var sel = el.checked;
  el = el.nextSibling.nextSibling;
  for(var i=0; i<el.childNodes.length; i++){
    if(el.childNodes[i].nodeType != 3){
      if(location.href.indexOf("admin")){
        el.childNodes[i].checked = sel;
      }else{
        el.childNodes[i].disabled = sel;
      }
    }
  }
}

function activatePlaceholders() {
  var inputs = document.getElementsByTagName("input");
  for(var i=0;i<inputs.length;i++) {
    if(inputs[i].getAttribute("type") == "text") {
      if(inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0){
        if(inputs[i].value.length < 1){
          inputs[i].value = inputs[i].getAttribute("placeholder");
          inputs[i].style.color = "#ACACAC";
        }
        inputs[i].onclick = function() {
        if (this.value == this.getAttribute("placeholder")){
          this.value = "";
          this.style.color = "";
        }
        return false;
        }
        inputs[i].onblur = function(){
          if (this.value.length < 1) {
            this.value = this.getAttribute("placeholder");
            this.style.color = "#ACACAC";
          }
        }
      }
    }
  }
  var fs = document.forms, form;
  for(var i=0; i<fs.length; i++){
    form = fs[i];
    AddClear(form);
  }
}
function AddClear(form){
  var last = form.onsubmit;
  if(typeof(last) == "function"){
    form.onsubmit = function(){clearPlaceholders(this); last()};
  }else{
    form.onsubmit = function(){clearPlaceholders(this);};
  }
}
function clearPlaceholders(form){
  var el = form.elements;
  for(var i=0; i<el.length; i++){
    if(el[i].getAttribute("placeholder") && el[i].value == el[i].getAttribute("placeholder")){
      el[i].value = "";
    }
  }
}
AddLoad(activatePlaceholders);

var curBanner = 0;
var banTimeout = 4000;
var banBlock = false;
var banTime = null;
function MoveBanner(id){
  var el = document.getElementById("banner");
  if(!el || banBlock) return false;
  var from = el.childNodes[curBanner];
  if(id){
    curBanner = Math.round(id-1)%4;
  }else{
    curBanner++;
    curBanner%=4;
  }
  var to = el.childNodes[curBanner];
  if(from === to){
    return false;
  }
  banBlock = true;
  to.style.zIndex = 2;
  to.style.display = '';
  changeOpac(to, 0);
  RunEffect(to, "fadeIn", {callback: function(e){
    from.style.display = "none";
    to.style.zIndex = "";
    doBanTime();
    for(var i=0; i<4; i++){
      document.getElementById("banact"+i).className = i==curBanner ? "act" : "";
    }
    banBlock = false;
  }});
  return false;
}
function doBanTime(){
  if(banTime){
    clearTimeout(banTime);
  }
  banTime = setTimeout(function(){MoveBanner()}, banTimeout);
}
AddLoad(doBanTime);

var curSub = 0;
var subBlock = false;
var subTimeout = 4000;
var subTime = null;
function MoveSub(id){
  var el = document.getElementById("subchange");
  if(!el || subBlock) return false;
  var from = el.childNodes[curSub];
  if(id){
    curSub += id+el.childNodes.length;
    curSub %= el.childNodes.length;
  }else{
    curSub++;
    curSub %= el.childNodes.length;
  }
  var to = el.childNodes[curSub];
  if(from === to){
    return false;
  }
  subBlock = true;
  to.style.zIndex = 2;
  to.style.display = '';
  changeOpac(to, 0);
  RunEffect(to, "fadeIn", {callback: function(e){
    from.style.display = "none";
    to.style.zIndex = "";
//     doSubTime();
    subBlock = false;
  }});
  return false;
}
function doSubTime(){
  if(subTime){
    clearTimeout(subTime);
  }
  subTime = setTimeout(function(){MoveSub()}, subTimeout);
}
// AddLoad(doSubTime);

function Measure(el){
  var pos = el.style.position;
  el.style.position = 'absolute';
  el.style.visibility = 'hidden';
  el.style.display = '';
  el.style.overflowY = '';
  el.style.height = '';
  var height = el.offsetHeight;
  el.style.display = 'none';
  el.style.visibility = '';
  el.style.position = pos;
  return height;
}

if(!(location.href.indexOf("admin.") > 0)){
  AddLoad(TableBarHandle);
}
var TBRinit = false;
var TBRel = null;
var TBRtop = 0;
var TBRleft = 0;
function TableBarHandle(){
  if(!TBRinit){
    var ts = document.getElementsByTagName("table");
    for(var i=0; i<ts.length; i++){
      if(ts[i].parentNode.className = "tablebox"){
        TBRel = ts[i].tHead;
        var el = ts[i];
        do{
          TBRtop += el.offsetTop;
          TBRleft += el.offsetLeft;
        }while(el = el.offsetParent);
        break;
      }
    }
    TBRinit = true;
  }
  if(!TBRel){
    return false;
  }
  window.onresize = TableBarHandle;
  window.onscroll = function(){
    var move = document.getElementById("titleboxermove");
    var scrollY=0;
    if(typeof(window.pageYOffset) == 'number'){
      scrollY = window.pageYOffset;
    }else if(document.body && document.body.scrollTop){
      scrollY = document.body.scrollTop;
    }else if(document.documentElement && document.documentElement.scrollTop){
      scrollY = document.documentElement.scrollTop;
    }
    if(scrollY > TBRtop && !move){
      move = document.createElement("div");
      move.appendChild(document.createElement('table'));
      move.firstChild.createTHead();
      move.firstChild.tHead = TBRel.cloneNode(true);
      var tr = move.firstChild.tHead.rows;
      for(var i=0; i<tr.length; i++){
        for(var j=0; j<tr[i].cells.length; j++){
          tr[i].cells[j].style.width = TBRel.rows[i].cells[j].offsetWidth+'px';
        }
      }
      move.style.left = TBRleft+"px";
      move.id = "titleboxermove";
      move.className = "tablebox";
      document.body.appendChild(move);
      TBRel.style.visibility = 'hidden';
    }else if(scrollY <= TBRtop && move){
      document.body.removeChild(move);
      TBRel.style.visibility = '';
    }
    if(move) move.style.width = (TBRel.offsetWidth)+'px';
  }
  window.onscroll();
}
