// PARAMETRAGE   
// ***************************************************************** 

var HM_MENU_LAYOUT= [
	128,			// menu_width
	0,				// left_position
	0,				// top_position
	"#FFFFFF",		// font_color
	"#086BC1",		// mouseover_font_color
	"#FFFFFF",		// background_color
	"#E0F0FE",		// mouseover_background_color
	"#FFFFFF",		// border_color
	"#ffffff",		// separator_color
	1,				// top_is_permanent
	0,				// top_is_horizontal
	0,				// tree_is_horizontal
	0,				// position_under
	1,				// top_more_images_visible
	1,				// tree_more_images_visible
	"null",			// evaluate_upon_tree_show
	"null",			// evaluate_upon_tree_hide
	,				// right_to_left
	,				// display_on_click
	0,			// top_is_variable_width
	0,				// tree_is_variable_width
	"#FFFFFF",		// tree_border_color
];


var TAB_HM_COUNTER=0;


// object HM_MENU
// ***************************************************************** 

function HM_Menu() {
	TAB_HM_COUNTER++;
	
	//data 
	this.id = TAB_HM_COUNTER;
	this.elements = new Array();
	this.elements_order = new Array();
	this.layout= new Array(HM_MENU_LAYOUT.size);
	for (var i in HM_MENU_LAYOUT) {
		this.layout[i] = HM_MENU_LAYOUT[i];
	}
	
	

	//methods 
	this.add_item = add_item;
	this.init = init;

	function add_item(ID_RUB,NOM,LIEN,ID_PARENT) {
		var index = this.elements_order.length+0;
		this.elements[ID_RUB] = new HM_Sousmenu(NOM, ID_PARENT, LIEN); 
		this.elements_order[index] = ID_RUB; 
	}

	function init() {
		var i;
		var root = new HM_Sousmenu();
		root.order = this.id;
		root.layout= this.layout;
		var result;
		var ID_RUB;

		// on supprime les elements qui n'ont pas de parent
		var boucle=true;
		while (boucle) {
			boucle=false;
			for(i in this.elements) { 
				if (this.elements[i].id_parent && !this.elements[this.elements[i].id_parent]) {
						delete this.elements[i];
						boucle=true;
				}
			}
		}

		//initialisation : parent, order, nb_elements
		for(i in this.elements) {
			if (this.elements[i].id_parent) {
				this.elements[i].parent = this.elements[this.elements[i].id_parent];
			}
			else { this.elements[i].parent = root; }
			this.elements[i].parent.block_nb_elements++;
			this.elements[i].order = this.elements[i].parent.block_nb_elements;
		}

		//initialisation : block_text
		for(i=0; i<this.elements_order.length; i++) {
			ID_RUB = this.elements_order[i];
			if (this.elements[ID_RUB]) {
				this.elements[ID_RUB].add_line();
			}
		}

		//print menus
		for(i=0; i<this.elements_order.length; i++) {
			ID_RUB = this.elements_order[i];
			if (this.elements[ID_RUB] && this.elements[ID_RUB].block_nb_elements > 0 ) {
				this.elements[ID_RUB].init();
			}
		}
		
		//add menu root
		root.init();
	}
}


// object HM_Sousmenu
// ***************************************************************** 

function HM_Sousmenu(NOM, ID_PARENT, LIEN) {
	this.name = NOM;
	this.id_parent = ID_PARENT;
	this.link = LIEN;
	this.block_text = "";
	this.block_nb_elements = 0;
	this.order = 0;
	this.parent = null;
	this.layout = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];

	this.get_name_block = get_name_block;
	this.add_line = add_line;
	this.init = init;

	function get_name_block() {
		var NOM = '';
		if (this.parent) {
			NOM = this.parent.get_name_block()+'_';
		}	
		NOM += this.order;
		return NOM;
	}

	function add_line() {
		var NB = 0;
		var line = "";
		if (this.block_nb_elements > 0) { NB = 1; }
		line += '["';
		line += this.name;
		line += '","';
		line += this.link;		
		line += '",1,0,';
		line += NB;
		line += '],\n';
		this.parent.block_text += line;	
	}

	function init() {
		var block = 'HM_Array';
		block += this.get_name_block();
		block += '= [\n';
		block += 'this.layout'+',\n';
		block += this.block_text;	
	    block = block.substring(0, (block.length-2));
		block +='\n]\n';
		eval (block);
	}

}

