#define ALIAS_SIZE 300

object Sounds_Alias[ALIAS_SIZE];
object Music_Alias[ALIAS_SIZE];
object Greetings_Alias[ALIAS_SIZE];
object Talks_Alias[ALIAS_SIZE];

extern void InitSounds();
extern void InitSoundsCommon();
extern void InitMusic();
extern void InitGreetings();
extern void InitTalks();

extern void RegisterMusicScheme();

void Alias_Init()
{
	string localization = LanguageGetLanguage();
	string music = GetMusicScheme();
	string sounds_file = "sound\alias\sounds_" + localization + ".c";
	string music_file = "sound\alias\music_" + music + ".c";
	string greetings_file = "sound\alias\greetings_" + localization + ".c";
	string talks_file = "sound\alias\talks_" + localization + ".c";
	int i = 0;

	for (i = 0; i < ALIAS_SIZE; i++) {
		DeleteAttribute(Sounds_Alias[i], "");
		DeleteAttribute(Music_Alias[i], "");
		DeleteAttribute(Greetings_Alias[i], "");
		DeleteAttribute(Talks_Alias[i], "");
	}

	if (FindFile("PROGRAM\sound\alias", "*.c", "sounds_" + localization + ".c") == "") {
		//trace("Alias_Init: Can't initialize "+ localization + " sounds, loading ENGLISH.");
		sounds_file = "sound\alias\sounds_english.c";
	}

	if (FindFile("PROGRAM\sound\alias", "*.c", "music_" + music + ".c") == "") {
		//trace("Alias_Init: Can't initialize "+ music + " music, loading standard.");
		music_file = "sound\alias\music_standard.c";
	}

	if (FindFile("PROGRAM\sound\alias", "*.c", "greetings_" + localization + ".c") == "") {
		//trace("Alias_Init: Can't initialize "+ localization + " greetings, loading ENGLISH.");
		greetings_file = "sound\alias\greetings_english.c";
	}
	if (FindFile("PROGRAM\sound\alias", "*.c", "talks_" + localization + ".c") == "") {
		//trace("Alias_Init: Can't initialize "+ localization + " talks, loading ENGLISH.");
		talks_file = "sound\alias\talks_english.c";
	}

	if(LoadSegment(sounds_file))
	{
		InitSounds();
		UnloadSegment(sounds_file);
	}

	if (FindFile("PROGRAM\sound\alias", "*.c", "sounds_common.c") != "") {
		if (LoadSegment("sound\alias\sounds_common.c")) {
			InitSoundsCommon();
			UnloadSegment("sound\alias\sounds_common.c");
		}
	}

	if (LoadSegment("sound\alias\music_standard.c")) {
		RegisterMusicScheme();
		if ("sound\alias\music_standard.c" == music_file) InitMusic();
		UnloadSegment("sound\alias\music_standard.c");
	}
	aref musicfiles = GetFiles("PROGRAM\sound\alias", "music_*.c");
	int n = GetAttributesNum(musicfiles);
	for (i = 0; i < n; i++) {
		aref arCur = GetAttributeN(musicfiles, i);
		string fname = GetAttributeValue(arCur);
		if (fname == "music_standard.c") continue;
		if (LoadSegment("sound\alias\" + fname)) {
			RegisterMusicScheme();
			if ("sound\alias\" + fname == music_file) InitMusic();
			UnloadSegment("sound\alias\" + fname);
		}
	}

	if(LoadSegment(greetings_file))
	{
		InitGreetings();
		UnloadSegment(greetings_file);
	}

	if(LoadSegment(talks_file))
	{
		InitTalks();
		UnloadSegment(talks_file);
	}
}

int FindSounds(string id)
{
	if (id == "") return -1;
	for (int i = 0; i < ALIAS_SIZE; i++) {
		if (!CheckAttribute(Sounds_Alias[i], "id")) continue;
		if (Sounds_Alias[i].id == id) return i;
	}
	return -1;
}

int FindMusic(string id)
{
	if (id == "") return -1;
	for (int i = 0; i < ALIAS_SIZE; i++) {
		if (!CheckAttribute(Music_Alias[i], "id")) continue;
		if (Music_Alias[i].id == id) return i;
	}
	return -1;
}

int FindGreetings(string id)
{
	if (id == "") return -1;
	for (int i = 0; i < ALIAS_SIZE; i++) {
		if (!CheckAttribute(Greetings_Alias[i], "id")) continue;
		if (Greetings_Alias[i].id == id) return i;
	}
	return -1;
}

int FindTalks(string id)
{
	if (id == "") return -1;
	for (int i = 0; i < ALIAS_SIZE; i++) {
		if (!CheckAttribute(Talks_Alias[i], "id")) continue;
		if (Talks_Alias[i].id == id) return i;
	}
	return -1;
}
