      var trackNum = 0; var trackCount = 0; var jsonObj = new Object(); dir = 1;
      google.load("swfobject", "2.1");
      $(document).ready(function() {
        $('[rel=tipsys]').tipsy({html: true, fade: true, gravity: 's'});
        $('[rel=tipsy]').tipsy({html: true, fade: true, gravity: 'n'});
        $('.social').css({ opacity: 0.8 });
        $('.social').hover(
          function () { $(this).css({ opacity: 1.0 }); }, 
          function () { $(this).css({ opacity: 0.7 }); }
        );
        $(window).resize(function () {
          var h = $(window).height() + 10;
          $('#myytplayer').attr('height',h);
          $('.centered').centerBox();
        });
        $('#startup').fadeIn('fast');
        setTimeout("playVideo('kuEc6lx2lv8', 0);",1000);
        $("#artist").bind("keydown", function(event) {
          var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
          if (keycode == 13) {
            $('#start').click();
            return false;
          }
          return true;
        });
        $('.centered').centerBox();
        $('#pause').click(function() {
          if (getPlayerState() == 1) pause();
          else play();
        });
        $('#savelist').click(function() {
          $('#loading').fadeIn('fast');
          $.post('/ajax',{p:jsonObj}, function(data) {
            $('#loading').fadeOut('fast');
            if (data.status == 1) {
              document.location = '#pl;' + data.pid;
              $('#savebox').html('Your playlist has been saved. You can share it using the following link:<br><br><input readonly="readonly" type="text" value="http://soundly.net/#pl;' + data.pid + '" onmouseover="this.select();" onclick="this.focus(); this.select();" style="width: 570px;"><br><br>[<a href="javascript:;" id="closesave">close</a>]');
              $('#closesave').click(function() {
                $('#savebox').fadeOut();
              });
            } else {
              $('#savebox').html('Your playlist couldn\'t be saved. Sorry about that.');
            }
            $('.centered').centerBox();
            $('#savebox').fadeIn('fast');
          }, 'json');
        });
        $('#newlist').click(function() {
          trackNum = 0;
          $('#startupmsg').html('Enter the name of a band or artist you like and we\'ll take care of the playlist.');
          $('#startup').fadeIn('fast');
          $('#playlist').fadeOut('fast');
          $('#savebox').fadeOut('fast');
          $('#infobox').fadeOut('fast');
          $('#infobar').fadeOut('fast');
          $('#controls').fadeOut('fast');
          clearInterval();
        });
        $('#closeinfo').click(function() {
          $('#infobox').fadeOut();
        });
        $('#closeplaylist').click(function() {
          $('#playlist').fadeOut();
        });
        $('#skip').click(function() {
          dir = 1;
          next();
        });
        $('#prev').click(function() {
          dir = -1;
          prev();
        });
        $('#moreinfo').click(function() {
          $('#infobox').fadeIn();
        });
        $('#showlist').click(function() {
          $('#playlist').fadeIn();
        });
        $('#start').click(function() {
          artistname = $('#artist').val();
          document.location = '#' + artistname;
          if (artistname) {
            $('#startup').fadeOut('fast', function() {
              $('#loading').fadeIn('fast');
            });
            $.getJSON('/ajax?s='+artistname, function(data) {
              jsonObj = data;
              if (data.status == 1) {
                trackCount = data.count;
                setText(0);
                $('#loading').fadeOut('fast');
                $('#infobar').fadeIn('fast');
                $('#controls').fadeIn('fast');
                playVideo(data.tracks[0].id, 0);
                setInterval("updateTime();",250);
                buildPlaylist();
              } else {
                $('#startupmsg').html('We couldn\'t build a playlist for that band or artist; pick another and try again!');
                if (data.status == -1) $('#startupmsg').html('We couldn\'t find any information about that band or artist; pick another and try again!');
                if (data.status == -2) $('#startupmsg').html('We couldn\'t find enough similar bands or artists to build a playlist; try again!');
                $('#loading').fadeOut('fast', function() {
                  $('#startup').fadeIn('fast');
                });
              }
            });
          }
        });
        if (location.hash) {
          if (location.hash.substring(0,4) == '#pl;') {
            $('#startup').fadeOut('fast', function() {
              $('#loading').fadeIn('fast');
            });
            $.getJSON('/ajax?g='+location.hash.substring(4), function(data) {
              jsonObj = data;
              if (data.status == 1) {
                trackCount = data.count;
                setText(0);
                $('#loading').fadeOut('fast');
                $('#infobar').fadeIn('fast');
                $('#controls').fadeIn('fast');
                playVideo(data.tracks[0].id, 0);
                setInterval("updateTime();",250);
                buildPlaylist();
              } else {
                $('#startupmsg').html('We couldn\'t retrieve that saved playlist. Pick a new band or artist and build your own instead!');
                $('#loading').fadeOut('fast', function() {
                  $('#startup').fadeIn('fast');
                });
              }
            });
          } else {
            $('#artist').val(unescape(location.hash.substring(1)));
            $('#start').click();
          }
        }
      });

      function updateTime() {
        duration = (ytplayer.getDuration() >= 0) ? ytplayer.getDuration() : 0;
        current = (ytplayer.getCurrentTime() >= 0) ? ytplayer.getCurrentTime() : 0;
        $('#timer').html(niceTime(current) + ' / ' + niceTime(duration));
      }

      function next() {
        dir = 1;
        trackNum = (trackNum < trackCount) ? trackNum + 1 : 0;
        setText(trackNum);
        playVideo(jsonObj.tracks[trackNum].id, 0);
        buildPlaylist();
      }

      function setText(tn) {
        $('#artisttext').html('<a target="_blank" href="javascript:;">' + jsonObj.tracks[tn].artist + '</a>');
        $('#tracktext').html('<a target="_blank" href="javascript:;">' + jsonObj.tracks[tn].title + '</a>');
        $.getJSON('ajax?i='+jsonObj.tracks[tn].artist+'+'+jsonObj.tracks[tn].title, function (data) {
          $('#artisttext').html('<a target="_blank" href="'+ data.artisturl + '">' + jsonObj.tracks[tn].artist + '</a>');
          $('#tracktext').html('<a target="_blank" href="'+ data.trackurl + '">' + jsonObj.tracks[tn].title + '</a>');
          $('#download').html('<a target="_blank" href="'+ data.trackurl + '">Download Track</a>');
        });
      }

      function prev() {
        dir = -1;
        trackNum = (trackNum > 0) ? trackNum - 1 : trackCount;
        setText(trackNum);
        playVideo(jsonObj.tracks[trackNum].id, 0);
        buildPlaylist();
      }

      function onYouTubePlayerReady(playerId) {
        ytplayer = document.getElementById("myytplayer");
        ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
        ytplayer.addEventListener("onError", "onPlayerError");
      }

      function onPlayerError(errorCode) {
        if (errorCode == 150 || errorCode == 101) {
          if (dir == 1) next();
          else prev();
        }
      }

      function onytplayerStateChange(newState) {
        switch (newState) { 
          case 0:
            next();
            break;

          case 1:
            $('.tipsy').hide();
            $('#pause').html('<img rel="tipsys" title="Pause" src="/img/player/pause.png" border=0>');
            $('[rel=tipsys]').tipsy({html: true, fade: true, gravity: 's'});
            break;

          case  2:
          case  3:
          case  5:
          case -1:
            $('.tipsy').hide();
            $('#pause').html('<img rel="tipsys" title="Play" src="/img/player/play.png" border=0>');
            $('[rel=tipsys]').tipsy({html: true, fade: true, gravity: 's'});
            break;
        }
      }

      function playVideo(id) {
        if (ytplayer) ytplayer.loadVideoById(id, 0);
      }

      function play() {
        if (ytplayer) ytplayer.playVideo();
      }

      function pause() {
        if (ytplayer) ytplayer.pauseVideo();
      }

      function stop() {
        if (ytplayer) ytplayer.stopVideo();
      }

      function getPlayerState() {
        if (ytplayer) return ytplayer.getPlayerState();
      }

      function getVideoUrl() {
        alert(ytplayer.getVideoUrl());
      }

      function niceTime(d) {
        d = Number(d);
        var h = Math.floor(d / 3600);
        var m = Math.floor(d % 3600 / 60);
        var s = Math.floor(d % 3600 % 60);
        return ((h > 0 ? h + ":" : "") + (m > 0 ? (h > 0 && m < 10 ? "0" : "") + m + ":" : "0:") + (s < 10 ? "0" : "") + s);
      }

      function share(type) {
        artistname = ($('#artist').val()) ? '%23'+$('#artist').val() : '';
        switch (type) {
          case 'twitter':
            width = 550; height = 400;
            url = 'http://twitter.com/share?text=Check+this+out%3a+http%3A%2F%2Fsoundly.net/'+artistname+' #soundly';
            break;

          case 'facebook':
            width = 600; height = 350;
            url = 'http://www.facebook.com/sharer.php?u=http://soundly.net/'+artistname;
            break;

          case 'reddit':
            width = 1000; height = 600;
            url = 'http://reddit.com/submit?url=http://soundly.net/'+artistname;
            break;

          case 'digg':
            width = 1050; height = 600;
            url = 'http://digg.com/submit?phase=2&url=http://soundly.net/'+artistname;
            break;

          case 'stumbleupon':
            width = 1050; height = 600;
            url = 'http://www.stumbleupon.com/submit?url=http://soundly.net/'+artistname;
            break;

          case 'delicious':
            width = 1050; height = 600;
            url = 'http://del.icio.us/post?url=http://soundly.net/'+artistname;
            break;

          case 'linkedin':
            width = 520; height = 570;
            url = 'http://www.linkedin.com/shareArticle?mini=true&url=http://soundly.net/'+artistname+'&source=XOMF.com';
            break;

          case 'myspace':
            width = 550; height = 450;
            url = 'http://www.myspace.com/Modules/PostTo/Pages/?u=http://soundly.net/'+artistname;
            break;
        }
        if (!window.open(url,'share','resizable=1,toolbar=0,status=0,scrollbars=yes,width='+width+',height='+height)) document.location.href=url;
      }

      function buildPlaylist() {
        tblbody = '<table id="playtable"><thead><tr><td style="width: 35px;" class="tblhead">#</td><td class="tblhead">Artist</td><td class="tblhead">Title</td></tr></thead>';
        for(i=0;i<trackCount;i++) {
          rowc = (i % 2) ? 'tb1' : 'tb2';
          rowc = (trackNum == i) ? 'tbp' : rowc;
          tblbody += '<tr class="' + rowc + '"><td style="width: 30px;" class="centertext">' + (i+1) + '</td><td>' + jsonObj.tracks[i].artist + '</td><td>' + jsonObj.tracks[i].title + '</td></tr>';      
        }
        tblbody += '</table>';
        $('#tblcontainer').html(tblbody);
        $('#playtable tr').click(function() {
          if (this.rowIndex) {
            trackNum = (this.rowIndex);
            prev();
          }
        });
        $("#playtable tr").hover(function() {
          $(this).addClass("tbh");
        },
        function() {
          $(this).removeClass("tbh");
        });
      }
