// uaktualnienie playlisty w playerze
updatePlaylist = function() {

	timeStamp = Math.round(new Date().getTime()*0.001);

	// skracanie dlugich tagow
	var short = function(text, maxLen) {
		if (!text) return '&nbsp;';
		if (maxLen < text.length) {
			return text.substr(0, maxLen-1) + '...';
		} else {
			return text + '&nbsp;';
		}
	}
	// uaktualnia zakladke w playerze
	var updateTab = function(id, data) {

		var albumName  = utfToiso(data.album);
		var artistName = utfToiso(data.artist);
		var songTitle  = utfToiso(data.title);

		var length = 30;
		if ($('t'+id+'_album')) {
			$('t'+id+'_album').innerHTML = short(albumName, 30);
		} else if (3 == id) {
			length = 18;
		}
		if (undefined !== window.forceMaxLen) length = forceMaxLen;
		if (0 != data.id) {
			$('t'+id+'_artist').innerHTML = short(artistName, length);
		} else {
			if (undefined !== window.forceMaxLen)
				$('t'+id+'_artist').innerHTML = short(artistName, length);
			$('t'+id+'_artist').innerHTML = artistName;
		}
		$('t'+id+'_title').innerHTML = short(songTitle, length);
		if (data.image) {
			var img = data.image;
		} else if (0 == data.id) {
			var img = 'http://i.wp.pl/a/i/wpradio/radio_reklama.jpg';
		} else {
			if ((undefined !== window.stationType) && ('kids' == window.stationType))
				var img = 'http://110.img.tp.ngsoft.pl/329.jpg';
			else
				var img = 'http://i.wp.pl/a/i/tpradio/blank-4.jpg';
		}
		$('t'+id+'_img').src = img;

	}

	var n = playlistElements.length;
	for (var i = 0; i < n - 1; i++) {

		var start = playlistElements[i].start;
		if (start <= timeStamp - timeDifference+3 && (playlistElements[i+1].start > timeStamp - timeDifference-3 || n-1 == i)) {

			// aktualny utwór
			if (currentTabs[1].start != playlistElements[i].start) {
				updateTab(3, playlistElements[i]);
				currentTabs[1].start = playlistElements[i].start;
				currentId = playlistElements[i].id;
			}

		}
	}

}

var currentTabs = [{start:0},{start:0},{start:0}];
timeStamp = new Date().getTime()*0.001;
currentId = -1;

runOutsidePlayer = function(num) {
	var pWindow = window.open('http://radio.wp.pl/player.html?station='+num, 'player', 'width=451,height=262,menubar=no,toolbar=no,location=no,scrollbars=no,resizable=no,status=no');
	pWindow.focus();
}


function utfToiso(strToRepl) {

	strToRepl = escape(strToRepl);
	strToRepl = unescape(strToRepl.replace(/%3Cdescription%3E.+?%3C\/description%3E/gi, ''));
	strToRepl = (strToRepl.replace(/&/gi, '&amp;'));
	strToRepl = escape(strToRepl);

	/*
	strToRepl = (strToRepl.replace(/%B1/gi,'%u0105'));  //ą
	strToRepl = (strToRepl.replace(/%B6/gi,'%u015B'));  //ś
	strToRepl = (strToRepl.replace(/%u013D/gi,'%u017A')); //ź
	strToRepl = (strToRepl.replace(/%u02C7/gi,'%u0104')); //Ą
	strToRepl = (strToRepl.replace(/%A6/gi,'%u015A')); //Ś
	strToRepl = (strToRepl.replace(/%AC/gi,'%u0179')); //Ź
	strToRepl = (strToRepl.replace(/%BF/gi, '%u017C')); //ż
	strToRepl = (strToRepl.replace(/%B3/gi, '%u0142')); //ł
	strToRepl = (strToRepl.replace(/%EA/gi, '%u0119')); //ę
	strToRepl = (strToRepl.replace(/%BC/gi, '%u017A')); //ź
	strToRepl = (strToRepl.replace(/%u015A/gi, '%u015A')); //Ś
	strToRepl = (strToRepl.replace(/%AF/gi, '%u017B')); //Ż
	strToRepl = (strToRepl.replace(/%F1/gi, '%u0144')); //ń
	strToRepl = (strToRepl.replace(/%E6/gi, '%u0107')); //ć
	*/

	strToRepl = (strToRepl.replace(/%3F/gi, '%B1')); //ą

	strToRepl = unescape(strToRepl);

	return strToRepl;

}
