function findeFlash(flash){
	if (document.all) {
		if (document.all[flash]) {
			return document.all[flash];
		}
		if (window.opera) {
			try {
				var movie = eval(window.document + flash);
			} 
			catch (exception) {
				return false;
			}
			if (movie.SetVariable) {
				return movie;
			}
		}
		return;
	}
	if (document.layers) {
		if (document.embeds) {
			var movie = document.embeds[flash];
			if (movie.SetVariable) {
				return movie;
			}
		}
		return;
	}
	if (!document.getElementById) {
		return;
	}
	var movie = document.getElementById(flash);
	if (!movie) return;
	if (movie.SetVariable) {
		return movie;
	}
	var movies = movie.getElementsByTagName('embed');
	if (!movies || !movies.length) {
		return;
	}
	movie = movies[0];
	if (movie.SetVariable) {
		return movie;
	}
	return;
}

function get_cookie_val(key){
	if (!document.cookie) return null;
	var sRE = "(?:; )?" + key + "=([^;]*);?";
	var oRE = new RegExp(sRE);
	if (oRE.test(document.cookie)) {
		return decodeURIComponent(RegExp["$1"]);
	}
	else {
		return null;
	}
}

function set_cookie_val(key, val){
	document.cookie = key + "=" + escape(val) + "; path=/;";
}

function sound_come(){
	var film = findeFlash('sound');
	if (!film) return;

	var variables = [], value, debug_cookie = [], debug_set = [];
	
	for (var v in flash_vars) {
		value = parseFloat(get_cookie_val(v));
		debug_cookie.push(v + '=' + value);
		if (isNaN(value) || !isFinite(value)) value = flash_vars_def[v];
		variables.push(value);
		debug_set.push(v + '=' + value);
	}
	
	//alert("Load from Cookie:\n"  + debug_cookie.join("\n")+"\n\nSet values:\n"  + debug_set.join("\n"));

	try {
		film.playSound(variables[0], variables[1], variables[2], variables[3]);
	}
	catch(e) {
		//console.log('Error playSound:', e);
	}
}

function sound_go(){
	var film = findeFlash('sound');
	if (!film) return;
	
	var debug_flash = [], value;
	
	for (var v in flash_vars) {
		try {
			value = film.GetVariable('_root.' + v);
			flash_vars[v] = value;
			debug_flash.push(v + '=' + value);
		}
		catch(e) {
			//console.log('Error GetVariable:'+v+':', e);
		}
	}
	
	//alert("Read from flash:\n" + debug_flash.join("\n"));
	
	for (var v in flash_vars) set_cookie_val(v, flash_vars[v]);
}


function sound_stop(){
	var film = findeFlash('sound');
	if (!film) return;
	film.stopMusic();
}

