 

   function Player ()
   {
      this.defaultPlaylist = 32;
	  this.playlistTitle;
      this.paused = false;
      this.stopped = true;
      this.sound = null;
      this.position = 0;
      this.frequency = 1000;
      this.isLoaded = false;      
      this.duration = 0;
      this.bytesTotal = 0;
      this.playerWidth = 252;      
      this.playlistId = 0;
      this.playlist = new Array();
	  this.currentTrack = -1;
	  this.volume = 4; 
      this.lastVolume = 4;
      this.expanded = false;
      this.soundId = null;
      this.nextTrackLoaded = false;
      this.nextTrackCalled = false;
      
   }
   Player.prototype.init = function(playlistTitle)
   {
   
   
    this.expand(this.expanded);       
   
    if (!this.getPositionCookie())    
        this.loadPlaylist(this.defaultPlaylist);   
   
    // image preloading
    tmp1 = new Image();
    tmp1.src = "/Images/Page/volume4.gif";
    
    tmp2 = new Image();
    tmp2.src = "/Images/Page/volume3.gif";
    
    tmp3 = new Image();
    tmp3.src = "/Images/Page/volume2.gif";
    
    tmp4 = new Image();
    tmp4.src = "/Images/Page/volume1.gif";
    
    tmp5 = new Image();
    tmp5.src = "/Images/Page/pausearrow.gif";
   
           
    this.registerCallback();  
    
   }
   function Track ()
   {
    this.artist = "";
    this.album = "";
    this.title = "";
    this.image1 = "";
    this.image2 = "";
    this.duration = "";   
    this.fileName = "";             
   }
   
   function d(text) {
   // $('debug').innerHTML += text + "<br/>";
   }
   
   
   Player.prototype.expand = function(expand)
   {
        this.expanded = expand;
        
        if (expand) 
        {
            $('fullplayer').style.display = 'inline';
            $('miniplayer').style.display = 'none';                    
        }
        else 
        {
            $('fullplayer').style.display = 'none';
            $('miniplayer').style.display = 'inline';
        }   
   }
   
   Player.prototype.setPositionCookie = function ()
   {           
   
        var position = this.position;
        var paused = this.paused;
        var stopped = this.stopped;
        var track = this.currentTrack;        
        var playlistId = this.playlistId;
        
        // don't update, this isn't right
        if (position == null || position < 1000)
        {           
            return;
        }          
                   
                   
        this.setCookie("paused",paused);            
        this.setCookie("stopped",stopped);
        this.setCookie("position",position);
        this.setCookie("track",track);
        this.setCookie("playlistId",playlistId);
        this.setCookie("volume", this.volume);
   }
   
   Player.prototype.getPositionCookie = function ()
   {    
               
        var paused = (this.getCookie("paused") == "true") ? true : false;
        var stopped = (this.getCookie("stopped") == "true") ? true : false;
        var position = this.getCookie("position"); 
        var track = this.getCookie("track");
        var playlistId = this.getCookie("playlistId");              
        var volume = this.getCookie("volume");
        

        
        if (paused == null || stopped == null || position == null || track == null)
            return false;
        


                  
         req = new Ajax.Request('/PlaylistJSON.aspx?id=' + playlistId, 
            { 
                method: 'GET',                
                onSuccess: function(transport,json) {                                                                                    
                   
                   
                   
                     
                    eval(transport.responseText);                              
                     
                    player.setPlaylist(playlist.tracklist,playlist.title, playlistId);
                    
                     
                    player.currentTrack = track;
                                                          
                    
                    player.stopped = stopped;
                    player.paused = paused;
                    player.volume = volume;
                    player.position = position;                                        
                    
                                          
                     
                    player.loadSong(player.playlist[player.currentTrack].fileName,player.position);                                            
                      
                    if (!stopped && !paused)
                    {                                                                                           
                        player.playSong();
                    }         
                         
                 },
                 onFailure: function() {
                    // error              
                 }
             });  
       
                
       
        
        return true;          
   }
   
   Player.prototype.setPlaylist = function(playlistObj,title, id) 
   {
        this.playlist = playlistObj  		
		this.playlistTitle = title;
		this.playlistId = id;		
        //this.paused = true;      		
		this.setPositionCookie();
		
		$('playlistdisplay').innerHTML = title;
   }
   
    Player.prototype.getCookie = function(cookie_name)
   {
      var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

      if ( results )
        return ( unescape ( results[2] ) );
      else
        return null;
   }
   
   Player.prototype.setCookie = function(name,value) {
	
    var date = new Date();
    date.setTime(date.getTime()+3600000);
    var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+value+expires+"; path=/"; // + window.location.hostname;
}
   

   
   Player.prototype.loadPlaylist = function (playlistId, play) 
   {		
	
        req = new Ajax.Request('/PlaylistJSON.aspx?id=' + playlistId, 
            { 
                method: 'GET',                
                onSuccess: function(transport,json) {                                                                                    
                     
                    eval(transport.responseText);                              
                     
                    player.setPlaylist(playlist.tracklist,playlist.title, playlistId);
                    player.firstTrack();
                   
                    if (play)
                        player.playSong();  
                                        
                 },
                 onFailure: function() {
                    // error              
                 }
             }); 
             
             
   }
   
     Player.prototype.loadPlaylistAndSong = function (playlistId, songNum, play) 
   {		
	
        req = new Ajax.Request('/PlaylistJSON.aspx?id=' + playlistId, 
            { 
                method: 'GET',                
                onSuccess: function(transport,json) {                                                                                    
                     
                    eval(transport.responseText);                              
                     
                    player.setPlaylist(playlist.tracklist,playlist.title, playlistId);
                    player.currentTrack = songNum;
                    player.loadSong(player.playlist[songNum].fileName);
                    
                    if (play)
                        player.playSong();
                     
                   
                     
                 },
                 onFailure: function() {
                    // error              
                 }
             }); 
             
             
   }
   
   Player.prototype.loadPlaylistFromSong = function (songId, play) 
   {		
	
        req = new Ajax.Request('/PlaylistJSON.aspx?sid=' + songId, 
            { 
                method: 'GET',                
                onSuccess: function(transport,json) {                                                                                    
                                                              
                    eval(transport.responseText);                              
                                                              
                    player.setPlaylist(playlist.tracklist,playlist.title, playlist.playlistId);
                    player.currentTrack = tracknum;
                    player.loadSong(player.playlist[tracknum].fileName);
                    
                    if (play)
                        player.playSong();

                 },
                 onFailure: function() {
                    // error              
                 }
             }); 
             
             
   }
   
   Player.prototype.loadPlaylistFromArtist = function (artistId, play) 
   {		
	
        req = new Ajax.Request('/PlaylistJSON.aspx?aid=' + artistId, 
            { 
                method: 'GET',                
                onSuccess: function(transport,json) {                                                                                    
                     
                    eval(transport.responseText);                              
                     
                    player.setPlaylist(playlist.tracklist,playlist.title, playlist.playlistId);
                    player.currentTrack = tracknum;
                    player.loadSong(player.playlist[tracknum].fileName);
                    
                    if (play)
                        player.playSong();
                     
                   
                     
                 },
                 onFailure: function() {
                    // error              
                 }
             }); 
             
             
   }
   
   
    Player.prototype.getSongId = function(song)
    {
        var slash = '/'
        if (song.match(/\\/)) {
            slash = '\\'
        } 
        
        return song.substring(song.lastIndexOf(slash) + 1, song.lastIndexOf('.'))     
    }
   
   
    Player.prototype.loadSong = function(song, poss) 
   {
   
        
      
   
      if (!this.paused && this.soundId != null) 
      {        
            soundManager.stop(this.soundId);     
      } 
                        
      soundId = this.getSongId(song);            
            
      sound = soundManager.getSoundById(soundId);
      
      
      if (poss == null)
           poss = 0;
      
            
      if (sound != null)
      {                 
        
         soundManager.setPosition(soundId, poss);
         soundManager.setVolume(soundId, this.volume * 25);
      }
      else
      {
       
          soundManager.createSound({
             id: soundId, // required
             url: song, // required            
             volume: this.volume * 25,
             autoLoad: true,
             autoPlay: false, 
             onfinish:function(){Player.nextTrack();},
             position: poss           
        });  
        
                        
      }                 
      this.soundId = soundId;     
      this.position = poss;          
      this.duration = 0;            
      
      if (this.expanded)
        $('position2').innerHTML = this.formatTime(0);
      else
        $('position').innerHTML = this.formatTime(0);

      this.setPositionCookie();
            
    }
    
   
   Player.prototype.firstTrack = function()
   {
        
        this.currentTrack = 0;
              
        track = this.playlist[this.currentTrack];        
        
        this.loadSong(track.fileName);
 
        
 
        if (!this.paused)
            this.playSong();
            
   }
   
   Player.prototype.setTrack = function(num)
   {
        this.currentTrack = num;
        
        track = this.playlist[this.currentTrack];

        stopped = this.stopped;
                   
        this.loadSong(track.fileName);
        
        if (!this.paused)
            this.playSong();
            
        this.onTimerEvent();
   }
   
   Player.prototype.nextTrack = function()
   {
        if (this.playlist.length <= this.currentTrack + 1) 
            this.currentTrack = 0;
        else
            this.currentTrack++;
         
        track = this.playlist[this.currentTrack];
                   
        this.loadSong(track.fileName);
        
        if (!this.paused)
            this.playSong();
            
        this.nextTrackLoaded = false;
        this.nextTrackCalled = false;
   }
   
   
    Player.prototype.loadNextTrack = function()
   {
        if (this.playlist.length <= this.currentTrack + 1) 
            currentTrack = 0;
        else
            currentTrack = this.currentTrack + 1;
         
      track = this.playlist[this.currentTrack];
                   
      soundId = this.getSongId(track.fileName);            
            
      sound = soundManager.getSoundById(soundId);
      
      soundManager.createSound({
             id: soundId, // required
             url: track.fileName, // required            
             volume: this.volume * 25,
             autoLoad: true,
             autoPlay: false,              
             position: 0          
        });  
        
        
        this.nextTrackLoaded = true;
               
   }
   
   
   Player.prototype.currentTrack = function() 
   {
        return this.playlist[this.currentTrack];
   }
   Player.prototype.previousTrack = function()
   {
        if (this.currentTrack == 0) 
            this.currentTrack = this.playlist.length - 1;
        else
            this.currentTrack--;
                   
        track = this.playlist[this.currentTrack];
        
        this.loadSong(track.fileName);
        
        if (!this.paused)
            this.playSong();
   
        
   
   }
   
    Player.prototype.setVolume = function(vol) 
    {
        // 0 - 4
        if (vol < 0) 
            vol = 0;
        else if (vol > 4)
            vol = 4;
            
        this.volume = vol;
        
        soundManager.setVolume(this.soundId, vol * 25);
        
        this.displayVolume();
        
        this.setPositionCookie();
        
    }
    
    Player.prototype.displayImage = function () 
    {
      
        
        track = this.playlist[this.currentTrack];
        
        if (track.image1.length == 0)
        {
            if (this.expanded) $('artwork_expanded').style.display = 'none';
            else $('artwork').style.display = 'none';
            return;    
        }
              
        
          if (!this.expanded)
          {
            if ($('artwork').style.display != 'block')
                $('artwork').style.display = 'block';
            if ($('artwork_image').innerHTML.indexOf(track.image) == -1)
            {
                $('artwork_image').innerHTML = "<img src=\"" + track.image2 + "\">";
            }          
          
          }
          else
          {
            if ($('artwork_expanded').style.display != 'block')
                $('artwork_expanded').style.display = 'block';
            if ($('artwork_expanded_image').innerHTML.indexOf(track.image1) == -1)
            {
                $('artwork_expanded_image').innerHTML = "<img src=\"" + track.image1 + "\">";
            }
          
          }    
    }

   
   Player.prototype.displayVolume = function()
   {       
       
       
       if (this.lastVolume == this.volume)
            return;
            
       this.lastVolume = this.volume;
       
       
       if (this.expanded)
       {
           if (this.volume < 1)
                 $('v1_2').innerHTML = "&nbsp;";
           if (this.volume < 2)
                 $('v2_2').innerHTML = "&nbsp;";
           if (this.volume < 3)  
                $('v3_2').innerHTML = "&nbsp;";
           if (this.volume < 4)
                $('v4_2').innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                
                
            switch (this.volume)
            {
                case 4:
                    $('v4_2').innerHTML = "<img src=\"/Images/Page/volume4.gif\">";
                case 3:
                    $('v3_2').innerHTML = "<img src=\"/Images/Page/volume3.gif\">";
                case 2:
                    $('v2_2').innerHTML = "<img src=\"/Images/Page/volume2.gif\">";
                case 1:
                    $('v1_2').innerHTML = "<img src=\"/Images/Page/volume1.gif\">";
            }
        }
        else
        {
           if (this.volume < 1)
                 $('v1').innerHTML = "&nbsp;";
           if (this.volume < 2)
                 $('v2').innerHTML = "&nbsp;";
           if (this.volume < 3)  
                $('v3').innerHTML = "&nbsp;";
           if (this.volume < 4)
                $('v4').innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                
                  
            switch (this.volume)
            {
                case 4:
                    $('v4').innerHTML = "<img src=\"/Images/Page/volume4.gif\">";
                case 3:
                    $('v3').innerHTML = "<img src=\"/Images/Page/volume3.gif\">";
                case 2:
                    $('v2').innerHTML = "<img src=\"/Images/Page/volume2.gif\">";
                case 1:
                    $('v1').innerHTML = "<img src=\"/Images/Page/volume1.gif\">";
            }
        } 
           
        
      
   
   }
   
  
   Player.prototype.playSong = function(pos) 
   {      
               
      if (pos == null)
      {   
          pos = this.position * 1;          
      }        
      else
      {      
        pos = pos * 1;  
        this.position = pos;        
      }                          
      
      
      this.setVolume(this.volume);
      
      if (this.currentTrack == -1)
        this.firstTrack();
            
      
      if (pos > 0 && this.paused)
      {
        soundManager.togglePause(this.soundId);           
      }
      else if (pos > 0)  
      {      
        song = soundManager.getSoundById(this.soundId);
        if (song.duration == null || song.duration < pos)
        {
            setTimeout("player.playSong(" + pos + ")",2500)            
            return;                
        }                
        soundManager.setPosition(this.soundId,pos);
        soundManager.play(this.soundId,{volume:this.volume*25,onfinish:function(){Player.nextTrack();}});     
      }
      else       
      {      
          song = soundManager.getSoundById(this.soundId);
        if (song.duration == null || song.duration < pos)
        {
            setTimeout("player.playSong()",1000)            
            return;                
        }                
         soundManager.play(this.soundId,{volume:this.volume*25,onfinish:function(){Player.nextTrack();}});
      }
      
      
      $('playpause_expanded').innerHTML = "<img src=\"/Images/Page/pausearrow.gif\">";
	  $('playpause').innerHTML = "<img src=\"/Images/Page/pausearrow.gif\">";
      
      this.stopped = false;   
      this.paused = false;  
		
		
	
	  
      this.setPositionCookie();    
    }
    
   Player.prototype.pauseSong = function() 
   {
        
		$('playpause_expanded').innerHTML = "<img src=\"/Images/Page/playarrow.gif\">";
		$('playpause').innerHTML = "<img src=\"/Images/Page/playarrow.gif\">";
		
		song = soundManager.getSoundById(this.soundId);
		if (song != null)
		    this.position = song.position;
		
		
		if (!song.paused)		
		    soundManager.togglePause(this.soundId);
		
        //this.sound.stop();
        this.paused = true;
        this.stopped = true;        
        
      
        this.setPositionCookie();
      
    }
    
    
   Player.prototype.playPause = function() {
       if (this.paused || this.stopped) 
            this.playSong();
       else
            this.pauseSong();
            
         
   }
   

   
   Player.prototype.registerCallback = function() {
      setInterval(this.onTimerEvent.bind(this), this.frequency);
   }
   
   Player.prototype.onTimerEvent = function() {
        

        var position = 0;
        var duration = 0;
       
        if (this.currentTrack == -1)
        {
            if (this.playlist.length > 0)
                this.firstTrack(); 
            else return;
        }
          
        this.displayVolume();    
       
       
       
       if (this.soundId == "" || this.soundId == null)
        return;
       song = soundManager.getSoundById(this.soundId);
       if (song == null)
        return;
            
       position = this.position = song.position;
        
        track = this.playlist[this.currentTrack];
       /* 
        if (track.duration > 0) 
        {
            duration = track.duration * 1000;            
        }
        else*/ if (this.duration != null && this.position != 0 && this.duration != 0)
            duration = this.duration;
        else 
        {
            duration = song.durationEstimate;
            if (duration == null || isNaN(duration))
            {
                duration = 0;                                            
            }
            this.duration = duration;
        }
        
        if (this.expanded) 
        {
            $('display2').innerHTML = track.artist + " - \"" + track.title + "\"";
            $('duration2').innerHTML = this.formatTime(duration); 
            this.songList();
        }
        else
        {
           $('display').innerHTML = track.artist + " - \"" + track.title + "\"";
           $('duration').innerHTML = this.formatTime(duration);         
        }    
        
        this.displayImage();
        
      //  if (!this.stopped) 
       // { 
            if (this.expanded)
                $('position2').innerHTML = this.formatTime(position);
            else
                $('position').innerHTML = this.formatTime(position);
         
         
         if (position > 10000 && position+20000 >= duration && !this.nextTrackLoaded)
         {
            this.loadNextTrack();
         }
            
         if (position > 10000 && position >= duration && !this.nextTrackCalled) 
        {           
           setTimeout("player.nextTrack()",3000);           
           this.nextTrackCalled = true;
        }             
      //  }
        
        this.setPositionCookie(); 
        
       
       
        
                 
   }
   
   Player.prototype.songList = function() {
       
       html = "";
       
       for (i=1; i <= this.playlist.length; i++) 
       {
            c = i -1;
            track = this.playlist[c];
            bold = (c == this.currentTrack) ? "font-weight:bold;" : "";
            html += "<div id=\"s" + c + "\" onclick=\"player.setTrack(" + c + ")\" style=\"" + bold + " cursor:pointer;cursor:hand;overflow:hidden;height:1.4em;\">" + i + ". " + track.artist + " - \"" + track.title + "\"</div>";            
            
       }
       
       html += "&nbsp;<br/>";
       
       $('songs').innerHTML = html;
   
   
   }
   
   Player.prototype.formatTime = function(time) {
        if (isNaN(time))
            return "0:00";
   
        time = Math.round(time / 1000);
        min = Math.floor(time / 60) ;
        sec = time - (min * 60);
        if (sec < 10) 
            sec = "0" + sec;
        
        return min + ":" + sec ;
        
        
   }
   
 
function PlaylistScroll(scrollname, div_name, up_name, down_name)
{
    
    
    this.div_name = div_name;
    this.name = scrollname;
    this.scrollCursor = 0;
    this.speed = 3;
    this.timeoutID = 0;
    this.div_obj = null;
    this.up_name = up_name;
    this.dn_name = down_name;

{
        if (document.getElementById) {
            div_obj = document.getElementById(this.div_name);
            if (div_obj) {
                this.div_obj = div_obj;
                this.div_obj.style.overflow = 'hidden';
            }
        }
    }

this.stopScroll = function() {
        clearTimeout(this.timeoutID);
    }

this.scrollUp = function() {
        if (this.div_obj) {
            this.scrollCursor = (this.scrollCursor - this.speed) < 0 ? 0 : this.scrollCursor - this.speed;
            this.div_obj.scrollTop = this.scrollCursor;
            this.timeoutID = setTimeout(this.name + ".scrollUp()", 60);
        }
    }

this.scrollDown = function() {

    if (this.div_obj) {
        this.scrollCursor += this.speed;
        this.div_obj.scrollTop = this.scrollCursor;
        if (this.div_obj.scrollTop == this.scrollCursor) {
            this.timeoutID = setTimeout(this.name + ".scrollDown()", 60);
        } else {
            this.scrollCursor = this.div_obj.scrollTop;
        }
    }
}

this.resetScroll = function() {
        if (this.div_obj) {
            this.div_obj.scrollTop = 0;
            this.scrollCursor = 0;
        }
    }
}   

// JavaScript Document