var $GU = {};

$GU.el = function(id) {
    var el = document.getElementById(id);
    if (!el) alert("no such element with id '" + id + "'");
    return el;
}

// element constructor
$GU.elem = function (tagname, atts, children) {
  var el = document.createElement(tagname);
  if (atts) {
    for (var p in atts) {
       el.setAttribute(p, atts[p]);
    }
  }
  if (children) {
    for (var i=0;i<children.length;i++) {
      el.appendChild(children[i]);
    }
  }
  return el;
}

$GU.json_to_obj = function(json) {
  var js = json.substring(json.indexOf("\/\*")+2, json.lastIndexOf("\*\/"));
  try { 
    return eval("(" + js + ")");
  }
  catch(e) {
    alert("bad json: '" + json + "'");
  }
}

$GU.basename = function(path) { return path.replace( /.*\//, "" ); } 

$GU.dirname = function(path) { return path.replace( /\/[^\/]*$/, ""); }

/*
$GU.dirname = function(path) { 
  var lasts = path.lastIndexOf("/");
  return lasts == -1 ? '' : path.substring(0, lasts);
}
*/

/*
versus
   w3: document.addEventListener('load', func, false) [Opera 7 only recognizes on the document object] 
   IE: window.attachEvent('onload', func)

See discussion at:
  http://simonwillison.net/2004/May/26/addLoadEvent/
  http://www.thefutureoftheweb.com/blog/adddomloadevent

  onload won't run until the images are all loaded.
  a script element right before the </body> would work, but
    apparently getElementById() won't work til onload though you can still walk the DOM?
  a setTimeout of 0 in that same element will work, but then there might be some flickering.
  a script element with defer will work without flickering.

There is also the problem of closures causing leaks.




*/
$GU.add_onload = function(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    };
  }
}

$GU.trim = function(s) {
  if (!s) s = '';
  return s.replace(/^\s+|\s+$/g, '');
} 

$GU.LAST_IDS = new Object();
$GU.show_el = function(id, group) {
  var old_id = $GU.LAST_IDS[group];
  if (old_id) $GU.remove_class(old_id, 'current');
  $GU.add_class(id, 'current');
  $GU.LAST_IDS[group] = id;
}
$GU.remove_class = function(id, cname) {
  var cn = $GU.el(id).className;
  if (cn) $GU.el(id).className = (cn == cname ? '' : cn.replace(' ' + cname,''));
  //alert("remove: element " + id + " now has class " + $GU.el(id).className);
}
$GU.add_class = function(id, cname) {
  var cn = $GU.el(id).className;
  if (cn && cn.match(' ?' + cname)) {}
  else {$GU.el(id).className = cn ? cn + ' ' + cname : cname;}
  //alert("add: element " + id + " now has class " + $GU.el(id).className);
}

$GU.set_selected = function(ddlID, value, change) {
    var ddl = document.getElementById(ddlID);
    for (var i = 0; i < ddl.options.length; i++) {
        if (ddl.options[i].value == value) {
            if (ddl.selectedIndex != i) {
		ddl.selectedIndex = i;
		if (change)
		    ddl.onchange();
            }
            break;
        }
    }
}

var DBG = false;

$GU.write_cookie = function (name, value, hours)
{
  if (DBG) alert("writeCookie(" + name + "," + value + "," + hours + ")");
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  var strCookie = name + "=" + escape(value) + expire + "; path=/";
  document.cookie = strCookie;
}

$GU.read_cookie = function(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  if (DBG) alert("readCookie(" + name + ") = "  + cookieValue);
  return cookieValue;
}
