function $(id) {
  return document.getElementById(id);
}

var tp = {};
tp.rows = {
  row1:{
    display:null,
    content:[],
    displayIndex:0,
    reachedLastImage:false,
    reachedFirstImage:true
  }
};
tp.rows.printThumb = function(thumb,offset) {
  var mT = Math.floor((145 - thumb.thumb_y) / 2);
  var mB = 145 - thumb.thumb_y - mT;
  var mL = Math.floor((115 - thumb.thumb_x) / 2);
  var mR = 115 - thumb.thumb_x - mL;
  var d = '';
  d += "<div class='thumb' style='margin: "+mT+"px "+mR+"px "+mB+"px "+mL+"px;'>";
  d += "<div class='shadow_rt'>";
  d += "<div class='shadow_rb' style='width:"+(thumb.thumb_x - 0 + 5)+"px; height:"+(thumb.thumb_y - 0 + 5)+"px;'>";
  d += "<div class='shadow_lb' style='width:"+(thumb.thumb_x - 0 + 5)+"px; height:"+(thumb.thumb_y - 0 + 5)+"px;'>";
  d += "<a class='a_img' href=\""+tp.rows.printShowImageLinkProfi(thumb.imageid,offset)+
       "\"><img src=\"/user/"+thumb.userid+"/thumbs/"+thumb.imageuid+".jpg\" title=\""+thumb.title+
       "\" alt=\""+thumb.title+"\" onmouseout=\"magnifier.hide()\" onmouseover=\"magnifier.select("+
       thumb.thumb_x*3+","+thumb.thumb_y*3+",'/user/"+thumb.userid+"/files/"+thumb.imageuid+"5.jpg');\" /></a>";
  d += "</div>";
  d += "</div>";
  d += "</div>";
  d += "</div>";
  return d;
};
tp.rows.printShowImageLinkProfi = function(imageid,offset) {
  return "/toonagent/showimage?imageid="+imageid+'&offset='+offset;
};
tp.rows.show = function(row,ind) {
  tp.rows[row].displayIndex += ind;
  if (tp.rows[row].displayIndex < 0) {
    tp.rows[row].displayIndex = 0;
  }
  if (tp.rows[row].displayIndex >= tp.rows[row].content.length - 1) {
    tp.rows[row].displayIndex -= ind;
    return;
  }
  if (!tp.rows[row].reachedLastImage && !ajax.activeCall && tp.rows[row].content.length - 1 - tp.rows[row].displayIndex < 20) {
    ajax.doRequest('/toonagent/calls/indexAjax?load[0]='+row+'&offset[0]='+tp.rows[row].content[(tp.rows[row].content.length - 1)].date+'&direction[0]=1&print[0]=0',false);
  }
  if (!tp.rows[row].reachedFirstImage && !ajax.activeCall && tp.rows[row].displayIndex < 20) {
    ajax.doRequest('/toonagent/calls/indexAjax?load[0]='+row+'&offset[0]='+tp.rows[row].content[0].date+'&direction[0]=2&print[0]=0',false);
  }
  $('reiter1Content').innerHTML = '';
  var d = '';
  for (var ende = tp.rows[row].displayIndex + 25, i = tp.rows[row].displayIndex; i < ende; i++) {
    if (tp.rows[row].content[i]) {
      d += tp.rows.printThumb(tp.rows[row].content[i],i);
    }
  }
  $('reiter1Content').innerHTML = d;
  ajaxnavi.setHash('#load[0]='+row+'&offset[0]='+tp.rows[row].content[tp.rows[row].displayIndex].date+'&direction[0]=3&print[0]=1');
};

var prosearch = {};
prosearch.categories = [];
prosearch.search = [];
prosearch.initPage = function() {
  var d = '';
  var cat;
  d += "<table>";
  for (var id in prosearch.categories) {
    if (prosearch.categories[id].parentid == 0) {
      cat = prosearch.categories[id];
      d += "<tr>";
      d += "<td id='cat_"+id+"' class='classnormal'>"+cat.category+"&nbsp;</td>";
      d += "<td>";
      d += "<a href=\"javascript:void(prosearch.addCategory("+id+"));\"><img src=\"/toonagent/misc/pics/icons/ps_plus.gif\" alt=\"add to selection\" /></a> <a href=\"javascript:void(prosearch.showSubcategory("+id+"));\"><img src=\"/toonagent/misc/pics/icons/ps_arrow.gif\" alt=\"open subcategories\" /></a>";
      d += "</td>";
      d += "</tr>";
    }
  }
  d += "</table>";

  $('categories').innerHTML += d;

  d = '';
  d += '<b>Auswahl</b><br />';
  $('categoriesSearch').innerHTML = d;
};
prosearch.showSubcategory = function(catid) {
  var d = '';
  var cat;
  var classe = '';
  d += "<table>";
  for (var id in prosearch.categories) {
    if (prosearch.categories[id].parentid == catid) {
      cat = prosearch.categories[id];
      if (!prosearch.search[id]) {
        classe = 'searchnormal';
      } else {
        classe = 'searchselected';
      }
      d += "<tr>";
      d += "<td id='cat_"+id+"' class='"+classe+"'>"+cat.category+"&nbsp;</td>";
      d += "<td>";
      d += "<a href=\"javascript:void(prosearch.addCategory("+id+"));\"><img src=\"/toonagent/misc/pics/icons/ps_plus.gif\" alt=\"add to selection\" /></a>";
      d += "</td>";
      d += "</tr>";
    }
  }
  d += "</table>";
  $('subcategories').innerHTML = d;
};
prosearch.addCategory = function(catid) {
  var s = prosearch.search;
  s[catid] = 1;
  prosearch.updateCategoriesSearch();
};
prosearch.updateCategoriesSearch = function() {
  var d = '<b>Auswahl</b><br />';
  var cat;
  for (var id in prosearch.search) {
    cat = prosearch.categories[id];
    d += cat.category+"&nbsp;<a href=\"javascript:void(prosearch.removeCategory("+id+"));\"><img src=\"/toonagent/misc/pics/icons/ps_minus.gif\" alt=\"remove from selection\" style=\"vertical-align: text-bottom;\" /></a>&nbsp;&nbsp;<br />";
    if ($('cat_'+id)) {
      $('cat_'+id).className = 'searchselected';
    }
  }
  $('categoriesSearch').innerHTML = d;
};
prosearch.removeCategory = function(catid) {
  delete prosearch.search[catid];
  if ($('cat_'+catid)) {
    $('cat_'+catid).className = 'searchnormal';
  }
  prosearch.updateCategoriesSearch();
};
prosearch.checkLegal = function() {
  var c;
  var s = [];//Suchdaten
  for (var id in prosearch.search) {//Alle parents hinzufügen
    c = prosearch.categories[id];
    if (c.parentid != 0) {
      prosearch.search[c.parentid] = 1;
      s[c.parentid] = '';
    } else {
      s[id] = '';
    }
  }
  for (var id in prosearch.search) {//Suche komplettieren
    c = prosearch.categories[id];
    if (c.parentid != 0) {
      s[c.parentid] += ','+id;
    }
  }
  for (var id in s) {//Suche säubern
    s[id] = s[id].substr(1);
    domHelpers.addElement($('prosearchFormDiv'),'input',{'type':'hidden','name':'categories['+id+']','value':s[id]},{});
  }

  var unit = '';
  for (var i = 0, end = $('prosearchForm').unit.length; i < end; i++) {
    if ($('prosearchForm').unit[i].checked) {
      unit = $('prosearchForm').unit[i].value;
      break;
    }
  }

  switch (unit) {//original_x/y in Pixel umrechnen, Komma in Punkt switchen
  case 'cm':
    $('prosearchForm').original_x.value = parseInt($('prosearchForm').original_x.value.replace(',','.') / 2.54 * 300, 10);
    $('prosearchForm').original_y.value = parseInt($('prosearchForm').original_y.value.replace(',','.') / 2.54 * 300, 10);
    break;
  case 'zoll':
    $('prosearchForm').original_x.value = parseInt($('prosearchForm').original_x.value.replace(',','.') * 300, 10);
    $('prosearchForm').original_y.value = parseInt($('prosearchForm').original_y.value.replace(',','.') * 300, 10);
    break;
  default:
    $('prosearchForm').original_x.value = parseInt($('prosearchForm').original_x.value,10);
    $('prosearchForm').original_y.value = parseInt($('prosearchForm').original_y.value,10);
    break;
  }
  return true;
};

var recentsearch = {};
recentsearch.state = false;
recentsearch.items = [];
recentsearch.selectedItem = null;
recentsearch.init = function() {
  if (recentsearch.state) {
    recentsearch.clear();
    return;
  }
  recentsearch.state = true;
  $('layer_holder').innerHTML += "<div id='recentSearchDiv' class='recentSearch'></div>";
  var pos = objHelpers.getObjPos($('recentSearchSelect'));
  pos.r = crossBrowser.getPageWidth() - pos.l - $('recentSearchSelect').offsetWidth;
  pos.b = pos.t + $('recentSearchSelect').offsetHeight;
  $('recentSearchDiv').style.right = pos.r + 'px';
  $('recentSearchDiv').style.top = pos.b + 'px';
  var d = '';
  for (var id in recentsearch.items) {
    d += "<div id='recentSearchEntry"+id+"' class='recentSearchNormal' onmouseover=\"recentsearch.updateState("+id+",true);\" onmouseout=\"recentsearch.updateState("+id+",false);\">"+recentsearch.items[id].feedback+"</div>";
  }
  $('recentSearchDiv').innerHTML = d;
  $('preventArrowKeyScroll').setAttribute("autocomplete","off");
  $('preventArrowKeyScroll').focus();
  crossBrowser.addEvent(window.document,'mousedown',recentsearch.click);
  crossBrowser.addEvent(window.document,'keydown',recentsearch.key);
};
recentsearch.clear = function() {
  recentsearch.state = false;
  domHelpers.delElement('recentSearchDiv');
  crossBrowser.removeEvent(window.document,'mousedown',recentsearch.click);
  crossBrowser.removeEvent(window.document,'keydown',recentsearch.key);
  $('preventArrowKeyScroll').blur();
  $('recentSearchSelect').blur();
};
recentsearch.updateState = function(id,state) {
  if (state) {
    recentsearch.selectedItem = id;
    for (var id1 in recentsearch.items) {
      $('recentSearchEntry'+id1).className = 'recentSearchNormal';
    }
    $('recentSearchEntry'+id).className = 'recentSearchSelected';
  } else {
    $('recentSearchEntry'+id).className = 'recentSearchNormal';
  }
};
recentsearch.click = function(e) {
  var mxy = crossBrowser.getMousePos(e);
  var oxy = objHelpers.getObjPos($('recentSearchDiv'));
  oxy.r = oxy.l + $('recentSearchDiv').offsetWidth;
  oxy.b = oxy.t + $('recentSearchDiv').offsetHeight;
  var bxy = objHelpers.getObjPos($('recentSearchSelect'));
  bxy.r = bxy.l + $('recentSearchSelect').offsetWidth;
  bxy.b = bxy.t + $('recentSearchSelect').offsetHeight;
  if (mxy.x >= bxy.l && mxy.x <= bxy.r && mxy.y >= bxy.t && mxy.y <= bxy.b) {
    return;
  }
  if (mxy.x < oxy.l || mxy.x > oxy.r || mxy.y < oxy.t || mxy.y > oxy.b) {
    recentsearch.clear();
  } else {
    window.location.href = recentsearch.items[recentsearch.selectedItem].query+'&recentsearch=1';
  }
};
recentsearch.key = function(e) {
  // 38 up, 40 down, 9 tab, 13 Enter
  e = e || event;
  var char = e.keyCode;
  switch (char) {
  case 38:
    recentsearch.searchPN(-1);
    break;

  case 40:
    recentsearch.searchPN(1);
    break;

  case 9:
    recentsearch.clear();
    break;

  case 13:
    window.location.href = recentsearch.items[recentsearch.selectedItem].query+'&recentsearch=1';
    break;
  }
};
recentsearch.searchPN = function(pn) {
  //Check ob man ggf nur geöffnet hat und Down drückt
  if (recentsearch.selectedItem == null && pn == 1) {
    for (var id in recentsearch.items) {
      recentsearch.updateState(id,true);
      break;
    }
    return;
  }
  var index = 0;
  var ids = new Array();
  for (var id in recentsearch.items) {
    ids.push(id);
    if (id == recentsearch.selectedItem) {
      index = ids.length - 1;
    }
  }
  if ((pn == -1 && index != 0) || (pn == 1 && index != ids.length - 1)) {
    recentsearch.updateState(ids[index],false);
    recentsearch.updateState(ids[(index+pn)],true);
  }
};

function check_js_cookie(dir) {
  var cookiename = 'toonpool_'+dir;
  var coo = document.cookie;
  if (coo.indexOf(cookiename) != -1) {
    var p = $('header_tb_top');
    var c = $('js_cookie_warning');
    p.removeChild(c);
  }
}

var objHelpers = {};
objHelpers.getCenterObj = function(p) {
  var pos = {l:0,t:0};
  var c = crossBrowser;
  var dim = {sX:c.getScrollX(),sY:c.getScrollY(),
             wX:c.getWindowInnerWidth(),wY:c.getWindowInnerHeight()};
  if (dim.wX > p.offsetWidth) {
    pos.l = Math.floor((dim.wX - p.offsetWidth) / 2 + dim.sX);
  }
  if (dim.wY > p.offsetHeight) {
    pos.t = Math.floor((dim.wY - p.offsetHeight) / 2 + dim.sY);
  }
  return pos;
};
objHelpers.getObjPos = function(obj) {
  var pos = {l:0, t:0};
  do {
    pos.l += obj.offsetLeft;
    pos.t += obj.offsetTop;
  } while (obj = obj.offsetParent);
  return pos;
};

var domHelpers = {};
/* Fügt ein Element hinzu
 * parent: Das Elternelement, an dass das neue Element angehängt werden soll (Element)
 * tag: Was für ein Element es sein soll, 'text' erzeugt einen Text-Knoten (String)
 * attributes: Mögliche Atrribute für das neue Element, oder Text-Inhalt für Text-Knoten (Objekt,String)
 * childs: Mögliche Kinder Syntax: tag:{attributes:'',childs:{}} (Objekt)
 */
domHelpers.addElement = function(parent,tag,attributes,childs) {
  if (tag != 'text') {
    var newElement = document.createElement(tag);
    for (var a in attributes) {
      newElement.setAttribute(a,attributes[a]);
    }
  } else {
    var newElement = document.createTextNode(attributes);
  }
  for (var c in childs) {
    domHelpers.addElement(newElement,c,childs[c].attributes,childs[c].childs);
  }
  parent.appendChild(newElement);
};
/* Löscht ein Element
 * id: Die id des zu löschenden Elements (String)
 */
domHelpers.delElement = function(id) {
  if (!$(id) || !$(id).parentNode) {
    return;
  }
  $(id).parentNode.removeChild($(id));
};

function checkEmail(email) {
  var suche = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,4}/i;
  if (suche.exec(email) != null) {
    return true;
  } else {
    return false;
  }
}

function changeSelect(id,sel,val) {
  if (val == 0) {
    return;
  }
  var f = $(id);
  var anzS = f[sel].options.length;
  for (var i = 0; i < anzS; i++) {
    if (f[sel].options[i].value == val) {
      f[sel].options[i].selected = true;
      break;
    }
  }
}

var indexartist = {};
indexartist.container = 'indexArtistContainer';
indexartist.oldContent = '';
indexartist.show = function(d) {
  indexartist.oldContent = $(indexartist.container).innerHTML;
  var h = '';
  h += '<img src="/user/'+d['userid']+'/files/'+d['imageuid']+'5.jpg" />';
  h += '<br />';
  h += '<h3>'+d['r']+'</h3>';
  h += '<br />';
  h += '<b>'+d['n']+'</b>';
  h += '<br />';
  h += d['c'];
  h += '<br />';
  h += d['a']+' Motive im Toon Agent';
  $(indexartist.container).innerHTML = h;
};
indexartist.hide = function() {
  $(indexartist.container).innerHTML = indexartist.oldContent;
}

//showimage Opener
if (window.location.href.indexOf('/showimage') == -1) {
  window.name = 'toonagentOpener';
}