﻿	function cLiveStream(textdiv, openlink, opendiv, menu, questionsarray)
	{
		this.ClockID = 0;
		this.typeInterval = 66;
		this.questionInterval = 10000;
		
		this.overMe = false;
		this.textDiv = textdiv;
		this.openLink = openlink;
		this.openDiv = opendiv;
		this.Menu = menu;
		this.questionsArray = questionsarray;
		this.questionIndex = -1;
		this.selected = -1;
		
		this.typeString = null;
		this.typePos = 0;
		
		if(this.textDiv != null && this.openLink != null && this.openDiv != null && this.Menu != null)
		{
			this.init();
		}
	}
	
	cLiveStream.prototype.init = function()
	{
		var ls = this;
		
		if(this.Menu.childNodes.length > 0)
		{
			this.openLink.onclick = function() 
			{
				ls.openOrClose();
				return false;	
			}
			this.openDiv.onkeydown = function(ev)
			{
				if (!ev)
					ev = window.event;
				ls.keyboard(ev);
				return noenter(ev);
			};
  			for (var i=0; i < this.Menu.childNodes.length; i++)
			{
				var nodeObj = this.Menu.childNodes[i];
				nodeObj.onmouseover = function(){ls.highlight(this);};
				nodeObj.onclick = function(){ls.select(this);return false;};
			}

			this.openDiv.onmouseover = function (ev)
			{
				ls.overMe = true;
				return true;	
			};	
						
			this.openDiv.onmouseout = function (ev)
			{
				ls.overMe = false;		
				return true;
			};			
			
			//addClickEvent(document.body, function(){ls.checkClose();});
			addSomeEvent(document.body, 'click', function(){ls.checkClose();});
			
			if(this.questionsArray != null)
			{
				this.typeit();
			}
		}
		else
		{
			this.openDiv.style.display = 'none';
		}
	}
	cLiveStream.prototype.keyboard = function(ev)
	{	
		var keynum;
		var charMin = '0'.charCodeAt(0);
		var charMax = 'z'.charCodeAt(0);
		if(window.event) // IE
		{
			keynum = ev.keyCode;
		}
		else if(ev.which) // Netscape/Firefox/Opera
		{
			keynum = ev.which;
		}

		switch(keynum)
		{
		  case 38: //up arrow
			  this.goUp();
			  break;
		  case 40: //down arrow 
			  this.goDown();
			  break;
		  case 13: //enter
		  {
			  if(-1==this.selected)
				  this.openOrClose();
			  else if(!this.select(this.Menu.childNodes[this.selected]))
				  this.openOrClose();
			  break;
		  }
		
		}
	};	
	cLiveStream.prototype.goUp = function()
	{
		var resultNodes = this.Menu.childNodes;
		if (resultNodes.length > 0)
		{
			if(this.selected == -1)
			{
				this.selected = resultNodes.length;
			}
				
			if (this.selected > 0)
			{
				var nodeObj = resultNodes[--this.selected];
				this.highlight(nodeObj);
			}
		}
	};
	cLiveStream.prototype.goDown = function()
	{
		var resultNodes = this.Menu.childNodes;
	    if (resultNodes.length > 0 && this.selected < resultNodes.length-1)
		{
			var nodeObj = resultNodes[++this.selected];
			this.highlight(nodeObj);
		}
	};		
	cLiveStream.prototype.highlight = function(node)
	{
		for (var i=0; i < this.Menu.childNodes.length; i++)
		{
			var nodeObj = this.Menu.childNodes[i];
			if (nodeObj == node)
			{
				this.selected = i;
				nodeObj.className = "lsselected";

			}
			else if (nodeObj.className == "lsselected")
				nodeObj.className = "";
		}		
	};
	cLiveStream.prototype.select = function(node)
	{
		this.openOrClose();
		
		if(node.id == 'allinprod')
		{
			logLiveStreamEvent(5362);
		}
		else if(node.id == 'allincat')
		{
			logLiveStreamEvent(5363);
		}
		else
		{
			logLiveStreamEvent(5361);
		} 
		location.href = node.firstChild.href;
	};
	cLiveStream.prototype.checkClose = function()
	{
		if(!this.overMe)
		{
			if(this.openDiv.className == 'canyou_w_open')
			{
				this.openDiv.className = 'canyou_w_closed';
			}
		}
	};
	cLiveStream.prototype.openOrClose = function()
	{
		if(this.openDiv.className == 'canyou_w_closed')
		{
			logLiveStreamEvent(5360);
			this.openDiv.className = 'canyou_w_open';
		}
		else
		{
			this.openDiv.className = 'canyou_w_closed';
		}
	};
	
	cLiveStream.prototype.typeit = function(user, st)
	{
		var ls = this;
		
		var length = this.questionsArray.length;
		
		this.questionIndex++;
		this.questionIndex = this.questionIndex%length;
		
		var nick = this.questionsArray[this.questionIndex].nick;
		this.typeString = this.questionsArray[this.questionIndex].title;
		this.typePos = 0;
		this.textDiv.innerHTML = '<b>' + nick + ' just asked:</b> _';
		this.ClockID = setInterval(function(){ls.typeit_interval();}, this.typeInterval);

	};
	cLiveStream.prototype.typeit_interval = function()
	{
		if(this.typePos <= this.typeString.length)
		{
			var stemp = this.typeString.substr(this.typePos, 1);
			if(this.typePos == this.typeString.length)
			{
				stemp += ' - <a href="' + this.questionsArray[this.questionIndex].link + '" onclick="return logLiveStreamEvent(5359)">Solve this!</a>';
				clearInterval(this.ClockID);
				var ls = this;
				
				this.ClockID = setTimeout(function(){ls.typeit('Jack', ls.typeString);}, this.questionInterval);
			}
			else
			{
				this.typePos++;
				stemp += '_';
				
			}
			this.textDiv.innerHTML = this.textDiv.innerHTML.substr(0, this.textDiv.innerHTML.length-1);
			this.textDiv.innerHTML += stemp;
		}
	};
	
	
function logLiveStreamEvent(e)
{
	if(typeof logEvent != 'undefined')
	{
		logEvent(e);		
	}
	return true;
}	