var posX = 0;
var posY = 0;

/**
 * Fetch and display program description
 */
function displayDesc(obj, id)
{
	if (!this.req)
	{
		posX = Util.pageX(obj);
		posY = Util.pageY(obj);

		if ((posX + 400) > Util.clientWidth())
		{
			posX = Util.clientWidth() - 400;
		}
		
		new net.ContentLoader("/index.php?do=callback", displayDesc, null, "POST", "&what=program&programId=" + id);
	}
	else
	{
		eval("var data="+this.req.responseText);
		showInfo(data);
	}
}

function hideInfo()
{
	if (document.body.infoDiv)
	{
	  document.body.removeChild(document.body.infoDiv);
	  document.body.infoDiv = null;
	}
}

function showInfo(show)
{
	if (!document.body.infoDiv)
	{
		document.body.infoDiv = document.createElement("div");
		document.body.infoDiv.className = "info";
		document.body.infoDiv.onclick = hideInfo;
		document.body.appendChild(document.body.infoDiv);
	}

	document.body.infoDiv.style.top = (posY + 14) + "px";
	document.body.infoDiv.style.left = posX + "px";
	document.body.infoDiv.innerHTML = "<b>"+ show.title +"</b><p>"+ show.description +"</p><i>Längd "+ show.length +" minuter</i>";
}

//Register event
window.onresize = function()
{
	hideInfo();
}

/**
 * Util
 */
//Object instance
var Util = new UtilMgr();

//The base object
function UtilMgr()
{
}

//getElement
UtilMgr.prototype.getElement = function(e)
{
  if (typeof(e) == 'string') 
  {
    if (document.getElementById)
    {
    	e = document.getElementById(e);
    }
    else if (document.all) 
    {
    	e = document.all[e];
    }
    else 
    {
    	e = null;
    }
  }
  return e;
};

//isDef
UtilMgr.prototype.isDef = function()
{
  for (var i = 0; i < arguments.length; ++i)
  {
  	if (typeof(arguments[i]) == 'undefined') 
  	{
  		return false;
    }
  }
  return true;
};

//pageX
UtilMgr.prototype.pageX = function(e)
{
  if (!(e = this.getElement(e))) 
  {
  	return 0;
  }

  var x = 0;
  while (e) 
  {
    if (this.isDef(e.offsetLeft))
    {
    	x += e.offsetLeft;
    }
    e = this.isDef(e.offsetParent) ? e.offsetParent : null;
  }
  return x;
};

//pageY
UtilMgr.prototype.pageY = function(e)
{
	if (!(e = this.getElement(e)))
	{
		return 0;
	}

	var y = 0;
	while (e)
	{
		if (this.isDef(e.offsetTop))
		{
			y += e.offsetTop;
		}
		e = this.isDef(e.offsetParent) ? e.offsetParent : null;
	}
	return y;
};

UtilMgr.prototype.clientWidth = function()
{
	var v = 0, d = document, w = window;
	if (d.compatMode == 'CSS1Compat' && !w.opera && d.documentElement && d.documentElement.clientWidth)
	{
		v = d.documentElement.clientWidth;
	}
	else if (d.body && d.body.clientWidth)
	{
		v = d.body.clientWidth;
	}
	else if (xDef(w.innerWidth, w.innerHeight, d.height))
	{
		v = w.innerWidth;
		if (d.height > w.innerHeight)
		{
			v -= 16;
		}
	}
	return v;
};


/**
 * ContentLoader
 */
var net = new Object();

net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING = 1;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;


net.ContentLoader = function(url, onload, onerror, method, params, contentType)
{
  this.req = null;
  net.currentLoader = this;
  this.onload = onload;
  this.onerror = (onerror) ? onerror : this.defaultError;
  this.loadXMLDoc(url, method, params, contentType);
}

net.ContentLoader.prototype.loadXMLDoc = function(url, method, params, contentType)
{
  if (!method)
  {
    method = "GET";
  }

  if (!contentType && method == "POST")
  {
    contentType = 'application/x-www-form-urlencoded';
  }

  if (window.XMLHttpRequest)
  {
    this.req = new XMLHttpRequest();
  } 
  else if (window.ActiveXObject)
  {
    this.req = new ActiveXObject("Microsoft.XMLHTTP");
  }

  if (this.req)
  {
    try
    {
      var loader = this;
      this.req.onreadystatechange = function()
      {
        net.ContentLoader.onReadyState.call(loader);
      }
      this.req.open(method, url, true);

      if (contentType)
      {
        this.req.setRequestHeader('Content-Type', contentType);
      }

      this.req.send(params);
    }
    catch (err)
    {
      this.onerror.call(this);
    }
  }
}

net.ContentLoader.onReadyState = function()
{
  var req = this.req;
  var ready = req.readyState;
  try
  {
  	var httpStatus = req.status;
    if (ready == net.READY_STATE_COMPLETE)
    {
      if (httpStatus == 200 || httpStatus == 0)
      {
        this.onload.call(this);
      }
      else
      {
        this.onerror.call(this);
      }
    }
  }
  catch(e) {}
}

net.ContentLoader.prototype.defaultError = function()
{
  alert("error fetching data!"
    + "\n\nreadyState:" + this.req.readyState
    + "\nstatus: " + this.req.status
    + "\nheaders: " + this.req.getAllResponseHeaders());
}
