var radio_stream_id = '';
var stream_update_next = 15; // in seconds...
var stream_update_pe = null;
var UpdateAfter = 15;


function stream_update_info() {


			var rand_no = Math.random();
	    rand_no = rand_no * 100;
	
		new Ajax.Request('static/data_onair/cityfm.json?t='+rand_no,
		{
			method:'get',
			onSuccess: function(transport) {
				var ret = transport.responseText.evalJSON()
							
				if (ret != '') {
					Try.these(
						function() {
							var s_text = '';
							var s_interpret = ret['xml']['now-interpret'];
							var s_title = ret['xml']['now-skladba'];
							var s_songid = ret['xml']['now-id'];
	
							if (s_interpret == null) s_interpret = '';
							if (s_title == null) s_title = '';
							
// i tady prestavam pouzivat "UpdateAfter", proste aktualizuju prave hranou pisnicku kazdych 15 sekund							
//							UpdateAfter = ret['xml']['refresh'] + rand(5, 15);
							if (UpdateAfter < -30) {
								s_interpret = '';
								s_title = '';
								UpdateAfter = rand(40, 60);
							}
							
							
							s_text = s_interpret;
							if (s_title != '') {
								if (s_text != '') {	s_text += ' - '; }
								s_text += s_title;
							}
							// s_text += ' | update in: ' + UpdateAfter;
						//puvodne:	
						//	stream_update_info_text(s_text);
						//	$('head_onair_song').href = 'index.php?pageid='+songinfo_pid+'&songid='+s_songid;
						
						//start
						stream_update_info_text(s_text);
							//odkaz na kontakt - edited by svetinsky
							if (s_interpret == 'radio City 93,7 FM') {
								$('head_onair_song').href = 'index.php?pageid=4075';
							}else{
							//konec odkaz na kontakt
							$('head_onair_song').href = 'index.php?pageid='+songinfo_pid+'&songid='+s_songid;
							}
						//konec
						
						}, 
						function() {
							stream_update_info_text('');
						}
					)
					
					Try.these(
						function() {
	
							var a_text = ret['xml']['onair-title'];
							
							onair_update_info_text(a_text);
						},
						function() {
							onair_update_info_text('');
						}
					);
				}
				
				if (UpdateAfter < 15) UpdateAfter = rand(20, 35);
				if (UpdateAfter > 120) UpdateAfter = rand(105, 130);
				
				stream_update_pe = new PeriodicalExecuter(function(pe) {
					pe.stop();
					stream_update_info();
				}, UpdateAfter);
			},
			
					
		onException: function(transport){
				//console.log("Nastala vyjimka. NEJEDU DAL.")
				
				stream_update_pe = new PeriodicalExecuter(function(pe) {
				pe.stop();
				stream_update_info();
			}, 15); 
			}
		
	
	});	
	
}

function onair_update_info_text(text) {
	if ($('head_onair_info').innerHTML != text) {
		setTimeout("Effect.Fade('head_onair_info', { duration: 0.7 } )", 250);
		cmd = "$('head_onair_info').innerHTML = '" + replaceAll(text, "'", "&#39;") + "';";
		setTimeout(cmd, 1000);
		setTimeout("Effect.Appear('head_onair_info', { duration: 0.7 } )", 1250);
	}
}
function stream_update_info_text(text) {
	if ($('head_onair_song').innerHTML != text) {
		setTimeout("Effect.Fade('head_onair_song', { duration: 0.7 } )", 250);
		cmd = "$('head_onair_song').innerHTML = '" + replaceAll(text, "'", "&#39;") + "';";
		setTimeout(cmd, 1000);
		setTimeout("Effect.Appear('head_onair_song', { duration: 0.7 } )", 1250);
	}
}

radio_stream_id = 'cityfmnew';
stream_update_pe = new PeriodicalExecuter(function(pe) {
		pe.stop();
		stream_update_info();
	}, 1
);
