// holds an instance of XMLHttpRequest
var msgHttp = createMsgHttpRequestObject();

// creates an XMLHttpRequest instance
function createMsgHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var msgHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    msgHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
    for (var i=0; i<XmlHttpVersions.length && !msgHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        msgHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!msgHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return msgHttp;
}

var s_cache = new Array();
var s_id = "";

function getWishMsgHandler()
{
  s_cache.push("");
  // only continue if xmlHttp isn't void
  if (msgHttp.readyState == 4 || msgHttp.readyState == 0)
  {
    // try to connect to the server
   try
    {	  
	  var cacheEntry = s_cache.shift();
      // initiate reading the async.txt file from the server
      msgHttp.open("POST", "ajax/getWishMsg.php?KUBISess=" + s_id, true);
	  msgHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      msgHttp.onreadystatechange = msgRequestStateChange;
      msgHttp.send(cacheEntry);
    }
    // display the error in case of failure
    catch (e)
 
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

// function that handles the HTTP response
function msgRequestStateChange() 
{
if (msgHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (msgHttp.status == 200) 
    {
      try
      {
        response = msgHttp.responseText;
        eval(response);
		getMesgList();
      }
      catch(e)
      {
//        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
//      alert("There was a problem retrieving the data:\n" + msgHttp.statusText);
    }
  }
}

var wishViewStep = 0;
var wishTimeDelay = 0;
var wishOffsetY = 8;
var wishMsgIndex = 0;
var wishMsgLists = [];

function WishPopDivOpenHandler()
{
	var obj = document.getElementById("WishPopDiv");
	var n = parseInt((parseInt(document.body.scrollTop,10)), 10);
	var m = document.body.offsetHeight - parseInt(obj.style.height, 10) - 5;
	obj.style.top = (n + m) + "px";
	obj.style.display = "block";
	obj.style.height = 1;
	wishViewStep = 0;
	wishTimeDelay = 0;
	wishMsgIndex = wishMsgLists.length;
	bodySetScroll();
	getMesgList();
}

function getMesgList()
{
	window.setTimeout("getWishMsgHandler()", 10000);
}

function bodySetScroll()
{
	var obj = document.getElementById("WishPopDiv");
	if(wishViewStep == 2)
	{
		var h = parseInt(obj.style.height, 10);
		if((h-wishOffsetY) > 1)
		{
			h -= wishOffsetY;
			obj.style.height = h + "px";
		}
		else
		{
			h = 1;
			obj.style.height = h + "px";
			wishTimeDelay = 0;
			wishViewStep = 3;
			obj.style.display = "none";
		}
	}
	if(wishViewStep == 1)
	{
		if(wishMsgIndex > 1)
		{
			wishMsgIndex --;
			if(document.getElementById(wishMsgLists[wishMsgIndex]))
			{
				document.getElementById(wishMsgLists[wishMsgIndex]).style.display = "none";
				if(wishMsgIndex > 0)
					document.getElementById(wishMsgLists[wishMsgIndex-1]).style.display = "block";
				wishTimeDelay = 3000;
				wishViewStep = 1;
			}
			else
			{
				wishViewStep = 0;
			}
		}
		else
		{
			wishViewStep = 2;
			wishTimeDelay = 0;
		}
	}
	if(wishViewStep == 0)
	{
		var h = parseInt(obj.style.height, 10);
		if((h+wishOffsetY) < 236)
		{
			h += wishOffsetY;
			obj.style.height = h + "px";
		}
		else
		{
			h = 236;
			obj.style.height = h + "px";
			wishTimeDelay = 5000;
			wishViewStep = 1;
		}
	}
	if(wishViewStep < 3)
	{
		var n = parseInt((parseInt(document.body.scrollTop,10)), 10);
		var m = document.body.offsetHeight - parseInt(obj.style.height, 10) - 5;
		obj.style.top = (n + m) + "px";
		window.setTimeout("bodySetScroll()", wishTimeDelay);
	}
}

function bodyOnScroll()
{
	var obj = document.getElementById("WishPopDiv");
	var n = parseInt((parseInt(document.body.scrollTop,10)), 10);
	var m = document.body.offsetHeight - parseInt(obj.style.height, 10) - 5;
	obj.style.top = (n + m) + "px";
	window.setTimeout("bodyOnScroll()", 0);
}

function WishPopDivCloseHandler()
{
	var obj = document.getElementById("WishPopDiv");
	obj.style.display = "none";
}

window.setTimeout("getWishMsgHandler()", 3000);
window.setTimeout("bodyOnScroll()", 1000);
