(function ($) {
     $.player = {
         init : function (sm) {
             $.player.sm = sm;
         },

         config : function (id, url) {
             return {
                 id : id,
                 url : url,
                 // autoLoad : true,
                 whileloading : function () {
                     var total = $('.slider').filter('.' + id).width();
                     var percent = (this.bytesLoaded / this.bytesTotal);
                     var width = Math.floor( (percent * total) );
                     var loading = $('.loading').filter('.' + id).css('width', width);
                 },

                 onload : function () {
                     $('.loading').filter('.'+id);
                 },

                 onfinish : function () {
                     $.player.sm.setPosition(id, 0);
                     $('.playing').filter('.'+id)
                         .animate({ width : '0' }, 1500);
                     var button = $('.play_and_pause-' + id);
                     button.trigger('click');
                     $.player.sm.stop(id);
                 },

                 whileplaying : function () {
                     var duration;
                     if (this.loaded) {
                         duration = this.duration;
                     } else {
                         duration = this.durationEstimate;
                     }
                     var total = $('.slider').filter('.' + id).width();
                     var width = Math.floor((this.position / duration) * total);
                     $('.playing').filter('.'+id).css('width', width);
                 }
             };
         },

         playlist : function (options) {
             var pl = options['songs'];
             var current_song = pl[0];
             var end = pl.length - 1;
             var ui = {
                 cp : $('.currently-playing'),
                 prev : $('.prev'),
                 play : $('.play'),
                 pause : $('.pause'),
                 stop : $('.stop'),
                 next : $('.next')
             };
             $.extend(ui, options['ui']);

             var update_ui = function (action) {
                 $.each(ui, function (k, v) {
                     if (k == action) {
                         v.css('border-bottom', '1px solid #eee');
                     } else {
                         v.css('border-bottom', '0');
                     }
                 });
             };

             var stop = function () {
                 $.player.sm.stop(current_song.name);
             };

             var play = function () {
                 ui.cp.text(current_song.name);

		 /* google page tracking */
		 var page_tracker = _gat._getTracker("UA-7253318-1");
		 page_tracker._trackPageview(current_song.url);

                 $.player.sm.play(current_song.name);
                 update_ui('play');
             };

             var pause = function (song) {
                 ui.cp.text(current_song.name);
                 $.player.sm.pause(current_song.name);
                 update_ui('pause');
             };

             var next = function () {
                 stop();
                 var ci = pl.indexOf(current_song);
                 if (ci == end) {
                     current_song = pl[0];
                 } else {
                     current_song = pl[ci + 1];
                 }
                 ui.play.click();
             };

             var prev = function () {
                 stop();
                 var ci = pl.indexOf(current_song);
                 if (ci == 0) {
                     current_song = pl[end];
                 } else {
                     current_song = pl[ci - 1];
                 }
                 ui.play.click();
             };

             ui.prev.click(prev);
             ui.play.click(play);
             ui.pause.click(pause);
             ui.stop.click(stop);
             ui.next.click(next) ;

             $.each(pl, function (i, song) {
                 if (i == 0) {
                     ui.cp.text(song.name);
                 }
                 $.player.sm.createSound({
                     url : song.url,
                     id : song.name,
                     onfinish : next
                 });
             });
             if (options['autoplay'])
                 ui.play.click();
         },


         add : function (selector) {
             $(selector).each($.player._add);
         },

         cls : {
             play : 'play',
             pause : 'pause',
             play_hover : 'play-hover',
             pause_hover : 'pause-hover'
         },

         ui : function (pid) {
             $('.play_and_pause' + '-' + pid).toggle(
                 function () {
                     $this = $(this);
                     $.player.sm.play(pid);
                     $this.removeClass($.player.cls.play);
                     $this.addClass($.player.cls.pause);
                 },
                 function () {
                     $this = $(this);
                     $.player.sm.pause(pid);
                     $this.removeClass($.player.cls.pause);
                     $this.addClass($.player.cls.play);
                 }
             );
             var sliders = $('.slider').filter('.'+pid);
             $('.slider').filter('.'+pid).slider(
                 {
                     handle : '.cursor',
                     change : function (evt, ui) {
                         var pos_percent = ui.value;
                         var snd = $.player.sm.getSoundById(pid);
                         var size = function () {
                             if (snd.loaded) {
                                 return snd.duration;
                             }
                             return snd.durationEstimate;
                         };
                         pos = (pos_percent / 100) * size();
                         $.player.sm.setPosition(pid, pos);
                     }
                 }
             );
         },

         move : function (evt, ui) {
             $(this).parent('');
         },

         _add : function (i, obj) {
             var href = $(obj).attr('href');
             var pid = $(obj).attr('id');
             // console.log('Creating sound: ', pid, href);
             $.player.sm.createSound($.player.config(pid, href));
             $.player.ui(pid);
         }
     };

 })(jQuery);

