
var PlayerAudio = Class.create({

	initialize: function(nombre, params) {
		var url = params.url;
		var autoplay = params.autoplay;
		var bar = params.bar;
		
		this._onInit = params.onInit.bind(this);
		this.onUpdate = params.onUpdate.bind(this);
		
		// indica si el player flash se inicializó
		this.initialized = false;
		
		// nombre de la variable dnd se almacenara este objeto, es necesario para que flash pueda comunicarse con este objeto
		this.nombre = nombre;
		
		// id del flash con el reproductor
		this.idFlash = this.nombre + "_flash";
		
		// posicion de reproducción
		this.position = "0";
		
		// indica si se está reproduciendo
		this.isPlaying = "false";
		
		// nivel de volumen del player
		this.volume = "100";
		
		// esto debería de actualizarlo el player
		
		// duracion
		this.duration = "0";
		
		// bytes descargados
		this.bytesLoaded;
		
		// total de bytes del fichero
		this.bytesTotal;
		
		// % cargado del fichero
		this.bytesPercent;
		
		// url del archivo en reproducción		
		if (url) {
			this.setUrl(url);
		}
		
		if (autoplay) {
			this.play();
		}
		
		// volumen antes de un mute
		this.volAnteriorMute;
		
		// volumen actual del reproductor
		this.volActual;
	},
	
	onInit: function() {
		this.initialized = true;
		if (this._onInit) {
			(this._onInit.bind(this))();
		}
	},
	
	// devuelve 1 referencia al player flash
	getFlashObject: function() {
		return $(this.idFlash);
	},
	
	// habilita la comunicación de eventos
	enable: function() {
		var player = this.getFlashObject();
		if (this.initialized && player) {
			player.SetVariable("enabled", "true");
		}
	},
	
	// deshabilita la comunicación de eventos
	disable: function() {
		var player = this.getFlashObject();
		if (player) {
			player.SetVariable("enabled", "false");
		}
	},
	
	// comienza la reproduccion
	play: function() {
		var player = this.getFlashObject();
		if (this.initialized && player) {
			player.SetVariable("method:play", "");
			this.enable();
			this.isPlaying = "true";
		}
	},
	
	// pausa la reproduccion
	pause: function() {
		var player = this.getFlashObject();
		if (this.initialized && player) {
			player.SetVariable("method:pause", "");
			this.disable();
			this.isPlaying = "false";
		}
	},
	
	// pausa la reproduccion
	pause2: function() {
		var player = this.getFlashObject();
		if (this.initialized && player) {
			player.SetVariable("method:pause", "");
		}
	},
	
	
	// detiene la reproduccion
	stop: function() {
		var player = this.getFlashObject();
		if (this.initialized && player) {
			player.SetVariable("method:stop", "");
			this.disable();
			this.isPlaying = "false";
		}
	},
	
	// establece el archivo a reproducir
	setUrl: function(url) {
		var player = this.getFlashObject();
		if (this.initialized && player) {
			this.url = url;
			player.SetVariable("method:setUrl", this.url);
		}
	},
	
	// desplaza la reproducción a una posición concreta
	setPosition: function(pos) {
		var player = this.getFlashObject();
		if (this.initialized && player) {
			this.position = pos;
			
			player.SetVariable("method:setPosition", this.position);
		}
	},
	
	// establece el volumen al nivel deseado
	setVolume: function(vol) {
		var player = this.getFlashObject();
		if (this.initialized && player) {
			this.volume = vol;
			player.SetVariable("method:setVolume", this.volume);
		}
	},
	
	getUrl: function () {
		return this.url;
	}
});


