var p_container = null;

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent; 
		}
	}
	return curleft;
}
function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent; 
		}
	}
	return curtop;
}
function showTip(e, text, l) {
	if (!e) var e = window.event;
	if (!obj) var obj = (e.target) ? e.target : e.srcElement;
	p_container.innerHTML = text;
	xPos = findPosX(obj) + obj.offsetWidth;
	yPos = findPosY(obj) + obj.offsetHeight + 2;
	if (xPos + p_container.offsetWidth > document.body.clientWidth) xPos = document.body.clientWidth - p_container.offsetWidth - 2;
	p_container.style.left = xPos + 'px';
	p_container.style.top = yPos + 'px';
	p_container.style.visibility = 'visible';
	if (!obj) return;
	obj.onmouseout = destroyTip;
}
function destroyTip(e) {
	if (!e) var e = window.event;
	if (!obj) var obj = (e.target) ? e.target : e.srcElement;
	p_container.style.visibility = 'hidden';
	p_container.innerHTML = '';
	if (!obj) return;
	obj.onmouseout = null;
}
function initTips() {
	p_container = document.createElement('div');
	p_container.className = 'tooltip';
	if (!p_container) return;
	p_container.style.visibility = 'hidden';
	p_container.style.zIndex = '100';
	p_container.style.position = 'absolute';
	document.body.appendChild(p_container);
}


function initAJAX()
{
	var o = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
	o = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	try {
	o = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
	o = false;
	}
	}
	@end @*/
	if (!o && typeof XMLHttpRequest!='undefined')
	{
	
		o = new XMLHttpRequest();
	}

	return o;
}

function LoadIntoElement( url, element_id, text )
{
	var X = initAJAX();
    var element = document.getElementById(element_id);
	var text;
	
	if (text == null)
	{
		text = 'Loading';
	}

    element.innerHTML = text;
	
	X.open("GET", url);
	X.send(null);

	X.onreadystatechange = function()
	{
        if ( X.readyState == 4 && X.status == 200 )
		{
            element.innerHTML = X.responseText;
        }
    }	
}

function ElementVal(element_id)
{
	var element = document.getElementById(element_id);

	return element.value;
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title)
		{
		  a.disabled = false;
			
			var title = getActiveStyleSheet();
			createCookie("style", title, 365);
		}
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();

  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

var defaultWhatText = "What?";
var defaultWhereText = "Where? (optional)";
	

// this makes sure we have our search form on focus
function init() {
	//document.getElementById('q').focus();
	if (document.getElementById('q').value == '') {
		document.getElementById('q').value = defaultWhatText;
		document.getElementById('w').value = defaultWhereText;
		document.getElementById('q').style.color='#999';
		document.getElementById('w').style.color='#999';
	}
}

function wqFocus() {
        var q = document.getElementById('q');
        var w = document.getElementById('w');
        if(q.value.toLowerCase() == '' || q.value.toLowerCase() == defaultWhatText.toLowerCase()) {
		document.getElementById('q').style.color='black';
		q.value = '';
	}
        if(w.value.toLowerCase() == '' || w.value.toLowerCase() == defaultWhereText.toLowerCase()) {
                document.getElementById('w').style.color='black';
		w.value = '';
        }

}

addEvent(window, 'load', init);
