// Globals
var marketIndicesTimerInterval = 60; // in seconds
var marketIndicesHighlight = 2; // in seconds
var marketIndicesUpdate = false;

function GetMarketIndicesData(piType,pblnLoading)
{
	PassAjaxResponseTextToFunction("/MarketIndicesRequest.aspx?type="+piType, 'ProcessMIResult','ProcessMIError', "\'"+piType+"\'");
}

function ProcessMIResult(pobjResponse,piType)
{
	ClearNoRefreshMessage('marketIndexMessage');
	if (pobjResponse.length > 2)
	{
		var items = Array();
		items = eval('('+pobjResponse+')');

		if (items.length > 0)
		{
			// first item is always date/status/timestamp
			ProcessMarketIndexInfo(items[0]);
			items.shift();  // remove market info
			for(var i=0;i<items.length;i++)
			{
				ProcessMarketIndexItem(items[i]);
			}
			items = null;
			marketIndicesUpdate = false;
		}
   }
}

function ProcessMIError(pobjResponse,piType)
{
	// display that the auto-refresh is off
	var iStyle = piType == 1 ? -1 : 2;
	DisplayNoRefreshMessage('marketIndexMessage','#c0c0c0',iStyle);
}

function ProcessMarketIndexInfo(pobjItem)
{
	if(pobjItem != null)
	{
		UpdateValue('marketIndexDate',pobjItem.marketdate);
		UpdateValue('marketIndexStatus',pobjItem.marketstatus);
	}
}

function ProcessMarketIndexItem(pobjItem)
{
	if(pobjItem != null)
	{
		var strId = pobjItem.id;
		var strType = pobjItem.type.toLowerCase();
		var strClass = 'data'+strType;
		
		//items to update if exist and if changed
		UpdateValue(strId+'Price',pobjItem.price0);
		UpdateValue(strId+'PriceChg',pobjItem.pricechg,strClass);
		UpdateValue(strId+'PricePctChg',pobjItem.pricepctchg,strClass);

		//change arrow image to match type (up/down)
		UpdateImage(strId+'Img', pobjItem.image, strType);
	}
}

function UpdateValue(pstrId, pstrValue, pstrClassName)
{
	var itemId = document.getElementById(pstrId);
	if(itemId != null && !CompareValues(itemId.innerHTML,pstrValue)) 
	{
		itemId.innerHTML = pstrValue;
		
		if(pstrClassName != undefined)
		{ itemId.className = SetDataClass(itemId.className,pstrClassName); }
		
		// only show update once
		if (!marketIndicesUpdate)
		{
			marketIndicesUpdate = true;
			DelayedShowHide('marketIndexMessage', 'marketIndexUpdatingMessage', marketIndicesHighlight); 
		}
	}
}

function UpdateImage(pstrId, pstrUrl, pstrAlt)
{
	var itemId = document.getElementById(pstrId);
	if(itemId != null)
	{
		itemId.style.display = '';
		if(itemId.src != pstrUrl)
		{
			itemId.src = pstrUrl;
			itemId.alt = pstrAlt;
		}
		if(pstrAlt == '')
		{itemId.style.display = 'none';}
	}
}

function SetDataClass(pstrOrgClass, pstrNewClass)
{
	var strClass = pstrOrgClass;
	strClass = RemoveClassName(strClass, 'dataup');
	strClass = RemoveClassName(strClass, 'datadown');
	return strClass + ' ' + pstrNewClass;
}

function MarketIndicesInit1()
{
	setTimeout('MarketIndicesRepeater(1)',marketIndicesTimerInterval*1000);
}

function MarketIndicesInit2()
{
   setTimeout('MarketIndicesRepeater(2)',marketIndicesTimerInterval*1000);
}

function MarketIndicesInit4()
{
   setTimeout('MarketIndicesRepeater(4)',marketIndicesTimerInterval*1000);
}

function MarketIndicesInit5()
{
   setTimeout('MarketIndicesRepeater(5)',marketIndicesTimerInterval*1000);
}

function MarketIndicesRepeater(piType)
{
	GetMarketIndicesData(piType,false);
	setTimeout('MarketIndicesRepeater('+piType+')',marketIndicesTimerInterval*1000);
}
