/*
* $Id: tools.js 19710 2010-11-06 08:52:41Z admin $
*/

// --- DOM-Funktionen ---

function dom_nodelist_to_objectlist(nodelist, record_nodename) // {{{
{
  if(nodelist == null || (typeof(nodelist) != "object" &&
                          typeof(nodelist) != "function") // fuer Safari
    )
  {
    YAHOO.log('dom_nodelist_to_object: parameter error: keine nodelist: typeof='+typeof(nodelist));
    return null;
  }

  var node_a = new Array();

  for (var j = 0; j < nodelist.length ; j++) 
  {
    var node = nodelist.item(j);

    if(!record_nodename || node.nodeName == record_nodename)
    {
      node_a[j] = dom_nodelist_to_object(node);
    }
  }

  return node_a;
} // }}}

function dom_nodelist_to_object(nodelist) // {{{
{
  if(nodelist == null || (typeof(nodelist) != "object" &&
                          typeof(nodelist) != "function") // fuer Safari
    )
  {
    YAHOO.log('dom_nodelist_to_object: parameter error: keine nodelist: typeof='+typeof(nodelist));
    return null;
  }

  var node_o = new Object();
  
  for (var j = 0; j < nodelist.length ; j++) 
  {
    var node = nodelist.item(j);
    //YAHOO.log('dom_nodelist_to_object: name='+node.nodeName+' type='+node.nodeType+'('+node.ELEMENT_NODE+')');
    if(node.nodeType == 1) //node.ELEMENT_NODE)
    {
      //YAHOO.log('dom_nodelist_to_object: element_node: '+node.nodeName+'='+node.firstChild.nodeValue);
      if(node.childNodes.length)
        node_o[node.nodeName] = node.firstChild.nodeValue;
    }
  }

  return node_o;
} // }}}

function find_first_node(domobj, nodename) // {{{
{
  var list = domobj.getElementsByTagName(nodename);

  if(list.length)
  {
    var node = list.item(0);
    return node;
  }

  return null;
} // }}}

function get_node_value(node) // {{{
{
  if(node.nodeType == 1) //node.ELEMENT_NODE)
  {
    //YAHOO.log('get_node_value: element_node: '+node.nodeName+'='+node.firstChild.nodeValue);
    if(node.childNodes.length)
      return node.firstChild.nodeValue;
  }

  return false;
} // }}}

function dom_find_elementnode(nodelist, from_pos) // {{{
{
  if(!nodelist || !nodelist.length)
  {
    YAHOO.log('dom_find_elementnode: invalid parameter nodelist');
    return null;
  }

  if(from_pos >= nodelist.length)
  {
    YAHOO.log('dom_find_elementnode: no length: name='+node.nodeName+' type='+node.nodeType);
    return null;
  }

  for (var j = from_pos; j < nodelist.length ; j++) 
  {
    var node = nodelist.item(j);
    YAHOO.log('dom_find_elementnode: name='+node.nodeName+' type='+node.nodeType+'('+node.ELEMENT_NODE+')');
    if(node.nodeType == 1) //node.ELEMENT_NODE)
    {
//      YAHOO.log('dom_find_elementnode: element_node: '+node.nodeName+'='+node.firstChild.nodeValue);
//      return node;
    }
  }

  return null;
} // }}}

// --- Gadget-Funktionen ---

function gg_get_entryhtml(id, classname, fn_mouseover, fn_mouseout, headtext, detailstext) // {{{
{
  var html = '';
  html += '<div id="'+classname+'_'+id+'" class="'+classname+'" onmouseover="'+fn_mouseover+'('+id+');" onmouseout="'+fn_mouseout+'('+id+');">';
  html += '  <div class="'+classname+'_head">'+headtext+'</div>';
  html += '  <div class="'+classname+'_details">'+detailstext+'</div>';
  //html += detailstext;
  html += '</div>';

  return html;
} // }}}

function gg_get_div(classname, text, id) // {{{
{
  if(id != undefined)
    return '<div id="'+id+'" class="'+classname+'">'+text+'</div>';

  return '<div class="'+classname+'">'+text+'</div>';
} // }}}

function gg_get_span(classname, text) // {{{
{
  return '<span class="'+classname+'">'+text+'</span>';
} // }}}


// --- YUI-Funktionen ---

var yui_module_ids = new Object();
var yui_module     = new Object();
var yui_buttons    = new Object();
var yui_tooltips   = new Object();
var yui_waitpanels = new Object();

function yui_init_module(classname) // {{{
{
  yui_module_ids[classname] = new Array();
  yui_module[classname]     = new Object();
} // }}}

function yui_get_modulhtml(id, classname, headtext, bodytext, foottext) // {{{
{
  var mod_id = classname+'_mod_'+id;

  if(!yui_module_ids[classname])
  {
    yui_init_module(classname);
  }

  yui_module_ids[classname].push(id);

  var html = '';
  html += '<div id="'+mod_id+'" class="'+classname+'_mod yui-module" style="display: none;">';
  html += '  <div class="hd">'+headtext+'</div>';
  html += '  <div class="bd">'+bodytext+'</div>';
  html += '  <div class="ft">'+foottext+'</div>';
  html += '</div>';

  return html;
} // }}}

function yui_build_module(classname) // {{{
{
  for(var i in yui_module_ids[classname])
  {
    var id     = yui_module_ids[classname][i];
    var mod_id = classname+'_mod_'+id;

    // Module zu aufklappen initialisieren
    yui_module[classname][id] = new YAHOO.widget.Module(mod_id, { visible: false });
  }
} // }}}

function yui_show_module(classname, id) // {{{
{
//  YAHOO.log('yui_show_module: '+classname+': '+id);
  for(var i in yui_module[classname])
  {
    var mod = yui_module[classname][i];
    if(i == id)
      mod.show();
    else  
      mod.hide();
  }
} // }}}

function yui_hide_module(classname, id) // {{{
{
//  YAHOO.log('yui_hide_module: '+classname+': '+id);
  var mod = yui_module[classname][id];
  if(mod)
    mod.hide();
} // }}}


function yui_confirm_dialog(id, params) // {{{
{
  var text      = getParam(params, 'text', 'Sicher?');
  var titel     = getParam(params, 'titel', '');
  var container = getParam(params, 'container', 'container');
  var width     = getParam(params, 'width', '300px');
  var yesfn     = getParam(params, 'yesHandler');
  var nofn      = getParam(params, 'noHandler');

  if(!yui_module['yui_confirm_dialog'])
    yui_init_module('yui_confirm_dialog');

  yui_module['yui_confirm_dialog'][id] =
    new YAHOO.widget.SimpleDialog(id, 
                                  { width: width,
                                    fixedcenter: true,
                                    visible: false,
                                    draggable: false,
                                    close: true,
                                    text: text,
                                    icon: YAHOO.widget.SimpleDialog.ICON_HELP,
                                    constraintoviewport: true,
                                    buttons: [ { text:"Yes", handler:function () {if(yesfn) yesfn(); this.hide()}, isDefault:true },
                                               { text:"No",  handler:function () {if(nofn) nofn(); this.hide()} } ]
                                  } );
  if(titel)
    yui_module['yui_confirm_dialog'][id].setHeader(titel);
  yui_module['yui_confirm_dialog'][id].render(container);
  yui_module['yui_confirm_dialog'][id].show();
} // }}}


function yui_init_dialog(id, params) // {{{
{
  var show        = getParam(params, 'show', false);
  var width       = getParam(params, 'width', '600px');
  var validate_fn = getParam(params, 'validate_fn', function () { return true; });
  var success_fn  = getParam(params, 'success_fn',  function (o) { alert('default'); document.innerHTML = o.responseText; });
  var failure_fn  = getParam(params, 'failure_fn',  function (o) { alert("Submission failed: " + o.status); });
  var show_btn    = getParam(params, 'show_btn');
  var argument    = getParam(params, 'argument', {});
  var postmethod  = getParam(params, 'postmethod', 'async');

  if(!yui_module['yui_dialog'])
    yui_init_module('yui_dialog');

  // Instantiate the Dialog
  yui_module['yui_dialog'][id] =
      new YAHOO.widget.Dialog(id, 
                          { width : width,
                            fixedcenter : true,
                            visible : false, 
                            constraintoviewport : true,
                            postmethod : postmethod,
                            buttons : [ { text:"Submit", handler:function() {this.submit()}, isDefault:true },
                                        { text:"Cancel", handler:function() {this.cancel()} } ]
                          });  

  // Validate the entries in the form to require that both first and last name are entered
  yui_module['yui_dialog'][id].validate = validate_fn;

  // Wire up the success and failure handlers
  yui_module['yui_dialog'][id].callback = { success: success_fn, failure: failure_fn, argument: argument};
  
  // Render the Dialog
  yui_module['yui_dialog'][id].render();

  if(show)
    yui_module['yui_dialog'][id].show();

  if(show_btn)
  {
    var btn_id        = getParam(show_btn, 'id');
    var btn_container = getParam(show_btn, 'container');
    var btn_label     = getParam(show_btn, 'label');
    var btn_ckickfn   = getParam(show_btn, 'fn');
    yui_button(btn_id, btn_container, btn_label, btn_ckickfn);
  }
} // }}}

function yui_get_dialog(id) // {{{
{
  if(yui_module['yui_dialog'][id])
    return yui_module['yui_dialog'][id];
  return null;  
} // }}}

function yui_set_dialog_title(id, title) // {{{
{
  yui_module['yui_dialog'][id].setHeader(title);
} // }}}

function yui_set_dialog_argument(id, argument) // {{{
{
  yui_module['yui_dialog'][id].callback.argument = argument;
} // }}}

function yui_show_dialog(id) // {{{
{
  yui_module['yui_dialog'][id].show();
} // }}}

function yui_hide_dialog(id) // {{{
{
  yui_module['yui_dialog'][id].hide();
} // }}}


function yui_button(id, container, label, clickfn) // {{{
{
  if(!container)
  {
    yui_buttons[id] = new YAHOO.widget.Button(id);
  }
  else
  {
    yui_buttons[id] = new YAHOO.widget.Button({ label:label, id:id, container:container });
  }

  if(clickfn)
  {
    yui_buttons[id].on("click", clickfn);
  }
} // }}}

function yui_get_button(id) // {{{
{
  return yui_buttons[id];
} // }}}


function yui_tooltip(id, text, container) // {{{
{
  var o = new Object();

  o.context = id;

  if(text)
    o.text = text;

  if(container)
    o.container = container;

  yui_tooltips[id] = new YAHOO.widget.Tooltip('tt_'+id, o);

} // }}}

function yui_destroy_tooltip(id) // {{{
{
  if(yui_tooltips[id])
  {
    yui_tooltips[id].destroy();
    yui_tooltips[id] = null;
  }
} // }}}

function yui_init_waitpanel(id) // {{{
{
  yui_waitpanels[id] = new YAHOO.widget.Panel("waitpanel_"+id,  
                                              { width: "240px", 
                                                fixedcenter: true, 
                                                close: false, 
                                                draggable: false, 
                                                zindex:4,
                                                modal: true,
                                                visible: false
                                                } 
                                              );
    
  yui_waitpanels[id].setHeader("Loading, please wait...");
  yui_waitpanels[id].setBody("<img src=\"/yui/images/loading.gif\"/>");
  yui_waitpanels[id].render(document.body);
} // }}}

function yui_show_waitpanel(id) // {{{
{
  yui_waitpanels[id].show();
} // }}}

function yui_hide_waitpanel(id) // {{{
{
  yui_waitpanels[id].hide();
} // }}}


// --- sonstige Funktionen ---

function getParam(obj, name, defval) // {{{
{
  if(obj && obj[name])
    return obj[name];
  return defval;  
} // }}}

function html_set_selectoptions(element, option_html) // {{{
{
//  var listener_o = YAHOO.util.Event.getListeners(element);
//  YAHOO.log(listener_o);

  if(element.outerHTML)
  {
    var begin = element.outerHTML.match(/(<select .*?>)/i);
    var end   = element.outerHTML.match(/(<\/select>)/i);
    if(begin && end)
    {
      var s = begin[1] + option_html + end[1];
      element.outerHTML = s;
    }

/*    for(var i=0; i<listener_o.length; i++)
    {
      alert('addListener: '+listener_o[i].type+' '+listener_o[i].fn);
      YAHOO.util.Event.addListener(element, listener_o[i].type, listener_o[i].fn, listener_o[i].obj);
    }*/
  }
  else
  {
    element.innerHTML = option_html;
  }
} // }}}

function html_set_selectoptions_node(element, option_html) // {{{
{
  if(element.outerHTML)
  {
    while (element.childNodes.length) {
      element.removeChild(element.firstChild);
    }
    var theOption = /<option[^>]*>([^<]*)<\/option>/gi;
    var theAttrib = /([^ =]+)=("|')([^"']+)("|')/gi;
    while (opt = theOption.exec(option_html)) {
      var newOption = document.createElement('option');
      //var newData   = document.createTextNode(opt[1]);
      //newOption.appendChild(newData);
      newOption.innerHTML = opt[1];
      while (attr = theAttrib.exec(opt[0])) {
        newOption.setAttribute(attr[1], attr[3]);
      }
      element.appendChild(newOption);
    }
  }
  else {
    element.innerHTML = option_html;
  }  
} // }}}

function find_node_by_attr_name(root_node, search_attr_name, offset, found_count) // {{{
{
  root_node = get_element(root_node);
  if(!root_node)
  {
    YAHOO.log('find_node_by_attr_name: root_node not found: '+root_node);
    return false;
  }

  var count = root_node.childNodes.length;

  if(!offset)
    offset = 0;

  if(!found_count)
    found_count = 0;
  
  for(var i=0; i<count; i++)
  {
    if(root_node.childNodes[i].nodeType != 1)
    {
      // kein Elementknoten
      continue;
    }

    var attr_name = root_node.childNodes[i].getAttributeNode("name");
    if(attr_name)
    {
      if(attr_name.nodeValue.toLowerCase() == search_attr_name.toLowerCase())
      {
        if(found_count >= offset)
        {
          return root_node.childNodes[i];
        }
        found_count++;
      }
    }

    if(root_node.childNodes[i].childNodes.length)
    {
      // Rekursion
      var node = find_node_by_attr_name(root_node.childNodes[i], search_attr_name, offset, found_count);
      if(node)
      {
        return node;
      }
    }
  }

  return false;
} // }}}

function foreach_node(fn_callback, root_node, search_node_name, recursiv) // {{{
{
  root_node = get_element(root_node);
  if(!root_node)
  {
    return false;
  }

  var count = root_node.childNodes.length;

  for(var i=0; i<count; i++)
  {
    if(!search_node_name ||
        search_node_name.toLowerCase() == root_node.childNodes[i].nodeName.toLowerCase())
    {
      if(fn_callback(root_node.childNodes[i]) == false)
      {
        return false;
      }
      if(recursiv)
      {
        if(root_node.childNodes[i].childNodes.length)
        {
          // Rekursion
          foreach_node(fn_callback, root_node.childNodes[i], search_node_name, recursiv);
        }
      }
    }
  }
} // }}}

function get_element(el, log) // {{{
{
  if(typeof el == "string")
  {
    elId = el;
    el = document.getElementById(elId);
    if(!el)
    {
      if(log)
        YAHOO.log('get_element: Element not found: Id='+elId);
      return null;
    }
  }
  return el;
} // }}}

// --- Formular-Funktionen ---

function form_select_reset(el) // {{{
{
  for (i = 0; i < el.length; i++)
    if (el.options[i].defaultSelected == true)
      el.options[i].selected = true;
} // }}}

function form_select_setoption(el, value) // {{{
{
  for (i = 0; i < el.length; i++)
  {
    if (el.options[i].value == value)
      el.options[i].selected = true;
  }
} // }}}

function form_select_getselected(el) // {{{
{
  for (i = 0; i < el.length; i++)
  {
    if(el.options[i].selected)
      return el.options[i].value;
  }
  return false;
} // }}}

// --- einfache "Sanduhr" ---

function initHourGlass(pOurGlass, pDomain) // {{{
{
  if (!pDomain)
    pDomain = this['domain'] ? domain : '';
    
  if (pOurGlass) {
    pOurGlass.style.position="absolute";
    pOurGlass.style.left="0";
    pOurGlass.style.top="0";
    pOurGlass.style.backgroundColor="#000";
    pOurGlass.style.width="100%";
    pOurGlass.style.height="100%";
    pOurGlass.style.margin="0";
    pOurGlass.style.padding="0";
    pOurGlass.style.zIndex="9999";
    pOurGlass.style.opacity = "0.7";
    pOurGlass.style.filter = "alpha(opacity=70)";

    var loadPic = _IG_GetImageUrl('http://'+pDomain+'/images/loading.gif');
    pOurGlass.style.backgroundImage="url('"+loadPic+"')";
    pOurGlass.style.backgroundPosition="center";
    pOurGlass.style.backgroundRepeat="no-repeat";
  }
} // }}}

function showHourGlass(pOurGlass, pHide, pHeight) // {{{
{
  if (pOurGlass) {
    if (pHide) {
      pOurGlass.style.display = "none";
    }
    else {
      if (!pHeight) {
        var parent = pOurGlass.parentNode;
        if (parent) {
          pHeight = parent.offsetHeight+'px';
        }
      }
      pOurGlass.style.height = pHeight;
      pOurGlass.style.display = "block";
    }
  }
} // }}}

function handleSmoothOpen(pID, pOn)
{
  var cont = document.getElementById(pID);
  var fxMove = new Fx.Styles(pID, {
    duration: 300,
    transition: Fx.Transitions.linear
  }).addEvent('onComplete', function(){
    if (pOn == 'On') { cont.style.height = '100%'; }
  });

  cont.style.overflow = 'hidden';
  var oOn  = document.getElementById(pID+'On');
  var oOff = document.getElementById(pID+'Off');
  if (pOn == 'On') {
    
    cont.style.height = '100%';
    cont.style.display = 'block';
    var xHeight = cont.offsetHeight;
    cont.style.height = 0;
    
    fxMove.start({'height': [0, xHeight]});
    if (oOff) {
      oOff.style.display = 'none';
    }
    if (oOn) {
      oOn.style.display = 'inline';
    }
  }
  else if (pOn == 'Off') {
    var xHeight = cont.offsetHeight;
    fxMove.start({'height': [xHeight, 0]});
    if (oOff) {
      oOff.style.display = 'inline';
    }
    if (oOn) {
      oOn.style.display = 'none';
    }
  }
}

function toggleSmoothOpen(pID)
{
  var cont = document.getElementById(pID);
  if (cont && (cont.showOff || cont.style.display == "none")) {
    cont.showOff = false;
    handleSmoothOpen(pID, 'On');
  }
  else if(cont) {
    cont.showOff = true;
    handleSmoothOpen(pID, 'Off');
  }
}

