var MPlayer = {
	currentSong: "",
	currentArtist: "",

	updateData: function()
	{
		if (config.playlist == "1") {
			setInterval("MPlayer.getPlaylist()",3000);
		}else{
			jQuery("#playlist, #playlistSwitch, #artistFoto, #currentTrack").hide();
		}
	},
	
	updateBrowserTitle: function()
	{
	    jQuery("title").text(this.currentArtist + ' - ' + this.currentSong + ' (' + config.title + ')');
	},

	setPlayer: function(streamURI)
	{
		jQuery(document).ready(function(){
			jQuery('#flashPlayer').flash(
				{ src: 'http://mjoy.ua/design/player/player.swf',
					wmode: 'transparent',
					allowscriptaccess: 'always',
					flashvars: {
						stream: streamURI,
						autoplay: config.autoplay
					},
					width: 500,
					height: 75 },
				{ version: 9,
					expressInstall: true }
	    	);
    	
	    	jQuery('.stationLogo img').attr("src", 'http://mjoy.ua/design/logos/' + config.station + '.gif');
		});
	},

	getPlaylist: function() {
		jQuery.getJSON("/json.php?s=mjoy.ua/radio/station/" + config.station + "/playlist.json", function(songlist){
			MPlayer.updatePlaylist(songlist);
		});
	},

	updatePlaylist: function(songlist) {
		output = "<h3>Плейлист:</h3>";
		songStatus = "";
		for(i = 0; i < songlist.length; i++) {
			tmp = '<span>' + songlist[i].time + '</span> ' + songlist[i].artist + ' - ' + songlist[i].title;
			
			switch(songlist[i].status) {
				case 'history': 
					tmp = '<em>' + tmp + '</em>';
					break;
				case 'current': 
					tmp = '<strong>' + tmp + '</strong>';
					if (this.currentSong == songlist[i].title) return;
					this.currentSong = songlist[i].title;
					songStatus = this.prepareCurrent(songlist[i]);
					break;
			}
			
			output += "<li>" + tmp + "</li>";
		}

		if (jQuery("#currentTrack").html() != songStatus) {
			jQuery("#artistFoto img").attr("src","http://mjoy.ua/radio/foto.php?a=" + encodeURIComponent(this.currentArtist));
			if (document.URL.indexOf('#foto') < 0) jQuery("#artistFoto img").attr("style","visibility: hidden;");
			jQuery("#currentTrack").html(songStatus);
			jQuery("#playlist").html(output);
			this.updateBrowserTitle();
		}
	},

	prepareCurrent: function(song) {
		this.currentArtist = song.artist;
		return '<strong>' + song.artist + '</strong>' + song.title + '<em>' + song.album + '</em>';
	}
};

 function filterData(data){
    // filter all the nasties out
     // no linebreaks
    data = data.replace(/[\r|\n]+/g,'');
    // no comments
    data = data.replace(/<--[\S\s]*?-->/g,'');
    // no noscript blocks
    data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g,'');
    // no script blocks
    data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g,'');
    // no self closing scripts
    data = data.replace(/<script.*\/>/,'');
    // [... add as needed ...]
    return data;
  }

function getMaskPosition(element) {
	var position = element*250;
	position = (position > 0) ? position : 0;
	return "-" + position + "px 0px";
}

function updateSongTitles() {
	var url = "config.json";
	jQuery.getJSON(url, function(data){
        jQuery.each(data, function(item,stream){
			if (item != "player") {
				if (stream.playlist == "1") setCurrentSong(item);
			}
		});
	});
}

function updateTitles() {
	setInterval('updateSongTitles()', 5000);
}

