



function Mp3Player () {
    this.soundlist = new Array();
    this.mp3list = new Array();
    
    this.addMp3 = function( name, url, desc ) {
        this.mp3list[ name ] = url;
    }

    this.getImage = function(src) {
        var ch = src.childNodes;
        for (var i=0; i<ch.length; i++) {
            if (ch[i].tagName == 'IMG') return ch[i];
        }
        return false;
    }
    
    this.simClick = function( el ) {
        var o = getel( el);
        if (o) {
            var s = o.onclick;
            s = '@' + s;
            s = '' + s.match( /[\w\.\/]+\.mp3/);
            //alert( ''+s);
            if (s) {
                this.click( o, s );
            }
        }
    }
    

    this.changeImg = function( img, fplay ) {
        if (!img) return;
            
        if (fplay) {
            img.src = img.src.replace(/_play\./, "_stop.");
            img.title = 'Click to Stop';
        } else {
            img.src = img.src.replace(/_stop\./, "_play.");
            img.title = 'Click to Play';
        }
    }

    this.onFinish = function( snd ) {
        var img = snd.lnkimg;
        if (img) this.changeImg( img, false );
    }

    this.click = function( obj, mp3 ) {
        var img;
        if (obj && obj.tagName != 'IMG')
            img = this.getImage(obj);
        else
            img = obj;
            
//        if (! this.mp3list[mp3]) return;
        
        if (this.soundlist[mp3]) {
            var snd = this.soundlist[mp3];
            if (snd.playState) {
                this.changeImg( img, false );
                snd.stop();
            } else {
                snd.play();
                this.changeImg( img, true );
            }
            snd.lnkimg = img;
        } else {
            var name = mp3.replace(/\//, "_");
            var snd = soundManager.createSound({
                id: name,
                url: mp3,
                volume: 50,
                onfinish : function () { mp3p.onFinish(this); }
                /*,onload: soundLoadedFunction*/
            });
            snd.play();                    
            snd.lnkimg = img;
            this.soundlist[mp3] = snd;
            this.changeImg( img, true );
        }
        return false;
    }

}

var mp3p = new Mp3Player();