var MenuItem = Class.create();

/**
 * Static vars / functions
 */
MenuItem.allItems = $A();
MenuItem.onShow = null;
MenuItem.onHide = null;

MenuItem.parseOptionString = function(optionsString) {
	var ret = $H();
	
	if(!optionsString.startsWith('menu['))
	return ret;
	
	optionsString = optionsString.substring(5, optionsString.length - 1)
	
	//console.log('parseOptionString: ' + optionsString);
	
	var nameBuffer  = '';
	var valueBuffer = '';
	
	var currentBuffer = 'name';
	var skip = false;

	for(i = 0; i < optionsString.length; i++) {
		
		//var character = optionsString[i];
		var character = optionsString.substr(i, 1);
		
		//console.log('Current char: ' + character);
		
		if(character == '(')
		skip = true;
		
		if(character == ')')
		skip = false;
		
		if(!skip && character == ':')
		currentBuffer = valueBuffer;
		
		else if(!skip && character == ',') {
			ret[nameBuffer] = valueBuffer;
			
			nameBuffer  = '';
			valueBuffer = '';
			
			currentBuffer = 'name';
		}
		
		else {
		  if(currentBuffer == 'name')
		  nameBuffer += character
		  
		  else
		  valueBuffer += character;
		}
	}
	
	//console.log('Option: ' + nameBuffer + '=' + valueBuffer);
	
	ret[nameBuffer] = valueBuffer;
	return ret;
};

/**
 * Object functions
 */
MenuItem.prototype = {

	element: null,
	options: null,
	
	parent:   null,
	children: $A(),
	
	initialize: function(element) {
		
		//console.log('MenuItem.initialize()');
		
		this.element = $(element);
        if(!this.element)
        return false;
        
        //console.log('Element: ' + element.inspect());

        if(!this.element.rel)
        return false;
        
        var menupointOptions = MenuItem.parseOptionString(this.element.rel);
        
        //console.log('Menu point options:');
        //console.log(menupointOptions.inspect());
                
		this.options = Object.extend({
			child_container: null,
			child_class:     null,
		    click:           null,
		    mouseover:       null,
		    mouseout:        null
		}, menupointOptions || {});
		
		//console.log('this.options:');
		//console.log($H(this.options).inspect());
		
		this.options.child_container = $(this.options.child_container);
		
		if(this.options.child_container)
		this.initializeChildren();

        $A(['click', 'mouseover', 'mouseout']).each(function(eventName) {
            if(!this.options[eventName])
        	return;
        	
            var commands = $A(this.options[eventName].split('|'));
            //console.log('Commands for ' + eventName + ': ' + commands.inspect());
        	Event.observe(this.element, eventName, function(event) {
                commands.each(function(command) {
                	
                	//console.log('Command: ' + command);
                	
                	args = '';
                	if(command.indexOf('(') != -1) {
                		//console.log('Has args');
                		var commandArgs = command.split('(');
                		
                		command = commandArgs[0];
                		args    = commandArgs[1];
                		args = args.substr(0, args.length - 1);
                		
                		args = $A(args.split(','));
                		
                		//console.log('Com: ' + command + ', Args: ' + args.inspect());
                	}
                	
                    switch(command) {
                        case 'showchild':
                            this.showChild(true);
                            break;
                            
                        case 'switchimage':
                            if(image = $(args[0]))
                            image.src = args[1];
                            
                            break;
                            
                        case 'setstyle':
                            var element = $(args[0]);
                            
                            var styleArg = $H();
                            styleArg[args[1]] = args[2];
                            
                            if(element)
                            element.setStyle(styleArg);
                            
                            break;
                    }
                }.bind(this));

        	}.bindAsEventListener(this, commands));
        }.bind(this));

		MenuItem.allItems.push(this);
	},
	
	//----------------------------------------------------------------------
	
	initializeChildren: function() {
		this.options.child_container.descendants().each(function(element){
			if(!this.options.child_class || element.hasClassName(this.options.child_class)) {
				if(element.rel) {
    				var item = new MenuItem(element);
	   		  	    this.children.push(item);
				}
			}
		}.bind(this));
	},
	
	//----------------------------------------------------------------------
	
	showChild:  function(hideOthers) {
		if(hideOthers)
		MenuItem.allItems.each(function(item) { item.hideChild(); })

        if(this.options && this.options.child_container)
        this.options.child_container.show();		

        if(MenuItem.onShow)
        MenuItem.onShow(this);
	},
	
	//----------------------------------------------------------------------
	
	hideChild: function() {
		
		if(this.options && this.options.child_container)
		this.options.child_container.hide();
		
        if(MenuItem.onHide)
        MenuItem.onHide(this);
    }
};

//------------------------------------------------------------------------------
function initMenu() { 
	$A(['topmenu_country', 'topmenu_jobs']).each(function(item) { new MenuItem(item) });
}

//Event.onReady( function() {initMenu();} );



