var mouseX = 0;
var mouseY = 0;
var IE = document.all?true:false;
var divDiscCard = null;
var timer = null;
var msgLoading = "Loading...";

//window.onload = init

function PositionDiscCard(dc)
{
	var viewportSize = getViewportSize();
	var scrollpos = getScrollingPosition();

	dc.style.left = mouseX + 10 + "px";

	var dcHeight = dc.offsetHeight > 300 ? dc.offsetHeight : 300;
		
	if (mouseY - scrollpos[1] + dcHeight < viewportSize[1])
	{
		dc.style.top = mouseY + 10 + "px";
		dc.style.bottom = "";
	}
	else
	{
		dc.style.top = "";
		dc.style.bottom = (viewportSize[1] - mouseY - 10) + "px";
	}
}


function getViewportSize()
{
 var size = [0, 0];

 if (typeof window.innerWidth != 'undefined')
 {
   size = [
       window.innerWidth,
       window.innerHeight
   ];
 }
 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
   size = [
       document.documentElement.clientWidth,
       document.documentElement.clientHeight
   ];
 }
 else
 {
   size = [
       document.getElementsByTagName('body')[0].clientWidth,
       document.getElementsByTagName('body')[0].clientHeight
   ];
 }

 return size;
}

function getScrollingPosition()
{
 var position = [0, 0];

 if (typeof window.pageYOffset != 'undefined')
 {
   position = [
       window.pageXOffset,
       window.pageYOffset
   ];
 }

 else if (typeof document.documentElement.scrollTop
     != 'undefined' && document.documentElement.scrollTop > 0)
 {
   position = [
       document.documentElement.scrollLeft,
       document.documentElement.scrollTop
   ];
 }

 else if (typeof document.body.scrollTop != 'undefined')
 {
   position = [
       document.body.scrollLeft,
       document.body.scrollTop
   ];
 }

 return position;
}

function getMouseXY(e)
{
	if (!e)
		e = window.event;
	
//alert(e);
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

	mouseX = posx;
	mouseY = posy;
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function ajaxFunction(id)
{
  var xmlHttp = GetXmlHttpObject();
  if (xmlHttp == null)
  {
      alert("Your browser does not support AJAX!");
      return false;
  }

  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4 && xmlHttp.responseText)
    {
//	if (divDiscCard)
//		divDiscCard.style.visibility = "hidden";

//      document.getElementById("DiscCards").innerHTML += "<div align=\"center\" id=\"disccard" + id + "\" class=\"disccard_c\">" + xmlHttp.responseText + "</div>";
      
//      var divDiscCard;
//      if (divDiscCard = document.getElementById("disccard" + id))
//        divDiscCard.innerHTML = xmlHttp.responseText;


	divDiscCard = document.getElementById("disccard" + id);
//	divDiscCard = document.createElement("div");
//	if (!divDiscCard)
//	{
//		document.getElementById("DiscCards").innerHTML += "<div id=\"disccard" + id + "\"></div>";
//		divDiscCard = document.getElementById("disccard" + id);
//	}
//	divDiscCard.style.align = "center";
//	divDiscCard.id = "disccard" + id;
//	divDiscCard.class = "disccard_c";
	divDiscCard.innerHTML = xmlHttp.responseText;

	PositionDiscCard(divDiscCard);
    }
  }

    xmlHttp.open("GET","/disccard.php?id=" + id, true);
    xmlHttp.send(null);
}



function ShowDiscCard(e, id, bShow)
{
	var pattern = new RegExp("showtopic[=](\\d+)", "g");
	var res = pattern.exec(id);
	var id = res ? res[1] : 0;
	if (!id) return;


	//get divDiscCard

	//hide old
	if (divDiscCard)
		divDiscCard.style.visibility = "hidden";
	
	divDiscCard = document.getElementById("disccard" + id);

	if (divDiscCard)
		divDiscCard.style.visibility = bShow ? "visible" : "hidden";
	
	if (bShow)
	{
		if (divDiscCard && divDiscCard.innerHTML == msgLoading)
			timer = setTimeout("ajaxFunction(" + id + ")", 400);
		else if (!divDiscCard)
		{
			document.getElementById("DiscCards").innerHTML += "<div align=\"center\" id=\"disccard" + id + "\" class=\"disccard_c\">" + msgLoading + "</div>";
			divDiscCard = document.getElementById("disccard" + id);

			timer = setTimeout("ajaxFunction(" + id + ")", 400);
		}


		getMouseXY(e);

		PositionDiscCard(divDiscCard);
	}
	else
		clearTimeout(timer);
}
