function SPPlaylist()
{
	this.request = null;
	this.intervalID = null;
	this.intervalTime = 45000; // ms
	this.data = null;
	this.isStopped = false;
	this.isRedirectValue = false;
	this.streamID = null;
}

SPPlaylist.prototype.redirect = function(isRedirectValue)
{
	this.isRedirectValue = isRedirectValue;
}

SPPlaylist.prototype.isRedirect = function()
{
	return this.isRedirectValue;
}

SPPlaylist.prototype.start = function(streamID)
{
	var obj = this;
	this.request = null;
	this.streamID = streamID;
		
	if($('#playlist-spc-stream-id-' + streamID).attr('value').split('.')[2] == 1)
	{
		this.request = PLAYLIST_DATA_BASE_URL + 'm/playlist/channel-' + streamID + '.jsonp';
	}
	
	if($('#playlist-spc-stream-id-' + streamID).attr('value').split('.')[4] == 1)
	{
		$('#tab-plistfabryka .pl-customtop').html('<img class="nowy-kanal" src="' + STATIC_PATH + '/fm-content/fm-pic/nowy-kanal.png" alt="" />');
	}
	else
	{
		$('#tab-plistfabryka .pl-customtop').html('');
	}

	$('#tab-plistfabryka').removeClass();
	$('#tab-plistfabryka').addClass('onetab');
	
	$('#tab-plistfabryka').addClass( 
		$('#playlist-spc-stream-id-' + streamID).attr('value').split('.')[0]
	);
	
	$('#playlist-spc-stream-id-' + streamID).attr('selected', 'selected');
	$('.onetab .pl-gotoplayer h4 a').attr('href', BASE_URL + 'player?streamId=' + streamID + '&title=' + $('#playlist-spc-stream-id-' + streamID).attr('rel'));
	$('.onetab .pl-gotoplayer .start-player').attr('href', BASE_URL + 'player?streamId=' + streamID + '&title=' + $('#playlist-spc-stream-id-' + streamID).attr('rel'));
	
	obj.render();
	this.stop();
	
	this.intervalID = setInterval ( function() 
	{
		obj.render()
	}, 
	this.intervalTime );
}

SPPlaylist.prototype.stop = function()
{
	if(this.intervalID)
	{
		clearInterval(this.intervalID);
	}
}

SPPlaylist.prototype.render = function()
{
	var obj = this;

	switch(obj.streamID)
	{
		case '11':
			$('#playlistTopLayer').html('<a href="http://eska.pl/moja10hitow">Zobacz stronę programu na eska.pl</a>');
			break;
		case '18':
			$('#playlistTopLayer').html('<a href="http://eska.pl/brit40">Zobacz stronę programu na eska.pl</a>');
			break;
	}
	if(this.request !== null)
	{
		$.jsonp({
                    url: this.request,
                    callback: 'jsonp',
                    "success": function(data) {
                        obj.data = $(data);
                        obj.renderList();
                    }

                });
	}
}

SPPlaylist.prototype.renderList = function()
{
	var playlistElements = new Array();
	var showElement = null;
	var firstElement = null;
	
	var htmlPlaylistElement = null;	
	var nameOfArtistLength = 20;
	var nameLength = 17;
	$.grep(this.data, function(song) {
		var id = song.id;
		// artists
		var artists = new Array();
		$.grep(song.artists, function(artist) {
			artists.push(artist.name);
		});
		var name = song.name;
		var status = song.status;
		var image = song.images['45x45'];
		
		// pierwszy lepszy
		if(firstElement == null)
		{
			firstElement = status;
		}		

		// aktualny status
		if(showElement == null && status == 0)
		{
			showElement = 0;
		}
		if(!id)
		{
			image = STATIC_PATH + '/fm-content/fm-pic/blank-45-hit.png';
		}	
		else if(image.indexOf('alsongreserve.jpg', 0) != -1)
		{
			image = STATIC_PATH + '/fm-content/fm-pic/blank-45-hit.png';			
		}

		var playlistElementCss = null;
		
		if(parseInt(status) == 0)
		{
			playlistElementCss = 'item-c';
		}
		else if(parseInt(status) == 1)
		{
			playlistElementCss = 'item-n1';
		}
		else if(parseInt(status) > 1)
		{
			playlistElementCss = 'item-n';
		}
		else if(parseInt(status) == -1)
		{
			playlistElementCss = 'item-p1';
		}
		else if(parseInt(status) < -1)
		{
			playlistElementCss = 'item-p';
		}	
		
		htmlPlaylistElement = 
		'<li id="playlist_status_' + status + '" class="' + playlistElementCss + '">' +
		'<div class="item">' +
		'<img alt="" width="45" height="45" src="' + image + '">' +
		'<p><strong>' + (name.length > nameLength  ? name.substring(0, nameLength) + '...' : name) + '</strong><br>' + ((artists.join(', ').length > nameOfArtistLength) ? artists.join(', ').substring(0, nameOfArtistLength) + '...' : artists.join(', ')) + '</p>' +
		'</div>' +
		'</li>';
		playlistElements.push(htmlPlaylistElement);
				
	});

	$('.pl-playlist ul.playlist').html(playlistElements.join(''));
	
	if(!this.isStopped)
	{
		this.show( showElement == null ? firstElement : showElement );
	}
}

SPPlaylist.prototype.show = function(status)
{	
	var element = null;
	var itemRenderHtml = new Array();
	
	var nameLength = 50;
	var artistTextMaxLength = 100;
	var tagsTextMaxLength = 20;
	
	$.grep(this.data, function(song) {
		
		var elementStatus = song.status;
		if(elementStatus == status)
		{
			element = song;
		}
	});
	if(element !== null)
	{
		// elements
		var id = element.id;
		var name = element.name;
		var status = element.status;
		var image = element.images['200x200'];
		
		var artists = new Array();
		var artist_names = new Array();
		
		var mp3 = '';
		var full_mp3 = '';
		var see_video = 0;
		
                var link = element.link ? element.link.replace('eska.pl', 'fabrykamuzy.pl') : '';
                var link_fb = element.fb_link ? element.fb_link.replace('eska.pl', 'fabrykamuzy.pl') : '';
                
		var isLyrics = element.isLyrics;
		
		if(!id.length)
		{
			image = STATIC_PATH + '/fm-content/fm-pic/blank-100-hit.png';
			
			artists.push(
				'<a href="javascript:void();">' + element.artists[0].name + '</a>'
			);
			artist_names.push(element.artists[0].name);
		}
		else
		{
			if(image.indexOf('alsongreserve.jpg', 0) != -1)
			{
				image = STATIC_PATH + '/fm-content/fm-pic/blank-100-hit.png';			
			}
			
			mp3 = element.mp3; 
			full_mp3 = element.full_mp3
			muzodajnia = element.muzodajnia;
			see_video = element.see_video;
			
			var artistTextLengthCounter = 0;
			
			$.grep(element.artists,
				function(artist) {
					if(artistTextMaxLength > artistTextLengthCounter)
					{
						var artistName = artist.name;
						
						if(artistName.length + artistTextLengthCounter > artistTextMaxLength)
						{
							artistName = artistName.substr(0, artistName.length + artistTextLengthCounter - artistTextMaxLength);
						}
						
						artists.push(
								'<a href="' + artist.link.replace('eska.pl', 'fabrykamuzy.pl') + '">' + artistName + '</a>'
						);
						
						artistTextLengthCounter = artistTextLengthCounter + artist.name.length;
						artist_names.push(artistName);
					}
			});
		}		

	    if (isLyrics)
	    {
	    	var lyrics_link = '<a href="' + (id.length ? link : "javascript:void(0);") + '#lyrics" class="txt-icon-hits">txt</a>';
	    }
	    else
	    {
	    	var lyrics_link = '';
	    }
		
		itemRenderHtml.push( 
				'<div class="plpart pl-playitem">' +
				'<div class="pli-details">' +
				'<a href="' + (id.length ? link : "javascript:void(0);") + '" class="pic-100x100"><img width="100" height="100" alt="' + artist_names.join(', ')+ ' - ' + name + '" title="' + artist_names.join(', ')+ ' - ' + name + '" src="' + image + '"></a>' +
				'<h4><a href="' + (id.length ? link : "javascript:void(0);") + '">' + (name.length > nameLength  ? name.substring(0, nameLength) + '...' : name) + '</a> '  + lyrics_link + '</h4>' +
				'<p>' +
				'	' + artists.join(', ') +
				'</p>'
		);
		
		if(id.length)
		{
			itemRenderHtml.push('<ul>');
			
		    if(mp3 !== '')
		    {
		    	itemRenderHtml.push('<li><a class="mm_player ico ico-listen png" href="' + mp3 + '">Posłuchaj fragmentu</a></li>');
		    }
		    
		    if(full_mp3 !== '')
		    {
		    	itemRenderHtml.push('<li><a id="alsong-' + id + '-' + (Math.random() + Math.random()) + '" class="mm_player ico ico-full png hit-redirect" href="' + STATIC_PATH + '/' + full_mp3 + '">Posłuchaj całości</a></li>');
		    }
		    
		    if(muzodajnia !== '')
		    {
		    	itemRenderHtml.push('<li><a class="ico ico-muzodajnia png muzodajnia-open" href="' + muzodajnia + '">Ściągnij z Muzodajnia.pl</a></li>');
		    };
		
		    if(see_video != 0)
		    {
		    	itemRenderHtml.push('<li><a class="ico ico-see png" href="' + BASE_URL + 'klipy#klip' + id + '">Zobacz teledysk</a></li>');
		    }
			
			itemRenderHtml.push('</ul>');
		}
		
		itemRenderHtml.push( 
			'</div>'
		);
		if(id.length)
		{
					
			itemRenderHtml.push( 
			'<div class="pli-actions">' +
			'	<ul class="flt">' +
			'			<li>' +
			'				<div class="rate_stars ocena" id="rate-SNG-' + id + '-playlist">'
			);
			
			var starts = new Array();
			var rating = Math.round(element.rating);
			
			for(var i = 0 ; i < 5; i++)
			{
				starts.push('<a title="Ocena ' + (i+1) + '" class="'  + (((i + 1) <= rating) ? 'hovered' : '') + '"></a>');
			}
			
			itemRenderHtml.push(starts.join(''));
			
			itemRenderHtml.push(		
			'				</div>' +
			'			</li>' +
			'			<li class="li-comm">' +
			'				<a class="ico ico-comm" href="' + link + '">Komentarze</a>' +
			'			</li>');
			
			var tags = new Array();
			var tagsInCloud = new Array();
			var tagsTextLengthCounter = 0;
			
			$.each(element.keywords, function() {
				
				if(tagsTextMaxLength > tagsTextLengthCounter)
				{
					tags.push('<a href="' + BASE_URL + '/hity/szukaj_hitow?opcja=0&szukaj_hitow=' + this + '">' + this + '</a>');
					tagsTextLengthCounter = tagsTextLengthCounter + this.length;
				}
				else
				{
					tagsInCloud.push(this);
				}
			});
			if(tags.length)
			{
				// complete
				if(tagsInCloud.length)
				{
					tags.push('<a class="playlist-show-tags" title="' + tagsInCloud.join(', ') + '" href="javascript:void(0);">&gt;&gt;</a>');
				}
	
				itemRenderHtml.push(
				'			<li>' +
				'				<p class="tagi flt">' +
				'					<strong>Tagi:</strong> '
				);
				
				itemRenderHtml.push(tags.join(' '));
				
				itemRenderHtml.push(
				'				</p>' +
			    '			</li>'
				);
			}
	
		    itemRenderHtml.push(		
			'		</ul>' +
			'		<ul class="frt">' +
			'			<li><a id="SNG-' + id + '" class="ico ico-fav" href="javascript:void(0);">Dodaj do ulubionych</a></li>' +
			'			<li><div class="fb-like-iframe"><iframe frameborder="0" scrolling="no" allowtransparency="true" style="border: medium none; overflow: hidden; width: 200px; height: 20px;" src="http://www.facebook.com/plugins/like.php?href=' + encodeURIComponent(link_fb) + '&layout=button_count&show_faces=&width=200&action=like&colorscheme=light&height=35" id="fb-like-source"></iframe></div></li>' +
		    '			<li><p class="frt"><a class="ico ico-alike" href="' + BASE_URL + '/hity/szukaj_hitow?podobne_hity=' + id + '">Podobne utwory</a></p></li>' +
			'		</ul>' +
			'	</div>'
			);
		}
	}


	$('#tab-plistfabryka .pl-playitem').html(itemRenderHtml.join(''));
}
