(function(){
    var userAgent = navigator.userAgent.toLowerCase();
    var msie = /msie/.test( userAgent ) && !/opera/.test( userAgent );
		  
    var mPlayer = window.mPlayer = function(options) {
        // If the context is a namespace object, return a new object
        return this instanceof mPlayer ?
        this.init(options) :
        new mPlayer(options);
    };

    mPlayer.prototype = {
        init: function(o) {
            this.songs = o.songs || [];
            this.pos = 0;
            this.currentSong = o.songs[this.pos] || "";
            this.path = o.path || "media/";
            this.autoplay = (typeof o.autoplay != 'undefined') ? o.autoplay : true;
            this.timer1, this.timer2, this.timer3;
            this.player = document.createElement("div");
            this.player.id = "mplayer";
            document.body.appendChild(this.player);
		
            this.load();
        },
        load: function() {
            var fullpath = this.path + this.currentSong;
            var autoplay = (this.autoplay) ? "true" : "false";
            if(msie){
                this.player.innerHTML = '<object id="mPlayer_object" '
                + 'classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6"'
                + 'type="application/x-oleobject" name="sound">'
                + '<param name="url" value="'+fullpath+'" />'
                + '<param name="volume" value="100" />'
                + '<param name="autostart" value="'+autoplay+'" />'
                + '<param name="uimode" value="invisible" />'
                + '<\/object>';
            }
            else {
                this.player.innerHTML = '<object id="sound" type="audio/mpeg" data="'+fullpath+'" codebase="http://www.apple.com/qtactivex/qtplugin.cab">'
                + '<param name="src" value="'+fullpath+'"/>'
                + '<param name="autoplay" value="'+autoplay+'"/>'
                + '<param name="controller" value="false"/>'
                + '<param name="volume" value="100"/>'
                + '<param name="hidden" value="true"/>'
                + '<param name="enablejavascript" value="true"/>'
                + '<param name="type" value="audio/mpeg"/>'
                + '<embed src="'+fullpath+'" type="audio/mpeg" pluginspage="www.apple.com/quicktime/download" enablejavascript="true" name="sound" autoplay="'+autoplay+'"/>'
                + '</object>';
            }
        },
        check: function() {
            var s = document.sound;
            if(msie){
                return s && s.controls && s.controls.play;
            }
            else {
                return s && s.Play;
            }
        },
        play: function() {
            var s = document.sound;
            if(this.check){
                (msie) ? s.controls.play() : s.Play();
            }
        },
        stop: function() {
            var s = document.sound;
            if(this.check){
                if(msie) s.controls.stop();
                else {
                    s.Stop(); s.Rewind();
                }
            }
        },
        pause: function() {
            var s = document.sound;
            if(this.check){
                (msie) ? s.controls.pause() : s.Stop();
            }
        },
        next: function() {
            var l = this.songs.length-1;
            this.pos++;
            if(this.pos <= l) this.currentSong = this.songs[this.pos];
            else {
                this.currentSong = this.songs[0];
                this.pos = 0;
            }
            this.load();
        },
        prev: function() {
            this.pos--;
            if(this.pos >= 0) this.currentSong = this.songs[this.pos];
            else {
                this.pos = this.songs.length-1;
                this.currentSong = this.songs[this.pos];
            }
            this.load();
        },
        volume: function(val) {
            var s = document.sound;
            if(this.check){
                (msie) ? s.settings.volume = val : s.SetVolume(val);
            }
        },
        fadeOut: function() {
            var s = document.sound;
            if(this.check){
                var currentVol = (msie) ? s.settings.volume : s.GetVolume();
                while (currentVol >= 0){
                    currentVol -= 5;
                    this.volume(currentVol);
                }
            }
        },
        fadeIn: function() {
            var s = document.sound;
            if(this.check){
                var currentVol = (msie) ? s.settings.volume : s.GetVolume();
                while (currentVol <= 100){
                    currentVol += 5;
                    this.volume(currentVol);
                }
            }
        }
    }
})();