/**
 * LangChange : a jQuery Plug-in
 * Samuel Garneau <samgarneau@gmail.com>
 * http://samgarneau.com
 * 
 * Released under no license, just use it where you want and when you want.
 */

$LANGUAGE = {
	// defaults
	dictionary:'',
	file: 'assets/js/language/language.xml',
	lang: 'da',
	fadeSpeed: 150,
	isLoaded:false,
	
	initialize: function() {
		//$LOG.console('languagechanger');
		
		$.ajax({
			async:false,
			cache:true,
			type: "GET",
			url: this.file,
			dataType: "xml",
			success: function(xml) {
				$LANGUAGE.dictionary = xml;		
				//console.log($LANGUAGE.dictionary);
	   		}
		});
		this.isLoaded = true;
	},
	translatePage:function(language) {
		
		this.lang = language;
		
		if (this.isLoaded == false) {
			this.initialize();
		}
		
		var aTexts = new Array();
					
		$(this.dictionary).find('text').each(function() {
			var textId = $(this).attr("id");
            var text = $(this).find(language).text();
							
			aTexts[textId] = text;
		});
					
		$.each($("*"), function(i, item) {
			if($(item).attr("langtag") != null) {
				$(item).fadeOut(this.fadeSpeed).fadeIn(this.fadeSpeed).text(aTexts[$(item).attr("langtag")]);
			}
		});
	},
	getTranslation: function(language, tag) {
		this.lang = language;
		
		if (this.isLoaded == false) {
			this.initialize();
		}
		//console.log(language);
		//console.log(tag);
		//console.log(this.language_file);
		var t = '';
		$(this.dictionary).find('text').each(function() {
			if (tag == $(this).attr("id")) {
				t = $(this).find(language).text();
			}
		});
		
		return t;
	}
};
