(function($) {
	
var sounds={};
var curSound=false;
var form_url='http://www.teknclips.com/feed/0/form';

var buildPosText=function(p) {
	var minutes = Math.floor(p / 60);
	var seconds = Math.floor(p) % 60 ;
	var secondsString;
	var minutesString;

	if (seconds < 10) { secondsString = "0" + seconds; } 
	else { secondsString = String(seconds); };
	if (minutes < 10) { minutesString = "0" + minutes; } 
	else { minutesString = String(minutes); };

	return (minutesString + ":" + secondsString);
};

soundManager.setup({
	  url: '/beatembed/soundManager/swf',
	  onready: function() {

		function playSound(soundid,url,$item) {
			soundManager.stopAll();
			if(typeof sounds[soundid] == "undefined") {
				sounds[soundid]=soundManager.createSound({
					id:soundid,
					url:url,
					onfinish: function() {
						//console.log('go next');
						$item.next().click();
					},
					whileplaying: function() {
						$(".player-control .pos-slider .handle" ).css('left',
							sounds[soundid].position/sounds[soundid].durationEstimate*400);
						$('.player-control .pos-slider .handle .tooltip').text(buildPosText(sounds[soundid].position/1000));
					}
				});
			}

			sounds[soundid].play();
			curSound=soundid;
		}

		$('.player-playlist tr').click(function() {
			var title=$(this).find('.title').text();
			var data=$(this).attr('id');

			$('.player-control .now-playing span').text(title);

			$('.player-playlist tr.selected').removeClass('selected');
			$(this).addClass('selected');
			$('.player-control .play-bttn').addClass('pause');

			data=data.split('|');
			playSound(data[0],data[1],$(this));
			form_url=data[2];
		});
		$('.player-control .play-bttn').click(function() {
			if(curSound==false) {
				$('.player-playlist tr:eq(1)').click();
			}
			else if($(this).hasClass('pause')) {
				soundManager.pause(curSound);
				$(this).removeClass('pause');
			}
			else {
				soundManager.resume(curSound);
				$(this).addClass('pause');
			}
		});
		var dragFunc=function() {
			if(curSound==false) return;
			var x=$('.player-control .pos-slider .handle').offset().left
				- $('.player-control .pos-slider').offset().left;
			sounds[curSound].setPosition(sounds[curSound].durationEstimate*x/400);
		};
		$(".player-control .pos-slider .handle" ).draggable({ 
			containment: ".player-control .pos-slider",
			axis: "x",
			drag: dragFunc,
			stop: dragFunc
		});
		$(".player-control .pos-slider").click(function(e) {
			var x=e.offsetX - $(".player-control .pos-slider").offset().left;
			x=Math.min(Math.max(0,x),400);
			sounds[curSound].setPosition(sounds[curSound].durationEstimate*x/400);
			$(".player-control .pos-slider .handle").css('left',x);
		});
		
		var addForm=function(lease) {
			if(curSound==false) return;
			$.ajax({url:form_url,
				success:function(data) {
					$('.cartform').remove();
					$('body').append('<div class="cartform">'+data+'</data>');
					if(!lease) {
						$('#edit-product-id option[selected=selected]').removeAttr('selected');
						$('#edit-product-id option:eq(1)').attr('selected','selected');
					}
					$('.cartform form').attr('action',
						'http://www.teknclips.com'
						//'http://localhost'
						+$('.cartform form').attr('action'));
					$('.cartform #edit-submit').click();
				}
			});
		};
		
		$(".player-control .purchase").click(function(e) { addForm(false); });
		$(".player-control .lease").click(function(e) { addForm(true); });
	  },
	  ontimeout: function() {
	  	// Hrmm, SM2 could not start. Missing SWF? Flash blocked? Show an error, etc.?
	  }
});
	
	
})(jQuery);