// A simple (heh) javascript library with functions for the following:

// tracking mouse motion
// getting element coordinates
// making xmlhttprequest calls
// setting cookies
// crossbrowser event handling
// scrolling to an element

// 

var xmlhttp = null;
var mouse_x = 0;
var mouse_y = 0;
var page_loaded = false;
var rightnow = new Date();

addLoadEvent(set_page_loaded);

function set_page_loaded() {
  page_loaded = true;
}

function addLoadEvent(func) {
  if (window.addEventListener)
    window.addEventListener("load",func,false);
  else if (document.addEventListener)
    document.addEventListener("load",func,false);
  else if (window.attachEvent)
    window.attachEvent("onload",func);
  else if (document.attachEvent)
    document.attachEvent("onload",func);
}


function window_x() {
  if (window.screenX)
    return window.screenX
  else if (window.screenLeft)
    return window.screenLeft;
}

function window_y() {
  if (window.screenY)
    return window.screenY
  else if (window.screenTop)
    return window.screenTop;
}

function mousemove(e) { 
  if (page_loaded)
  { 
    if (e && e.clientX && window.scrollX) { // Moz
      mouse_x = e.clientX + window.scrollX;
      mouse_y = e.clientY + window.scrollY;
      event_target = e.target;
    }
    else if (window.event && document.documentElement.scrollLeft) { // IE
      if (document.documentElement && document.documentElement.scrollTop)   // Explorer 6 Strict
      {
        mouse_x = window.event.clientX + document.documentElement.scrollLeft - 4;
        mouse_y = window.event.clientY + document.documentElement.scrollTop - 4;
      }
      else if (document.body) // all other Explorers
      {
        mouse_x=window.event.clientX+document.body.scrollLeft-4;
        mouse_y=window.event.clientY+document.body.scrollTop-4;
      }
 
      mouse_window_x = window.event.clientX;
      mouse_window_y = window.event.clientY;
    }
  }
}





/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setcookie(name, value, expires, path, domain, secure)
{
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function XBrowserAddHandler(target,eventName,handlerName) { 
  if ( target.addEventListener ) { 
    target.addEventListener(eventName, function(e){target[handlerName](e);}, false);
  } else if ( target.attachEvent ) { 
    target.attachEvent("on" + eventName, function(e){target[handlerName](e);});
  } else { 
    var originalHandler = target["on" + eventName]; 
    if ( originalHandler ) { 
      target["on" + eventName] = function(e){originalHandler(e);target[handlerName](e);}; 
    } else { 
      target["on" + eventName] = target[handlerName]; 
    } 
  } 
}

function scrollto(el, xoffset, yoffset) {
  var x = getRealLeft(el) + xoffset;
  var y = getRealTop(el) + yoffset;
  window.scrollTo(x, y);
}


function get_property(p, k, d) {
  if (p == null) return d;
	return (p[k]) ? p[k] : d;
}


function xmlhttp_init() {
  //alert(" (xmlhttp_init) start");
  //if (xmlhttp) return;
  
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }

  if (!xmlhttp && typeof XMLHttpRequest!='undefined')
    xmlhttp = new XMLHttpRequest();

  //alert(" (xmlhttp_init) done");
}

function getRealLeft(imgElem) {
  xPos = imgElem.offsetLeft;
  tempEl = imgElem.offsetParent;
    //alert("element " + imgElem.id + "\\nparent "+ tempEl.id);
    //alert("element " + imgElem + "\\nparent "+ tempEl);
    while (tempEl != null) {
      xPos += tempEl.offsetLeft;
      tempEl = tempEl.offsetParent;
    }
  return xPos;
}

function getRealTop(imgElem) {
  yPos = imgElem.offsetTop;
  tempEl = imgElem.offsetParent;
  while (tempEl != null) {
      yPos += tempEl.offsetTop;
      tempEl = tempEl.offsetParent;
    }
  return yPos;
}

function removename(el, name) {
  var i, curList, newList;
  // Remove the given class name from the className property of the element.
  newList = new Array();
  curList = el.className.split(" ");
  for (i = 0; i < curList.length; i++)
    if (curList[i] != name)
      newList.push(curList[i]);
  el.className = newList.join(" ");
}

function blink(el, times, onoff)
{
  if (times==0) 
  {
    if (onoff == 0 && document.getElementById(el).className.match(/blink/))
      removename (document.getElementById(el),"blink");
    if (onoff == 1 && !document.getElementById(el).className.match(/blink/))
      document.getElementById(el).className += " blink";
    return;
  }
  
  if (document.getElementById(el).className.match(/blink/))
    removename (document.getElementById(el),"blink");
  else
    document.getElementById(el).className += " blink";
  
  setTimeout("blink('"+el+"',"+(times-1)+", "+onoff+")", 100);
  
}


function debug(text) {
	if (!document.getElementById('debug_info')) return;
	//if (!text || text == '') text = "null";
	text += '';

	document.getElementById('debug_info').innerHTML += text+'<br/>';
}

function contains(a,t) {
  for (var i=0;i<a.length;i++)
    if (a[i] == t)
      return true;
  return false;
}


