/*
 * some useful functions
 */

var using_ie = (/MSIE/.test(navigator.userAgent));

/*
 * lightens a color specified as rgb in a string
 * p is a number between 0 and 1, 0 will return color, 1 will return white
 */
function lightenColor(color, p)
{
    try {
        if (p==null) p=0.5;
        
        var c = color.split('(')[1];
        c = c.split(')')[0];
        c = c.split(',');
        var r = parseInt(c[0]);
        var g = parseInt(c[1]);
        var b = parseInt(c[2]);
        
        var i = 1.0-p
        r = (r*i+255*p)|0;
        g = (g*i+255*p)|0;
        b = (b*i+255*p)|0;
        
        return "rgb("+r+","+g+","+b+")";
    }
    catch(e){return "black"}
}

/*
 * attempt to load a javascript source file by appending it to the header.
 * in case more then one file is to be loaded, elem should be a unique identifier for each file
 * elem is used to remove an existing node, in case the script is being reloaded.
 */
function include(url)
{
  var js=document.createElement('script');
  js.setAttribute("type","text/javascript");
  js.setAttribute("src", url);
  document.getElementsByTagName("head")[0].appendChild(js);
}

/*
 * get the absolute page offset of an html element
 * returns an object with an x and y value
 */
function getPageOffset(obj)
{
  var x=y=0;
  while(obj)
  {
    x += obj.offsetLeft;
    y += obj.offsetTop;
    obj = obj.offsetParent;
  }
  return {x:x,y:y};
}

/* get the x and y coordinates of a mouse event */
function getEventPosition(event)
{
    if (event==null) event = window.event;
    var x = (event.pageX)?event.pageX:(event.clientX+document.body.scrollLeft);
    var y = (event.pageY)?event.pageY:(event.clientY+document.body.scrollTop);
    return {x:x,y:y};
}

/* ajax for IE */
if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
  throw new Error( "This browser does not support XMLHttpRequest." )
};

/* make an ajax call, pass data as a string to callback function */
function getDataWithCallback(url,callback)
{
    var client = new XMLHttpRequest();
    client.onreadystatechange = function(){if (client.readyState == 4)callback(client.responseText);};
    client.open("GET", url, true);
    client.send(null);
}
