/**
 * Sound for Mootools 1.2
 * @license		MIT-style license
 * @author		Anquetil Manuel <manu [at] baphira.com>
 * @copyright	Author
 */
var Sound = new Class({
	
	Implements: [Options],
	
	options: {
		id: 'mysound', /* NE JAMAIS UTILISER DE TIRET DANS L'ID POUR IE */
		autostart: 'true',
		loop: 'true',
        swfUrl: 'dewplayer.swf'
	},

    flashCompatibility: function() {
        return Browser.Plugins.Flash.version > 0;
    },
	
	initialize: function(url, options) {
		this.setOptions(options);
		
		this.url = url;

        if(this.flashCompatibility()) {

			// La lecture du son par flash est beaucoup plus efficace

			if(!Browser.Engine.trident) {
				// Utilisé pour Firefox et les autres car Swiff ne charge pas un flash caché !?
				this.sound = new Element('object', {
					id: this.options.id,
					type : 'application/x-shockwave-flash',
					data : this.options.swfUrl,
					width: '0',
					height: '0',
					html:
						'<param name="movie" value="'+this.options.swfUrl + '?mp3=' + this.url+'" />' +
						'<param name="flashvars" value="mp3=' + this.url+'&autostart='+(this.options.autostart == 'true' ?'1':'0')+'&autoreplay='+(this.options.loop == 'true' ?'1':'0')+'" />'
				}).inject($(document.body));
			} else {
				// Fonctionne pour IE... mais pas sur Firefox qui considère qu'un flash caché n'a pas à être chargé...
				this.sound = new Swiff(this.options.swfUrl, {
					id: this.options.id,
					name: this.options.id,
					container: new Element('div').inject($(document.body)),
					height: 1, // si 0, le flash ne sera jamais chargé
					width: 1, // si 0, le flash ne sera jamais chargé
					params: {
						wmode: 'transparent',
						movie: this.options.swfUrl + '?mp3=' + this.url
					},
					vars: {
						mp3: this.url,
						autostart: (this.options.autostart == 'true' ?'1':'0'),
						autoreplay: (this.options.loop == 'true' ?'1':'0')
					}
				});
			}
        } else {
			// Dans le cas où flash n'est pas activé, on utilise la méthode standard
			// Ne fonctionne pas sur mac
            this.deleteAndCreateAudioMpegObject(this.options.autostart);
        }
		
	},

	deleteAndCreateAudioMpegObject: function(autostart) {
		if(this.sound) {
			this.sound.destroy();
		}

		this.sound = new Element('object', {
			id: this.options.id,
			name: this.options.id,
			type: 'audio/mpeg',
			data: this.url,
			width: '0',
			height: '0',
			html:
				'<param name="filename" value="'+this.url+'" />' +
				'<param name="autostart" value="'+autostart+'" />' +
				'<param name="loop" value="'+this.options.loop+'" />' +
				'<param name="showcontrols" value="0" />'
		}).inject($(document.body));
	},

	play: function() {
        if(this.flashCompatibility()) {
			$(this.sound).dewplay();
        } else {
			this.sound.Play();
        }
	},

	pause: function() {
		if(this.flashCompatibility()) {
			$(this.sound).dewpause();
        } else {
			this.sound.Stop();
        }
	},
	
	stop: function() {
		if(this.flashCompatibility()) {
			$(this.sound).dewstop();
        } else {
			this.deleteAndCreateAudioMpegObject(false);
        }
	}
	
});
