SHOW_DELAY = 2000;
HIGHLIGHT_TIME = 10000;
REFRESH_TIME = 20000;
STOP_TIME = 1800;

var response = 0;
var request = 0;
var in_progress = 1;
var g_tm = ''

function go(typ) {  // bezi REFRESH_TIME/1000 sekund * pocet requestov, napr. 30*30 sekund, 15 minut 
  request++;        // alert((REFRESH_TIME/1000) * request)

  var sec_in_progress = (REFRESH_TIME/1000) * request
  if (( sec_in_progress < STOP_TIME) && (in_progress)) {
    write_streamer_data(typ)

	if (request > 0 && typ == 'title') {
      rs = randomString(32)
      $("#graf_px").empty().hide();
	  $("#graf_px").html('<img src="http://www.akcie-2000.cz/g/online-index-px-burza-praha.png?x='+rs+'" alt="online index px" border="0" width="234" height="150" />');
      $("#graf_px").fadeIn(1000);
      $("#graf_rm").empty().hide();  
      $("#graf_rm").html('<img src="http://www.akcie-2000.cz/g/online-index-rm-system-praha.png?x='+rs+'" alt="online index rm" border="0" width="234" height="150" />');
      $("#graf_rm").fadeIn(1000);
    }
    
    //if (request > 1) {
      //$("#advskymove").empty() 
        //$("#advskymove > iframe").remove();        
      //$("#advskymove").html("<script type='text/javascript'>\n_google_ad_client = 'pub-8271295218880500'\n_google_ad_comment ='/* 160x600, Kurzy.cz main */'\n_google_ad_slot = '8316290599'\n_google_ad_width = '160'\n_google_ad_height = '600'\n</script>\n<script language='javascript1.2' type='text/javascript' src='http://ad1.kde.cz/stickygooglesky_view.js'></script>") 
    //}

	if  ((request % 3 == 0) || (request<2)) //#kazdu  ( % 3 ) poziadavku nacitaj spravy  
	{
        write_streamer_data("zpr_ptek")   
        write_streamer_data("naz")
	}
    setTimeout("go('"+typ+"')", REFRESH_TIME);
  } else {
	in_progress = 0
    stream_stoped()
  }
}

function stream_stoped() {
    $("#control_status").text("Vypnuto").css("color", "red")
    $("#control_button").attr("value", "Zapnout").css("color", "green").css("font-size", "12px")
    $("#data_online_tab").css("color", "#808080")    
}

function online_arrows(val_old, hodnota, typ, j) {
    if (hodnota != " ") {
        var id_img = "#span_" + typ + "_" + j
        if (val_old != hodnota) {
            if (val_old > hodnota) { pom = "arrd.gif" } 
            else { pom = "arru.gif" }            
            setTimeout('$("' +id_img + '").html("<img src=http://i.fin.cz/i/flag/'+pom+' border=0 width=8 height=10 />")', SHOW_DELAY)
            setTimeout('$("' +id_img + '").html("  ")', HIGHLIGHT_TIME)
        } 
    }
}

function child_val_delay(id, val, type) {
    if (val != " ") {
        if (request > 1) {
            setTimeout('$("#' + id + '").html = ""', SHOW_DELAY)
            if (type == "zm") val += "%"
            setTimeout('$("#' + id + '").text("' +val+ '")', SHOW_DELAY)
        } else {
            child_val(id, val, type)
        }
    }
}

function green_red(id, hodnota) {
  if (hodnota>0)
    setTimeout('$("#' + id + '").css("color", "green")', SHOW_DELAY)  
  else
    setTimeout('$("#' + id + '").css("color", "red")', SHOW_DELAY) 
}

function gs(res_str, cas) { g_write(res_str, cas, "spad") }    

function g(res_str, cas) { g_write(res_str, cas, "title") }

function g_write(res_str, cas, typ) {
  response++;
  g_tm = cas.substring(9) //"16.01.09 13:09:03"    

  res_str = decode(res_str)
  var results = res_str.split(",")
  var g_result_length = get_result_length(results)
  if (typ == "title")   {
    var show_change_req = 0
    var sp = new Array("ka","zm","ob")
  } else {
    var show_change_req = 1
    var sp = new Array("ka","zm", "po", "na", "ob", "ok", "o2" , "t1", "ce", "k2", "t2")
  }
  var j = 0

  for (var i = 0; i < g_result_length; i = i + sp.length) {
    var k = 0
    var change_on_row = 0
    for(var sp_index in sp) {
      var id = "span_" + sp[sp_index] + "_" + j

      try 
      { 
          var val_old = document.getElementById(id).innerHTML.replace('%', '')
      } catch(e) {
        continue
      }

      var hodnota = results[i + k]

      if (sp[sp_index].substring(0,1) == "t")  { 
          if (hodnota.substring(2,3) != ".")
            hodnota = hodnota.substring(0,2) + ':' + hodnota.substring(2,4) + ':' + hodnota.substring(4,6) 
      }
      if (sp[sp_index] == "ka")  { online_arrows(val_old, hodnota, "im", j) }
      if (response > show_change_req) {
        if (sp[sp_index] == "na")  { online_arrows(val_old, hodnota, "imna", j) }
        if (sp[sp_index] == "po")  { online_arrows(val_old, hodnota, "impo", j) }
      }

      if ((val_old != hodnota) && (hodnota != -1) && (hodnota != ' ')) {
	    child_val_delay(id, hodnota, sp[sp_index])
	    if (sp[sp_index] == "zm") green_red(id, hodnota)
	    if (val_old != "" ) {
            setTimeout('$("#' + id + '").attr("class", "val_bold")', SHOW_DELAY)
	        setTimeout('$("#' + id + '").attr("class", "val_nobold")', HIGHLIGHT_TIME)
	    }        
        if (sp[sp_index] != "t1") change_on_row = 1
      } k++
    }

    if ((change_on_row == 1) && (response > show_change_req)) {
      id = "tr_" + j
      $("#" + id).attr("class", "gold") 
      var p = "nogoldgray"
      if ((j % 2) == 0)  p = "nogold" 
      setTimeout('$("#' + id + '").attr("class", "' +p+ '")', HIGHLIGHT_TIME)
    } j++
  }
}
