function CMessages(parent)
{
  this.parent = parent;
  this.messages = new Array();
  
  this.show = function(id, content)
  {
    gE("footer").style.display = "none";
    
    //remove any messages with the same id
    for(var i = 0; i < this.messages.length; i++)
    {
      if(this.messages[i].id == id)
      {
        this.messages.splice(i, 1);
        i--;
      }
    }
    
    this.messages.push(new CMessage(id, content));
    
    if(this.messages[0].visible == 0)
      this.messages[0].show();
  }
  
  this.hide = function(id)
  {
    gE("footer").style.display = "block";
    
		window.onresize = null;
		gE("message").style.display = "none";
		
		if(id)
		{
      //remove any messages with the same id
      for(var i = 0; i < this.messages.length; i++)
      {
        if(this.messages[i].id == id)
        {
          this.messages.splice(i, 1);
          i--;
        }
      }
    }
    else
		  this.messages.splice(0, 1);
		
		//show next
    if(this.messages.length > 0)
      this.messages[0].show();
    else
      game.show();
  }
  
  this.updatePosition = function()
	{
		var m = gE("message");

		if(m && m.style.display == "block")
		{
			//m.style.top = Math.round((getDocHeight() / 2) - (gE("message-content").offsetHeight / 2)) + "px";
			var h = Math.round((getWindowHeight() / 2) - (m.offsetHeight / 2));

			if(h < 40)
				h = 40;

			m.style.top = h + "px";

			window.scrollTo(0, h);
		}
	}
	
	this.isVisible = function()
	{
    for(var i = 0; i < this.messages.length; i++)
    {
      if(this.messages[i].visible == 1)
        return 1;
    }
    
    return 0;
  }
}

function CMessage(id, content)
{
  this.id = id;
  this.content = content;
	
	this.visible = 0;
	
	this.show = function()
	{
    game.hide();
    
    this.visible = 1;
    
    window.onresize = game.messages.updatePosition;
    
		var e = gE("message-content");
		e.innerHTML = this.content;

		var m = gE("message");
		m.style.display = "block";
		
		var h =  Math.round((getWindowHeight() / 2) - (e.offsetHeight / 2));
		
		m.style.top = h + "px";
		//m.style.top = Math.round((getDocHeight() / 2) - (e.offsetHeight / 2)) + "px";
		
		window.scrollTo(0, h);
	}
}

