/*
	Hadefied Audio Player
	Author: Glen Gringgo Bangkilla
*/
	(function( $ ){
	
		var defualtOptions = {
			top: 0,
			left: 0,
			right: 0,
			bottom: 0,	
			width: 400,
			playlist: false,
			loop: 0,
			volume: 50,
			autoplay: false
		};
		
		var methods = {
			
			build: function( options ) {
			
				options.top = (options.top) ? 'top:' + options.top + 'px;' : '';
				options.left = (options.left) ? 'left:' + options.left + 'px;' : '';
				options.right = (options.right) ? 'right:' + options.right + 'px;' : '';
				options.bottom = (options.bottom) ? 'bottom:' + options.bottom + 'px;' : '';
				options.width = (options.width) ? 'width:' + options.width + 'px;' : '';		
				var html = "";
				
				
				html += '<div id="' + methods.hash() + '" class="hadefied space">';
				html += '	<div class="hadefied-panel">';
				if( options.playlist ) {html += '		<a class="button sprite prev">&nbsp;</a>';}
				html += '		<a class="button sprite play">&nbsp;</a>';
				html += '		<a class="button sprite pause">&nbsp;</a>';
				if( options.playlist ) {html += '		<a class="button sprite next">&nbsp;</a>';}
				html += '		<a class="button sprite stop">&nbsp;</a>';				
				html += '		<div class="hadefied-time-frame">';
				html += '			<div class="hadefied-frame-buffer"><div class="hadefied-frame sprite loader"></div></div><div class="frame-title"></div>';
				html += '		</div>';
				html += '		<a class="sprite time space">';
				html += '				<div class="divide pos">0:00</div>';
				html += '				<div class="divide dur">0:00</div>';
				html += '		</a>';
				/* if( options.playlist ) {
				html += '		<a class="button sprite shuffles unshuffle" rel="1">&nbsp;</a>';
				html += '		<a class="button sprite shuffles shuffle" rel="1">&nbsp;</a>';
				} */
				html += '		<a class="button sprite repeats repeatall" rel="1">&nbsp;</a>';
				if( options.playlist ) {html += '		<a class="button sprite repeats repeatone" rel="2">&nbsp;</a>';}
				html += '		<a class="button sprite repeats norepeat repeat-cur" rel="0">&nbsp;</a>';
				html += '		<div class="hadefied-volume">';				
				html += '			<a class="button sprite mute">&nbsp;</a>';		
				html += '			<a class="button sprite unmute">&nbsp;</a>';
				html += '			<div class="' + ((options.playlist) ? 'control' : 'vcontrol' )+ '">';
				html += methods.buildVolumeControls(options);
				html += '			</div>';
				html += '		</div>';
				html += '	</div>';
				if( options.playlist ) {html += '	<div class="hadefied-playlist"><div class="hadefied-playlist-container">&nbsp;</div></div>';}
				html += '</div>';
				return html;				
			},
			
			reset: function(parent, options, settings) {
				parent.find('.hadefied .play').show();
				parent.find('.hadefied .pause').hide();
				parent.find('.hadefied .time .pos').html('0:00');
				parent.find('.hadefied .time .dur').html('0:00');
				parent.find('.hadefied .hadefied-time-frame .hadefied-frame').css('width', '100%');
				if(options.playlist) {
					parent.find('.hadefied .hadefied-time-frame  .frame-title').html(parent.find('.hadefied .hadefied-playlist li.cur').html());
				} else {
					parent.find('.hadefied .hadefied-time-frame  .frame-title').html(settings.title);	
				}				
			},

			buildPlaylist: function(settings) {
				var html = "";
				html += '<ul class="track-list">';
				for(var i = 0; i < settings.url.length; i++) {
					if(i == 0) {
						html += '<li id="' + settings.id[i] + '" class="cur" rel="' + settings.url[i] + '">' + settings.title[i] + '</li>';
					} else {
						html += '<li id="' + settings.id[i] + '"  rel="' + settings.url[i] + '">' + settings.title[i] + '</li>';
					}
				}
				html += '</ul>';
				return html;
			},
			
			buildVolumeControls: function(options) {
				var html = "";
				for(var i = 10; i >= 0 ; i--) {
					if(parseInt(options.volume) == parseInt( i + '0' )) {
						html += '				<div rel="' + parseInt( i + '0' ) + '" title="' + parseInt( i + '0' ) + '" class="vc vc-cur">&nbsp;</div>';
					} else {
						if(i == 0) {
							html += '				<div rel="' + parseInt( i + '0' ) + '" title="' + parseInt( i + '0' ) + '" class="vc vc-mute">&nbsp;</div>';
						} else {
							html += '				<div rel="' + parseInt( i + '0' ) + '" title="' + parseInt( i + '0' ) + '" class="vc">&nbsp;</div>';
						}
					}
				}
				return html;
			},
			
			getSettings: function(parent) {
				var settings = {
					url 	: parent.find('.hadefied .hadefied-playlist li.cur').attr('rel'),
					id		: parent.find('.hadefied .hadefied-playlist li.cur').attr('id'),
					title	: parent.find('.hadefied .hadefied-playlist li.cur').html()
				};
				return settings;				
			},
			
			getVolume: function(parent) {
				return parent.find('.hadefied .hadefied-volume .vc-cur').attr('rel');
			},
			
			getRepeat: function(parent) {
				return parent.find('.repeats .repeat-cur').attr('rel');
			},
			
			togglePlayPause: function(parent) {
				parent.find('.hadefied .play').toggle();
				parent.find('.hadefied .pause').toggle();
			},
			
			toggleShuffle: function(parent) {
				parent.find('.hadefied .unshuffle').toggle();
				parent.find('.hadefied .shuffle').toggle();	
			},
			
			createSound: function( id, url, title, obj, options, settings) {
				var temp = null;
				var tmp = null;
				obj.find('.hadefied .hadefied-time-frame  .frame-title').html("Now playing " + title);
				soundManager.createSound({
					id: id ,
					url: url,
					autoPlay: options.autoplay,
					volume: parseInt(obj.find('.hadefied .hadefied-volume .vc-cur').attr('rel')),
					whileplaying: function() {
						var tmpSec = Math.abs(Math.round(this.position / 1000 % 60));
						var _position = Math.abs((Math.ceil(this.position / 1000 / 60) - 1)) + ':' + ((tmpSec < 10) ? "0" + tmpSec : tmpSec);
						var tmpSec =  Math.abs(Math.round(this.duration / 1000 % 60));
						var _duration = Math.abs((Math.ceil(this.duration / 1000 / 60) - 1)) + ':' + ((tmpSec < 10) ? "0" + tmpSec : tmpSec);
						
						obj.find('.hadefied .time .pos').html( _position);
						obj.find('.hadefied .time .dur').html( _duration); 
						obj.find('.hadefied .hadefied-time-frame .hadefied-frame').css('width', ((this.position / this.duration) * 100) + '%');
					},
					whileloading: function() {
						obj.find('.hadefied .hadefied-time-frame .hadefied-frame-buffer').css('width', ((this.bytesLoaded / this.bytesTotal) * 100) + '%');	
					},
					onfinish: function() {						
						if(options.playlist) {
							temp = obj.find('.hadefied .hadefied-playlist ul.track-list li.cur').index();		
							tmp = obj.find('.hadefied .hadefied-playlist ul.track-list li:last').index();
							methods.reset(obj, options, settings);
							if(obj.find('.repeat-cur').attr('rel') != "2") { 
								if(!(obj.find('.repeat-cur').attr('rel') == "0" && temp == tmp)) {
									methods.seekNextTrack(obj, settings, options);
								}
																
							}
							if(!(obj.find('.repeat-cur').attr('rel') == "0" && temp == tmp)) {
								methods.togglePlayPause(obj);
								methods.playSound(obj, options, settings); 	
							} else {
								methods.seekNextTrack(obj, settings, options);
							}
						} else {
							methods.reset(obj, options, settings);
							if(obj.find('.repeat-cur').attr('rel') == "1") { 								
								soundManager.play(settings.id);
							}
						}
					}
				});
			},
			
			
						
			playSound: function(parent, options, settings) {
				settings = methods.getSettings(parent);
				methods.createSound( settings.id, settings.url, settings.title, parent, options, settings);		
				soundManager.play(settings.id);
				/* volume = methods.getVolume(parent);
				var temp = soundManager.getSoundById(settings.id);
				temp.setVolume(volume); */
			},
			
			stopSound: function(parent) {				
				settings = methods.getSettings(parent);				
				soundManager.stop(settings.id);				
			},
			
			pauseSound: function(parent) {
				settings = methods.getSettings(parent);
				soundManager.pause(settings.id);
			},
			
			muteSound: function(parent) {
				settings = methods.getSettings(parent);
				soundManager.mute(settings.id);
			},
			
			unmuteSound: function(parent) {
				settings = methods.getSettings(parent);
				soundManager.unmute(settings.id);
			},
			
			setVolume: function(parent) {
				settings = methods.getSettings(parent);
				volume = methods.getVolume(parent);
				
				var temp = soundManager.getSoundById(settings.id);
				temp.setVolume(volume);	
			},
			
			clearTracks: function(parent) {
				parent.find('.hadefied .hadefied-playlist li').each(function() {
					$(this).removeClass('cur');
				});			
			},
			
			seekNextTrack: function(parent, settings, options) {
			
				cur = parent.find('.hadefied .hadefied-playlist ul.track-list li.cur');
				cur.removeClass('cur');
				if(cur.next().length == 0) {
					parent.find('.hadefied .hadefied-playlist ul.track-list li:first').addClass('cur');
				} else {
					cur.next().addClass('cur');
				}
				return cur.next().length;
			},
			
			seekPrevTrack: function(parent) {
			
				cur = parent.find('.hadefied .hadefied-playlist ul.track-list li.cur');
				cur.removeClass('cur');
				if(cur.prev().length == 0  ) {
					parent.find('.hadefied .hadefied-playlist ul.track-list li:last').addClass('cur');
				} else {
					cur.prev().addClass('cur');
				}
				return cur.prev().length;
			},
			
			hash: function () {
				var S4 = function() {
				   return Math.round((1+Math.random() * 0x100000000));
				};
				return (S4()+S4()+S4()+S4()+S4()+S4()+S4()+S4());
			}, 
			
			configVolumeControls: function(parent) {
				var start = parent.find('.hadefied .hadefied-volume .vc-cur').index();
				var end = parent.find('.hadefied .hadefied-volume  .vc').length;
				parent.find('.hadefied .hadefied-volume  .vc').each(function(index) {
					if(index >= start && index < end) {
						$(this).css('background', '#32798D');
					} else {
						$(this).css('background', '#000');
					}
				});
			},
			
			toggleVolumeControl: function(parent, options) {
				methods.configVolumeControls(parent);
				if(options.playlist) {
					if(Math.round(parent.find('.hadefied .hadefied-volume .control').height()) == 0) {
						parent.find('.hadefied .hadefied-volume .control').animate({
							height: 80
						}, 300);	
					} else {
						parent.find('.hadefied .hadefied-volume .control').animate({
							height: 0
						}, 300);
					}
				} else {
					if(Math.round(parent.find('.hadefied .hadefied-volume .vcontrol').height()) == 0) {
						parent.find('.hadefied .hadefied-volume .vcontrol').animate({
							height: 11
						}, 300);	
					} else {
						parent.find('.hadefied .hadefied-volume .vcontrol').animate({
							height: 0
						}, 300);
					}

				}
			}

		
		};
		
		$.fn.hadefied = function( options ) {
			var parent = this;
			var settings;
			var tracks = new Array();
			var hash = methods.hash();
			
			if(!options) {
				options = defualtOptions;
			} else  {				  
				options.loop = (options.loop) ? options.loop : 0;
				options.volume = (options.volume) ? options.volume : 100;	
				options.autoplay = (options.autoplay) ? options.autoplay : false;					  
			}
			
			if(!options.playlist) {
				settings = {
					url : escape(this.find('a.playme').attr('href')),
					title : this.find('.playme').html(),
					id : 'soundmanager_' + this.attr('id')
				}
				this.find('.playme').remove();				
			} else {
				var urls = new Array();
				var titles = new Array();
				var ids = new Array();
				this.find('.playme li').each(function( index ) {
					urls[index] = $(this).find('a').attr('href');
					titles[index] = $(this).find('a').html();
					ids[index] = 'hadefied' + parent.attr('id') + 'playlisttrack' + (index + 1);
				});
				settings = {
					url: urls,
					title: titles,
					id: ids
				}
				this.find('.playme').remove();
			}		
			
			/*
				Build player
				Add Playlist
				Config Volume Controls
			*/
			this.append(methods.build(options));			
			if(options.playlist) { 
				parent.find('.hadefied .hadefied-playlist .hadefied-playlist-container').html(methods.buildPlaylist(settings));
			}
			methods.configVolumeControls(parent);
			
			if(options.autoplay) {
				soundManager.onload = function() { 
					if(!options.playlist) {
						methods.togglePlayPause(parent);
						methods.createSound( settings.id, settings.url, settings.title, parent, options, settings);
					} else {
						methods.togglePlayPause(parent);
						methods.playSound(parent, options, settings);
					}
				}
			}
				
			this.find('.play').click(function() {
				methods.togglePlayPause(parent);
				if(!options.playlist) { // Non-playlist mode
					methods.createSound( settings.id, settings.url, settings.title, parent, options, settings);	
					soundManager.play(settings.id);					
				} else { // Playlist mode					
					methods.playSound(parent, options, settings);
				}				
			});
			
			this.find('.stop').click(function() {								
				methods.togglePlayPause(parent);	
				methods.reset(parent, options, settings);
				if(options.playlist) {					
					methods.stopSound(parent);					
					methods.unmuteSound(parent);
				} else {
					soundManager.stop(settings.id);														
					soundManager.unmute(settings.id);
				}
			});
			
			this.find('.pause').click(function() {
				methods.togglePlayPause(parent);
				if(options.playlist) {
					methods.pauseSound(parent);
				} else {
					soundManager.pause(settings.id);
				}
			});
			
			this.find('.mute').click(function() {	
				methods.toggleVolumeControl(parent, options);
			});
			
			this.find('.unmute').click(function() {	
				methods.toggleVolumeControl(parent, options);
			});
			
			/* this.find('.shuffle').click(function() {	
				methods.toggleShuffle(parent);
			});
			
			this.find('.unshuffle').click(function() {	
				methods.toggleShuffle(parent);
			}); */
			
			this.find('.next').click(function() {	
					methods.stopSound(parent);
					methods.reset(parent, options, settings);
					methods.seekNextTrack(parent);
					methods.togglePlayPause(parent);					
					methods.playSound(parent, options);
			});
			
			this.find('.prev').click(function() {	
					methods.stopSound(parent);
					methods.reset(parent, options, settings);
					methods.seekPrevTrack(parent);			
					methods.playSound(parent, options);
					methods.togglePlayPause(parent);
			});
			
			this.find('.norepeat').click(function() {			
				$(this).removeClass('repeat-cur');
				parent.find('.repeatall').addClass('repeat-cur');
				
			});
			
			this.find('.repeatall').click(function() {			
				$(this).removeClass('repeat-cur');
				if(options.playlist) {
					parent.find('.repeatone').addClass('repeat-cur');
				} else {
					parent.find('.norepeat').addClass('repeat-cur');
				}
				
			});
			
			this.find('.repeatone').click(function() {			
				$(this).removeClass('repeat-cur');
				parent.find('.norepeat').addClass('repeat-cur');
				
			});
			
			
			this.find('.hadefied .hadefied-volume .vc').click(function() {
				methods.toggleVolumeControl(parent, options);
				parent.find('.hadefied .hadefied-volume .vc').removeClass('vc-cur');
				$(this).addClass('vc-cur');
				if($(this).attr('rel') == "0") {
					parent.find('.hadefied .unmute').hide();
					parent.find('.hadefied .mute').show();		
					if(options.playlist) {
						methods.muteSound(parent);
					} else {							
						soundManager.mute(settings.id);
					}
				} else {
					parent.find('.hadefied .mute').hide();
					parent.find('.hadefied .unmute').show();				
					if(options.playlist) {
						methods.unmuteSound(parent);
						methods.setVolume(parent);
					} else {					
						volume = methods.getVolume(parent);
						soundManager.unmute(settings.id);
						var x = soundManager.getSoundById(settings.id);
						x.setVolume(volume);
					}
					
				}
			});
			
			this.find('.hadefied .hadefied-volume .vc').hover(function() {	
				var start = $(this).index();
				var end = parent.find('.hadefied .hadefied-volume .vc').length;
				parent.find('.hadefied .hadefied-volume .vc').each(function(index) {
					if(index >= start && index < end) {
						$(this).css('background', '#32798D');
					} else {
						$(this).css('background', '#000');
					}
				});
			}, function () {
				$(this).css('background', '#32798D');
			});
			
			if(options.playlist) { 
				this.find('.hadefied .hadefied-playlist li').click(function() {
					methods.stopSound(parent);
					methods.clearTracks(parent);
					$(this).addClass('cur');		
					parent.find('.hadefied .hadefied-time-frame  .frame-title').html(parent.find('.hadefied .hadefied-playlist li.cur').html());			
					methods.reset(parent, options, settings);
				});		
				
				this.find('.hadefied .hadefied-playlist li').dblclick(function() {
					methods.stopSound(parent);
					methods.playSound(parent, options);
				});
				
				parent.find('.hadefied .hadefied-time-frame  .frame-title').html(parent.find('.hadefied .hadefied-playlist li.cur').html());
			} else {
				parent.find('.hadefied .hadefied-time-frame  .frame-title').html(settings.title);
			}
			if(options.playlist) { 
				if(jQuery.jScrollPane) {
					$('.hadefied-playlist .hadefied-playlist-container').jScrollPane();				
				} else {
					$('.hadefied-playlist .hadefied-playlist-container').css('overflow', 'auto');
				}

			}			
		}    

	})( jQuery );

