var Player = Class.create();

Player.prototype = {

    initialize: function(soundObjectId, sound, mainControlId, stopControlId, playingImageSrc, soundUrl){
    
        this.sound = sound;
        this.soundObjectId = soundObjectId;
        this.mainControl = $(mainControlId);
        this.stopControl = $(stopControlId);
        this.soundUrl = soundUrl;
        var playingImage = new Image();
        playingImage.src = playingImageSrc;
        this.playingImageSrc = playingImageSrc;
        if (this.mainControl) {
            this.pausedImageSrc = this.mainControl.src;
        }
        this.playing = false;
        this.paused = false;
        this.ready = false;
        
        this.soundMan = new SoundManager('/flash/soundmanager2.swf');
        window.soundManager = this.soundMan;
        
        // attach onload handler
        if (window.addEventListener) {
            window.addEventListener('load', this.soundMan.beginDelayedInit, false);
            window.addEventListener('beforeunload', this.soundMan.destruct, false);
        }
        else 
            if (window.attachEvent) {
                window.attachEvent('onload', this.soundMan.beginInit);
                window.attachEvent('beforeunload', this.soundMan.destruct);
            }
            else {
                // no add/attachevent support - safe to assume no JS->Flash either.
                this.soundMan.onerror();
                this.soundMan.disable();
            }
        
        this.soundMan.onload = function(){
            // soundManager is initialised, ready to use.
            this.ready = true;
        }
.bind(this);
        
        if (this.mainControl) {
        
            this.mainControlClickHandler = this.toggle.bindAsEventListener(this);
            Event.observe(this.mainControl, 'click', this.mainControlClickHandler);
            
        }
        
        if (this.stopControl) {
        
            this.stopControlClickHandler = this.stop.bindAsEventListener(this);
            Event.observe(this.stopControl, 'click', this.stopControlClickHandler);
            
        }
        
    },
    
    createSound: function(){
    
        if (!this.ready) {
            alert("Загружаем звук и прочую инфраструктуру, подождите секундочку!");
            return;
        }
        
        this.soundMan.createSound({
            id: this.sound,
            url: this.soundUrl + '?id=' + this.soundObjectId,
            onfinish: function(){
                this.playing = false;
                this.paused = false;
                this.setMainControlImageSource(this.pausedImageSrc);
            }
.bind(this)
        });
    },
    
    setSoundObjectId: function(soundId, soundObjectId){
        this.soundObjectId = soundObjectId;
        this.sound = soundId;
    },
    
    isPlaying: function(){
    
        return this.playing;
    },
    
    toggle: function(){
    
        if (this.playing) {
            this.pause();
        }
        else {
            this.play();
        }
    },
    
    play: function(){
    
        if (!this.ready) {
            alert("Загружаем звук и прочую инфраструктуру, подождите секундочку!");
            return;
        }
        
        if (this.playing) {
            return;
        }
        
        this.createSound();
        
        this.soundMan.play(this.sound);
        this.playing = true;
        this.paused = false;
        this.setMainControlImageSource(this.playingImageSrc);
    },
    
    pause: function(){
    
        if (!this.ready) {
            alert("Загружаем звук и прочую инфраструктуру, подождите секундочку!");
            return;
        }
        
        if (!this.playing || this.paused) {
            return;
        }
        
        this.soundMan.pause(this.sound);
        this.paused = true;
        this.playing = false;
        this.setMainControlImageSource(this.pausedImageSrc);
    },
    
    stop: function(){
    
        if (!this.ready) {
            alert("Загружаем звук и прочую инфраструктуру, подождите секундочку!");
            return;
        }
        
        if (!this.playing) {
            return;
        }
        
        this.soundMan.stop(this.sound);
        this.playing = false;
        this.paused = false;
        this.setMainControlImageSource(this.pausedImageSrc);
    },
    
    setMainControlImageSource: function(imageSource){
        if (this.mainControl && imageSource) {
            this.mainControl.src = imageSource;
        }
    }
}

/*

 var AbstractSoundProvider = Class.create();

 AbstractSoundProvider.prototype = {

 initialize: function(soundFolder, soundNames) {

 this.soundMan = new SoundManager('/flash/soundmanager2.swf');

 window.soundManager = this.soundMan;

 // attach onload handler

 if (window.addEventListener) {

 window.addEventListener('load', this.soundMan.beginDelayedInit, false);

 window.addEventListener('beforeunload', this.soundMan.destruct, false);

 } else if (window.attachEvent) {

 window.attachEvent('onload', this.soundMan.beginInit);

 window.attachEvent('beforeunload', this.soundMan.destruct);

 } else {

 // no add/attachevent support - safe to assume no JS->Flash either.

 this.soundMan.onerror();

 this.soundMan.disable();

 }

 }

 }

 var RandomSoundProvider = Class.create();

 RandomSoundProvider.prototype = {

 initialize: function(soundFolder, soundNames) {

 this.soundMan = new SoundManager('/flash/soundmanager2.swf');

 window.soundManager = this.soundMan;

 // attach onload handler

 if (window.addEventListener) {

 window.addEventListener('load', this.soundMan.beginDelayedInit, false);

 window.addEventListener('beforeunload', this.soundMan.destruct, false);

 } else if (window.attachEvent) {

 window.attachEvent('onload', this.soundMan.beginInit);

 window.attachEvent('beforeunload', this.soundMan.destruct);

 } else {

 // no add/attachevent support - safe to assume no JS->Flash either.

 this.soundMan.onerror();

 this.soundMan.disable();

 }

 }

 }

 var DoodadSoundProvider = Class.create();

 DoodadSoundProvider.prototype = {

 initialize: function(doodadId) {

 this.doodadId = doodadId;

 },

 createSound : function() {

 this.soundMan.createSound(

 {

 id : this.sound,

 url :  '/4duk/sound!doodad.action?id=' + this.soundObjectId,

 onfinish : function() {

 this.playing = false;

 this.paused = false;

 this.setMainControlImageSource(this.pausedImageSrc);

 }.bind(this)

 }

 );

 }

 */


