logs = function() {
	for (var c=0;c<arguments.length;c++) {
		switch(typeof arguments[c]) {
			case 'string': 
			case 'array':
				log(arguments[c])
			break;
			default:
				for (var i in arguments[c]) log(i,':',arguments[c][i])
			break;
		}
	}
}

/**
 * a wrapper for the imp.swf flex object
 */
InvisibleMusicPlayer = function(id, options) {
	try {
		var self = this;
		this.loaded = false
		// the queue is for loading files before imp is loaded
		this.load_queue = []
		this.player_id = 'imp_' + new Date().getTime()
		
		InvisibleMusicPlayer.players[this.player_id] = this
		InvisibleMusicPlayer.callbacks[this.player_id] = {}
		
		callbacks = {
			onLoad: function() {
				this.loaded = true
				//log(this.player_id, "loaded.")
				while (this.load_queue.length) {
					var url = this.load_queue.shift()
					this.load(url)
				}
				signal(this, 'onload')
			},
			onID3: function(sound) {
				signal(this, 'onid3', sound)
			},
			onOpen: function(sound) {
				signal(this, 'onopen', sound)
			},
			onProgress: function(sound) {
				signal(this, 'onprogress', sound)
			},
			onComplete: function(sound) {
				signal(this, 'oncomplete', sound)
			},
			onIOError: function(e) {
				signal(this, 'onioerror', e)
			},
			/**
			 * is called when the player is playing
			 * @param e is a flex soundChannel
			 */
			onPosition: function(channel) {
				signal(this, 'onposition', channel)
			},
			/**
			 * called when the player starts to play
			 */
			onPlay: function(channel) {
				signal(this, 'onplay', channel)
			},
			/**
			 * called when the sound is complete
			 */
			onSoundComplete: function(sound) {
				signal(this, 'onsoundcomplete', sound)
			},
			/**
			 * is called on a general error
			 */
			onError:function(error) {
				signal(this, 'onerror', error)
			}
		}
		var properties = {height: options.height || "0", width: options.width || "0", data:options.path || "imp.swf"}
		for (var callback in callbacks){
			InvisibleMusicPlayer.callbacks[this.player_id][callback] = (function(option){
				return (bind(function(){
					return option.apply(self, arguments);
				}, this))
			})(callbacks[callback]);
		}
		var params = {flashVars:'id=' + this.player_id};
		
		this.player = swfobject.createSWF(properties, params, id)
	} catch (ex) {
		logs(ex)
	}
}
InvisibleMusicPlayer.prototype = {
	toString:function() {
		return "<imp: " + this.player_id + ">";
	},
	load: function(url) {
		if (this.loaded) 
			this.player.load(url)
		else {
			//log("queueing: ", url)
			this.load_queue.push(url)
		}
	},
	play: function(url, volume) {
		if (volume) this.set_volume(volume)
		url = url || null
		this.player.play(url)
	},
	pause: function(toggle) {
		toggle = toggle || false
		this.player.pause(toggle)
	},
	stop: function() {
		this.player.stop()
	},
	set_volume:function(volume) {
		this.player.set_volume(volume)
	},
	is_playing:function () {
		return this.player.is_playing()
	},
	is_paused:function () {
		return this.player.is_paused()
	},
	get_channel: function() {
		return this.player.get_channel()
	},
	get_sound: function() {
		return this.player.get_sound()
	},
	loaded:function() {
		return this.player.loaded()
	}
}
InvisibleMusicPlayer.players = {}
InvisibleMusicPlayer.callbacks = {}
