/**
* Don't change or remove the head comments!
* DHTML hierarchical, object oriented menu: LUCID MENU 0.9
* Copyright (C) 2007  Vladimir Bodurov
* 								
*   Version  0.9 BETA  Feb 25 2007  
* 								
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* 																  
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 																		 
* Copy of GNU Lesser General Public License at: http://www.gnu.org/copyleft/lesser.txt
* 													 
* Source code home page: http://www.bodurov.com/lucidmenu
*/

// -------- ObjectAnalyser object STARTS HERE
ObjectAnalyser = Class.create();

Array.prototype.toString = function(){
    var str = "";
    for(var i = 0; i < this.length; i++) str += this[i].toString();
    return str;
}
ObjectAnalyser.outerHTML = function(element){
    if(element == null) return "null";
    var testDiv = document.createElement("div");
    testDiv.appendChild(element)
    return testDiv.innerHTML;
}
ObjectAnalyser.assign = function(object, properties, onFound){
    var numberFound = 0;
    if(!onFound) onFound = function(){ return 1; }; 
    for(var each in properties){
        if(onFound(object, properties, each)){
            if(ObjectAnalyser.isPublicAndExists(each, object[each])){
                numberFound++;
                object[each] = properties[each];
            }
        }
    }
    return numberFound;
}
ObjectAnalyser.add = function(parentObject, object, index){
    if(ObjectAnalyser.callMethod(object,"isType",false,"LucidMenuView")){
        parentObject.view = object; return true;
    }else if(ObjectAnalyser.callMethod(object,"isType",false,"LucidMenuItem")){
        parentObject.items.push(object); return true;
    }
    return false;
}
ObjectAnalyser.showText = function(txt){
    var textarea = $("__ObjectAnalyser_show_textarea");
    var btn = $("__ObjectAnalyser_show_button");
    if(textarea == null && btn == null){
        textarea = document.createElement("textarea");
        btn = document.createElement("input");
        
        textarea.id = "__ObjectAnalyser_show_textarea";
        textarea.style.position = btn.style.position = "absolute";
        textarea.style.left = "0";
        textarea.style.top = "0";
        textarea.style.width = "380px";
        textarea.style.height = "200px";
        document.getElementsByTagName("body")[0].appendChild(textarea);
        
        btn.id = "__ObjectAnalyser_show_button";
        btn.value = "Close";
        btn.type = "Button";
        btn.style.left = "320px";
        btn.style.top = "205px";
        btn.style.width = "50px";
        Event.observe(btn, 'click', ObjectAnalyser.hideText);
        document.getElementsByTagName("body")[0].appendChild(btn);
    }
    btn.style.display = "block";
    textarea.style.display = "block";
    textarea.innerHTML = "";
    textarea.appendChild(document.createTextNode(txt));
}
ObjectAnalyser.hideText = function(){
    var textarea = $("__ObjectAnalyser_show_textarea");
    var btn = $("__ObjectAnalyser_show_button");
    if(textarea != null && btn != null) textarea.style.display = btn.style.display = "none";
}
ObjectAnalyser.callMethod = function(object,methodName,returnIfNotFound){
    if(object == null || ObjectAnalyser.isUndefined(object) || !ObjectAnalyser.isFunction(object[methodName])){
        return returnIfNotFound;
    }
    var numberArgs = arguments.length;
    var args = new Array();
    for(var i = 3; i < numberArgs; i++){
        args.push(arguments[i]);
    }
    return object[methodName].apply(object, args);
}
ObjectAnalyser.objectToString = function(object, showPrivate){
    var result = "";
    for(var each in object){
        if(object[each] == null) continue;
        if(showPrivate || ObjectAnalyser.isPublic(each)){ 
            if(!ObjectAnalyser.isFunction(object[each]) || ObjectAnalyser.isMenuObject(object[each])){
                result += "<"+ each + ">" + object[each] + "</"+ each + ">";
            }
        }
    }
    return result;
}
ObjectAnalyser.getInfo = function(element){
    var pos = Position.cumulativeOffset(element);
    var dim = Element.getDimensions(element);
    return {x:pos[0],y:pos[1],width:dim.width,height:dim.height}
}

ObjectAnalyser.isUndefined = function(object){
    return (object === undefined);
}
ObjectAnalyser.parseInt = function(num){
    return (isNaN(num = parseInt(num)))?null:num;
}
ObjectAnalyser.coalesce = function(object){
    var found = null;
    var all = arguments.length;
    $A(arguments).each(function(current, index){
        if(found !== null) return;
        if((index + 1 === all) || (!ObjectAnalyser.isUndefined(current) && current !== null)){ found = current; }
    });
    return found;
}
ObjectAnalyser.isFunction = function(object){
    return (typeof object === "function");
}
ObjectAnalyser.isMenuObject = function(object){
    return (ObjectAnalyser.callMethod(object,"isMenuObject",false));
}
ObjectAnalyser.isPublic = function(property){
    return property.charAt(0) != "_";
}
ObjectAnalyser.isPublicAndExists = function(propertyName, property){
    return (!ObjectAnalyser.isUndefined(property) && ObjectAnalyser.isPublic(propertyName));
}
ObjectAnalyser.extend = function(destination, source) {
  for (var property in source) {
    if(ObjectAnalyser.isUndefined(destination[property])) destination[property] = source[property];
  }
  return destination;
}
// -------- ObjectAnalyser object ENDS HERE

// -------- BitMapManager object STARTS HERE
BitMapManager = Class.create();
BitMapManager.mapToString = function(map, flagsContainer){
    var arr = [];
    for(var each in flagsContainer){
        if(typeof flagsContainer[each] === "number"){
            if((map & flagsContainer[each]) > 0){
                arr.push(each);
            }
        }
    }
    return arr.join(", ");
}
BitMapManager.numberBits = function(map) {
       var num = 0;
       for(var i = 0; i < 32; i++) if((map & (1<<i)) > 0) num++;
       return num;
}
BitMapManager.hasConflict = function(map, group) {
       return (BitMapManager.numberBits(map) - BitMapManager.numberBits(map ^ group)) > 1;
}
BitMapManager.prototype = {
    _map: 0,
    groups: null,
    flagsContainer: null,
    suppressErrorMessage: false,
    initialize: function(map, flagsContainer, groups, suppressErrorMessage){
        if(!map) map = 0;
        this.flagsContainer = flagsContainer;
        this.groups = groups;
        this.suppressErrorMessage = suppressErrorMessage;
        this.setMap(map);
	},
	setMap: function(map){
	    xmap = this._prepare(map);
	    if(this._check(map)) this._map = map;
	},
	getMap: function(){
	    return this._map;
	},
	contains: function(flag){
	    return ((this._map & flag) > 0);
	},
	add: function(flag){
	    flag = this._prepare(flag);
	    if(this._check(flag)){
	        this._clearFroup(flag);
	        this._map |= flag;
	    }
	},
	remove: function(flag){
	    flag = this._prepare(flag);
	    if(!isNaN(flag)) this._map &= ~flag;
	},
	toString: function(){
	    return BitMapManager.mapToString(this.getMap(),this.flagsContainer);
	},
	_clearFroup: function(flag){
	    var manager = this;
	    this.groups.each(function(group){
	        if((flag & group) > 0) manager.remove(group);
	    });
	},
	_prepare: function(flag){
	    var parsedFlag = parseInt(flag);
	    if(!isNaN(parsedFlag)) return parsedFlag;
	    else return parseInt((flag+"").split(" ").join(""))
	},
	_check: function(flag){
	    if(isNaN(flag)){ this._alert("Bit Map flag passed is '"+flag+"' - it must be integer number"); return false; }
	    var manager = this;
	    var returnValue = true;
	    this.groups.each(function(group){
	        if(BitMapManager.hasConflict(flag,group)){
	            manager._alert("Flags in the group "+BitMapManager.mapToString(group,manager.flagsContainer)+" are mutually exclusive and can not be set at the same time!"); 
	            returnValue = false;
	            return;
	        }
	    });
	    return returnValue;
	},
	_alert: function(txt){
	    if(!this.suppressErrorMessage) alert("BitMapManager error: "+txt);
	}
}
// -------- BitMapManager object ENDS HERE

// -------- LucidMenu object STARTS HERE

LucidMenuFlag = new Object();
LucidMenuFlag.HORIZONTAL = 1<<0;
LucidMenuFlag.VERTICAL = 1<<1;
LucidMenuFlag.MOUSE_CLICK_OUT = 1<<2;
LucidMenuFlag.MOUSE_ROLL_OUT = 1<<3;
LucidMenuFlag.LEFT_TO_RIGHT = 1<<4;
LucidMenuFlag.RIGHT_TO_LEFT = 1<<5;


LucidMenu = Class.create();
LucidMenu.BODY = null;
LucidMenu.DEFAULT_HIDE_DELAY = 400;
LucidMenu.CSS_CLASS = "LucidMenuBlock";
LucidMenu.CSS_CLASS_VERT_BASE_LEVEL = "VerticalLucidMenu_BaseLevel";
LucidMenu.CSS_CLASS_HORZ_BASE_LEVEL = "HorizontalLucidMenu_BaseLevel";

LucidMenu.SUPPRESS_ERROR_WARNING = false;
LucidMenu.GROUPS_FLAGS = [
                            LucidMenuFlag.HORIZONTAL | LucidMenuFlag.VERTICAL,
                            LucidMenuFlag.MOUSE_CLICK_OUT | LucidMenuFlag.MOUSE_ROLL_OUT,
                            LucidMenuFlag.LEFT_TO_RIGHT | LucidMenuFlag.RIGHT_TO_LEFT
                         ];
LucidMenu.DEFAULT_FLAGS = LucidMenuFlag.VERTICAL | LucidMenuFlag.MOUSE_ROLL_OUT | LucidMenuFlag.LEFT_TO_RIGHT;
LucidMenu.VIEW_PREOPERTIES = ["flags","icon","css","arrow"];
LucidMenu._instances = [];
LucidMenu.baseLevelCss = function(flag){
    return ((flag & LucidMenuFlag.HORIZONTAL) > 0)? LucidMenu.CSS_CLASS_HORZ_BASE_LEVEL: LucidMenu.CSS_CLASS_VERT_BASE_LEVEL;
}
LucidMenu.add = function(id, instance){
	if(LucidMenu.get(id) != null) alert("LucidMenu with ID '"+id+"' has already been defined"); 
	else LucidMenu._instances.push(instance);
};
LucidMenu.get = function(id){
	if(LucidMenu._instances.length == 1) return LucidMenu._instances[0];
	return $A(LucidMenu._instances).detect( function(elem){ return elem.id == id; } );
}
LucidMenu.viewFound = function(object, properties, current){
    if(current === "flags"){
        object.flags.add(properties[current]);
        return false;       
    }else{
        return true;
    }
}
LucidMenu.baseViewFound = function(object, properties, current){
	LucidMenu.VIEW_PREOPERTIES.each(function(current){
	    if(current.indexOf("BaseLevel") < 0) return;
		if(current === "flagsBaseLevel") object.flags.add(properties[current]);
		else if(!ObjectAnalyser.isUndefined(properties[current])) object[current.split("BaseLevel").join("")] = properties[current];
	});
    return false;
}
LucidMenu.propertyFound = function(object, properties, current){
    if(current === "items"){
        var num = 0;
        var collection = properties[current];
        for(var i = 0; i < collection.length; i++){
			var currentProperties = collection[i];
			var item = object.getMenu().newItem();
			if(ObjectAnalyser.assign(item, currentProperties, LucidMenu.propertyFound) > 0){           
				object.items[num] = item;
				var view = object.getMenu().newView();
				if(ObjectAnalyser.assign(view, currentProperties, LucidMenu.viewFound) > 0){
					object.items[num].view = view;
				}
				num++;
			}
		}
        return false;       
    }else{
        return true;
    }
}
LucidMenu.prototype = {
    _id: "",
    _blockUI: null,
    _ajaxRequest: null,
    _path: null,
    _view: null,
    _viewBaseLevel: null,
    _generator: null,
    _root: null,
    _activeItem: null,
    _mouseX: 0,
    _mouseY: 0,
    _isTrackingMousePosition: false,
    _delay: LucidMenu.DEFAULT_HIDE_DELAY,
    items: null,
    level: 0,
    initialize: function(id, generator){
        LucidMenu.BODY = document.getElementsByTagName("body")[0];
        this._root = document.getElementById(id);
        if(this._root == null) this._alert("No DIV element with ID='"+id+"' was found!");
        this._id = id
        this.items = [];
        this._generator = ObjectAnalyser.coalesce(generator,LucidMenuGenerator);;
        Event.observe(LucidMenu.BODY, 'mousemove', this.onMouseMove.bindAsEventListener(this));
	},
	getId: function(){
	    return this._id;
	},
	getSystemId: function(){
	    return this.getId();
	},
	getBlockUI: function(){
	    return this._blockUI;
	},
	setBlockUI: function(ui){
	    this._blockUI = ui;
	},
	getRoot: function(){
	    return this._root;
	},
	load: function(data){
	    var type = typeof data;
        if(data != null && type === "object"){
            this.parseData(data);
        }else if(type === "string"){
            this.loadData(data);
        }else{
            this._alert("Please supply an argument for menu.load(data) menu method (the menu data or path to the file with that data)");
        }
        if(!this._ajaxRequest){
            this._setParents();
        }
	},
	getLevel: function(){
	    return 0;
	},
	getView: function(callingLevel){
	    return (ObjectAnalyser.coalesce(callingLevel,0) == 0)?this._viewBaseLevel:this._view;
	},
	isMenuObject: function(){
	    return true;
	},
	parseData: function(data){
	    ObjectAnalyser.assign(this._view = this.newView(LucidMenu.DEFAULT_FLAGS,LucidMenu.CSS_CLASS), data, LucidMenu.viewFound);
	    ObjectAnalyser.assign(this._viewBaseLevel = this.newView(LucidMenu.DEFAULT_FLAGS,LucidMenu.baseLevelCss(LucidMenu.DEFAULT_FLAGS)), data, LucidMenu.baseViewFound);
	    this._view._systemId = this.getId()+"_view";
	    this._viewBaseLevel._systemId = this.getId()+"_viewBaseLevel";
	    ObjectAnalyser.assign(this, data, LucidMenu.propertyFound);
	},
	loadData: function(path){
	    path += (path != null && path.indexOf('?') < 0)?'?':'&';
	    this._ajaxRequest = new Ajax.Request(this._path = path + 'timestamp='+(new Date()).getTime(),
								{method: 'get', onSuccess: this._ajaxCallColmplete.bindAsEventListener(this), onFailure:this._ajaxCallError.bindAsEventListener(this)});
	},
	newItem: function(text,url,target,checkGroup,checked,value){
	    return new LucidMenuItem(this,text,url,target,checkGroup,checked,value);
	},
	newView: function(flags,cssClass,iconUrl,arrow){
	    return new LucidMenuView(this,flags,cssClass,iconUrl,arrow);
	},
	add: function(value, index){
	    ObjectAnalyser.add(this, value, index);
	},
	toString: function(){
	    return "<LucidMenu ID=\""+this.getId()+"\">"+ObjectAnalyser.objectToString(this, false)
	                    +"<view>"+this._view+"</view><viewBaseLevel>"+this._viewBaseLevel+"</viewBaseLevel>"+
	           "</LucidMenu>";
	},
	isType: function(type){
	    return (type === "LucidMenu");
	},
	getMenu: function(){
	    return this;
	},
	hideGeneration: function(){},
	onBlockMouseOut: function(){},
	isWithinGeneration: function(){ return false; },
	resetMouseCheck: function(){
	    this._enableMouseCheck = true;
	},
	onMouseMove: function(evt){
	    this._mouseX = Event.pointerX(evt);
	    this._mouseY = Event.pointerY(evt);
	},
	startTrackingMousePosition: function(item){
	    if(this._isTrackingMousePosition) return;
	    this._isTrackingMousePosition = true;
	    setTimeout(this.checkMousePosition.bind(this), this._delay);
	},
	checkMousePosition: function(){
	    if(this._activeItem != null && this._activeItem.getLevel() > 1){
	        this._activeItem.checkGenerationState(this._mouseX, this._mouseY);
	    }
	    this._isTrackingMousePosition = false;
	},
	_getActiveItem: function(){
	    return this._activeItem;
	},
	_setActiveItem: function(item){
	    if(this._activeItem != null) this._activeItem.hideGeneration(item);
	    this._activeItem = item;
	},
	_setParents: function(){
	    var parent = this;
	    this.items.each(function(value, index){
			value.setParent(parent, index);
	    });
	    if(this.view){
	        this.view.setParent(parent, "view");
	    }
	    this._generateBlock(this);
	},
	_generateBlock: function(item){
		this._generator(item);
		for(var i = 0; i < item.items.length; i++)
			if(item.items[i].items.length > 0)
				this._generateBlock(item.items[i]);
	},
	_ajaxCallColmplete: function(req){
	    try{
	        var data = eval('(' + req.responseText + ')');
	        this.parseData(data);
	        this._setParents();
	    }catch(e){
	        this._alert("Error in LucidMenu defenition in file '" + this._path + "' please check your syntax.");
	    }
	},
	_ajaxCallError: function(req){
	    this._alert("Error calling script '" + this._path + "' response status: "+req.status);
	},
	_alert: function(txt){
	    if(!LucidMenu.SUPPRESS_ERROR_WARNING) alert("LucidMenu Error: "+txt);
	}
}
// -------- LucidMenu object ENDS HERE
// -------- LucidMenuElement object STARTS HERE
LucidMenuElement = Class.create();

LucidMenuElement.prototype = {
    _systemId: null,
    _parent: null,
    _menu: null,
    level: 0,
    getSystemId: function(){
	    return this._systemId;
	},
	getMenu: function(){
	    return this._menu;
	},
	getParent: function(){
	    return this._parent;
	},
	getView: function(callingLevel){
	    if(this.isType("LucidMenuView")) return this;
	    if(this.view != null) return this.view;
	    return this.getParent().getView(ObjectAnalyser.coalesce(ObjectAnalyser.parseInt(callingLevel),this.level));
	},
	setParent: function(parent, index){
	    this._parent = parent;
	    this.level = this._parent.getLevel() + (this.isType("LucidMenuView")?0:1);
	    this._systemId = this._parent.getId()+"_"+index;
	    if(this.id === null) this.id = this._systemId
	    this._setParents();
	},
	_setParents: function(){},
	isMenuObject: function(){
	    return true;
	}
}
// -------- LucidMenuElement object ENDS HERE


// -------- LucidMenuItem object STARTS HERE
LucidMenuItem = Class.create();

LucidMenuItem.prototype = {
	_blockUI: null,
	_elementUI: null,
	_opacity: 0,
    items: null,
    view: null,
    text: null,
    url: null,
    target: null,
    checkGroup: null,
    selected: false,
    value: null,
    id: null,
    initialize: function(menu,text,url,target,checkGroup,selected,value){
        this._menu = menu;
        this.items = [];
        this.text = ObjectAnalyser.coalesce(text,'');
        this.url = ObjectAnalyser.coalesce(url,null);
        this.target = ObjectAnalyser.coalesce(target,null);
        this.checkGroup = ObjectAnalyser.coalesce(checkGroup,null);
        this.selected = ObjectAnalyser.coalesce(selected, false);
        this.value = ObjectAnalyser.coalesce(value,null);
	},
	getId: function(){
	    return this.id;
	},
	getBlockUI: function(){
	    return this._blockUI;
	},
	setBlockUI: function(ui){
	    this._blockUI = ui;
	},
	getElementUI: function(){
	    return this._elementUI;
	},
	setElementUI: function(ui){
	    this._elementUI = ui;
	},
	getLevel: function(){
	    return this.level;
	},
	getOpacity: function(){
	    return this._option;
	},
	setOpacity: function(opacity){
	    this._opacity = (opacity > 100)?100:opacity;
	    var mozilaVal = (opacity < 100)?"."+opacity:"1.0";
	    Element.setStyle(this._blockUI,{"filter":"alpha(opacity='"+opacity+"')","-moz-opacity":mozilaVal,"opacity":mozilaVal});
	},
	isType: function(type){
	    return (type === "LucidMenuItem");
	},
	add: function(value, index){
	    ObjectAnalyser.add(this, value, index);
	},
	isParentOf: function(item){
	    return (item._systemId.indexOf( this._systemId ) == 0);
	},
	changeTransparacy: function(){
	    if(this._opacity < 100){
	        this.setOpacity(this._opacity + 25);
	        setTimeout(this.changeTransparacy.bind(this), 100);
	    }else{
	        this.setOpacity(100);
	    }
	},
	isWithinGeneration: function(x, y){
	    var status = false;
	    if(this._blockUI != null) status = Position.within(this._blockUI, x, y);
	    if(!status) status = this.getParent().isWithinGeneration(x, y);
	    return status;
	},
	isWithin: function(x, y){
	    var status = false;
	    if(this._blockUI != null) status = Position.within(this._blockUI, x, y);
	    if(!status && this._elementUI != null) status = Position.within(this._elementUI, x, y);
	    return status;
	},
	show: function(){
	    var block = this.getBlockUI();
	    var element = this.getElementUI();
	    this._menu._setActiveItem(this); 
	    if(block == null) return;
	    if(element != null){
	        block.style.display = "block";
	        var info = ObjectAnalyser.getInfo(element);
	        block.style.left = (info.x + info.width)+"px";
	        block.style.top = info.y+"px";
	        if(!document.all){
	            this.setOpacity(0);
	            setTimeout(this.changeTransparacy.bind(this), 100);
	        }
	    }
	},
	hide: function(){
	    var block = this.getBlockUI();
	    if(block != null) block.style.display = "none";
	},
	onElementMouseOver: function(evt){
	    this.show();
	},
	onElementMouseOut: function(evt){
	    this._menu.startTrackingMousePosition(this);
	},
	onElementClick: function(evt){
	    if(!this.url) return;
	    this.hideGeneration(null);
	    if(this.url.indexOf("javascript:") == 0){
	        eval(this.url.substring(11)); return;
	    }
	    if(!this.target){
	        document.location.href = this.url; return;
	    }
	    if(!!window.parent[this.target]){
	        window.parent[this.target].location.href = this.url; return;
	    }
	    window.open(this.url, this.target);   
	},
	hideGeneration: function(candidate){
        if(candidate == null || !this.isParentOf(candidate)) this.hide();
        var parent = this.getParent();
        if(parent != null) parent.hideGeneration(candidate);
	},
	checkGenerationState: function(x, y){
	    if(!this.isWithinGeneration(x, y)){
	        this.hideGeneration(null);
	    }
	},
	_setParents: function(){
	    var parent = this;
	    this.items.each(function(value, index){
	         value.setParent(parent, index);
	    });
	    if(this.view){
	        this.view.setParent(parent, "view");
	    }
	},
	toString: function(){
	    return "<LucidMenuItem ID=\""+this.getId()+"\" SystemID=\""+this.getSystemId()+"\"><ElementUI>"+this._elementUI+"</ElementUI><BlockUI>"+this._blockUI+"</BlockUI>"+ObjectAnalyser.objectToString(this, false)+"</LucidMenuItem>";
	}
}
ObjectAnalyser.extend(LucidMenuItem.prototype, LucidMenuElement.prototype);
// -------- LucidMenuItem object ENDS HERE
// -------- LucidMenuView object STARTS HERE
LucidMenuView = Class.create();

LucidMenuView.prototype = {
    flags: null,
    css: null,
    icon: null,
    arrow: null,
    initialize: function(menu,bitMap,cssClass,iconUrl,arrow){
        this._menu = menu;
        this.flags = new BitMapManager(0, LucidMenuFlag, LucidMenu.GROUPS_FLAGS, LucidMenu.SUPPRESS_ERROR_WARNING);
        this.flags.add(ObjectAnalyser.coalesce(bitMap, LucidMenu.DEFAULT_FLAGS));
        this.css = ObjectAnalyser.coalesce(cssClass, "LucidMenuElement");
        this.icon = ObjectAnalyser.coalesce(iconUrl, null);
        this.arrow = ObjectAnalyser.coalesce(arrow, "images/LucidMenu.Arrow.Right.gif");
	},
	isType: function(type){
	    return (type === "LucidMenuView");
	},
	toString: function(){
	    return "<LucidMenuView SystemID=\""+this.getSystemId()+"\">"+ObjectAnalyser.objectToString(this, false)+"</LucidMenuView>";
	}
}
ObjectAnalyser.extend(LucidMenuView.prototype, LucidMenuElement.prototype);
// -------- LucidMenuView object ENDS HERE


// -------- LucidMenuGenerator object STARTS HERE

function LucidMenuGenerator(parentItem){
	var ul = document.createElement("ul");
    ul.id = parentItem.getSystemId();
    ul.className = parentItem.getView().css;
    var menu = parentItem.getMenu();
    
    for(var i = 0; i < parentItem.items.length; i++){
		var item = parentItem.items[i];
		var view = item.getView();
		var li = document.createElement("li");
		li.id = item.getSystemId();
		item.setElementUI(li);
		ul.appendChild(li);
		if(item.text == "-"){
		    li.className = "Limiter";
		    continue;
		}
		Event.observe(li, 'mouseover', item.onElementMouseOver.bindAsEventListener(item));
		Event.observe(li, 'mouseout', item.onElementMouseOut.bindAsEventListener(item));
		Event.observe(li, 'click', item.onElementClick.bindAsEventListener(item));
		var div = document.createElement("div");
		if(item.level > 1 && item.items.length > 0){
			div.style.background="url('"+view.arrow+"') right no-repeat";
		}
		li.appendChild(div);
		var spanImage = document.createElement("span");
		spanImage.className = "Image";
		if(view.icon != null){
			spanImage.style.background="url('"+view.icon+"') left no-repeat";
		}
		spanImage.appendChild(document.createTextNode(" "));
		div.appendChild(spanImage);
		var spanText = document.createElement("span");
		spanText.className = "Text";
		spanText.appendChild(document.createTextNode(item.text));
		div.appendChild(spanText);
    }
    parentItem.setBlockUI(ul);
    // uncomment to view generated HTML
    // prompt(parentItem.getId(),ObjectAnalyser.outerHTML(ul));
    menu.getRoot().appendChild(ul);
}

// -------- LucidMenuGenerator object ENDS HERE

