Namespace.Register("ponte.ajaxdata.PriceScroll");


function copyFunction(func, arg)
{
	return function()
	{
		func(arg);
	}
}

/**
 * A TransactionData portlet Ajax valasza alapjan valo kitoltesenek fuggvenyei.
 * 
 * @argument {Node} portletNode - 
 * @argument {HttpRequest} xmlData - A portlet xml tartalma.
 */
var loads = 0;
var maxVisibleBlocks = 8;

// uj blockok innerHTML-je, ha van id-hez tartozo elem, akkor kiszedi innen 
ponte.ajaxdata.PriceScroll.blocks = null;

ponte.ajaxdata.PriceScroll.Filler = function(portletNode, xmlData) {	
	try
	{
		loads ++;
		
		if(ponte.ajaxdata.PriceScroll.blocks != null)
			preLength = ponte.ajaxdata.PriceScroll.blocks.length;
		else
			preLength = 5;
			
		ponte.ajaxdata.PriceScroll.blocks = new Array();
		// adatok
		tbody = xmlData.getElementsByTagName('tbody')[0];
		// hany sor jott vissza
		rows = tbody.getElementsByTagName("tr");
		
		try
		{
			AJAX_refreshRate = tbody.parentNode.getAttribute("ajaxrefreshinterval");
			SCROLL_refreshRate = parseInt(tbody.parentNode.getAttribute("scrollrefreshinterval"));
			SCROLL_step = parseInt(tbody.parentNode.getAttribute("speed"));
		}catch(e)
		{
			AJAX_refreshRate = 10000;
			SCROLL_refreshRate = 100;
			SCROLL_step = 2;
		}
		
		html_content = "";
		
		// ha mar nincs kereskedes
		if((rows.length == 1))
		{
			block = ponte.ajaxdata.PriceScroll.NoTradingBlock;
			text = tbody.getElementsByTagName("td")[1].firstChild.nodeValue;
			for(i = 0; i<preLength; i++)
			{
				span = document.createElement("span");
				span.className = "PCBlock";
				span.setAttribute('name', loads);
				span.setAttribute('id', i);
				
				html_content = block[0] + text + block[1];
				
				span.innerHTML = html_content;
				ponte.ajaxdata.PriceScroll.blocks.push(span);
				
				// ami nem látszodik, azt display none-ra tesszuk
				if(i > maxVisibleBlocks )
					span.style.display="none";
			}			
		}
		// kerekedesi idoben
		else if(rows.length != 1)
		{
			/*
				<td name="Name">ANY</td>
				<td name="ChangeWay">arfolyam_up.gif<td>
				<td name="URL">magyar_egyeb/dinportl/companyprofile</td>
				<td name="secID">security=4042</td>
				<td name="Price">725</td><td name="ChangePrice">+3,57%</td>
			 */
			block = ponte.ajaxdata.PriceScroll.TradingBlock;
			rows = tbody.getElementsByTagName("tr");
			
			for(i = 0; i < rows.length; i++)
			{
				cells = rows[i].getElementsByTagName("td");
				
				c_name = cells[0].firstChild.nodeValue;
				c_link = cells[2].firstChild.nodeValue + "?" + cells[3].firstChild.nodeValue;
				c_price = cells[4].firstChild.nodeValue;
				c_change = cells[5].firstChild.nodeValue;
				
				span = document.createElement("span");
				
				sign = document.createElement("span");
				sign.className = "sign";
				
				if(c_change.indexOf('-') > -1)
				{
					span.className = "PCBlock Down";
					sign.appendChild(document.createTextNode("▼"));				
				}
				else if(c_change.indexOf('+') > -1)
				{
					span.className = "PCBlock Up";
					sign.appendChild(document.createTextNode("▲"));
				}
				else
				{
					span.className = "PCBlock St";
					sign.appendChild(document.createTextNode("■"));
				}
				span.appendChild(sign);
				
				span.setAttribute('name', loads);
				span.setAttribute('id', i);
				
				html_content = block[0] + c_link + block[1] + c_name + block[2] + c_price + block[3] + c_change + block[4];
				
				span.innerHTML += html_content;
				ponte.ajaxdata.PriceScroll.blocks.push(span);
				
				// ami nem látszodik, azt display none-ra tesszuk
				if(i > maxVisibleBlocks )
					span.style.display="none";
			}
			// ha kevesebb volt elozoleg, akkor feltoltjuk az elso nehanyat
			if(preLength < rows.length)
			{
				for(i = preLength -1; i < rows.length; i++)
				{
					span = ponte.ajaxdata.PriceScroll.blocks[i];					
					portletNode.appendChild(span);
				}
			}
		}	
	
		//	csak elso betolteskor
		if(loads == 1)
		{
			// Node-ok beszurasa, frissitesnel ebbol a tombbol szedi majd ki oket, csak forditva
			max = ponte.ajaxdata.PriceScroll.blocks.length;
			for(i = 0; i < max; i++)
			{
				span = ponte.ajaxdata.PriceScroll.blocks[i];					
				portletNode.appendChild(span);
			}
			
			// mozgatás elinditasa
			mover = new ponte.ajaxdata.PriceScroll.Mover(portletNode);
			mover.moverInit(SCROLL_refreshRate, SCROLL_step);
		}
		
		//if(AJAX_refreshRate < 60000)
		//	AJAX_refreshRate = 60000;
		
		// Frissites
		//AJAX_refreshRate = AJAX_refreshRate;
		//if(AJAX_refreshRate < 60000) AJAX_refreshRate = 60000;
		//setTimeout(ponte.ajaxdata.Service.portlets[portletNode.id].generateData, AJAX_refreshRate, ponte.ajaxdata.Service.portlets[portletNode.id]);
	}catch(e)
	{
		loads --;
	}
}


// nincs kereskedes html-je
ponte.ajaxdata.PriceScroll.NoTradingBlock = new Array();
ponte.ajaxdata.PriceScroll.NoTradingBlock.push("<div class=\"price\">");
ponte.ajaxdata.PriceScroll.NoTradingBlock.push("</div>");

// kereskedesi ido html-je
ponte.ajaxdata.PriceScroll.TradingBlock = new Array();
ponte.ajaxdata.PriceScroll.TradingBlock.push("<a href=\"");
ponte.ajaxdata.PriceScroll.TradingBlock.push("\"><div class=\"PCname\">");
ponte.ajaxdata.PriceScroll.TradingBlock.push("</div></a><div class=\"PCprice\">");
ponte.ajaxdata.PriceScroll.TradingBlock.push("</div><div class=\"PCchange\">");
ponte.ajaxdata.PriceScroll.TradingBlock.push("</div>");


ponte.ajaxdata.PriceScroll.Mover = function(container)
{
	return{
		tween : null,
		_container : container,
		_minLeft : -600,
		_step : 0,
		_currLeft : 0,
		_hidedList : new Array(),
		// hany blockot mozgasson egyszerre
		_moveBlockCnt : 0,
		// blockCnt-nyi block szelesseg
		_preBlockWidth : 0,
		
		moverInit : function(refreshRate, step)
		{
			this._step = - this.getPreBlockWidth(this); 
			this._currLeft = this._step;
			this.tween = new ponte.ajaxdata.PriceScroll.Tween(this._container.style, this.onTweenEnd, step, refreshRate); 
			this.tween.onFinished = copyFunction(this.onTweenEnd, this);
			this.tween.start(-this._step);
		},
		
		onTweenEnd : function(obj)
		{
			// ha nagyobb a hatarnal atmozgatjuk
			if(obj._moveBlockCnt > 0)
			{
				obj.moveBlocks(obj);
			}
			
			// mennyit kell mozgatni
			width = obj.getPreBlockWidth(obj);
			obj._step = -width;
			obj._currLeft += obj._step;
			obj.tween.start(-obj._step);
			
		},
		// a kovetkezo alkalommal eltuno block merete
		getPreBlockWidth : function(obj)
		{
			width = obj._container.childNodes[obj._moveBlockCnt].offsetWidth;
			obj._moveBlockCnt++;
			obj._preBlockWidth += width;
			obj._hidedList.push(width);
			return width;
		},
		
		moveBlocks : function(obj)
		{
			blocks = obj._container.childNodes;
			
			for(i = 0; i < obj._moveBlockCnt; i++)
			{
				if(blocks.length > maxVisibleBlocks)
					blocks[i].style.display = "none";
				
				// ha mar nem az aktualis adatokat tartalmazza, akkor torol és lekéri a kovetkezot, ha van
				if(parseInt(blocks[i].getAttribute("name")) < loads )
				{					
					blockID = parseInt(blocks[i].getAttribute('id'));

					if(ponte.ajaxdata.PriceScroll.blocks.length > blockID)
					{
						if(ponte.ajaxdata.PriceScroll.blocks[blockID] != null)
						{
							obj._container.removeChild(blocks[i]);
							if(ponte.ajaxdata.PriceScroll.blocks.length > 0)
							{
								obj._container.appendChild(ponte.ajaxdata.PriceScroll.blocks[blockID]);
								ponte.ajaxdata.PriceScroll.blocks[blockID] = null;
							}
						}
					}

				}
				else
				{
					obj._container.appendChild(blocks[i]);
				}
					
				obj._currLeft += obj._hidedList[i];
				obj._hidedList[i] = 0;
			}
			obj._moveBlockCnt = 0;
			// megjelenitjuk a kovetkezot
			if(blocks.length > maxVisibleBlocks)
				blocks[maxVisibleBlocks].style.display = "block"; 
		}
	}
}

ponte.ajaxdata.PriceScroll.Tween = function(objStyle, onfinished, step, interval)
{
	return{
		onFinished : onfinished,
		objStyle : objStyle,
		interval : interval,
		step : step,
		moveby : null,
		
		start : function(moveby, start)
		{
			this.moveby = moveby;
			if(start)
				objStyle.left = start + "px";
			else
				objStyle.left = "0px";
			setTimeout(copyFunction(this.move, this), interval);
		},
		
		move : function(obj)
		{
			if(obj.moveby >= obj.step)
			{
				obj.objStyle.left = parseInt(objStyle.left) - obj.step + "px";
				obj.moveby -= obj.step;
				setTimeout(copyFunction(obj.move, obj), interval);
			} 
			else
				obj.onFinished(obj);
		}
		
	}
}
