function InlinePlayer() {
  var self = this;
  var pl = this;
  var sm = soundManager; // soundManager instance
  this.excludeClass = 'inline-exclude'; // CSS class for ignoring MP3 links
  this.links = [];
  this.sounds = [];
  this.soundsByURL = [];
  this.indexByURL = [];
  this.lastSound = null;
  this.soundCount = 0;

  this.config = {
    playNext: false, // stop after one sound, or play through list until end
	autoPlay: false  // start playing the first sound right away
  }

  this.css = {
    // CSS class names appended to link during various states
    sDefault: 'sm2_link', // default state
    sLoading: 'sm2_loading',
    sPlaying: 'sm2_playing',
    sPaused: 'sm2_paused'
  }

  this.getSoundByURL = function(sURL) {
    return (typeof self.soundsByURL[sURL] != 'undefined'?self.soundsByURL[sURL]:null);
  }

  /*this.isChildOfNode = function(o,sNodeName) {
    if (!o || !o.parentNode) {
      return false;
    }
    sNodeName = sNodeName.toLowerCase();
    do {
      o = o.parentNode;
    } while (o && o.parentNode && o.nodeName.toLowerCase() != sNodeName);
    return (o.nodeName.toLowerCase() == sNodeName?o:null);
  }*/

  this.events = {

    // handlers for sound events as they're started/stopped/played

    play: function() {
      this._data.oLink.removeClass(this._data.className);
      this._data.className = pl.css.sPlaying;
      this._data.oLink.addClass(this._data.className);
    },

    stop: function() {
      this._data.oLink.removeClass(this._data.className);
      this._data.className = '';
    },

    pause: function() {
      this._data.oLink.removeClass(this._data.className);
      this._data.className = pl.css.sPaused;
      this._data.oLink.addClass(this._data.className);
    },

    resume: function() {
      this._data.oLink.removeClass(this._data.className);
      this._data.className = pl.css.sPlaying;
      this._data.oLink.addClass(this._data.className);
    },

    finish: function() {
        this._data.oLink.removeClass(this._data.className);
      //pl.removeClass(this._data.oLink,this._data.className);
      this._data.className = '';
      /*if (pl.config.playNext) {
        var nextLink = (pl.indexByURL[this._data.oLink.href]+1);
        if (nextLink<pl.links.length) {
          pl.handleClick({'target':pl.links[nextLink]});
        }
      }*/
    }

  }


  this.handleClick = function(e) {
    e.preventDefault();

    var soundURL = $(this).attr('href');
    var thisSound = self.getSoundByURL(soundURL);
    if (thisSound) {
      // already exists
      if (thisSound == self.lastSound) {
        // and was playing (or paused)
        thisSound.togglePause();
      } else {
        // different sound
        thisSound.togglePause(); // start playing current
        if (self.lastSound) self.stopSound(self.lastSound);
      }
    } else {
      // create sound
      thisSound = sm.createSound({
       id:'inlineMP3Sound'+(self.soundCount++),
       url:soundURL,
       onplay:self.events.play,
       onstop:self.events.stop,
       onpause:self.events.pause,
       onresume:self.events.resume,
       onfinish:self.events.finish
      });
      // tack on some custom data
      thisSound._data = {
        oLink: $(this), // DOM node for reference within SM2 object event handlers
        className: self.css.sPlaying
      };
      self.soundsByURL[soundURL] = thisSound;
      self.sounds.push(thisSound);
      if (self.lastSound) self.stopSound(self.lastSound);
      thisSound.play();
      // stop last sound
    }

    self.lastSound = thisSound; // reference for next call

    return false;
  }

  this.stopSound = function(oSound) {
    soundManager.stop(oSound.sID);
    soundManager.unload(oSound.sID);
  }

  this.init = function() {
    var that = this;
    $(".previewLnk").each(function(){
        $(this).addClass(that.css.sDefault);
        $(this).click(that.handleClick);
    });

  }

  this.init();

}

var inlinePlayer = null;

$(document).ready(function(){
    soundManager.debugMode = false; // disable or enable debug output
    soundManager.waitForWindowLoad = true;

    soundManager.url = '/js/soundmanager/swf/'; // path to directory containing SM2 SWF
    soundManager.nullURL = '/js/soundmanager/null.mp3';

    soundManager.onload = function() {
      inlinePlayer = new InlinePlayer();
    }

});



