$(document).ready( function () {

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// players object
	
	var players = { 
		type	: 'flash', 
		length	: 0, 
		current	: 0, 
		playall	: false,
		loop	: false 
	};

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// html5 player type setting
	
	if ($.browser.webkit || $.browser.safari) {
		players.type = 'html5';
	}
	
	if ($.browser.msie && $.browser.version >= 9.0) {
		players.type = 'html5';
	}
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// play all setting
	
	if ($('.playlist').hasClass('playall')) {
		players.playall = true;
		if ($('.playlist .playall').attr('checked','checked'));
	}
	
	if ($('.playlist .playall').attr('checked')) {
		players.playall = true;
	}

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// methods
	
	players.play = function (track) {

		if (!track) return;
		
			console.log('play',track);

		var THIS  = this;
		var audio = this[track];
		
		if (!audio.playing) { 
		
			this.pause(this.current);
			
			audio.player.play();
			
			audio.interval = setInterval(function() {
				
				var currentTime = (audio.type == 'html5') 
					? audio.player.currentTime 
					: audio.player.getPosition();
				
				audio.time.html(formatTime(currentTime));
				
			},1000);
			
			this.current = track;
			audio.playing = true;
			audio.container.addClass('playing');
			audio.control.html('Pause');
		}
	};

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	players.pause = function (track) {

		if (!track) return;
		
		var audio = this[track];
		
		if (audio.playing) { 
		
			audio.player.pause();
				
			audio.playing = false;
			audio.container.removeClass('playing');
			audio.control.html('Play');
				
			clearInterval(audio.interval);
		}
	};

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	players.stop = function (track) {

		if (!track) track = this.current;
		
		var audio = this[track];
		
		audio.playing = false;
		audio.container.removeClass('playing');
		audio.control.html('Play');
				
		clearInterval(audio.interval);
	};

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	players.next = function () {
		
		if (parseInt(this.current) < this.length) {
		
			this.play(parseInt(this.current) + 1);
		}
	};

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	players.prev = function () {
		
		if (parseInt(this.current) > 1) {
		
			this.play(parseInt(this.current) - 1);
		}
	};

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	players.all = function () {
		
		if (this.playall) {
		
			this.playall = false;
		
		} else {
		
			this.playall = true;
			
			if (this.current == 0) { 
				this.play(1);
			}
		}
	};
		
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// setup players
	
	$('.audio-player').each( function() {
	
		var swf      = "/plugins/audioplayer/jwplayer/player.swf";
		
		var container = $(this);
		var control   = container.find('.download');
		var time      = container.find('.time');
		var track     = ++players.length;
		var src       = control.attr('href');
		var type	  = players.type;
		var playnext  = container.hasClass('playnext');
		var duration  = '00:00';
		
		control.attr('data-track',track);
		control.attr('id','track-'+track);
		control.addClass('control');
		control.html('Play');
		
		var player = document.createElement('div');
		player.setAttribute('class','player');
		container.append(player);
		
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		// firefox: use html5 player if ogg file is available
		// otherwise use flash player with mp3 file
		
		if ($.browser.mozilla) {
		
			container.find('.type').each( function() {
				
				if ($(this).attr('id') == 'ogg') { 
					
					type = 'html5';
					src  = $.trim($(this).html());
				}
			});
		
		};
		
		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		// Load HTML5 Player
		
		if (type == 'html5') { 
		
			console.log('html5',src);
			
			var audio = document.createElement('audio');
			audio.setAttribute('src',src);
			player.appendChild(audio);
			
			audio.addEventListener("loadedmetadata", function() { 
			
				duration = formatTime(audio.duration);
				time.html(duration);
				
			}, true);
			
			audio.addEventListener("ended", function() { 
			
				players.stop();
				
				if (players.playall || players[track].playnext) 
					players.next();
					
			}, true);
		}

		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		// Load FLASH Player
		
		if (type == 'flash') { 
			
			console.log('flash',src);
			
			var trackid = 'track'+track+'player';
			
			player.setAttribute('id',trackid);
			
			var audio = jwplayer(trackid);
			
			audio.setup({
				flashplayer:swf, file:src, height:0, width:0
			});
			
			audio.onReady( function () {
				// console.log(audio.getDuration());
			});
			
			audio.onComplete( function () {
			
				players.stop();
				
				if (players.playall || players[track].playnext) 
					players.next();
			});
			
			$('#'+trackid+'_wrapper').css('height','0px');
			$('#'+trackid+'_wrapper').css('clear','both');
		}

		// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		
		players[track] = { 
			type	  : type,
			src       : src,
			container : container,
			control   : control,
			player    : audio, 
			playing   : false, 
			interval  : null, 
			time	  : time, 
			duration  : duration,
			playnext  : playnext
		};
		
	});
	
	// console.log(players);
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	$('.audio-player .control').click( function () {
		
		var track = $(this).attr('data-track');
		
		if (players[track].playing) { 
		
			players.pause(track);
		
		} else {
		
			players.play(track);
		}
		
		return false;
	});
			
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	$('.playlist .playnext').click( function () {
		
		players.next(); return false;
	});

	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	$('.playlist .playprev').click( function () {
		
		players.prev(); return false;
	});
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	$('.playall').click( function () {
		
		players.all();
	});
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	function formatTime(time) {
	
		var sec = Math.round(time);
		var min = Math.floor(sec / 60);
		sec = sec - (min * 60);
		sec = (sec < 10) ? '0'+sec : sec;
		
		return min+':'+sec;
	}
	
});
