var soundManager = new SoundManager();
jQuery.soundManager = $.extend(soundManager, {
	debugMode: false,
	consoleOnly: true,
	current: {},
	createPlaylist: function (array) {
		if (array && array[0]) {
			var p = this.playlist;
			for (var i = 0; i < array.length; i = i + 1) {
				p[i] = this.createSound({
					numericId: i,
					id: 'track' + i,
					onfinish: function () {
						this.crossfading = false;
					},
					onjustbeforefinish: function () {
						this.crossfading = true;
						p[this.instanceOptions.numericId + 1].play();
					},
					onjustbeforefinishtime: 1200,
					onid3: function () {
						var blah = "";
					},
					onplay: function () {
					    $("#the_song_listbb li a").css({color:"#000",backgroundColor:"#fff"});
					    
						$("#the_song_listbb li").css({color:"#000",backgroundColor:"#fff"});
						
						$("#the_song_listbb li a").css({color:"#000",backgroundColor:"#fff"});
						
						var key = "#the_song_listbb li:eq("+this.instanceOptions.numericId+")";
						var key2 = "#the_song_listbb li:eq("+this.instanceOptions.numericId+") a";
						$(key).css({color:"#fff",backgroundColor:"#000"});
						$(key2).css({color:"#fff",backgroundColor:"#000"});
						
						jQuery.soundManager.currentSound = this;
						jQuery.soundManager.prevSound =  p[this.instanceOptions.numericId - 1] || p[this.instanceOptions.numericId];
						jQuery.soundManager.nextSound = p[this.instanceOptions.numericId + 1] || p[this.instanceOptions.numericId];
					},
					onstop: function () {
						jQuery.soundManager.positionToTime(0, this.duration);
						$('#progress-bar .inside').css('width', jQuery.soundManager.current.currentPercent + '%');
						//$('#status-bar').html(jQuery.soundManager.current.currentTime + ' of ' + jQuery.soundManager.current.endTime);
						$('#status-bar').html(jQuery.soundManager.current.currentTime);
						
					},
					whileplaying: function () {
						jQuery.soundManager.positionToTime(this.position, this.duration);
						$('#progress-bar .inside').css('width', jQuery.soundManager.current.currentPercent + '%');
						//$('#status-bar').html(jQuery.soundManager.current.currentTime + ' of ' + jQuery.soundManager.current.endTime);
						$('#status-bar').html(jQuery.soundManager.current.currentTime);
					},
					url: array[i]
				});
				p[i].realPlay = p[i].play;
				p[i].realPause = p[i].pause;
				p[i].realStop = p[i].stop;
				p[i].crossfading = false;
				p[i].play = function () {
					if (jQuery.soundManager.currentSound.sID !== this.sID && !jQuery.soundManager.currentSound.crossfading) {
						soundManager.stopAll();
					}
					if (this.playState !== 1 || this.pauseState === 1) {
						this.pauseState = 0;
						this.realPlay();
					}
				};
				p[i].pause = function () {
					if (this.playState === 1 && this.pauseState === 0) {
						this.pauseState = 1;
						this.realPause();
					}
				};
				p[i].stop = function () {
					this.pauseState = 0;
					this.realStop();
				};
			}
			jQuery.soundManager.currentSound = jQuery.soundManager.prevSound = p[0];
			jQuery.soundManager.nextSound = p[1] || p[0];
		}
	},
	nextSound: {},
	playlist: [],
	positionToTime: function (currentPosition, endPosition) {
		var outTime = function (inTime) {
			var hor, min, sec, mil = inTime;

			hor = Math.floor(mil / (3600000));
			mil %= 3600000;
			min = Math.floor(mil / (60000));
			mil %= 60000;
			sec = Math.floor(mil / 1000);
			mil %= 1000;
			hor = '' + ((hor < 10) ? '0' + hor : hor);
			min = '' + ((min < 10) ? '0' + min : min);
			sec = '' + ((sec < 10) ? '0' + sec : sec);
			mil = '' + ((mil < 10) ? '00' + mil : (mil < 100) ? '0' + mil : mil);

			return [min, sec].join(':');
		};

		var c = this.current;
		c.currentPosition = currentPosition;
		c.currentTime = outTime(currentPosition);

		if (!c.endPosition || !c.endTime || c.endPosition !== endPosition) {
			c.endPosition = endPosition;
			c.endTime = outTime(endPosition);
		}

		c.currentPercent = (c.currentPosition / c.endPosition) * 100;
	},
	prevSound: {},
	url: '/assets/flash/sd2/'
});

jQuery.soundManager.onload = function () {
	var song_list = jQuery("#the_song_listbb");
	if(song_list.length > 0) {
	    jQuery("#the_song_listbb li")
		var lis = jQuery("#the_song_listbb li");
		var i = 0;
		var songs = [];
		var li = "";
		for(i =0;i <= lis.length; i = i + 1){
			li = lis.get(i);
			if(li){
				songs.push($(li).attr( "alt" ));
			    $(li).attr( "onclick","jQuery.soundManager.playlist["+i+"].play();return false;" )
			}
		}
		jQuery.soundManager.createPlaylist(songs);
	}
};
