/*
	Uses divmanipulatoion.js
*/

function Message(nickname, content, img, visits, color)
{
	this.nickname = nickname;
	this.visits = visits;
	this.color = color;
	this.content = content;
	this.img = img;
} // Message

function MessagesContainer(messages)
{
	this.messages = messages;
	this.mesIndex = -1;
	
	// initialize the member function references 
  // for the class prototype
  if (typeof(_messagescontainer_prototype_called) == 'undefined')
  {
     _messagescontainer_prototype_called = true;
     MessagesContainer.prototype.getNextMessage = getNextMessage;
  }
  
  function getNextMessage()
  {
  	this.mesIndex++;
		if (this.mesIndex >= this.messages.length)
			this.mesIndex = 0;
			
		return this.messages[this.mesIndex];
  } // getNextMessage
  
} // MessagesContainer

function SmsChatLayer(divId, nameId1, visitsId1, genderId1, contentId1, imageName1, tblId1, nameId2, visitsId2, genderId2, contentId2, imageName2, tblId2, base)
{
	this.divId = divId;
	this.nameId1 = nameId1;
	this.visitsId1 = visitsId1;
	this.genderId1 = genderId1;
	this.contentId1 = contentId1;
	this.imageName1 = imageName1;
	this.tblId1 = tblId1;
	this.nameId2 = nameId2;
	this.visitsId2 = visitsId2;
	this.genderId2 = genderId2;
	this.contentId2 = contentId2;
	this.imageName2 = imageName2;
	this.tblId2 = tblId2;
	this.base = base;
	
	// initialize the member function references 
  // for the class prototype
  if (typeof(_smschatlayer_prototype_called) == 'undefined')
  {
     _smschatlayer_prototype_called = true;
     SmsChatLayer.prototype.setMessages = setMessages;
  }
  
  function setMessages(message1, message2)
  {  	
  	document.getElementById(this.genderId1).style.backgroundColor = message1.color;
  	
  	//divSetContent(this.nameId1, '<a class="linkNickname" target="_parent" href="'+message1.nickname+'">'+message1.nickname+'</a>');
  	document.getElementById(this.tblId1).onclick = function() { parent.location= base + '/' + message1.nickname };
  	divSetContent(this.nameId1, message1.nickname);
  	divSetContent(this.visitsId1, message1.visits);
  	divSetContent(this.contentId1, message1.content);
  	document.images[this.imageName1].src = message1.img;
  	
  	document.getElementById(this.genderId2).style.backgroundColor = message2.color;
  	
  	//divSetContent(this.nameId2, '<a class="linkNickname" target="_parent" href="'+message2.nickname+'">'+message2.nickname+'</a>');
  	document.getElementById(this.tblId2).onclick = function() { parent.location= base + '/' + message2.nickname };
  	divSetContent(this.nameId2, message2.nickname);
  	divSetContent(this.visitsId2, message2.visits);
  	divSetContent(this.contentId2, message2.content);
  	document.images[this.imageName2].src = message2.img;
  } // setMessage
} // SmsChatLayer

function SmsChatBar(chatLayerOne, chatLayerTwo, messages)
{
	this.layerOne = chatLayerOne;
	this.layerTwo = chatLayerTwo;
	this.messagesContainer = new MessagesContainer(messages);
	
	this.layerOnTop = 1;
	
	// store this, because setTimeOut cannot handle object/classes
	this.indexInArray = -1;
	for (i=0; i<__smsChatBars.length; i++)
	{
		if (typeof(__smsChatBars[i]) == 'undefined')
		{
			this.indexInArray = i;
			__smsChatBars[i] = this;
			break;
		}
	}
	if (this.indexInArray == -1)
			alert('Increase __smsChatBars size');
	
	// initialize the member function references 
  // for the class prototype
  if (typeof(_smschatbar_prototype_called) == 'undefined')
  {
     _smschatbar_prototype_called = true;
     SmsChatBar.prototype.initialize = initialize;
     SmsChatBar.prototype.move = move;
     SmsChatBar.prototype.moveStep = moveStep;
  }
  
  function initialize()
  {
  	this.layerOne.setMessages(this.messagesContainer.getNextMessage(), this.messagesContainer.getNextMessage());
  	this.layerTwo.setMessages(this.messagesContainer.getNextMessage(), this.messagesContainer.getNextMessage());
  	
  	this.startTopLayerY = divGetPosY(this.layerOne.divId);
  	this.startBotLayerY = divGetPosY(this.layerTwo.divId);
  	
  	if (this.startTopLayerY > this.startBotLayerY)
  		alert("error: topLayer is below botLayer");
  	
  } // initialize
	
	function move()
	{
		window.setTimeout('__smsChatBarMoveStep('+this.indexInArray+')', 4000);
	} // move
	
	function moveStep()
	{
		
		// fetch right layer
		if (this.layerOnTop == 1)
		{
			topLayer = this.layerOne;
			botLayer = this.layerTwo;
		}
		else
		{
			topLayer = this.layerTwo;
			botLayer = this.layerOne;
		}
		
		// move speed
		dy = 2;
		
		/* 
			calculate what the y-pos would be of the layer at the bottom.
			This may not exceed the starting y-pos of the starting layer
			at the top. If it does exceed, this will be the last step.
		*/
		botY = divGetPosY(botLayer.divId);
		botY = botY - dy;
		
		if (botY <= this.startTopLayerY)
		{
			// last step
			botY = this.startTopLayerY;
			topY = this.startBotLayerY;
			divSetPosY(topLayer.divId, topY);
			divSetPosY(botLayer.divId, botY);
			
			// move top layer to bottom
			this.layerOnTop = 1 - this.layerOnTop;
				
			topLayer.setMessages(this.messagesContainer.getNextMessage(), this.messagesContainer.getNextMessage());
			divSetPosY(topLayer.divId, this.startBotLayerY);
			this.move();
		}
		else
		{
			divMove(topLayer.divId, 0, -dy);
			divMove(botLayer.divId, 0, -dy);
			window.setTimeout('__smsChatBarMoveStep('+this.indexInArray+')', 30);
		}
			
	} // moveStep
	
} // SmsChatBar

/*
	setTimeOut cannot handle object/classes as parameters, so we
	need to store the smsCharBar globally. Most of the time you will
	use only one chatbar, but with code below the page can handle 4
	chatbars
*/
var __smsChatBars = new Array(4);

function __smsChatBarMoveStep(indexInArray)
{
	smsChatBar = __smsChatBars[indexInArray];
	smsChatBar.moveStep();
} // __smsChatBarMoveStep
