// sounds.js

// J. Bernal <earful@bitako.com> - http://earful.bitako.com - 04/28/2005
// Public domain library

// Flash movie dynamic loading based on SoundManager by Scott Schiller <scott@schillmania.com>

var is_ie = navigator.appName.toLowerCase().indexOf("internet explorer") + 1;
var is_mac = navigator.appVersion.toLowerCase().indexOf("mac") + 1;

function Sounds(sounds_path, container) {
	var self = this;
	this.sounds_path = sounds_path;
	this.movies = [];
	this.container = container;
	this.unsupported = 0;
	this.defaultName = "default"; 
	
	this.FlashObject = function(url) {
		var me = this;
		this.o = null;
		this.loaded = false;
		this.isLoaded = function() {
			if (me.loaded) return true;
			if (!me.o) return false;
			me.loaded = ((typeof(me.o.readyState) != "undefined" && 
				me.o.readyState == 4) || (typeof(me.o.PercentLoaded) != "undefined" && me.o.PercentLoaded() == 100));
			return me.loaded;
		}
		this.mC = document.createElement("div");
		this.mC.className = "movieContainer";
		with (this.mC.style) {
			position = "absolute";
			left = "-1000px";
			width = "1px";
			height = "1px";
		}
	
		var html = ['<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"><param name="movie" value="' + url + '"><param name="quality" value="high"></object>', '<embed src="'+url+'" width="1" height="1" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>'];
		if (navigator.appName.toLowerCase().indexOf("microsoft") + 1) {
			this.mC.innerHTML = html[0];
			this.o = this.mC.getElementsByTagName("object")[0];
		} 
		else {
			this.mC.innerHTML = html[1];
			this.o = this.mC.getElementsByTagName("embed")[0];
		}
		document.getElementsByTagName("div")[0].appendChild(this.mC);
	}

	this.addMovie = function(movie_name,url) {
		self.movies[movie_name] = new self.FlashObject(url);
	}

	this.checkMovie = function(movie_name) {
		movie_name = movie_name || self.defaultName;
		if (!self.movies[movie_name]) return false;
		return (self.movies[movie_name].isLoaded()) ? self.movies[movie_name] : false;
	}

	this.play = function(sound_file, movie_name) {
		if (self.unsupported) return false;
		movie = self.checkMovie(movie_name);
		movie.o.SetVariable("sound_file", self.sounds_path + "/" + sound_file);
		movie.o.TGotoLabel("/", "play");
	}

	if (is_ie && is_mac) this.unsupported = 1;
	if (!this.unsupported) this.addMovie(this.defaultName, self.sounds_path + "/sounds.swf");
}

function SoundsNull() {
	this.movies = []; 
	this.container = null;
	this.unsupported = 1;
	this.FlashObject = function(url) {}
	this.addMovie = function(name,url) {}
	this.play = function(sound_file, movie_name) { return false; }
	this.defaultName = "default";
}

var sounds = null;
function soundsInit(sounds_path) { sounds = new Sounds(sounds_path); }
