/*
copyright @compas 2010-
author Liang Huang
*/
// charset = utf-8;
// Switches
var withDDZ = true;

// Globals
var dataObj = {};
var currItems = [];
var currDate = '00000000'
var sNumber = -1;
var avgVol = 0;
var totalVol = 0;
var changeRate = 0;

var lastValue = null;
var stockFix = null;

//realstock variables
var znzReal = null;
var znzIdx = null;
var znzLast = null;
var znzMystocks = null;
var znzDS = null;
var znzNews = null;
var znzEntrst = null;
var znzF10Instance = null;
var znzBoardInstance = null;
var znzPriceTab = null;
var znzFin = null;
var znzBigWeight = null;
var znzBigTrade = null;

var znzK = null;

var isIndexStock = false;
var indcs = {};
var times = ["min5","min15","min30","min60","day","week","month","min1"];
var currentIndc = null;
var currentTime = "";

var znzAD1 = null;
var znzAD2 = null;
var znzAD3 = null;
var znzAD4 = null;
var znzAD5 = null;
var znzAD6 = null;

//2008/07/21  huangliang add
var realBlink = null;

znzBoard.prototype._set = function(dataStr)
{
    if(!dataStr)
        return;
        
    var infoObj = KISSY.JSON.parse(dataStr);
    
    if(infoObj.length == 1 && infoObj[0] == -1)
        return;
        
    var boardList = [];
    for(var i in infoObj){
        if (i != "toJSONString")
        {
            boardList.push([i, infoObj[i][0], infoObj[i][1]]);
        }    
    }  
    
    function rateCmp(a, b)
    {
        return b[2] - a[2];
    }
        
    boardList.sort(rateCmp);
    
    var inne = '<table>';    

    inne += '<tr  class="hasbtm"><td>地区板块：</td><td>';
    for(var i = 0; i< boardList.length; i++)
    {
        if( boardList[i][0].substr(0, 1) == "3")
        {
            color = "nocolor";
            if(boardList[i][2] < 0)
            {
                color = "decolor";
            }
            if(boardList[i][2]> 0)
            {
                color = "incolor";
            }                
              
            inne += '<a href="newsort.php?boardNo=' + boardList[i][0] +'&high='+this.stockType + 'HQ' + this.stockCode+'">' + decodeURIComponent(boardList[i][1])+'</a>'
            inne += '&nbsp;<span class="' + color + '">' + boardList[i][2].toFixed(2) + '%</span>&nbsp;&nbsp;';
        }
        
    }
    inne += '</td></tr>';
    
    inne += '<tr class="hasbtm"><td>行业板块：</td><td>';
    for(var i = 0; i< boardList.length; i++)
    {
        if( boardList[i][0].substr(0, 1) == "1")
        {
            color = "nocolor";
            if(boardList[i][2] < 0)
            {
                color = "decolor";
            }
            if(boardList[i][2]> 0)
            {
                color = "incolor";
            }
              
            inne += '<a href="newsort.php?boardNo=' + boardList[i][0] +'&high='+this.stockType + 'HQ' + this.stockCode+'">' + decodeURIComponent(boardList[i][1])+'</a>'
            inne += '&nbsp;<span class="' + color + '">' + boardList[i][2].toFixed(2) + '%</span>&nbsp;&nbsp;';
        }
        
    }
    inne += '</td></tr>';
    
    
    inne += '<tr><td style="width:60px;vertical-align:top">概念板块：</td><td>';
    for(var i = 0; i< boardList.length; i++)
    {
        if( boardList[i][0].substr(0, 1) == "2")
        {
            color = "nocolor";
            if(boardList[i][2] < 0)
            {
                color = "decolor";
            }
            if(boardList[i][2]> 0)
            {
                color = "incolor";
            }
              
            inne += '<a href="newsort.php?boardNo=' + boardList[i][0] +'&high='+this.stockType + 'HQ' + this.stockCode+'">' + decodeURIComponent(boardList[i][1])+'</a>'
            inne += '&nbsp;<span class="' + color + '">' + boardList[i][2].toFixed(2) + '%</span>&nbsp;&nbsp;';
        }
        
    }
    inne += '</td></tr>';
        
    inne += "</table>";
    $(this.divID + "-table").innerHTML = inne;
    if($(this.divID))
    {
        $(this.divID).style.display = "block";
    }
}
    
function start()
{
    var onloadtime = new Date().getTime();
      
    var sugReal = new suggestQuery('realstock-query-query', 'realstock-query-select', 'realstock-query-code', 'realstock-query-form', null, null, null, true);
    if(typeof addLogo == 'undefined')
        addLogo = true;
    if(!isHtml5Mobile){
        znzK = new znzHDStock("znzKLineSWF", "realstock-swf-kline",
            stockCode, stockName, stockType, onDataChange, 3 * 1000, realstock_data, 545, 360, 1, 0.18, null, addLogo);
    }
    else{
        var _stockType = stockType.toLowerCase() == 'sh' ? 'SHHQ' : 'SZHQ';
        var p = {dataHost:rdmDataDomainNameGet(), 'cid':'#stock_down_html5', 'indList':['real', 'rvol'], 'ktype':'real',
                   'cid_up':'#stock_up_html5', stockCode:stockCode, prereq:['k-day-current', {cb:onDataChange}],
                   stockType:_stockType, stockName:stockName, 'w':542, 'h':360};
        znzK = new znzKLine.znzStock.znzStock(p);
        znzK.start();
    }
    znzIdx = new znzIdxSummary('stock-index-all', 10 * 1000, idxsumry_data);
    znzDS = new znzDDZSort('stock-ddzsort-table', 60 * 1000, 'stock-ddzsort-order', ddzsort_data);        
   
    znzLast = new znzLastVisit('stock-lastv-table', 10 * 1000, true, stockCode, stockType);
    $('stock-lastv').style.display = "block";
    
    if(withDDZ && !znzEntrst && stockType == 'SH')
    {
        znzEntrst = new znzDDX('realstock-entrust-table', 'DDZ', 'realstock-entrust-graph-swf', 60 * 1000, stockCode, stockType)
    }    
    
    znzF10Instance = new znzF10("stock-F10", stockCode, stockType, f10_data);   
    znzBoardInstance = new znzBoard("stock-boardInfo", 60 * 1000, stockCode, stockType, board_data);
    
    if(isIndexStock = isStockIndex(stockCode, stockType))
    {
        znzBigWeight = new znzBWStock("realstock-bigweight", 30 * 1000);
        znzBigTrade = new znzMarketBigTradeStock("realstock-marketbigtrade", 10 * 1000, stockType);
        
        $('realstock-bs').style.display = 'block';
        $('realstock-bs-table').style.display = 'none';
        $('realstock-indec-table').style.display = 'block';
        $('realstock-price-name').style.display = 'none';
        $('realstock-bigtrade-name').style.display = 'none';
        $('realstock-entrust-name').style.visibility = 'hidden';
        $('realstock-research').style.display = 'block';    
    }
    else
    {
        znzFin = new znzFinance("realstock-finance", fin_data);
        //znzNews = new znzStockNews('realstock-news-table', 'realstock-notice-table', 60 * 1000, stockCode, stockType, false, stocknews_data);
        znzBigTrade = new znzBigTradeStock("realstock-bigtrade", 10 * 1000, stockCode, stockType);
        znzPriceTab = new znzPriceTable(stockCode, stockType, onPriceTableChange, 60 * 1000);
       
        $('realstock-bs').style.display = 'block';
        $('realstock-bs-table').style.display = 'block';
        $('realstock-indec-table').style.display = 'none';
        
        $('realstock-price-name').style.display = 'block';
        $('realstock-bigtrade-name').style.display = 'block';
        $('realstock-finance-infotable').style.display = 'none';
        $('realstock-finance-partnertable').style.display = 'none';
        $('realstock-finance-gstable').style.display = 'none';
        $('realstock-news').style.display = 'block';
        var iframe = $('realstock-notice-iframe');
        iframe.src = iframe.getAttribute('frmsrc');
        $('add-index-div').style.display = "inline";
        if(stockType == "SH")
        {
            $('index-ind-value').value = "SHHQ000001";
        }
        else
        {
            $('index-ind-value').value = "SZHQ399001";
        }
        //$('realstock-finance-reportdate').style.display = 'none';
    }
    znzMystocks =  new  znzMyStock('stock-mystock-table-content', 10 * 1000, 'znzMystocks', stockType + stockCode, 'realstock-add-mystock');
    //spcial 
    eventInit();
    var inittime = new Date().getTime();     
    
}

function indexAddOp(marketCode, flag)
{   
    if(currentTime != "" || znzK == null)
    {
        return;
    }
    if(!isHtml5Mobile){
        
        var stockType = marketCode.substr(0, 4);
        var stockCode = marketCode.substr(4);
        
        znzK.postIndSet('overlap', stockType, stockCode, flag);
    }
    else
    {
        var p = {'stockName':'上证指数', 'stockCode':'000001', stockType:'SHHQ', interval:30 * 1000};
        if(marketCode.substr(0, 2).toLowerCase() == 'sz' && marketCode.substr(4) == '399001')
            var p = {'stockName':'深证指数', 'stockCode':'399001', stockType:'SZHQ', interval:30 * 1000};
        znzK.indReplace(['overlap'], p);
    }
}

function addMyStock(stockCode, stockName)
{
    if(znzMystocks.addStock(stockCode, stockName))
    {
        $('stock-mystock').onclick();
        $('realstock-add-mystock').style.display='none';
    }
}
function resetState()
{
    var list = ['realstock-kline-real', 'realstock-kline-day', 'realstock-kline-week', 'realstock-kline-month'];
    var i = 0;
    for(i = 0; i < list.length; i ++)
    {
        $(list[i]).className = "";
    }
}
function onKeyDown(keyCode){
    if(isHtml5Mobile)
        znzK.onkeydown({'keyCode':keyCode});
}  
function eventInit()
{
    if($('stock-lastv-name'))
    {
        $('stock-lastv-name').onclick = function()
        {
            this.className = 'title-current';
            $('stock-mystock').className = "title-uncurr";
            
            $('stock-lastv-table').style.display = "block";
            $('stock-mystock-table').style.display = "none";
        }
    }
    
    if($('stock-mystock'))
    {
        $('stock-mystock').onclick = function()
        {
            this.className = 'title-current';
            $('stock-lastv-name').className = "title-uncurr";
            
            $('stock-lastv-table').style.display = "none";
            $('stock-mystock-table').style.display = "block";
        }
    }
    $('realstock-kline-real').onclick = function()
    {
        resetState();
        $('realstock-kline-per-sel').value = "";
        $('realstock-kline-ind-sel').value = "";        
        $('realstock-kline-ind-sel').setAttribute('disabled', 'disabled');
        this.className = "act";        
        onKTypeChange("");
        if(!isIndexStock )
        {
            $('add-index-div').style.display = "inline";
            if(!isHtml5Mobile)
                indexAddOp($('index-ind-value').value, $('index-ind-value').checked);
        }
    }
    $('realstock-kline-day').onclick = function()
    {
        resetState();
        $('realstock-kline-per-sel').value = "day";        
        $('realstock-kline-ind-sel').removeAttribute('disabled');
        $('realstock-kline-ind-sel').value = currentIndc || '';
        
        this.className = "act";
        $('add-index-div').style.display = "none";
        //onIndChange("");
        onKTypeChange("day");
    }
    $('realstock-kline-week').onclick = function()
    {
        resetState();
        $('realstock-kline-per-sel').value = "week";        
        $('realstock-kline-ind-sel').removeAttribute('disabled');
        $('realstock-kline-ind-sel').value = currentIndc || '';
        this.className = "act";

        $('add-index-div').style.display = "none";

        //onIndChange("");
        onKTypeChange("week");
    }
    
    $('realstock-kline-month').onclick = function()
    {
        resetState();
        $('realstock-kline-per-sel').value = "month";
        $('realstock-kline-ind-sel').value = currentIndc || '';
        $('realstock-kline-ind-sel').removeAttribute('disabled');
        this.className = "act";
        $('add-index-div').style.display = "none";
        //onIndChange("");
        onKTypeChange("month");
    }
    
    $('realstock-kline-ind-sel').onchange = function()
    {
        if(!currentTime)
            return;
        
        onIndChange(this.value);
    }
    
    $('realstock-kline-per-sel').onchange = function()
    {
        resetState();        
        onKTypeChange(this.value);
        if(!this.value)
        {
            $('realstock-kline-ind-sel').setAttribute('disabled', 'disabled');
            $('realstock-kline-real').className = 'act';
            if(!isIndexStock)
            {
                $('add-index-div').style.display = "inline";
                if(!isHtml5Mobile)
                    indexAddOp($('index-ind-value').value, $('index-ind-value').checked);
            }
        }
        else
        {
             $('realstock-kline-ind-sel').removeAttribute('disabled');
             $('add-index-div').style.display = "none";
        }
        
        if($('realstock-kline-' + this.value))
        {
            $('realstock-kline-' + this.value).className = "act";
        }
    }
    
    $('realstock-notice-name').style.cursor = 'pointer';
    $('realstock-news-name').style.cursor = 'pointer';
    $('realstock-notice-name').onclick = function(){
        $('realstock-news-name').className = 'notnow-tab';
        $('realstock-institute-name').className = 'notnow-tab';
        $('realstock-notice-name').className = '';
        $('realstock-invest-name').className = 'notnow-tab';
        $('realstock-profit-name').className = 'notnow-tab';
        //znzNews.isNews = false;
        //znzNews._refresh();
        $('realstock-news-table').style.display = 'none';
        $('realstock-notice-table').style.display = 'block';
        $('realstock-institute-table').style.display = 'none';
        $('realstock-invest-table').style.display = 'none';
        $('realstock-profit-table').style.display = 'none';
        
        var iframe = $('realstock-notice-iframe');
        if(iframe.src == '')
        {
            iframe.src = iframe.getAttribute('frmsrc'); 
        }
       
    }

    $('realstock-news-name').onclick = function(){
        $('realstock-notice-name').className = 'notnow-tab';
        $('realstock-institute-name').className = 'notnow-tab';
        $('realstock-news-name').className = '';
        $('realstock-invest-name').className = 'notnow-tab';
        $('realstock-profit-name').className = 'notnow-tab';
        
        //znzNews.isNews = true;
        //znzNews._refresh();
        $('realstock-news-table').style.display = 'block';
        $('realstock-notice-table').style.display = 'none';
        $('realstock-institute-table').style.display = 'none';
        $('realstock-invest-table').style.display = 'none';
        $('realstock-profit-table').style.display = 'none';
        
        var iframe = $('realstock-news-iframe');
        if(iframe.src == '')
        {
            iframe.src = iframe.getAttribute('frmsrc'); 
        }
    }
    
    $('realstock-institute-name').onclick = function(){
        $('realstock-notice-name').className = 'notnow-tab';
        $('realstock-institute-name').className = '';
        $('realstock-news-name').className = 'notnow-tab';
        $('realstock-invest-name').className = 'notnow-tab';
        $('realstock-profit-name').className = 'notnow-tab';

        $('realstock-news-table').style.display = 'none';
        $('realstock-notice-table').style.display = 'none';
        $('realstock-institute-table').style.display = 'block';
        $('realstock-invest-table').style.display = 'none';
        $('realstock-profit-table').style.display = 'none';
        
        var iframe = $('realstock-institute-iframe');
        if(iframe.src == '')
        {
            iframe.src = iframe.getAttribute('frmsrc'); 
        }
    }

    $('realstock-invest-name').onclick = function(){
        $('realstock-notice-name').className = 'notnow-tab';
        $('realstock-institute-name').className = 'notnow-tab';
        $('realstock-news-name').className = 'notnow-tab';
        $('realstock-invest-name').className = '';
        $('realstock-profit-name').className = 'notnow-tab';
        
        $('realstock-news-table').style.display = 'none';
        $('realstock-notice-table').style.display = 'none';
        $('realstock-institute-table').style.display = 'none';
        $('realstock-invest-table').style.display = 'block';
        $('realstock-profit-table').style.display = 'none';
        
        var iframe = $('realstock-invest-iframe');
        if(iframe.src == '')
        {
            iframe.src = iframe.getAttribute('frmsrc'); 
        }
    }
    
    $('realstock-profit-name').onclick = function(){
        $('realstock-notice-name').className = 'notnow-tab';
        $('realstock-institute-name').className = 'notnow-tab';
        $('realstock-news-name').className = 'notnow-tab';
        $('realstock-invest-name').className = 'notnow-tab';
        $('realstock-profit-name').className = '';
        
        $('realstock-news-table').style.display = 'none';
        $('realstock-notice-table').style.display = 'none';
        $('realstock-institute-table').style.display = 'none';
        $('realstock-invest-table').style.display = 'none';
        $('realstock-profit-table').style.display = 'block';
        
        var iframe = $('realstock-profit-iframe');
        if(iframe.src == '')
        {
            iframe.src = iframe.getAttribute('frmsrc'); 
        }
    }
    
    $('realstock-bs-name').style.cursor = 'pointer';
    $('realstock-entrust-name').style.cursor = 'pointer';
    $('realstock-bs-name').onclick = function(){
        $('realstock-entrust-name').className = 'notnow-tab';
        $('realstock-bs-name').className = '';
        $('realstock-entrust-box').style.display = 'none';
        $('realstock-bs-box').style.display = 'block';
    }
    
    $('realstock-entrust-name').onclick = function(){
        $('realstock-bs-name').className = 'notnow-tab';
        $('realstock-entrust-name').className = '';
        $('realstock-entrust-box').style.display = 'block';
        $('realstock-bs-box').style.display = 'none';
        znzEntrst.upd2swf();
    }
    

    
    $('realstock-trade-name').onclick = function(){
        $('realstock-price-table').style.display = "none";
        $('realstock-trade-table').style.display = "block";
        $('realstock-bigtrade-table').style.display = "none";
        $('realstock-trade-name').className = "";
        $('realstock-price-name').className = "notnow-tab";
        $('realstock-bigtrade-name').className = "notnow-tab";
        $('realstock-jump-links').innerHTML="&nbsp;&nbsp<a href='history.php?code=" + stockType.substr(0,2).toLowerCase() + stockCode + "&type=trade'>显示全部成交明细</a>";
    }

    $('realstock-price-name').onclick = function(){
        $('realstock-price-table').style.display = "block";
        $('realstock-trade-table').style.display = "none";
        $('realstock-bigtrade-table').style.display = "none";
        $('realstock-trade-name').className = "notnow-tab";
        $('realstock-price-name').className = "";
        $('realstock-bigtrade-name').className = "notnow-tab";
        $('realstock-jump-links').innerHTML = "&nbsp;&nbsp";
    }
    
    $('realstock-bigtrade-name').onclick = function(){
        $('realstock-price-table').style.display = "none";
        $('realstock-trade-table').style.display = "none";
        $('realstock-bigtrade-table').style.display = "block";
        $('realstock-trade-name').className = "notnow-tab";
        $('realstock-price-name').className = "notnow-tab";
        $('realstock-bigtrade-name').className = "";
        $('realstock-jump-links').innerHTML="&nbsp;&nbsp<a href='history.php?code=" + stockType.substr(0,2).toLowerCase() + stockCode + "&type=bigtrade'>显示全部主力大单</a>";
    }

    $('realstock-finance-dataname').onclick = function(){
        $('realstock-finance-datatable').style.display = "block";
        $('realstock-finance-infotable').style.display = "none";
        $('realstock-finance-partnertable').style.display = "none";
        $('realstock-finance-gstable').style.display = "none";
        $('realstock-finance-dataname').className = "";
        $('realstock-finance-infoname').className = "notnow-tab";
        $('realstock-finance-partnername').className = "notnow-tab";
        $('realstock-finance-gsname').className = "notnow-tab";
        $('realstock-finance-reportdate').style.display = 'block';
        gsIntroChange(false);
    }
    
    $('realstock-finance-infoname').onclick = function(){
        $('realstock-finance-datatable').style.display = "none";
        $('realstock-finance-infotable').style.display = "block";
        $('realstock-finance-partnertable').style.display = "none";
        $('realstock-finance-gstable').style.display = "none";
        $('realstock-finance-dataname').className = "notnow-tab";
        $('realstock-finance-infoname').className = "";
        $('realstock-finance-partnername').className = "notnow-tab";
        $('realstock-finance-gsname').className = "notnow-tab";
        $('realstock-finance-reportdate').style.display = 'block';
        gsIntroChange(false);
    }
        
    $('realstock-finance-partnername').onclick = function(){
        $('realstock-finance-datatable').style.display = "none";
        $('realstock-finance-infotable').style.display = "none";
        $('realstock-finance-partnertable').style.display = "block";
        $('realstock-finance-gstable').style.display = "none";
        $('realstock-finance-dataname').className = "notnow-tab";
        $('realstock-finance-infoname').className = "notnow-tab";
        $('realstock-finance-partnername').className = "";
        $('realstock-finance-gsname').className = "notnow-tab";
        $('realstock-finance-reportdate').style.display = 'block';
        gsIntroChange(false);
    }
    
   $('realstock-finance-gsname').onclick = function(){
        $('realstock-finance-datatable').style.display = "none";
        $('realstock-finance-infotable').style.display = "none";
        $('realstock-finance-partnertable').style.display = "none";
        $('realstock-finance-gstable').style.display = "block";
        $('realstock-finance-dataname').className = "notnow-tab";
        $('realstock-finance-infoname').className = "notnow-tab";
        $('realstock-finance-partnername').className = "notnow-tab";
        $('realstock-finance-gsname').className = "";
        $('realstock-finance-reportdate').style.display = 'none';
    }
    
    // 研究报告 start
    $('realstock-research-macro-name').onclick = function(){        
        resetResearchReportTab();
        this.className = "";
        
        $('realstock-research-macro-content').style.display = "block";
        var iframe = $('realstock-macro-research');
        if(iframe.src == '')
        {
            iframe.src = iframe.getAttribute('frmsrc'); 
        }
    }
    
    $('realstock-research-industry-name').onclick = function(){        
        resetResearchReportTab();
        
        $('realstock-research-industry-content').style.display = "block";
        this.className = "";
        
        var iframe = $('realstock-industry-anaylze');
        if(iframe.src == '')
        {
            iframe.src = iframe.getAttribute('frmsrc');            
        }
    }
    
    $('realstock-research-invest-name').onclick = function(){
        
        resetResearchReportTab();
        
        $('realstock-research-invest-content').style.display = "block";
        this.className = "";
        
        var iframe = $('realstock-invest-tactics');
        if(iframe.src == '')
        {
            iframe.src = iframe.getAttribute('frmsrc');            
        }
    }

    $('realstock-research-morning-name').onclick = function(){
        
        resetResearchReportTab();
        $('realstock-research-morning-content').style.display = "block";
        this.className = "";
        var iframe = $('realstock-morning-memo');
        if(iframe.src == '')
        {
            iframe.src = iframe.getAttribute('frmsrc');            
        }
    }
    
    $('realstock-research-compay-name').onclick = function(){
        
        resetResearchReportTab();
        $('realstock-research-company-content').style.display = "block";
        this.className = "";
        var iframe = $('realstock-company-research');
        if(iframe.src == '')
        {
            iframe.src = iframe.getAttribute('frmsrc');            
        }
    }
    
}

function resetResearchReportTab()
{
    $('realstock-research-macro-content').style.display = "none";
    $('realstock-research-industry-content').style.display = "none";
    $('realstock-research-invest-content').style.display = "none";
    $('realstock-research-morning-content').style.display = "none";        
    $('realstock-research-company-content').style.display = "none";
    
    $('realstock-research-macro-name').className = "notnow-tab";
    $('realstock-research-industry-name').className = "notnow-tab";
    $('realstock-research-invest-name').className = "notnow-tab";
    $('realstock-research-morning-name').className = "notnow-tab";
    $('realstock-research-compay-name').className = "notnow-tab";
}

function onDataChange(obj, s)
{
    if (obj['ret'] == 'ERR_STOCK_CODE')
    {
        var stateInfo = "已退市";
        innerSet($('realstock-values-state'), stateInfo, null);
    }
    
    if (obj['ret'] != 'OK')
        return;
        
    dataObj = obj;
    upd2Finance();
    upd2KLine();    
    valuesSet();
}

function onPriceTableChange(obj)
{
    if (!obj.length || obj.length < 1)
        return;
        
    var tableStr = '<table cellpadding=0 cellspacing=0><tr class=\'hasbtm\'><td align=\'left\'>成交价</td><td align=\'right\'>成交量</td><td align=\'right\'>内盘</td><td align=\'right\'>外盘</td></tr>';
    var maxArr = {};
    var maxVal = 0;
    for(var i=0; i < obj.length; i++)
    {
        var curVal = parseInt(obj[i][1][0]);
        if (curVal > maxVal){
            maxArr = {};
            maxVal = curVal;
        }
        if (curVal == maxVal){
            maxArr[i]=1;
        }      
    }

    for(var i=0; i < obj.length; i++)
    {
        var valColor = 'nocolor';
        if (lastValue){
          if(obj[i][0] > lastValue)
              valColor = 'incolor';
              
          if(obj[i][0] < lastValue)
              valColor = 'decolor';
        }
        if(i == obj.length-1)
            tableStr += '<tr><td align=\'left\' class=\''+valColor+'\' width=\'50px\'>' + (obj[i][0]) + '</td>';
        else          
            tableStr += '<tr class=\'hasbtm\'><td align=\'left\' class=\''+valColor+'\' width=\'50px\'>' + (obj[i][0]) + '</td>';
        tableStr += '<td align=\'right\'';
        if (maxArr[i]){
           tableStr += ' class=\'incolor\' ';
        }
        tableStr += '>'+obj[i][1][0].toFixed(0)+'</td>';
        tableStr += '<td align=\'right\' class=\'decolor\'>'+obj[i][1][1].toFixed(0)+'</td>';
        tableStr += '<td align=\'right\' class=\'incolor\'>'+obj[i][1][2].toFixed(0)+'</td>';
        tableStr += '</tr>';       
    }
    tableStr +='</table>';
    innerSet($('realstock-price-table'), tableStr, null);   
  
}

var finTimer = null;
function upd2Finance()
{
    if(!dataObj)
        return;
    
    var lastv = dataObj['info']['instant'][0];
    var currv = dataObj['info']['instant'][2];
    if (currv.toFixed(3) == 0.000){
        currv = lastv;
    }

    if (znzFin)
        znzFin.update(currv);
    else
    {   
        if(finTimer)
            window.clearTimeout(finTimer);
            
        finTimer = window.setTimeout(upd2Finance, 100);    
    }    
}

var dataStrBuff = [];

function upd2KLine()
{
    //if (!dataObj['info'])
        //return;
        
    dataStrBuff.push(KISSY.JSON.stringify(dataObj));

    //updateKLineFunc();
}

var updTimer = null;
function updateKLineFunc()
{
    if (dataStrBuff.length > 0){
        datastr = dataStrBuff[0];
        if (znzK&&znzK.update(datastr)){
            dataStrBuff.shift();
        }
    }
    if (dataStrBuff.length > 0)
    {
        if(updTimer)
            window.clearTimeout(updTimer);
            
        updTimer = window.setTimeout(updateKLineFunc, 100);
    }
}

function valuesSet()
{
    var lastv = dataObj['info']['instant'][0];
    lastValue = lastv;
    var openv = dataObj['info']['instant'][1];
    var currv = dataObj['info']['instant'][2];
    if (currv.toFixed(3) == 0.000){
        currv = lastv;
    }
    var volumn = dataObj['info']['instant'][3];
    var amount = dataObj['info']['instant'][4];
    var highv = dataObj['info']['instant'][5];
    var lowv = dataObj['info']['instant'][6];
    if (lowv > 99900)
        lowv = 0;
    var ina = dataObj['info']['in'];
    var outa = dataObj['info']['out'];
    var longPrice = dataObj['info']['longPrice'];
    var buya = 0;
    var sella = 0;
    var buyas = [];
    var sellas = [];
    var nfix = 2;
    
    if (longPrice == 1)
        nfix = 3;
    
    stockFix = nfix;
    
    var limitRate = 0.1;
    if(stockName.indexOf('ST') >= 0 || stockName.indexOf('PT') >= 0)
    {
        limitRate = 0.05;
    }

    var hlimitv = (lastv * (1 + limitRate));
    var llimitv = (lastv * (1 - limitRate));
    
    innerSet($('realstock-values-hlimit'), hlimitv.toFixed(nfix).toString(), hlimitv - lastv);
    innerSet($('realstock-values-hlimit2'), hlimitv.toFixed(nfix).toString(), hlimitv - lastv);
    innerSet($('realstock-values-llimit'), llimitv.toFixed(nfix).toString(), llimitv - lastv);
    innerSet($('realstock-values-llimit2'), llimitv.toFixed(nfix).toString(), llimitv - lastv);
    
    innerSet($('realstock-marks-last'), lastv.toFixed(nfix).toString(), lastv - lastv);
    innerSet($('realstock-marks-open'), openv.toFixed(nfix).toString(), openv - lastv);
    innerSet($('realstock-marks-open2'), openv.toFixed(nfix).toString(), openv - lastv);
    innerSet($('realstock-values-value'), currv.toFixed(nfix).toString(), currv - lastv);
    innerSet($('realstock-values-value2'), currv.toFixed(nfix).toString(), currv - lastv);
    innerSet($('realstock-bs-cvalue'), currv.toFixed(nfix).toString(), currv - lastv);
    if (dataObj['info']&&dataObj['info']['items']&&dataObj['info']['items'].length>2&&dataObj['info']['items'][2].length > 0){
      var newamount = dataObj['info']['items'][2][dataObj['info']['items'][2].length-1];
      if (currDate != dataObj['info']['timeInfo'][0] && currDate != '00000000'){
        newamount = 0;
      }
      innerSet($('realstock-bs-camount'), newamount[2], 0);
      innerSet($('realstock-bs-camount2'), newamount[2], 0);
    }
    innerSet($('realstock-marks-volumn'), volumn.toString(), null);
    innerSet($('realstock-marks-volumn2'), volumn.toString(), null);
    avag = 0;
    if (volumn > 0)
        avag = (amount*100/volumn).toFixed(2);
    if (amount > 10000){
        amount = (amount/10000).toFixed(2) + " 亿元";
    }else{
        amount = amount + " 万元";
    }
    innerSet($('realstock-marks-amount'), amount.toString(), null);
    innerSet($('realstock-marks-amount2'), amount.toString(), null);
    innerSet($('realstock-marks-amount22'), amount.toString(), null);
    innerSet($('realstock-marks-avag'), avag.toString(), null);
    innerSet($('realstock-marks-high'), highv.toFixed(nfix).toString(), highv - lastv);
    innerSet($('realstock-marks-low'), lowv.toFixed(nfix).toString(), lowv - lastv);
    innerSet($('realstock-marks-high2'), highv.toFixed(nfix).toString(), highv - lastv);
    innerSet($('realstock-marks-low2'), lowv.toFixed(nfix).toString(), lowv - lastv);

    innerSet($('realstock-inout-in'), ina.toFixed(2).toString(), -1);
    innerSet($('realstock-inout-out'), outa.toFixed(2).toString(), 1);
    if (ina+outa>0){
        $('realstock-inout-outratio').style.width = parseInt(98*outa/(ina+outa))+"%";
        $('realstock-inout-inratio').style.width = (98-parseInt(98*outa/(ina+outa)))+"%";
    }else{
        $('realstock-inout-outratio').style.width = "49%";
        $('realstock-inout-inratio').style.width = "49%";
    }
    
    
    //2008/07/21 huangliang add
    if(realBlink == null)
    {
      realBlink = new blinkCharacter($('realstock-values-value'));
    }
    realBlink.blinks(currv, currv - lastv);
    
    var rate = (currv-lastv)/lastv;
    var delta = currv - lastv;
    var rateStr = (rate*100).toFixed(2).toString() + '%';
    var deltaStr = delta.toFixed(nfix).toString();
    var rateStr2 = rateStr; 
    if (delta > 0)
        rateStr = '+' + deltaStr + ' ' + '+' + rateStr; 
    else
        rateStr = deltaStr + ' ' + rateStr;     
    
    innerSet($('realstock-values-rate'), rateStr, delta);
    innerSet($('realstock-values-rate2'), deltaStr, delta);
    innerSet($('realstock-values-rate22'), deltaStr, delta);
    innerSet($('realstock-values-rate3'), rateStr2, delta);
    innerSet($('realstock-values-rate32'), rateStr2, delta);
    
    var timeStr = timeStrGen(dataObj['info']['timeInfo'][1]);
    var dataStr = dataObj['info']['timeInfo'][0].toString();
    dataStr = "行情时间：" + dataStr.substring(0,4)+"年"+dataStr.substring(4,6)+"月"+dataStr.substring(6,8)+"日";
    innerSet($('realstock-values-time'), dataStr + ' ' + timeStr, null);
    
    var stateInfo = "已退市";
    var currTime = dataObj['info']['timeInfo'][1];
    
    if(currTime < "093000" || currTime > "150000" || currTime > "113000" && currTime < "130000")
    {
        stateInfo = "交易结束";
    }
    else
    {
        stateInfo = "交易中...";
        if(volumn == 0)
            stateInfo = "停牌";
    }
    innerSet($('realstock-values-state'), stateInfo, null);
    
    if ( dataObj['info']['index'] == false)
    {
        for(var i=0; i< 5; i++)
        {
            //alert(i);
            var bv = dataObj['info']['instant'][2 * i + 7];
            var ba = dataObj['info']['instant'][2 * i + 8];
            buya += ba;
            buyas[i] = ba;
            innerSet($('realstock-bs-bvalue' + (i+1).toString()), bv.toFixed(nfix).toString(), bv - lastv); 
            innerSet($('realstock-bs-bamount' + (i+1).toString()), ba.toString(), null);
            
            var sv = dataObj['info']['instant'][2 * i + 17];
            var sa =  dataObj['info']['instant'][2 * i + 18];
            sella += sa;
            sellas[i] = sa;
            innerSet($('realstock-bs-svalue' + (i+1).toString()), sv.toFixed(nfix).toString(), sv - lastv); 
            innerSet($('realstock-bs-samount' + (i+1).toString()), sa.toString(), null);
        }
        
        if(currDate != dataObj['info']['timeInfo'][0])
        {
            currDate = dataObj['info']['timeInfo'][0];
            currItems = [];
            innerSet($('realstock-trade-table'), '', null);   
            var args = {
                method : 'get', onComplete : function(rep){
                    
                    //set stock info
                    var ret = rep;
                    var retObj;
                    try
                    {
                        retObj = KISSY.JSON.parse(ret);
                    }
                    catch(E)
                    {                       
                        //alert(E);
                        return;
                    }
                    
                    if (retObj.length == 2)
                    {
                        var start = 0;
                        if (retObj[0].length != 0 && currDate == retObj[0][0][0])
                            start  = 1;
                        
                        var len = retObj[0].length - start;
                        
                        for (var i= start; i< start + len; i++)
                            avgVol += retObj[0][i][1];
                        avgVol /= len;
                        
                        if (currDate < retObj[1][0][0])
                            totalVol = retObj[1][1][1];
                        else
                        {
                            
                            totalVol = retObj[1][0][1];
                         }   
                        energeSet();
                    }
                    
                    //upd2KLine();
                    
                
                }
            };
            
            var infoURL;
            if ( stockType == 'SH')
                infoURL = 'http://' + rdmDataDomainNameGet() + '/test/info.py/handler.znzDo?cmd=SHHQ' + stockCode + '|' + Math.random().toString();
            else
                infoURL = 'http://' + rdmDataDomainNameGet() + '/test/info.py/handler.znzDo?cmd=SZHQ' + stockCode + '|' + Math.random().toString();
            
            var myAjaj = new Ajaj(infoURL, args); 
        }
        else
        {
            energeSet();
        } 
        
    }
    else
    {
        //上涨下跌平盘
        var nInc = dataObj['info']['instant'][7];
        var nEql = dataObj['info']['instant'][8];
        var nDec = dataObj['info']['instant'][9];
        
        innerSet($('realstock-indec-inc'), nInc.toString(), 1);
        innerSet($('realstock-indec-eql'), nEql.toString(), null);
        innerSet($('realstock-indec-dec'), nDec.toString(), -1);
        
        $('realstock-indec-incratio').style.width = parseInt(nInc/(nInc+nEql+nDec)*100)+"%";
        $('realstock-indec-eqlratio').style.width = parseInt(nEql/(nInc+nEql+nDec)*100)+"%";
        $('realstock-indec-decratio').style.width = parseInt(nDec/(nInc+nEql+nDec)*100)+"%";
        
        
        //委买委卖
        buya =  dataObj['info']['instant'][10];
        sella =  dataObj['info']['instant'][11];
        
    }

    innerSet($('realstock-analyst-sell'), sella.toFixed(0).toString(), -1);
    innerSet($('realstock-analyst-buy'), buya.toFixed(0).toString(), 1);
    
        
    var bsRate;
    if (buya + sella !== 0)
        bsRate = (buya - sella)/(buya + sella);
    else
        bsRate = 0;
    var bsDelta = buya - sella;
    var bsRateStr = (bsRate * 100).toFixed(2).toString() + '%';
    innerSet($('realstock-analyst-rate'), bsRateStr, bsDelta);
    innerSet($('realstock-analyst-delta'), bsDelta.toFixed(0).toString(), bsDelta);
    innerSet($('realstock-analyst-buysell'), bsDelta.toFixed(0)+"&nbsp;&nbsp;&nbsp;"+bsRateStr, bsDelta);
    for (var i = 0; i < 5; i++){
        if (typeof buyas[i] == "undefined")
            break;
        if (buya + sella !== 0){
            $('realstock-bs-buyratio'+i).style.width = parseInt((buyas[5-i-1]/(buya + sella) * 190)) + 'px';
            $('realstock-bs-sellratio'+i).style.width = parseInt((sellas[i]/(buya + sella) * 190)) + 'px';
        }else{
            $('realstock-bs-buyratio'+i).style.width = '19px';
            $('realstock-bs-sellratio'+i).style.width = '19px';
        }
    }
    $('realstock-ibs-buyratio').style.width = ((98+parseInt(bsRate * 98))/2) + '%';
    $('realstock-ibs-sellratio').style.width = (98-((98+parseInt(bsRate * 98))/2)) + '%';

    if (dataObj['info']&&dataObj['info']['items']&&dataObj['info']['items'].length>2)
    {
        var tmpbsV;
              
        for( var i=0; i< dataObj['info']['items'][2].length; i++)
        {                    
            if( currItems.length == 10)
            {
                tmpbsV = currItems[0][1];
                currItems.remove(0);
            }
            currItems.push(dataObj['info']['items'][2][i]);
        }
            
        var tableStr = '<table cellpadding=0 cellspacing=0>';
        for(i=currItems.length-1; i>= 0; i--)
        {
            var volColor = 'nocolor';
            var valColor = 'nocolor';
            
            if (currItems[i][3] == 'B' && dataObj['info']['index'] == false)
            {
                volColor = 'incolor';
            }
            if (currItems[i][3] == 'S' && dataObj['info']['index'] == false)
            {
                volColor = 'decolor';
            }
            
            if(currItems[i][1] > lastv)
                valColor = 'incolor';
                
            if(currItems[i][1] < lastv)
                valColor = 'decolor';
            
            if(i == 0)
                tableStr += '<tr><td align=\'left\' width=\'50px\'>' + timeStrGen(currItems[i][0]) + '</td>';
            else          
                tableStr += '<tr class=\'hasbtm\'><td align=\'left\' width=\'50px\'>' + timeStrGen(currItems[i][0]) + '</td>';
                
            tableStr += '<td align=\'right\' class=\'' + valColor + '\'>' + currItems[i][1].toFixed(nfix) + '</td>'
            if( i==0 && tmpbsV && currItems[i][1] > tmpbsV || i !=0 && currItems[i][1] > currItems[i-1][1])
                tableStr += '<td align=\'center\'><img src=\'images/inc_bg.gif\'/></td>';
            else if( i==0 && tmpbsV && currItems[i][1] < tmpbsV || i !=0 && currItems[i][1] < currItems[i-1][1])
                tableStr += '<td align=\'center\'><img src=\'images/dec_bg.gif\'/></td>';
            else
                tableStr += '<td align=\'center\'>&nbsp;&nbsp;</td>';
                
            tableStr += '<td align=\'right\' class=\'' + volColor + '\'>' + currItems[i][2] + '</td>';
            tableStr += '</tr>';
            
            //tableStr += '<td width=\'20%\' align=\'right\' class=\'' + color + '\'>' + tradeType + '</td></tr>';
            
        }
        tableStr +='</table>';
        innerSet($('realstock-trade-table'), tableStr, null);   

    }

    //upd2KLine();
}

function energeSet()
{
    if(avgVol == 0 || totalVol == 0)
        return;
    
    changeRate = (dataObj['info']['instant'][3]*100)/(totalVol*10000);
    
    var changeStr = (changeRate * 100).toFixed(2).toString() + '%';
    innerSet($('realstock-energe-change'), changeStr, 0);

    
    var ticks = dataObj['info']['start'] + dataObj['info']['history'].length -1;
    var rateStr;
    if(ticks == 0)
        rateStr = '--';
    else
        rateStr = ((dataObj['info']['instant'][3]*100)/(avgVol*ticks/240)).toFixed(2).toString();
    
    innerSet($('realstock-energe-rate'), rateStr, 0);
    
}

//2008/07/21 huangliang add 
function blinkCharacter(obj)
{	
	this.obj = obj;
	this.before = 0.00;
}

blinkCharacter.prototype._blink = function()
{
		if(this.timer)
			  window.clearTimeout(this.timer);
		if( this.times == 11)
		{
		    this.obj.style.color = this.color;
			  return;
		}
		
		if(this.times % 2 == 0)
		{
			  this.obj.style.color = this.color;
		}
		else
		{
			this.obj.style.color = "#000000";
		}
		
		this.times ++;
		this.timer = window.setTimeout(this._blink.bind(this) , 150);
}

blinkCharacter.prototype.blinks = function(cur, val)
{
	if(cur == this.before)
		return ;
	this.color = val > 0 ? "#FF0000" : (val == 0 ? "#000000" : "#00A000");
	this.times = -1;
	this._blink();
	this.before = cur;
}


function onIndChange(indKey)
{
    if(currentIndc == indKey)
    {
        return;
    }
    if(!isHtml5Mobile)
    {
        if(currentIndc != null)
        {
            znzK.indRemove(currentIndc);
        }
        currentIndc = indKey;
        if(!indKey)
        {
            return;
        }
        znzK.indAdd(indKey);
    }
    else
    {
        var p = [];
        if(indKey)
            p.push(indKey);
         
        if(currentIndc)
            p.push(currentIndc);
        
        znzK.indReplace(p);
        currentIndc = indKey;
    }
    
}

function onKTypeChange(ktype)
{
    if(isHtml5Mobile)
    {   if(ktype != '')
            $('add-index-div-html5').style.display = "";
        else
            $('add-index-div-html5').style.display = "none";
    }
    if (currentTime != ktype){
        currentTime = ktype;
        if (znzK){
            if(!isHtml5Mobile){
                
                znzK.restart(ktype, stockCode, stockType, stockName);
                znzK.updateSpecial();
            }
            else
            {
                ktype = ktype || 'real';
                znzK.changeKType(ktype);
            }
        }
        
        
    }
}

var gsInfoShowStat = false;
function gsIntroChange(show)
{
    if(show == null)
    {
        show = gsInfoShowStat ? false: true;
    }
    
    if(show == true)
    {
        $('realstock-finance-gsbutton').innerHTML = '隐藏说明文字';
        $('realstock-finance-gsintro').style.display = 'block';
        gsInfoShowStat = true;
    }
    else
    {
        $('realstock-finance-gsbutton').innerHTML = '显示说明文字';
        $('realstock-finance-gsintro').style.display = 'none';
        gsInfoShowStat = false;
    }
}
window.onload = start;

