(function($) {
	$.fn.mhPlayer = function(settings) {
		
		settings = $.extend({width:575}, settings);
		
		var key = {
			consumer: 'o48dcwb25al69iKWK5erw', 
			secret: 'hl2hvJRBfzXbal9erStRbBDV1G6lGXi1Sk3OU1JQWEo' // (ok, it's not so secret - you can read it there :D)
		};
		
		$('button').live('click', function(event) {
			if ($(this).find('span').hasClass('play')) {
				$(this).parent().find('audio').get()[0].play();
			} else {
				$(this).parent().find('audio').get()[0].pause();
			}
			$(this).find('span').toggleClass('play').toggleClass('pause');
		});
		
		$.formatTime = function(sec) {
			time = [];
			if (sec>3600) {
				hod = Math.floor(sec/3600);
				time.push(hod);
				sec = sec - (hod*3600);
			}
			
			if (sec>60) {
				min = Math.floor(sec/60);
				time.push(min);
				sec = sec - (min*60);
			} else {
				time.push(0);
			}
			
			zbytek = Math.floor(sec);
			time.push((zbytek<10)?"0"+zbytek:zbytek);
			
			return time.join(":");
		};
		
		$.playData = {
			cron: setInterval(function() {
				$('audio').each(function() {
					if ($(this).closest('.player').find('button').find('span').hasClass('pause')) {
						$(this).closest('.player').find('.progress').css("width", ($(this).get()[0].currentTime/($(this).get()[0].duration)*settings.width)+"px");
						
						cas = $(this).closest('.player').find('.time .mark').text();
						newtime = $.formatTime($(this).get()[0].currentTime)+"/"+$.formatTime($(this).get()[0].duration);
						if (cas != newtime) {
							$(this).closest('.player').find('.time .mark').text(newtime);
							Cufon.replace(".mark");
						}
						
						if (Math.ceil(($(this).get()[0].currentTime/($(this).get()[0].duration))*100) == 100) {
							$(this).closest('.player').find('button').find('span').toggleClass('play').toggleClass('pause');
							$(this).closest('.player').find('.progress').css("width", settings.width+"px");
							$(this).closest('.player').find('.time .mark').text($.formatTime($(this).get()[0].duration)+"/"+$.formatTime($(this).get()[0].duration));	
							Cufon.replace(".mark");
							setTimeout(function() {
								$('.player').each(function() {
									if($(this).find('button').hasClass('play') && $(this).find('.progress').css('width') == settings.width+"px") {
										alert($(this).find('.progress').css('width'));
										$(this).find('.time .mark').fadeOut(200).text($(this).find('.time .mark').text().split("/")[1]);
										Cufon.replace('.mark');
										$(this).find('.time .mark').fadeIn("slow");
										$(this).find('.progress').animate({width: 0}, "slow", "swing");
									}
								});
							}, 1500);
						}
					}
				});
			}, 200)
		};

		this.each(function() {
			if ($.browser.webkit) {
				href = $(this).find('param[value^=http://player.soundcloud.com/]').attr("value").split("url=")[1].split("&")[0];
				$.ajax({
					url: 'soundcloud/', 
					data: {url: href},
					type: 'post',
					dataType: 'json',
					context: $(this), 
					success: function(data) {
						$(this).replaceWith("<section class='player'><div class='letterbox'></div><div class='bg'></div><div class='progress'></div><img src='" + data.waveform_url + "' /><button><span class='play'>Přehrát/pozastavit</span></button><div class='time'><span class='name'>"+data.title+"</span> – <span class='mark'>"+$.formatTime(data.duration/1000)+"</span></div><audio preload='metadata' src='"+data.stream_url+"'></section>");
						Cufon.replace(".time");
					}
				});
			} else {
				clearInterval($.playData.cron);
			}
		});
		return this;
	};
})(jQuery);
