/************************************************************************ 
  Copyright 2011-2012, IceJS Team. All rights reserved.
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:
  
      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above
        copyright notice, this list of conditions and the following
        disclaimer in the documentation and/or other materials provided
        with the distribution.
      * Neither the name of Update Freezer Team nor the names of its
        contributors may be used to endorse or promote products derived
        from this software without specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ***********************************************************************/

if (typeof(Util) == "undefined")
	Util = {};
	
Util.Locale = 
{
	Strings: {},
	defaultLocale: "en",
	userLocale: "",
	
	SetLocale: function(ulocale)
	{
		this.userLocale = ulocale;
	},
	
	GetLocale: function()
	{
		return  (this.userLocale != "") ? this.userLocale : _GetCurrentLanguage();
	},
	
	GetInfoFileName: function(subject)
	{
		var fileName = Util.IO.GetCurrentDirectory() + "\\Locale\\" + subject + "." + this.GetLocale() + ".html";
		if (Util.IO.CheckFileExists(fileName))
			return fileName;
		return Util.IO.GetCurrentDirectory() + "\\Locale\\" + subject + "." + this.defaultLocale + ".html";
	},
	
	Load: function()
	{
		this.LoadLocale(this.defaultLocale);	
		
		var locale = this.GetLocale();
		
		if (locale != this.defaultLocale)
		{
			this.LoadLocale(locale);
		}
		
		return this.Strings;
	},
	
	LoadLocale: function(locale)
	{
		var files = Util.IO.GetFilesInFolder(Util.IO.GetCurrentDirectory() + "/Locale");
		
		for(var i = 0; i < files.length; i++)
		{
			var fileName = files[i];
			var pos = fileName.indexOf("." + locale + ".json");
			if (pos != -1)
			{
				var prefix = fileName.substring(0, pos);
				this.ParseFile("Locale/" + fileName, prefix);
			}
		}			
		return true;
	},
	
	ParseFile: function(fileName, prefix)
	{
		var contents = Util.IO.ReadFile(fileName);
		if (contents)
		{
			try
			{
				// var localStrings = JSON.parse(contents);
				eval("var localStrings = " + contents);
				if (localStrings)
				{
					for (var attr in localStrings)
					{
						this.Strings[prefix + "." + attr] = localStrings[attr];
					}
				}
			}
			catch(e)
			{
				Error("Locale file '" + fileName + "'syntax is incorrect: " + e);
			}
		}
		return true;
	},
	
	GetString: function(str)
	{
		if (this.Strings[str] != undefined)
		{
			return this.Strings[str];
		}
		
		return "No such string loaded: " + str;
	},
	
	OutputDependencies: function(prefix)
	{
		if(typeof(_dependencies)!="undefined")
		{
			var localePath = Util.IO.GetCurrentDirectory() + "/Locale";
			var files = Util.IO.GetFilesInFolder(localePath);
			for(var i = 0; i < files.length; i++)
			{
				var fileName = files[i];
				if (fileName.indexOf(prefix) == 0)
					_dependencies[fileName] = {name: fileName, path: localePath + "/" + fileName};
			}
		}
	}
}

function S(str)
{
	return Util.Locale.GetString(str);
}

function SF(str, args)
{
	return S(str).format(args);
}