function showPopup(name, dsc, width, height, _show) {
    if (!width) {
        width = 500;
    }
    if (!height) {
        height = 350;
    }
    var show = false;
    if (typeof(_show) != 'undefined') {
        show = true;
    }
    //alert(typeof(_show)+"\n"+show);

    win = window.open("info_popup.php?name=" + name + "&dsc="+dsc+"&showAP=" + (show ? "true" : "false"), "Foo", "width=" + width + ",height=" + height);
    return false;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  wnd=window.open(theURL,winName,features + ',status=yes,scrollbars=yes');
  wnd.focus();

  return false;
}

function dom_removeAllChildren(element) {
    for (i = element.childNodes.length - 1; i >= 0; i--) {
        element.removeChild(element.childNodes[i]);
    }
}

function dom_firstTextNodeValue(element) {
    var nodeValue = '';
    if (element.firstChild && element.firstChild.nodeType == 3) {
        nodeValue = element.firstChild.nodeValue;
    }
    return nodeValue;
}

var dndContainer = null;
var dndContainerPosX = 0;
var dndContainerPosY = 0;

function dnd_factoryCreateDroppable(container, offsetX, offsetY, width, height, refId) {

    dnd_initDndContainer(container);

    var droppable = document.createElement('b');
    var css       = droppable.style;
    css.position = 'absolute';
    css.left     = (dndContainerPosX + offsetX) + 'px';
    css.top      = (dndContainerPosY + offsetY) + 'px';
    css.width    = width   + 'px';
    css.height   = height  + 'px';

    Element.addClassName(droppable, refId);
    Element.addClassName(droppable, 'dnd_droppable');

    Droppables.add(droppable, {
      onDrop: function(element) {
          dnd_changeDraggable(this.element);
          var id = element.getAttribute('id');
          element.parentNode.removeChild(element);
          this.element.setAttribute('id', id);
          this.element.innerHTML = element.innerHTML;
          }});

    dndContainer.appendChild(droppable);
}

function dnd_initDndContainer(container) {
    if (dndContainer == null) {
        dndContainer     = container;
        dndContainerPosX = Position.cumulativeOffset(dndContainer)[0];
        dndContainerPosY = Position.cumulativeOffset(dndContainer)[1];

        dndContainer.style.top  = dndContainerPosY + 'px';
        dndContainer.style.left = dndContainerPosX + 'px';
    }
}

function dnd_changeDraggable(element) {
  if (element != null && element.innerHTML != '') {

    var parentId = element.parentNode.getAttribute('id');
    var listId   = 'dnd_draggables_' + parseInt(parentId.substring(parentId.lastIndexOf('_') + 1));

    var list = $(listId);
    var item = document.createElement('li');
    item.setAttribute('id', element.getAttribute('id'));
    item.innerHTML = dom_firstTextNodeValue(element);

    dom_removeAllChildren(element);

    element.setAttribute('id', '');
    element.innerHTML = '';
    if (item.innerHTML != '') {
        list.appendChild(item);
        new Draggable(item, {revert: true});
    }
  }
}


var ddContainer = null;
var ddContainerPosX = 0;
var ddContainerPosY = 0;

function dd_factoryCreateDropdownList(container, offsetX, offsetY, width, height, options) {

    dd_initContainer(container);

    var select = dd_factoryCreateDropdownBox(offsetX, offsetY, width, height, options);

//    Element.addClassName(select, refId);
//    Element.addClassName(select, 'dnd_droppable');

    ddContainer.appendChild(select);
}

function dd_initContainer(container) {
    if (ddContainer == null) {
        ddContainer     = container;
        ddContainerPosX = Position.cumulativeOffset(ddContainer)[0];
        ddContainerPosY = Position.cumulativeOffset(ddContainer)[1];

        ddContainer.style.top  = ddContainerPosY + 'px';
        ddContainer.style.left = ddContainerPosX + 'px';
    }
}

function dd_factoryCreateDropdownBox(offsetX, offsetY, width, height, options) {

    var select = document.createElement('select');
    var css       = select.style;
    css.position = 'absolute';
    css.left     = (ddContainerPosX + offsetX) + 'px';
    css.top      = (ddContainerPosY + offsetY) + 'px';
    css.width    = width   + 'px';
//    css.height   = height  + 'px';

    var option = null;
    for (x in options) {
        option = document.createElement('option');
        option.setAttribute('value', options[x]);
        option.innerHTML = x;

        select.appendChild(option);
    }

    return select;
}

var tooltipObjects = new Array();

function showTooltip(anchor, tooltipId) {

    if (!tooltipObjects[tooltipId]) {
        var tooltip = $(tooltipId);
        tooltipObjects[tooltipId] = { element: tooltip,
                                      width:   tooltip.offsetWidth,
                                      height:  tooltip.offsetHeight};
    }

    var elem   = tooltipObjects[tooltipId].element;
    var width  = tooltipObjects[tooltipId].width;
    var height = tooltipObjects[tooltipId].height;

    elem.style.position = 'absolute';
    elem.style.left = (anchor.offsetLeft - width - 5) + 'px';
    elem.style.top  = (anchor.offsetTop  - height - 5) + 'px';
    elem.style.width = width + 'px';
    elem.style.height = height + 'px';

    Element.setOpacity(elem, 0);

    elem.style.visibility = 'visible';

            new Effect.Opacity(elem, {
                    duration: 1.0,
                    transition: Effect.Transitions.linear,
                    from: 0.0, to: 0.9 });

    return false;
}

function hideTooltip(tooltipId) {

    if (tooltipObjects[tooltipId]) {
        new Effect.Opacity(tooltipObjects[tooltipId].element, {
                duration: 0.7,
                transition: Effect.Transitions.linear,
                from: 0.9, to: 0.0 });
    }
    return false;
}


