Ext.namespace('fcbm.model');

fcbm.model.List = function (url, list) {
	
	this.id = '';
	this.lang = '';
	this.maxSel = -1;
	
	this.categories = [];
	this.ready = false;
	
	
	/*
	 * Initialise la liste � partir d'un objet JSON repr�sentant le fichier XML
	 */
	this.XML2List = function (xml) {
		var json = eval("(" + xml2json (xml, '') + ")");
		
		//var rootObj = json.ServeurResponse.Data;
		var rootObj = json;
		
		this.id = rootObj.List.id;
		this.lang = rootObj.List.lang;
		this.maxSel = rootObj.List.maxSel;
		
		
		if (rootObj.List.Cat != null && rootObj.List.Cat.length != null) {
			for (var i = 0; i < rootObj.List.Cat.length; i++) {
				this.populateCat(rootObj.List.Cat[i]);
			}
		} else {
			this.populateCat(rootObj.List.Cat);
		}
		this.ready = true;
	};
	
	/*
	 * ajoute une catégorie à partir du xml 
	 */
	this.populateCat = function (C) {
		
		var cat = new fcbm.model.Categorie();
		cat.id = C.id;
		if (C.Desc != null) 
			cat.desc = C.Desc;
		cat.shortdesc = C.ShortDesc;
		
		var isArray = true;
		try {
			C.Item[0].id;
		} catch (error) {
			isArray = false;
		}
		var hasItems = true;
		if (!isArray) {
			try {
				C.Item.id;
			} catch (error) {
				var hasItems = false;
			}
		}
	
		if (isArray) {
			for (var j=0; j<C.Item.length; j++) {
				var I = C.Item[j];
				var item = new fcbm.model.Item ();
				item.id = I.id;
				item.desc = I._text;
				if (I.isUnique != null) item.isUnique = I.isUnique;
				if (I.idSynonyme != null) item.idSynonyme = I.idSynonyme;
				item.idCat = C.id;
				
				cat.items.push(item);
			}
		} else {
			if (hasItems) {
				var I = C.Item;
				var item = new fcbm.model.Item();
				item.id = I.id;
				item.desc = I._text;
				if (I.isUnique != null) 
					item.isUnique = I.isUnique;
				if (I.idSynonyme != null) 
					item.idSynonyme = I.idSynonyme;
				item.idCat = C.id;
				
				cat.items.push(item);
			}
		}
		
		this.categories.push (cat);		
	};
	
	/*
	 * Renvoi la liste sous forme Ext.data.Store
	 */
	this.toDataStore = function (forceCatNameTo) {
		
		var myData = new Array();
		for (var i=0; i<this.categories.length; i++) {
			var cat = this.categories[i];
			for (var j=0; j<cat.items.length; j++) {
				var item = cat.items[j];
				if (item.isUnique) {
					myData.push([item.id, item.desc, item.isUnique, item.idSynonyme, 999999, 'ZZZ']);
				}
				else {
					if (forceCatNameTo!=null)
						myData.push([item.id, item.desc, item.isUnique, item.idSynonyme, item.idCat, forceCatNameTo]);
					else
						myData.push([item.id, item.desc, item.isUnique, item.idSynonyme, item.idCat, cat.desc]);
				}
			}
		}
		
		var ds = new Ext.data.GroupingStore({
			proxy: new Ext.data.MemoryProxy(myData),
			reader: new Ext.data.ArrayReader(
				{}, 
				[
					{name: 'id'},
					{name: 'desc'},
					{name: 'isUnique'},
					{name: 'idSynonyme'},
					{name: 'idCat'},
					{name: 'descCat'}
				]
			),
			sortInfo:{field: 'desc', direction: "ASC"},
            groupField:'descCat'
		});
		ds.load();
		
		return ds;
	};
	
	/*
	 * 
	 */
	this.load = function (url, list) {
		Ext.Ajax.request({
			url: url,
			success: function (conn, response, options){
				this.XML2List(conn.responseXML);
			},
			scope: this,
			failure: function (response, options){
				Ext.MessageBox.show({
					title: 'Erreur',
					msg: "fcbm.model.List: Erreur au chargement d'une liste.",
					buttons: Ext.Msg.OK,
					icon: Ext.MessageBox.ERROR
				});
				;
			}
		});
		
	}

	this.getItemById = function (id) {
		
		for (var i=0; i<this.categories.length; i++) {
			for (var j=0; j<this.categories[i].items.length; j++) {
				if (parseInt(this.categories[i].items[j].id) == parseInt(id)) {
					var item = this.categories[i].items[j];
					item.descCat = this.categories[i].desc;
					return item;
				}
			}
		}
		
	};
	
	this.load(url);
}

fcbm.model.Categorie = function () {
	
	this.id = -1;
	this.desc = '';
	this.shortdesc = '';
	this.items = [];	
}

fcbm.model.Item = function () {
	this.id = -1;
	this.desc = '';
	this.isUnique = 0;
	this.idSynonyme = -1;
	this.idCat = -1;
}

