var MS_Created = false, MS_Visible = false;
var MS_ConId, MS_Id, MS_BGId;
var MS_Width=320, MS_Height=240, MS_WinWidth, MS_WinHeight;
var MS_AnimTmpX, MS_AnimTmpY, MS_AnimInterval;
var MS_Redirect=false, MS_RedirectURL;
var MS_IsIE = false;
var MS_Req;

if(!navigator.appName.toLowerCase().indexOf("microsoft"))MS_IsIE = true;

//Update position of scroll in IE
function MS_IEUpdate(){
 //Only continue while message is visible
 if(MS_Visible == false)return;
 
 //If needed, update scroll
 window.scrollTo(0, 0);
 
 //Run interval
 setTimeout("MS_IEUpdate()", 100);
}

//Set size of a created message box
function MS_SetSize(x, y){
 MS_Width = x;
 MS_Height = y;
}

//Command to use inside links to cause correct message marking
function MS_SetRedirect(url){
 if(MS_Created == false)return;
 
 //Set redirect data
 MS_Redirect = true;
 MS_RedirectURL = url;
 
 //Close Message and mark as read
 MS_MarkClick();
}

//Create message box with message
function MS_CreateMessage(caption, body, alternate){
 //If needed, create messagebox container
 if(MS_Created == false){
  document.body.innerHTML += "<div id=\"MessageCont\"></div>";
  MS_ConId = document.getElementById("MessageCont");
  MS_Created = true;
 }
 
 if(MS_Visible == false){
  //Create messagebox/cover inside container if needed
  MS_ConId.innerHTML = "<div id=\"MessagePopup\">Messagebox</div><div id=\"MessageBG\"></div>";
  MS_Id = document.getElementById("MessagePopup");
  MS_BGId = document.getElementById("MessageBG");
 }
 
 //We are making it visible, make it globally known
 MS_Visible = true;
 
 //Set correct message box size and overflow
 MS_Id.style.width = MS_Width+"px";
 MS_Id.style.height = MS_Height+"px";
 MS_Id.style.overflow = "auto";
 
 //Set body html based on arguments
 if(alternate == "" || !alternate)alternate = "javascript:void(MS_CloseMessage());";
 MS_Id.innerHTML  = "<a href=\""+alternate+"\" style=\"float: right;\">Close [X]</a><br />";
 MS_Id.innerHTML += "<h3>"+caption+"</h3>";
 MS_Id.innerHTML += "<p>"+body+"</p><br />";
 
 //Center the message box
 //First get width/height of window
 MS_WinHeight = GetWinHeight();
 MS_WinWidth = GetWinWidth();
 
 //Now reposition messagebox
 //Firefox does fine
 MS_BGId.style.position = "fixed";
 MS_Id.style.position = "fixed";
 MS_Id.style.left = Math.floor(MS_WinWidth/2.0 - MS_Width/2)+"px";
 MS_Id.style.top = Math.floor(MS_WinHeight/2.0 - MS_Height/2)+"px";
 
 //Use hack for IE
 if(MS_IsIE){
  //Change everything to use absolute (IE6 doesn't support fixed)
  MS_BGId.style.position = "absolute";
  MS_BGId.style.top = "0px";
  MS_BGId.style.height = MS_WinHeight+"px";
  MS_Id.style.position = "absolute";
  MS_Id.style.left = Math.floor(MS_WinWidth/2.0 - MS_Width/2)+"px";
  MS_Id.style.top = Math.floor(MS_WinHeight/2.0 - MS_Height/2)+"px";
  
  //Call updating function for scroll height
  MS_IEUpdate();
 }
 
 //Finally, make the message and its background hider visible
 MS_Id.style.visibility = "visible";
 MS_BGId.style.visibility = "visible";
}

//Step in the closing message animation
function MS_CloseAnimationStep(){
 var tmpx, tmpy, tmpz;
 
 //Make sure message is showing to do animation
 if(MS_Visible == false){
  clearInterval(MS_AnimInterval);
  return;
 }
 
 //Shrink the width and height of the box, and move vertically
 if(MS_AnimTmpX > 0){
  //Calculate next point, and set new size sin(0->pi/2)
  MS_AnimTmpX -= 1;
  tmpx = Math.sin((MS_AnimTmpX/39.0)*3.1415927)*MS_Width;
  tmpy = Math.sin((MS_AnimTmpX/39.0)*3.1415927)*MS_Height;
  tmpz = Math.sin((MS_AnimTmpX/39.0)*3.1415927)*(MS_WinHeight/2.0);
  
  //Change size and re-center
  MS_Id.style.width = tmpx+"px";
  MS_Id.style.height = tmpy+"px";
  MS_Id.style.left = Math.floor(MS_WinWidth/2 - tmpx/2)+"px";
  MS_Id.style.top = Math.floor(tmpz - tmpy/2)+"px";
 } else{ //No more shrinking, kill window
  clearInterval(MS_AnimInterval);
  MS_CloseMessage();
 }
}

//Animate the closing of a message
function MS_CloseAnimation(){
 if(MS_Visible == false)return;
 
 //Set overflow setting to resize without scrolls
 MS_Id.style.overflow = "hidden";
 
 //Set temporary x/y variables to movement left
 MS_AnimTmpX = 20; //Shrinking left (horizontal, number of steps)
 MS_AnimTmpY = parseInt(MS_Id.style.top); //Falling left (vertical)
 
 //Set interval for animation
 MS_AnimInterval = setInterval("MS_CloseAnimationStep()", 20);
}

//Hide already existent message
function MS_CloseMessage(){
 if(MS_Created == false)return;
 
 //Make closure globally known
 MS_Visible = false;
 
 //Hide both message and backdrop by purging container
 //Also, set visibility to false for backdrop (fixes Opera refresh issue)
 MS_BGId.style.display = "none";
 MS_ConId.innerHTML = "";
 
 //If we have a redirect, follow it
 if(MS_Redirect == true){
  document.location = MS_RedirectURL;
 }
}

//Handle callback from XML request object for message marking
function MS_MarkCallback(){
 if(MS_Req.readyState != 4)return; //Only handle final status
 
 //If there was an error, it won't be 200 status. Notify user
 if(MS_Req.status != 200){
  MS_CloseAnimation();
  MS_CreateMessage("Error!", "The previously shown message could not be marked as read. This may be due to security settings in your browser or an issue on our side. In any case, because of this, you'll probably see messages popup <i>every</i> time you load a page! If you're not sure what happened, please let us know -- cjmovie AT gmail DOT com.", "");
  return;
 }
 
 //No error, simply close message box (with animation)
 if(MS_Req.status == 200){
  MS_CloseAnimation();
  return;
 }
}

//Mark a status of a message as read/unread/etc (given id, and time for security)
function MS_MarkMessage(id, time, status){
 //First, create request. Firefox is easy, MS has two possibilities
 if(MS_IsIE){
  try {
   MS_Req = new ActiveXObject("Msxml2.XHTMLHTTP");
  } catch(varientmicrosoft){
   MS_Req = new ActiveXObject("Microsoft.XMLHTTP");
  }
 } else{ //Firefox, Opera, Netscape, etc.
  MS_Req = new XMLHttpRequest();
 }
 
 //Make sure object even exists
 if(!MS_Req){
  MS_CloseAnimation();
  MS_CreateMessage("Error!", "Failure in creation of XMLHTTPRequest Object (could not mark message as read).", "");
 }
 
 //Fill out request and send
 var url = "http://d5robotics.org/Scripts/MS_Mark.php?id="+id+"&time="+time+"&status="+status;
 MS_Req.open("GET", url, true);
 MS_Req.onreadystatechange = MS_MarkCallback;
 MS_Req.send(null);
}
