application.mapping = {
	mappingEntries: null,
	
	init: function() {
		var result = $.ajax({
			url:application.baseUrl + '/getMapping', 
			async: false,
			type: "GET",
			error: function (request, status, error) {
				application.display.dialog.errorDialog(request.status);
		    },
		    success: function (data, text) {
		    	application.mapping.mappingEntries = (eval('('+data+')'));
		    }
			}
			).responseText;
	},
	
	getMapping: function(keyWord) {
		var result = new Array();
		for (var i=0; i<application.mapping.mappingEntries.length; i++) {
			if (application.mapping.mappingEntries.keyword == keyWord)
				result.push(application.mapping.mappingEntries.theme);
		}
		return result;
	},
	
	addEmptyEntry: function() {
		//application.display.hideDialogs()
		application.mapping.mappingEntries.push({keyword:'',theme:''});
	},
	
	deleteEntry: function(rank) {
		//application.display.hideDialogs()
		application.mapping.mappingEntries.splice(rank,1);
		application.display.refreshMapping();
	},
	
	getMappingHelp: function(actualValue) {
		var path = actualValue.split('/');
		var result = [];
		
		if (path.length == 1) {
			for (var theme in application.themeTree) {
				var label = application.getThemeLabel(theme)
				if (label.toLowerCase().indexOf(actualValue.toLowerCase()) == 0){
					result.push(label + "/");
				}
			}
		} else if (path.length == 2) {
			var themeLabel = path[0];
			var ssThemeSearched = path[1];
			var theme = application.getThemeKey(themeLabel);
			for (var ssTheme in application.themeTree[theme]) {
				var ssThemeLabel = application.getThemeLabel(theme+"."+ssTheme);
				if (ssThemeSearched.length == 0 || ssThemeLabel.toLowerCase().indexOf(ssThemeSearched.toLowerCase()) == 0)
					result.push(themeLabel + "/" + ssThemeLabel + "/" );
			}
		} else if (path.length == 3) {
			var themeLabel = path[0];
			var ssThemeLabel = path[1];
			var sssThemeSearched = path[2];
			var theme = application.getThemeKey(themeLabel);
			var ssTheme = application.getThemeKey(ssThemeLabel).replace(theme+".","");
			
			for (var i=0; i<application.themeTree[theme][ssTheme].length; i++) {
				var sssTheme = application.themeTree[theme][ssTheme][i];
				var sssThemeLabel = application.getThemeLabel(theme+"."+ssTheme+"."+sssTheme);
				if (sssThemeSearched.length == 0 || sssThemeLabel.toLowerCase().indexOf(sssThemeSearched.toLowerCase()) == 0)
					result.push(themeLabel + "/" + ssThemeLabel + "/" + sssThemeLabel);
			}
		}
			
		return result;
	},
	
	setMappingValue: function(value, rank) {
		$("#mapping_th_" + rank).val(value).focus();
		$("#mappingHelp").css("display","none");
		application.mapping.mappingEntries[rank].theme = value;
		path = value.split('/');
		if (path.length <3){
			application.display.openMappingHelp(rank);
		}
		if (path.length==3 && path[2]==""){
			application.display.openMappingHelp(rank);
		}
	},
	
	setMappingKey: function(rank) {
		application.mapping.mappingEntries[rank].keyword = $("#mapping_kw_" + rank).val();
	},
	
	serialize: function() {
		var result = "";
		
		for (var i=0; i<application.mapping.mappingEntries.length; i++) {
			result += application.mapping.mappingEntries[i].keyword + "|"
				+ application.mapping.mappingEntries[i].theme;
			
			if (i < application.mapping.mappingEntries.length-1)
				result += "||";
		}
		
		return result;
	}
};
