﻿var indexContent = null;
var cachedCollection = null;
var _timer = null;
var _timeOut = 300;
var _tickHandler = null;
var _setTimer = false;
var refreshTimer = null;
var tickRefreshHandler = null;

function startIndexTimer()
{
    var activeTab;
    var activeTabIndex;
    
    activeTabIndex = indexTab.control._activeTabIndex;
    
    if(activeTabIndex < 0) activeTabIndex = 0;
    
    activeTab = indexTab.control._tabs[activeTabIndex];
    indexContent = activeTab.get_element().getElementsByTagName('DIV')[0];
   
    var splt = indexContent.id.split('_');
    var splLength = splt.length;
    var countryid = splt[splLength-1];    
    
    startRefreshTimer(countryid,indexContent);       
}

function setCache(key, value)
{
  if(cachedCollection == null)
        cachedCollection = new Hashtable();
        
   cachedCollection.put(key, value);
}

function getCache(key)
{   
    var value;
     
    if(cachedCollection != null)
        value = cachedCollection.get(key);     
     
    return value;        
}

function onIndexTabChanged(e)
{
    stopRefreshTimer();    
        
    var activeTab = e.get_tabs()[e.get_activeTabIndex()];        
    indexContent = activeTab.get_element().getElementsByTagName('DIV')[0];
    
    var splt = indexContent.id.split('_');
    var splLength = splt.length;
    var countryid = splt[splLength-1];
    
    startRefreshTimer(countryid,indexContent);
    
    var cacheValue = getCache(indexContent.id);
    
    if(cacheValue == undefined) 
    { 
        getDailyIndexValues(countryid,indexContent);
    }
    else
        indexContent = cacheValue;   
}

function getDailyIndexValues(countryid, content) 
{
    if(countryid) 
    {
        var country = new StockAnalitics.Country(countryid);
        if(country)
            country.GetDailyIndexValues([],onSucceeded,onFailure,content);
    }
}

function onSucceeded(result,arg)
{
    indexContent.innerHTML = "";
    var count = 0;
    
    for(var r in result) 
    {
        var divBox = document.createElement('div');
        divBox.className = 'box03';
        divBox.id = 'box_'+ result[r].StockMarketIndex.StockMarketIndexID;
        divBox.style.height = "21px";
        divBox.style.weight = "300px";
        
        var divIndex = document.createElement('div');
        divIndex.setAttribute('align','right');
        divIndex.style.cursor = 'hand';
        divIndex.id = result[r].StockMarketIndex.StockMarketIndexID;
        
        var spanStockMarketIndex = document.createElement('span');
        
        var stockMarketIndex = result[r].StockMarketIndex;
        spanStockMarketIndex.innerHTML = stockMarketIndex.Name;
        divIndex.appendChild(spanStockMarketIndex);
        
        var spanStockMarketIndexValue = document.createElement('span');
        spanStockMarketIndexValue.className = 'numb';
        spanStockMarketIndexValue.innerHTML = String.localeFormat("{0:n}",result[r].ClosePrice);
        divIndex.appendChild(spanStockMarketIndexValue);
        
        var spanChangePercentElement = document.createElement('span');        
        var colorClass;
        
        if(result[r].PriceChange < 0) colorClass = 'minus';
        else colorClass = 'minus2';
        
        spanChangePercentElement.className = colorClass;        
        spanChangePercentElement.innerHTML = String.localeFormat("{0:n}",result[r].PriceChange)+"%";
        divIndex.appendChild(spanChangePercentElement);
        
        var spanChangeElement = document.createElement('span');
        if(result[r].PriceDiff < 0) colorClass = 'minus';
        else colorClass = 'minus2';
        spanChangeElement.className = colorClass;
        spanChangeElement.innerHTML = String.localeFormat("{0:n}",result[r].PriceDiff);
        
        divIndex.appendChild(spanChangeElement);
        
        divBox.appendChild(divIndex);
               
        divBox.onmouseover = function() { showDChart(this,true) };
        divBox.onclick = function () { stopTimer(); showDChart(this,false); };       
        divBox.onmouseout = function() { stopTimer() };
        
        indexContent.appendChild(divBox);
        
        if(count == 0) 
            if(getOpenedDiv() == null) 
                showDChart(divBox, false);
        
        count++;
    }     
    
    setCache(indexContent.id,indexContent);    
}

function showDChart(divChart,timerSet) 
{
    setTimer = timerSet;
    
    var divBoxIframe = getDivIframe(divChart);    
    
    if(divBoxIframe != null) 
    {    
        var openedDiv = getOpenedDiv();    
        if(!openedDiv) return;
        if(divChart.id == openedDiv.id) return;
    }
        
    if(timerSet)
        startTimer(divChart);
    else 
        showChart(divChart);
}

function showChart(selectedDiv) 
{
    var divBox = selectedDiv;
    var content = divBox.parentNode;
    var contentBody = content.parentNode;
    var openDiv = getOpenedDiv();
    
    stopTimer();
     
    var indexId = divBox.firstChild.id;
                   
    var iframeCol = divBox.getElementsByTagName('iframe');
    var len = iframeCol.length;
    
    if(len == 0) 
    {
        var iframe = document.createElement('iframe');               
        iframe.id = "iframe_" + indexId;
        iframe.setAttribute('src', 'http://fin.poslovni.hr/Chart.aspx?tid='+indexId+'&t=index&w=297&h=165&n=180&temp='+tempChart+'');
        iframe.setAttribute('width', '297');
        iframe.setAttribute('height', '155');
        iframe.setAttribute('marginheight','0');
        iframe.setAttribute('marginwidth','0');
        iframe.setAttribute('scrolling','no');
        iframe.frameBorder = 'no';
        divBox.appendChild(iframe);
    }
    
    if(openDiv != null)
    {   
        var openedIframe =  openDiv.getElementsByTagName('iframe')[0];
        var closedIframe = $get('iframe_'+indexId);     
        var parallelAnim = new AjaxControlToolkit.Animation.ParallelAnimation(openDiv, 0.3, 35, null);
        var closeIframe = new AjaxControlToolkit.Animation.ResizeAnimation(openedIframe, 0.3, 35, 300, 0, 'px');
        var closeAnim = new AjaxControlToolkit.Animation.ResizeAnimation(openDiv, 0.2, 35, 300, 21, 'px');
        var openAnim = new AjaxControlToolkit.Animation.ResizeAnimation(divBox, 0.2, 35, 300, 190, 'px');
        var openIframe = new AjaxControlToolkit.Animation.ResizeAnimation(closedIframe, 0.3, 35, 297, 155, 'px');
        parallelAnim.add(closeIframe);
        parallelAnim.add(closeAnim);
        parallelAnim.add(openAnim);
        parallelAnim.add(openIframe);        
        parallelAnim.play();        
    } 
    else 
    { 
        divBox.style.height = '190px';
    }    
    
}

function getDivIframe(box) 
{
    var divBox = box;
    var divBoxNodes = divBox.childNodes;
    var divBoxNodesLength = divBoxNodes.length;
    
    for(var j = 0; j < divBoxNodesLength; j++) 
    {
        var divBoxNode = divBoxNodes[j];
        var tName = divBoxNode.tagName;
        
        if(tName == 'IFRAME') 
        {
            if(parseInt(divBoxNode.height,null) > 0) 
                return divBoxNode.parentNode;
        }
    }
        
    return null; 
}

function getOpenedDiv() 
{
    var content = indexContent;
    var contentNodes = content.childNodes;
    var contentNodesLength = contentNodes.length;
    
    for(var i = 0; i < contentNodesLength; i++) 
    {
        var divBox = contentNodes[i];
        if(divBox.tagName == 'DIV')
            if(divBox.style.height == '190px') return divBox;
    }
    
    return null;  
}

function startTimer(divChart)
{
    if (!_timer)
    {
        _tickHandler = Function.createDelegate(this, function() { onTimerTick(divChart) });
        _timer = new Sys.Timer();
        _timer.set_interval(_timeOut);
        _timer.add_tick(this._tickHandler);      
        _timer.set_enabled(true);
    }
}

function stopTimer()
{
    if (_timer)
    {
        _timer.remove_tick(_tickHandler); 
        _timer.set_enabled(false);
        _timer.dispose();
        _tickHandler = null;
        _timer = null;
    }
}

function startRefreshTimer(countryid, indexContent)
{
    if (!refreshTimer)
    {
        tickRefreshHandler = Function.createDelegate(this, function() 
            {   
                refreshTimer.set_interval(refreshTimer.get_interval() * 1.5);
                getDailyIndexValues(countryid,indexContent) 
            });
                
        refreshTimer = new Sys.Timer();        
        refreshTimer.set_interval(300000);
        refreshTimer.add_tick(tickRefreshHandler);      
        refreshTimer.set_enabled(true);
    }
}

function stopRefreshTimer()
{
    if (refreshTimer)
    {
        refreshTimer.remove_tick(tickRefreshHandler); 
        refreshTimer.set_enabled(false);
        refreshTimer.dispose();
        tickRefreshHandler = null;
        refreshTimer = null;
    }
}

function onTimerTick(divChart) 
{
    showChart(divChart);
    stopTimer();
}


Sys.Application.add_load(startIndexTimer);
Sys.Application.add_unload(stopRefreshTimer);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
