var isIE = document.all ? true : false;
if (!isIE) document.captureEvents(Event.MOUSEMOVE);

function show(id) {
  var e = document.getElementById(id);
  if (!e) return;
  document.getElementById(id).tagName.toLowerCase();
  e.style.display = e.tagName.toLowerCase() == 'span' ? 'inline' : 'block';
}

function hide(id) {
  var e = document.getElementById(id);
  if (!e) return;
  e.style.display = 'none'; 
}

function toggle(id) {
  var e = document.getElementById(id);
  if (e.style.display == 'none')
    show(id);
  else
    hide(id);
}

var flashtimer = null;
function flash_merkliste(r, g, b) {
  $('merkliste_quickinfo').style.backgroundColor = 'rgb('+r+', '+g+', '+b+')';
  var nr = Math.min(255, r + 5);
  var ng = Math.min(255, g + 5);
  var nb = Math.min(255, b + 5);
  if (nr+ng+nb < 255*3)
    flashtimer = setTimeout('flash_merkliste('+nr+', '+ng+', '+nb+')', 50);
}

function flash_reset() {
  clearTimeout(flashtimer);
  $('merkliste_quickinfo').style.backgroundColor = '';
}

function vergessen(url) {
  new Ajax.Updater('merkliste', '/domains/ajax/merkliste.php', {
    'parameters': {'action' : 'remove', 'url' : url, 'output' : 'long'}
  });
}

function setTreuhand(url) {
  new Ajax.Updater('merkliste', '/domains/ajax/merkliste.php', {
    'parameters': {'action' : 'setTreuhand', 'url' : url, 'output' : 'long'}
  });
}

function unsetTreuhand(url) {
  new Ajax.Updater('merkliste', '/domains/ajax/merkliste.php', {
    'parameters': {'action' : 'unsetTreuhand', 'url' : url, 'output' : 'long'}
  });
}

function whoisinfo (url) {
  var whoiswindow = window.open('/domains/whoisinfo.php?url='+url, "WHOIS", "width=500,height=500,left=100,top=100,scrollbars=yes");
  whoiswindow.focus();
}

function loaddomainlist(cat) {
  new Ajax.Updater('ajax_domainliste', '/domains/ajax/domainliste.php', { 'parameters' : {'cat' : cat} });
}

function sign(x) { return x == 0 ? 0 : (x / Math.abs(x)); }

// Damit das Ajax Icon nicht ewig rumhängt, wenn mal was hängen bleibt
var ajaxtimer = null;

function showajax() {
  if (ajaxtimer) clearTimeout(ajaxtimer);
  ajaxtimer = setTimeout('hideajax()', 15000);
  $('ajax').style.display = 'block';
}

function hideajax() {
  if (ajaxtimer) clearTimeout(ajaxtimer);
  $('ajax').style.display = 'none';
}

function selectdomain() {
  var docid = document.forms.check.endung.value;
  $('domaininfo').innerHTML = $('domaininfo_'+docid).innerHTML;
}

function loadselectbox(cat) {
  new Ajax.Updater('selectbox', '/domains/ajax/selectbox.php', { 'parameters' : {'cat' : cat}, 'onComplete': selectdomain });
}

var queue = [];
var running = false;

function checkdomain(input, docid) {
  queue.push({'input' : input, 'docid': docid});
  if (!running) checkfirstinqueue();
}

function checkdomains(input, docids) {
  for (var i = 0; i < docids.length; i++)
    checkdomain(input, docids[i]);
}

function checkfirstinqueue() {
  if (queue.length == 0) {
    running = false;
  } else {
    running = true;
    showajax();
    var job = queue.shift();
    new Ajax.Request('/domains/ajax/history.php', { 'parameters' : { 'action': 'add', 'input' : job.input, 'docid' : job.docid }, onSuccess: function() { reloadhistory(job.input, job.docid); checkfirstinqueue(); } });
  }
}

function remove(url) {
  showajax();
  new Ajax.Request('/domains/ajax/history.php', { 'parameters' : {'action': 'remove', 'url': url}, onSuccess: function() { reloadhistory(); } });
}

function reloadhistory(input, docid) {
  // Wenn input und docid gegeben, wird nur die entsprechende Statuszeile aktualisiert, falls diese existiert;
  // ansonsten wird die komplette History neu geladen.
  showajax();
  if (input && docid && document.getElementById('tldstatus_'+input+'_'+docid))
    new Ajax.Updater('tldstatus_'+input+'_'+docid, '/domains/ajax/history.php', { parameters: { 'action': 'printtldstatus', 'input': input, 'docid': docid }, onSuccess: function() { if (queue.length == 0) hideajax(); } });
  else if (input && docid && document.getElementById('sldstatus_'+input+'_'+docid))
    new Ajax.Updater('sldstatus_'+input+'_'+docid, '/domains/ajax/history.php', { parameters: { 'action': 'printsldstatus', 'input': input, 'docid': docid }, onSuccess: function() { if (queue.length == 0) hideajax(); } });
  else
    new Ajax.Updater('history', '/domains/ajax/history.php', { parameters: { 'action': 'printfull' }, onSuccess: function() { if (queue.length == 0) hideajax(); } });
}

function aufmerkliste(input, docid) {
  showajax();
  new Ajax.Updater('merkliste_quickinfo', '/domains/ajax/merkliste.php', {
    'parameters': {'action' : 'add', 'input': input, 'docid': docid},
    'onSuccess' : function() {
      reloadhistory(input, docid);
      flash_reset();
      flash_merkliste(100, 255, 100);
      hideajax();
    }
  });
}

// Das Ajax-Icon immer an den Mauspointer drankleben
function updateAjaxIcon(mp) {
  var x;
  var y;
  if (!isIE) { x = mp.pageX; y = mp.pageY; }
  if (isIE) { x = event.clientX + document.documentElement.scrollLeft; y = event.clientY + document.documentElement.scrollTop; }
  $('ajax').style.top = (y+15)+'px';
  $('ajax').style.left = (x+12)+'px';
  return true;
}
document.onmousemove = updateAjaxIcon;

function elementPos(e) {
  var x = e.offsetLeft;
  var y = e.offsetTop;
  if (e.offsetParent) {
    var pos = elementPos(e.offsetParent);
    x += pos.x;
    y += pos.y;
  }
  return {'x': x, 'y': y};
}

// Diese Funktion gibt das Scrollverhalten an.
// Definitionsbereich: [0, 1], Wertebereich: [0, 1]
// Die Funktion gibt aus, zu welchem Zeitpunkt wie weit gescrollt sein soll
function f(t) {
  return (1 - Math.cos(Math.PI * t)) / 2;
}
 
function smoothScroll(id) {
  var e = document.getElementById(id);
  var source_y = (isIE ? document.documentElement.scrollTop : window.scrollY);
  var target_y = (elementPos(e).y - 50);
  scrollStep(0, source_y, target_y)
}

function scrollStep(t, source_y, target_y) {
  if (t > 1) return;
  //$('debug').innerHTML = source_y + '|' + target_y + '|' + f(t);
  window.scrollTo(0, Math.round(source_y + (target_y-source_y) * f(t)));
  setTimeout('scrollStep('+(t+0.03)+', '+source_y+', '+target_y+')', 20); // Timesteps hier ändern
}