$(document).ready(function() {
    
	var mm_player_id = 'mm_player_' +  Math.floor(Math.random()*1000);
	var mm_player_actual_play = '0';
	var actualPlayerAnchor = '';
	
	$('body').append('<div style="z-index:-99999;width:1px;height:1px;position:absolute;bottom:0;right:0;clip:rect(0 1 1 0)" width="1" height="1" id="' + mm_player_id + '"></div>');
	
	$f(mm_player_id, CONTENT_PATH + 'thrdparty/flowplayer/flowplayer.commercial-3.1.0.swf', {
        key: '#@55fe2305d770ceb7cfc',
        onLoad: function() {
            this.setVolume(70); 
        }, 
		clip: {
	    	autoBuffering: true,
	    	autoPlay: false,
			onBegin: function() {
				this.setVolume(50);
			},
	    	onFinish: function(clip) {
				$(actualPlayerAnchor).removeClass('mm_player_now_playing');
			}
		},
		onLoad: function(clip) {
			this.hide();
		},
		plugins: {
			audio: {
	    		url: CONTENT_PATH + 'thrdparty/flowplayer/flowplayer.audio-3.1.0.swf' 
			}, 
			controls: {
				playlist: false,
				fullscreen: false
			}
		}
	});
	
	
	$(".mm_player").live('click',
		function() {
			
			if($(this).hasClass('hit-redirect'))
			{
				var elmIdComponents = $(this).attr('id').split('-', 3);
				$(this).attr('href', 'http://fabrykamuzy.pl/hit/totalfresh/' + elmIdComponents[1] + '#play');
				return true;
			}
		
			elm = $(this);
			$f(mm_player_id).stop();
									
			if($(this).hasClass("mm_player_now_playing"))
			{
				$f(mm_player_id).stop();
				$(this).removeClass("mm_player_now_playing");
				
				//console.log('mm_player stop');
			}
			else
			{
				$f(mm_player_id).play({
					url: $(this).attr('href')
				});
				
				//console.log('mm_player play');
				actualPlayerAnchor = this;
				elmIdComponents = $(this).attr('id').split('-', 3);
				elmId = elmIdComponents[0] + '-' + elmIdComponents[1] + '-' + Math.floor(Math.random()*1000);
				
				if( elm.hasClass('mm_player_podcast') )
				{
					new STPodcastlisten().add(elmIdComponents[1], elmId);
					statHtmlFile = 'podcast.html';
				}
				else
				{
					//new ALSonglisten().add(elmIdComponents[1]);
					statHtmlFile = 'huk.html';
				}
				
				if($('#g-stat-footer').length)
				{
//				    var cookieData = $.cookie(statHtmlFile);
//				    if(cookieData == null) cookieData = 0;
//								    
//				    if(parseInt(cookieData) > 0)
//			    	{
//				    	cookieData = 0;
//				    	
//				    	var statFrameHuk = '<iframe width="1" height="1" marginheight="0" marginwidth="0" scrolling="no" noresize="noresize" frameborder="no" id="gallery-stat-frame" src="' + CONTENT_PATH + '/content/stat/' + statHtmlFile + '" ></iframe>';
//			        	$('#g-stat-footer').html(statFrameHuk);
//			    	}
//			    	else
//			    	{
//			    		cookieData = parseInt(cookieData) + 1; 
//			    	}
//				    
//				    $.cookie(statHtmlFile, cookieData, { expires: 10 });
				    				    
				    var statFrameHuk = '<iframe width="1" height="1" marginheight="0" marginwidth="0" scrolling="no" noresize="noresize" frameborder="no" id="gallery-stat-frame" src="' + CONTENT_PATH + '/content/stat/' + statHtmlFile + '" ></iframe>';
		        	$('#g-stat-footer').html(statFrameHuk);
				}
				
				
				
				mm_player_actual_play = elmId;
				
				$(this).attr('id', elmId);				
				$(this).addClass('mm_player_now_playing');
			}
			
			$(".mm_player").each(
				function() { if (mm_player_actual_play != $(this).attr('id') && $(this).hasClass('mm_player_now_playing')) $(this).removeClass('mm_player_now_playing');}
			);
			
			return false;			
		}
	);
});