/*----------------------------------\*
||           respource.js           ||
||==================================||
||          Resource loader         ||
||Efficiently load & store resources||
||         By DaVince, 2010         ||
\*----------------------------------*/
//Modified from project Moon Blade.
//Note: some filetypes left out.


/** Object makes loading different resources easy. */
function ResourceManager()
{
	this.items = [];
}


/** One-time initializer for the resource loading system. Loads fallback resources. */
ResourceManager.prototype.init = function()
{
  this.inited = true;
  this.filetypes = [
    ["png", "images"],
    ["rss", "spritesets"],
    ["ogg", "sounds"],
    ["mp3", "sounds"],
    ["wav", "sounds"],
    ["rfn", "fonts"]
    ];
  
  this.items["fallback.png"] = LoadImage("fallback.png");
  this.items["fallback.rss"] = LoadSpriteset("smallspike.rss");
  this.items["fallback.wav"] = LoadSound("fallback.wav");
  this.items["fallback.mp3"] = this.items["fallback.wav"];
  this.items["fallback.ogg"] = this.items["fallback.wav"];
  //this.items["fallback.rmp"] = new Map("fallback.rmp");
  //this.items["fallback.mng"] = LoadAnimation("fallback.mng");
  this.items["fallback.rfn"] = LoadFont("tinyfont.rfn");
  //this.items["fallback.rws"] = LoadWindowStyle("fallback.rws");
  for (i in this.items)
    i.fallback = true;
}


/** Load any kind of standard resource. */
ResourceManager.prototype.load = function(filename)
{
  if (!this.inited) this.init();
  
  var extdir = this.getPath(filename);
  if (extdir.length > 0) extdir += "/";  //Add trailing slash if there's a subdirectory.
  
  //Check if the file was already loaded and immediately return it if it was, unless it has a fallback replacement.
  if (this.items[extdir+filename] && !this.items[extdir+filename].fallback)
    return this.items[extdir+filename];
  
  var ext = this.getExtension(filename);
  var file = this.items["fallback."+ext];  //Begin by opening a fallback resource.
  var basedir = this.getFileTypeDir(ext);
  filename = this.getFileName(filename);
  
  //Check for existence of file.
  //If it exists:
  if (this.exists(filename, basedir+"/"+extdir))
  {
    //Try loading the file, adding the right path in front.
    if (ext == "png") file = LoadImage(extdir+filename);
    else if (ext == "ogg") file = LoadSound(extdir+filename);
    else if (ext == "rss") file = LoadSpriteset(extdir+filename);
    else if (ext == "wav") file = LoadSound(extdir+filename);
    else if (ext == "mp3") file = LoadSound(extdir+filename);
    else if (ext == "it")  file = LoadSound(extdir+filename);
    //else if (ext == "rmp") file = new Map(extdir+filename);
    else if (ext == "rfn") file = LoadFont(extdir+filename);
    else if (ext == "rws") file = LoadWindowStyle(extdir+filename);
    //Abort("Opened file " + basedir + "/" + extdir + filename + ".");
  }
  
  //Memoize the loaded file, return stuff.
  this.items[extdir+filename] = file;
  return file;
}


/** Get file extension. */
ResourceManager.prototype.getExtension = function(filename)
{
  return filename.substr(filename.length-3);
}


/** Retreive directory information from a string. */
ResourceManager.prototype.getPath = function(file)
{
  return file.substring(0, file.lastIndexOf("/"));
}

ResourceManager.prototype.getFileName = function(file)
{
	return file.substring(file.lastIndexOf("/")+1);
}


/** Get the path the file is in depending on its type (ex. images for .png) */
ResourceManager.prototype.getFileTypeDir = function(ext)
{
  for (var i = 0; i < this.filetypes.length; i++)
    if (ext == this.filetypes[i][0]) return this.filetypes[i][1];
  return "other";  //When other extension
}


/** Tests whether a resource file actually exists and returns a default resource if it doesn't. */
var file_list_cache = [];
 
ResourceManager.prototype.exists = function(p_filename, p_directory)
{
  if (file_list_cache[p_directory] == undefined)
    file_list_cache[p_directory] = GetFileList(p_directory);
  
  for (var f in file_list_cache[p_directory])
  {
    if (file_list_cache[p_directory][f] == p_filename) return true;
  }
  return false;  //Only runs if none of the items matched.
}




/** Colors are a type of resource too, and are stored here. */
var COLORS = [];
COLORS.WHITE = CreateColor(255,255,255);
COLORS.BLACK = CreateColor(0,0,0);
COLORS.RED = CreateColor(255,0,0);
COLORS.PINK = CreateColor(255,160,160);
COLORS.DARKRED = CreateColor(160,0,0);
COLORS.GREEN = CreateColor(0,255,0);
COLORS.LIGHTGREEN = CreateColor(160,255,160);
COLORS.DARKGREEN = CreateColor(0,160,0);
COLORS.BLUE = CreateColor(0,0,255);
COLORS.LIGHTBLUE = CreateColor(160,160,255);
COLORS.DARKBLUE = CreateColor(0,0,160);
COLORS.YELLOW = CreateColor(255,255,0);
COLORS.GOLD = CreateColor(52,133,133);
COLORS.GREY = CreateColor(180,180,180);
COLORS.GRAY = COLORS.GREY;
COLORS.DARKGREY = CreateColor(100,100,100);
COLORS.DARKGRAY = COLORS.DARKGREY;
COLORS.ORANGE = CreateColor(255, 128, 0);
COLORS.TRANSPARENT_BLACK = CreateColor(0, 0, 0, 180);



/** Make a color transparent. */
COLORS.transparent = function(c, amt)
{
	return CreateColor(c.red, c.green, c.blue, amt);
}
