// current demo track playing
var smCurrentTrack = '';



// toggleDemo()
// Toggles between playing/pausing the demo that fired this event
function smToggleDemo(e)
{
    // the demo track
    var demo = soundManager.getSoundById(this.id);
    
    // whether or not the demo is currently paused
    var paused = demo.paused || demo.playState == 0;
    
    // pause all the tracks
    soundManager.pauseAll();
    
    // check if the track was paused, then we should play
    if (paused)
    {
        // check if we switched tracks
        if (this.id != smCurrentTrack)
        {
            // check if there was another track played before
            if (smCurrentTrack != '')
            {
                // reset track to beginning
                soundManager.getSoundById(smCurrentTrack).setPosition(0);
                
                // previous track is no longer playing
                document.getElementById(smCurrentTrack).className = '';
            }
            
            // start this track at beginning
            demo.setPosition(0);
        }
        
        // store the id of the current track
        smCurrentTrack = this.id;
        
        // play the track
        demo.play();
    }
    else // track was playing
    {
        // mark track as paused
        document.getElementById(smCurrentTrack).className = 'paused';
    }
}



// updateDemo()
// Updates status of demo depending on state of demo.
// Displays percent loaded or current time position
function smUpdateDemo()
{
    // the demo list item element
    var demo = document.getElementById(this.sID);
    
    // the demo time/buffer element
    var demotime = document.getElementById(this.sID + '-time');
    
    // whether or not the demo is currently not playing
    var notplaying = this.paused || this.playState == 0;
    
    // if the track is not playing, or it's buffering,
    // and it's not the current track
    // then display the percent loaded
    if ((notplaying || this.isBuffering) && this.sID != smCurrentTrack)
    {
        // calculate loaded percent
        var pct = Math.round((this.bytesLoaded / this.bytesTotal * 100)) || 0;
        
        // mark class as loading to change style
        demo.className = 'loading';
        
        // display percent loaded
        demotime.innerHTML = pct + '%';
    }
    else // current track is playing or paused, so display time
    {
        // calculate minutes and seconds for current position
        var sec = Math.floor(this.position / 1000 % 60);
        var min = Math.floor(this.position / 1000 / 60);

        // mark class as playing or paused for style
        demo.className = this.paused ? 'paused' : 'playing';
        
        // display current time
        demotime.innerHTML = min + (sec < 10 ? ":0" : ":") + sec;
    }
}



// nextDemo()
// Finds the next demo in the list and toggles it
function smNextDemo()
{
    // first update the status of the demo
    
    // the demo list item element
    var demo = document.getElementById(this.sID);
    
    // the demo time/buffer element
    var demotime = document.getElementById(this.sID + '-time');
    
    // calculate loaded percent
    var pct = Math.round((this.bytesLoaded / this.bytesTotal * 100)) || 0;
    
    // mark class as loaded for style
    demo.className = 'loading';
    
    // display percent loaded
    demotime.innerHTML = pct + '%';
    
    
    // find the next demo in the list
    var nextDemo = demo.nextSibling || demo.parentNode.firstChild;
    
    // make sure the next item is valid
    if (nextDemo)
    {
        // toggle the next item
        nextDemo.onclick();
    }
}



soundManager.debugMode = false;
soundManager.multiShot = false;
soundManager.url = 'demos/';
soundManager.waitForWindowLoad = true;

soundManager.onerror = function()
{
    document.getElementById('demos-error').style.display = 'block';
}
