radio = new function(){
	$j = jQuery.noConflict();
	$j.ajaxSetup({async: false});
	
	this.has_songs = false;
	this.active = 'now_playing';
	this.next_second = false;
	this.current_tst = 0;
	this.timestamp = 0;
	this.next_tst = 0;
	this.checks = 0;
	
	$j(document).ready(function(){
		radio.init();
	});
	
	this.songs = new Array();
	
	function song( title, artist, tst, artist_id, cover_id ){
		this.title = title;
		this.artist = artist;
		this.tst = tst;
		this.artist_id = artist_id;
		this.cover_id = cover_id;
	}
	
	this.init = function(){
		radio.updateSongs();
		radio.updatePlaylist();
		if(radio.has_songs){
			$j('#radio_player').show();
			radio.nowPlayingActive();
			setInterval( 'radio.checkSwitch()', 10000);
		}
		return true;
	}
	
	this.checkSwitch = function(){
		radio.timestamp = radio.timestamp*1 + 10;
		//alert(radio.timestamp + ' ' + radio.switch_at);
		if(radio.timestamp >= radio.switch_at + 10){
			//alert('update');
			radio.updateSongs();
			radio.updatePlaylist();
		}
	}
	
	
	this.displayData = function(teraz_tyt, teraz_wyk, teraz_id_wyk, teraz_idu,
						pozniej_tyt, pozniej_wyk, pozniej_id_wyk, pozniej_idu){

		$j('#now_playing #song_title').text(teraz_tyt);
		$j('#now_playing #artist_name').text(teraz_wyk);
		artist_link = 'http://miastomuzyki.pl/bio,' + teraz_id_wyk;
		$j('#now_playing #artist_name').attr({href: artist_link});
		
		id_short = Math.floor(teraz_idu/1000);
		cover_src = 'http://doc.rmf.pl/media/img_muzyka/utwor/' + id_short + '/' + teraz_idu + '.jpg';
		$j('#now_playing .cover img').attr({src: cover_src});
		
		$j('#next_playing #song_1 .line_1 a').text(pozniej_wyk);
		if(pozniej_id_wyk){
			artist_link = 'http://miastomuzyki.pl/bio,' + pozniej_id_wyk;
		}else{
			artist_link = 'http://miastomuzyki.pl';
		}
		$j('#next_playing #song_1 .line_1 a').attr({href: artist_link});
		$j('#next_playing #song_1 .line_2 .song_title').text(pozniej_tyt);
		
	}
	
	this.insertData = function(data){
		if(data.teraz){
			radio.has_songs = true;
			
			radio.displayData(data.teraz.tyt, data.teraz.wyk, data.teraz.id_wyk,
							data.teraz.idu, data.pozniej.tyt, data.pozniej.wyk,
							data.pozniej.id_wyk, data.pozniej.idu);
			
			radio.current_tst = data.teraz.tst;
			radio.timestamp = data.tczas;
			radio.switch_at = data.pozniej.tst;
			
			return true;
		}else{
			return false;
		}
	}
	
	this.updateSongs = function(){
	    tmp = $j.getJSON("radio.php", radio.insertData);
		return tmp;
	}
	
	this.updatePlaylist = function(){
	    tmp = $j.getJSON("radio_playlista.php", radio.insertPlaylistData);
		return tmp;
	}
	
	this.insertPlaylistData = function(data){
		if(data.items){
			i = 0;
			for(i=0;i<data.items.length;i++){
				//songs[i] = new song(item.song_title, item.artist_name, item.tst, item.artist_id, 0);
				if(data.items[i].tst>radio.switch_at){
					radio.next_second = true;
					$j('#next_playing #song_2 .line_1 a').text(data.items[i].artist_name);
					if(data.items[i].artist_id){
						artist_link = 'http://miastomuzyki.pl/bio,' + data.items[i].artist_id;
					}else{
						artist_link = 'http://miastomuzyki.pl/';
					}
					
					$j('#next_playing #song_2 .line_1 a').attr({href: artist_link});
					$j('#next_playing #song_2 .line_2 .song_title').text(data.items[i].song_title)
					break;
				}
			}
		}else{
			return false;
		}
	}
	
	
	this.nextPlayingActive = function(){
		this.active='next_playing';
		$j('#now_playing h3 img').attr({src: "/i/radioArrowsInactive.gif"});
		$j('#next_playing h3 img').attr({src: "/i/radioArrowsActive.gif"});
		$j('#now_playing .cover').hide();
		$j('#now_playing .label_1').hide();
		$j('#now_playing .label_2').hide();
		
		$j('#next_playing #song_1 .label_1').show();
		$j('#next_playing #song_1 .label_2').show();
		
		if(this.next_second){
			$j('#next_playing #song_2').show();
		}
	}
	
	this.nowPlayingActive = function(){
		this.active='now_playing';
		$j('#now_playing h3 img').attr({src: "/i/radioArrowsActive.gif"});
		$j('#next_playing h3 img').attr({src: "/i/radioArrowsInactive.gif"});

		$j('#now_playing .cover').show();
		$j('#now_playing .label_1').show();
		$j('#now_playing .label_2').show();
		
		$j('#next_playing #song_1 .label_1').hide();
		$j('#next_playing #song_1 .label_2').hide();
		
		$j('#next_playing #song_2').hide();
	}
	
	this.switchPanel = function(){
		if(this.active=='now_playing'){
			this.nextPlayingActive();
		}else{
			this.nowPlayingActive();
		}
	}
}