/*
 * Copyright 2009 Eric Wahlforss for SoundCloud Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * For more information and documentation refer to
 * http://soundcloud.com/api
 * 
 */

(function($) {
	$.fn.tauiPlayer = function(callerSettings) {
		var i = 0;
		return this.each(function() {
			var settings = $.extend({width: 640, collapse : true, autoplay: false},callerSettings || {}); // default settings
			
			var track = {}; // the track data
			var sound; // the soundmanager 2 sound object
			var container = $(this);
			var link = $(this).children('a'); // the player a-tag
			var dom; // the root player dom node, created on first play
			var loading; // the loading progress
			var progress; // the playback progress
			var inited = false; // player is not inited

			// init the player on first click of the link
			link
				.click(function() {
					if(!inited) { // if not initied, then load track data, and init the sound
						init(true); // passing true here means init and autoplay
					} else { // if inited, the toggle between play/pause
						togglePlay();
					}
					return false;
				});

			//init the player, get the track data from soundcloud, init sound
			var init = function(autoplay) {
				link.wrap("<div class='player'></div>");
				dom = link.parent("div.player");
				link.addClass("controls");
				dom.attr("id", link.attr("id"));
				link.attr("id","");
				
				{
					track = {
						'user' : {
							'username' : 'Tocotronic'
						},
						'permalink_url' : 'http://www.tocotronic.de/?x',
						'duration': 90,
						'title': link.text(),
						'id': 'toc' + Math.random(),
						'stream_url': link.attr('href')
					};
					
					$("<div class='load'></div><div class='progress'></div><div class='progress-bar'></div>").appendTo(dom);
					
					var progressBar = $(dom).find(".progress-bar");
					loading = $(".load",dom);
					progress = $(".progress",dom);
					progress.css('background-color', container.children('span').css('background-color'));
					
					// expand out the player to the width in the settings								
					if($.easing) {
						dom.animate({width: settings.width}, 500,"easeinout");
					}
					
					// set up progress
					progressBar.click(function(ev) {
						var percent = (ev.clientX-progressBar.offset().left)/(progressBar.width());
						if(sound.durationEstimate*percent < sound.duration) {
							play();
							sound.setPosition(sound.durationEstimate*percent);
						}
					});
					
					var timer = setInterval(function() {	// this is kind of ugly but don't know a better way of waiting for SoundManager 2 when multiple players are on the page
						if(soundManager.swfLoaded) {
							clearInterval(timer);
							
							sound = soundManager.createSound({
								id: track.id,
								url: track.stream_url,
								//volume: 0,
								whileloading : throttle(200,function() {
									loading.css('width',(sound.bytesLoaded/sound.bytesTotal)*100+"%");
								}),
								whileplaying : throttle(200,function() {
									progress.css('width',(sound.position/sound.durationEstimate)*100+"%");
								}),
								
								onfinish : function() {
									dom.removeClass("playing");
									sound.setPosition(0);
									sound.pause();
									progress.css('width', "0%");
									dom.parent('.track').next('.track').children('a').click();
								},
								onload : function () {
									loading.css('width',"100%");
								},
								
								onpause : function() {
									sound.setPosition(0);
									stop();
								}
								
							});
							
							if(autoplay) {
								play();
							}
							
						}
						
					},200);

					inited = true;
				}
			};

			var togglePlay = function() {
				dom.hasClass("playing") ? stop() : play();
			};

			var stop = function() {
				if(sound) {
					sound.pause();
					dom.removeClass("playing");
				}
			};

			var play = function() {
				if(sound) {
					soundManager.pauseAll();
					sound.paused ? sound.resume() : sound.play();
					$(".delimiter",dom).show();
					dom.addClass("playing");
				}
			};
			
			// throttling function to minimize redraws caused by soundmanager
			var throttle = function(delay, fn) {
				var last = null,
						partial = fn;

				if (delay > 0) {
					partial = function() {
						var now = new Date(),
								scope = this,
								args = arguments;

						// We are the last call made, so cancel the previous last call
						clearTimeout(partial.futureTimeout);

						if (last === null || now - last > delay) { 
							fn.apply(scope, args);
							last = now;
						} else {
							// guarentee that the method will be called after the right delay
							partial.futureTimeout = setTimeout(function() { fn.apply(scope, args); }, delay);
						}
					};
				}
				return partial;
			};

			// expand the player on init if collapse=false
			if(!settings.collapse) {
				settings.autoplay ? init(true) : init();
				dom.width(settings.width);
			}

		});
	};
})(jQuery);

 /*
	* jQuery Easing v1.1.1 - http://gsgd.co.uk/sandbox/jquery.easing.php
	*
	* Uses the built in easing capabilities added in jQuery 1.1
	* to offer multiple easing options
	*
	* Copyright (c) 2007 George Smith
	* Licensed under the MIT License:
	*	 http://www.opensource.org/licenses/mit-license.php
	*/

/* Extending jQuery easing functions here with the one required for the player */

jQuery.easing = jQuery.extend({
	easeinout: function(x, t, b, c, d) {
		if (t < d/2) return 2*c*t*t/(d*d) + b;
		var ts = t - d/2;
		return -2*c*ts*ts/(d*d) + 2*c*ts/d + c/2 + b;		
	}
},jQuery.easing);