function findChildTag(el, tag) {
  if (!tag) tag = 'LI';
  p = el.childNodes
  c = false;
  
  for (i=0; i < p.length; i++) {
    try {
      if (p[i].nodeName == tag) {
        c = p[i];
        break;
      }
    } catch (error) {}
  }
  if (!c)
    return false;

  return c;
}

function findNextTag(el, tag) {
  if (!tag) tag = 'LI';
  p = el.parentNode.childNodes;
  flag = false;
  n = false;

  for (i=0; i < p.length; i++) {
    if (flag) {
      try {
        if (p[i].nodeName == tag) {
          n = p[i];
          break;
        }
      } catch (error) {}
    }
    
    if (el == p[i]) {
      flag = true;
    }
  }
  if (!n)
    return false;

  return n;
}

function findPreviousTag(el, tag) {
  if (!tag) tag = 'LI';
  e = el;

  while (e.previousSibling) {
    if (e.previousSibling.nodeName == tag)
      return e.previousSibling;
    else
      e = e.previousSibling;
  }
      
  return false;
}

function moveListItemLeft(el) {
  el = $(el);
  el.id = '';

  current = el.parentNode;
  pnode = current.parentNode;

  previous = current.parentNode.parentNode;

  if (current.className.indexOf('odd') != -1) 
    current.className = current.className.replace('odd', 'even'); 
  else
    current.className = current.className.replace('even', 'odd'); 

  
  if (previous.nodeName == 'LI') {
    next = findNextTag(previous);
    if (next)
      next.parentNode.insertBefore(current, next);
    else
      previous.parentNode.appendChild(current);

    new Effect.Highlight(findChildTag(current, 'A'));

    if (!findChildTag(pnode)) {
      img = findChildTag(pnode.parentNode, 'IMG');
      img.style.display = 'none';
    }

    return true;
  }

  new Effect.Highlight(findChildTag(current, 'A'), {startcolor:'#ff9999', restorecolor:'#ffffff'});
  return false;
}

function moveListItemRight(el) {
  el = $(el);
  el.id = '';

  current = el.parentNode;

  previousli = findPreviousTag(current);

  if (previousli) {
    nextul = findChildTag(previousli, 'UL');

    if (nextul) {
      nextul.appendChild(current);

      img = findChildTag(previousli, 'IMG');
      if (img) {
        img.style.display = '';
        openTree(img);
      } else {
        alert('noimg');
      }

      new Effect.Highlight(findChildTag(current, 'A'));
      return true;
    } else {
      alert('noUL');
    }
  }

  new Effect.Highlight(findChildTag(current, 'A'), {startcolor: '#ff9999'});
  return false;
}

function moveListItemUp(el) {
  el = $(el);
  el.id = '';

  current = el.parentNode;
  previous = findPreviousTag(current);

  if (previous) {
    previous.parentNode.insertBefore(current, previous);
    new Effect.Highlight(findChildTag(current, 'A'));

    return true;
  }


  new Effect.Highlight(findChildTag(current, 'A'), {startcolor: '#ff9999'});
  return false;
}

function moveListItemDown(el) {
  el = $(el);
  el.id = '';

  current = el.parentNode;
  next = findNextTag(current);

  if (next) {
    current.parentNode.insertBefore(next, current);
    new Effect.Highlight(findChildTag(current, 'A'));
    
    return true;
  }

  new Effect.Highlight(findChildTag(current, 'A'), {startcolor: '#ff9999'});
  return false;
}

function submitListChange(el, direction, href) {
  el.id = 'item_to_move';

  if (!href) href = '/admin/menus/:direction/:menu';
  
  if (direction == 'UP')
    href = href.replace(':direction', 'move_up');
  else if (direction == 'DOWN')
    href = href.replace(':direction', 'move_down');
  else if (direction == 'LEFT')
    href = href.replace(':direction', 'move_left');
  else if (direction == 'RIGHT')
    href = href.replace(':direction', 'move_right');
  
  //href = href.replace(':menu', encodeURIComponent(el.name));
  href = href.replace(':menu', el.name);

  new Ajax.Request(href, {asynchronous:true, evalScripts:true, method:'post'});
}

function toggleTree(el) {
  if (el.name == 'open') {
    closeTree(el);
  } else {
    openTree(el);
  }

  return false;
}

function openTree(el) {
  el.name = 'open';
  el.src = '/images/treenode_expand_minus.gif';
  current = el.parentNode;
  list = findChildTag(current, 'UL');
  list.style.display = '';
}

function closeTree(el) {
  el.name = 'closed';
  el.src = '/images/treenode_expand_plus.gif';
  current = el.parentNode;
  list = findChildTag(current, 'UL');
  list.style.display = 'none';
}
  

