$(function() { 
  try { gallerySetup(); }
  catch(err) {}

  attachClickCounting();
});

function attachClickCounting() {
  $(".prod_item a[rel=click]").each(function() {
    var href = '/click/?href='+this.href.replace(/^http:\/\/[^\/]+(.*)$/, "$1");
    if ($.browser.msie) href = encodeURI(href); // IE fix
    this.href = href;
  });
}

function setMenuComment(el,act) {
  var div = el.parentNode.getElementsByTagName('div')[0];
  if (div){
    switch(act) {
      case 'show':
        $removeClass(div,'hidden');
      break;
      case 'hide':
        $addClass(div,'hidden');
      break;
    }
  }
}

function addInput() {
  var max = document.getElementById('iMax').value;
  var countI = document.getElementById('iAdded');
  var count = countI.value*1;

  if (count < max) {
    count ++;

    var oldCur = document.getElementById('curTr');
    var trs = oldCur.parentNode.getElementsByTagName('tr');
    var i = 0;
    var found = false;
    var showNext = false;
    while ((i < trs.length) && !found) {
      if (showNext) {
        oldCur.removeAttribute('id');
        trs[i].setAttribute('id','curTr');
        trs[i].className = trs[i].className.replace(/\s?hidden\s?/g, "");
        found = true;
      }
      if (trs[i].getAttribute('id') == 'curTr') {
        showNext = true;
      }
      i ++;
    }

    countI.value = count;

    if (count >= max) {
      var but = document.getElementById('addTr');
      but.parentNode.removeChild(but);
    }
  } else {
    alert("addInput error!\nWon't add more inputs!");
  } 
}

function insertAfter(parent,node,referenceNode) {
  parent.insertBefore(node,referenceNode.nextSibling);
}
