/*

showAll( [gr=true], [sgr=true]) - wy�wietla wszystkie elementy menu. Parametr false blokuje wy�wietlenie grup, lub podgrup
hideAll( [gr=true], [sgr=true]) - ukrywa wszystkie j.w.

show( gr, sgr ) pokazuje jeden element podgrupy
hide( gr, sgr ) ukrywa jeden element podgrupy

showGroup( gr ) pokazuje ca�a grup� (��cznie z podgrup�)
hideGroup( gr ) ukrywa ca�� grup� j.w.

select( gr, [sgr]) wybiera jeden element (grup� lub podgrup�)
deselect( gr, [sgr]) dezaktywuje jeden element (grup� lub podgrup�)
deselectAll( [gr=true], [sgr=true]) - dezaktywuje wszystkie elementy menu z wyj�tkiem grup, lub podgrup oznaczonych paramertrem false

*/ 


function menu( el, struct, clickHandler ){
	this.name = 'Menu v0.1';
	this.structure = {};
	this.mainElement = null;
	this.type='ONELIST';
	this.onClick = clickHandler;

	this.loadStructure = function(s){
		this.structure = s;
	}
	this.click = function click(){
		var el=$(this);
		var data={ element : this, group : el.data('gr'), menuStruct : el.data('menuStruct') };
		if (el.data('subgr')) data.subgroup = el.data('subgr');
		var z = el.data('_click');
		if (z) z(data);
	}

	this.drawOneList = function(){
		this.mainElement.append('<ul></ul>');
		var $mainList=this.mainElement.children();
		for ( var i in this.structure.fields ){
			var currStruct = this.structure.fields[i];
			if (!currStruct.hidden)	{
				$mainList.append('<li class="group">'+currStruct.name+'</li>');
				var current = $mainList.children().slice(-1);
				currStruct.element = current;
				current.data('gr',i).click( this.click ).data('_click',this.onClick);
				current.data('menuStruct',currStruct.fields[0]);
				if ( this.structure.fields[i].fields ){
					for ( var j in currStruct.fields ){
						if (!currStruct.fields[j].hidden)	{
							$mainList.append('<li>'+currStruct.fields[j].name+'</li>');
							var current = $mainList.children().slice(-1);
							currStruct.fields[j].element = current;
							current.data('gr',i).data('subgr',j).data('menuStruct',currStruct.fields[j]).click( this.click ).data('_click',this.onClick);
						}
					}
				}
			}
		}
	}
	this.drawTwoLines = function(){
		this.mainElement.append('<ul class="groups"></ul><ul class="subgroups"></ul>');
		var $gropusList=this.mainElement.children().slice(0,1);
		var $subgroupsList=this.mainElement.children().slice(-1);
		for ( var i in this.structure.fields ){
			var currStruct = this.structure.fields[i];
			if (!currStruct.hidden)	{
				$gropusList.append('<li class="group">'+currStruct.name+'</li>');
				var current = $gropusList.children().slice(-1);
				currStruct.element = current;
				current.data('gr',i).click( this.click ).data('_click',this.onClick);
				current.data('menuStruct',currStruct.fields[0]);				
				if ( this.structure.fields[i].fields ){
					for ( var j in currStruct.fields ){
						if (!currStruct.fields[j].hidden){
							$subgroupsList.append('<li>'+currStruct.fields[j].name+'</li>');
							var current = $subgroupsList.children().slice(-1);
							currStruct.fields[j].element = current;
							current.data('gr',i).data('subgr',j).data('menuStruct',currStruct.fields[j]).click( this.click ).data('_click',this.onClick);
						}
					}
				}
			}
		}
	}
	this.draw = function(){
		switch(this.type){
			case 'ONELIST' : return this.drawOneList(); break;
			case 'TWOLINES' : return this.drawTwoLines(); break;
		}
	}
	
	this.hideGroup = function( group ){
		this.iterateGroup(group,function(x){x.hide()});
	}
	this.showGroup = function( group ){
		this.iterateGroup(group,function(x){x.show()});
	}
	this.show = function(group, subgroup){
		this.getElement(group, subgroup).show();
	}
	this.hide = function(group, subgroup){
		this.getElement(group, subgroup).hide();
	}
	this.deselect = function(group, subgroup){
		this.getElement(group, subgroup).removeClass( ((typeof subgroup=='undefined')?'gr':'sgr')+'selected');
	}
	this.select = function(group, subgroup){
		this.getElement(group, subgroup).addClass(((typeof subgroup=='undefined')?'gr':'sgr')+'selected');
	}
	this.deselectAll = 	function( gr, sgr ){
		this.iterateAll(function(x){x.removeClass('grselected sgrselected')}, gr, sgr);
	}
	this.hideAll = function( gr, sgr ){
		this.iterateAll(function(x){x.hide()}, gr, sgr);
	}
	this.showAll = function( gr, sgr ){
		this.iterateAll(function(x){x.show()}, gr, sgr);
	}
	this.appendHandler = function( func ){
		this.iterateAll(function(x){x.data('_click',func)});
	}
	this.getElement = function(group, subgroup){
		if (typeof subgroup!='undefined'){
			return $(this.structure.fields[group].fields[subgroup].element);
		} else {
			return $(this.structure.fields[group].element);
		}
	}
	this.iterateGroup = function( group, callback ){
		callback( $(this.structure.fields[group].element) );
		for (var n in this.structure.fields[group].fields ) {
			if ( this.structure.fields[group].fields[n].hidden!=true) callback( $(this.structure.fields[group].fields[n].element) );
		}
	}
	this.iterateAll = function(callback, gr, sgr ){
		for ( var i in this.structure.fields ){
			var currStruct = this.structure.fields[i];
			if (currStruct.hidden!=true) {
				if (typeof gr=='undefined' || gr==true) callback( $(currStruct.element), i );
				if (this.structure.fields[i].fields ){
					for ( var j in currStruct.fields ){
						if ((typeof sgr=='undefined' || sgr==true) && currStruct.fields[j].hidden!=true) callback( $(currStruct.fields[j].element), i, j );
					}
				}
			}
		}
	}
	this.SelectAndShowOne = function( gr, sgr ){
		this.hideAll(true,true);
		this.showAll(true,false);
		this.showGroup(gr);
		if (typeof sgr=='undefined') sgr = 0;
		this.deselectAll();
		this.select(gr,sgr);
		this.select(gr);
	}

	this.mainElement = el;
	if (struct) this.loadStructure(struct);
}
