function CMessages(parent)
{
  this.parent = parent;
  this.messages = new Array();
  
  this.show = function(id, content)
  {
    mE("footer").hide();
    
    //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(null);
  }
  
  this.showTimed = function(id, delay, content)
  {
    mE("footer").hide();
    
    //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(delay);
  }
  
  this.hide = function(id)
  {
		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
    {
    	mE("footer").show();
    
			window.onresize = null;
			gE("message").style.display = "none";
			
    	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.delay = null;
	
	this.show = function(delay)
	{
		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));
		var h =  Math.round((getScrollHeight() / 2) - (e.offsetHeight / 2));
		
		m.style.top = h + "px";
		//m.style.top = Math.round((getDocHeight() / 2) - (e.offsetHeight / 2)) + "px";
		
		window.scrollTo(0, h);
		
		if(delay > 0)
			this.delay = setTimeout("game.messages.hide('"+this.id+"')", delay);
	}
}

