Ext.namespace ('fcbm.model');

fcbm.model.ImageWall = function () {

	return {
		graphicalFormuleData: new Ext.util.MixedCollection(), 
		imgWall: {
			hasWall2: false,
			hasSol2: false,
			hasCrobar: false,
			wall1: new Array(),
			wall2: new Array(),
			sol1: new Array(),
			sol2: new Array(),
			crobar: new Array()
		},
		
		/*
		 * Parse la partie GraphicalFormuleData du Document.xml
		 */
		parseDocumentJSON: function(jsonObject){
			for (var i = 0; i < jsonObject.length; i++) {
				var cur = jsonObject[i];
				
				var isArray = true;
				try {
					cur.Item[0].id;
				} catch (error) {
					isArray = false;
				}
				var hasItems = true;
				if (!isArray) {
					try {
						cur.Item.id;
					} catch (error) {
						var hasItems = false;
					}
				}
				if (hasItems) {
					var items = new Array();
					
					if (isArray) {
						for (var j=0; j<cur.Item.length; j++) {
							items.push(new fcbm.model.Annotation(cur.Item[j]));
						}
					} else {
						items.push(new fcbm.model.Annotation(cur.Item));
					}
					this.graphicalFormuleData.add(cur.Number, items);
				}				
			}
		},
		
		/*
		 * 
		 */
		setBaseUrl: function (baseUrl) {
			this.baseUrl = baseUrl;
		},
		
		/*
		 * 
		 */
		setCodeCountry: function (codeCountry) {
			this.codeCountry = codeCountry;
		},
		
		/*
		 * 
		 */
		parseImagesJSON: function (jsonObject) {
			var J = jsonObject;
			this.imgWall.wall1 = this.setImagesInArray(
				this.findJsonObjectById(J.ImgWalls.ImgWall, 'Init1').Image,
				"/IMG/"
			);
			this.imgWall.wall2 = this.setImagesInArray(
				this.findJsonObjectById(J.ImgWalls.ImgWall, 'Init2').Image,
				"/IMG/"
			);
			this.imgWall.sol1 = this.setImagesInArray(
				this.findJsonObjectById(J.ImgWalls.ImgWall, 'Sol1').Image,
				"/I18N/" + this.codeCountry + "/IMG/"
			);
			this.imgWall.sol2 = this.setImagesInArray(
				this.findJsonObjectById(J.ImgWalls.ImgWall, 'Sol2').Image,
				"/I18N/" + this.codeCountry + "/IMG/"
			);
			this.imgWall.crobar = this.setImagesInArray(
				this.findJsonObjectById(J.ImgWalls.ImgWall, 'Crobar').Image,
				"/I18N/" + this.codeCountry + "/IMG/"
			);
			
			if (this.imgWall.wall2.length > 0)
				this.imgWall.hasWall2 = true;
			if (this.imgWall.sol2.length > 0)
				this.imgWall.hasSol2 = true;
			if (this.imgWall.crobar.length > 0)
				this.imgWall.hasCrobar = true;
			
		},
		
		setImagesInArray: function (jsonNode, path) {
			var array = new Array();
			for (var i=0; i<jsonNode.length; i++) {
				if (jsonNode[i].name != "NULL") {
					array.push({
						id: jsonNode[i].name,
						url: this.baseUrl + path + jsonNode[i].name + ".jpg"
					});
				}
			}
			return array;
		},
		
		/*
		 * Cherche dans un noeud json de type Array quels est le premier dont l'id correspond 
		 * à l'@id passé en paramètre.
		 * Renvoi le premier noeud correspondant 
		 */
		findJsonObjectById: function (jsonNode, id) {
			for (var i=0; i<jsonNode.length; i++) {
				var cur = jsonNode[i];
				if (cur.id == id)
					return cur;			
			}
		}
	}
};


