var menu = Class.create();
menu.prototype = {

        initialize: function() {

		this.params = new Object;        
		this.el = new Object;        
                this.showDiv = '';
		this.proxyURL = '/proxy.php';


		
                this.el['3'] = 'oculistica';
                this.el['4'] = 'politica';
                this.el['5'] = 'solidarieta';
                this.el['15'] = 'sogni';
                
                for(i in this.el){
                        var single = this.el[i];
                        if(typeof(single) != 'function'){
                                if($(single)){
		
                                        $(single).obj = this;
                                       	$(single).key = i; 
                                        $(single).onmouseover = function(){
                                                if(!($('sm_' + this.id))){
                                                        this.obj.elRemove(this.id);
                                                        this.obj.show(this.id,this.key);
                                                }
                                        }
                                }
                        }
                }

                var body = $$('body');
                        
                body[0].obj = this;
                body[0].onclick = function(){
                        this.obj.elRemove(null);
                };
        },

        
        
        show: function(el,elKey){
		this.setParams('method','menuData');
		this.setParams('cat',elKey);
		this.setParams('element',el);
		this.callProxy();


        },

        elRemove: function(el){
                for(i in this.el){
                        var single = this.el[i];

                        if(typeof(single) != 'function'){
                                if(el != single){
                                        if($('sm_' + single)) $('sm_' + single).remove();
                                }
                        }
                }
        },

        setShowDiv: function(str){
                this.showDiv = str;
        },

        clearShowDiv: function(){
                this.showDiv = '';
                
        },

	callProxy: function(){
                 try {

                        var obj = this;
                        var strParams = '';

                        new Ajax.Request(this.proxyURL,
                        {
                                method:'get',

                                /*asynchronous: false,*/

                                parameters : obj.params,

                                onSuccess: function(transport){
                                        obj.responseJSON = transport.responseText;
                                        obj.methodCall();
                                        /* $(obj.divLoading).innerHTML = '&nbsp;'; */
                                },

                                onLoading: function(){
					/*
                                        $(obj.divLoading).innerHTML = "<img style='border:0px' src='" + obj.gifLoading + "'> Loading...";
                                        $(obj.divLoading).setStyle({
                                                fontSize: '11px',
                                                fontWeight: 'normal'
                                        });
					*/
                                },

                                onFailure: function(){
                                        alert('Something went wrong...')
                                }
                        });
                } catch(e) {}

        },

        setParams: function(name,value){
                this.params[name] = value;
        },

        removeParams: function(name){
                if(this.name != undefined) delete this.name;
                delete this.params[name];

        },

        methodCall: function(){
                if(!this.validateResponseJSON()) return false;

                switch(this.params['method']){
                        default:
                                this.showParentMenu(this.params['element']);
                        break;

                }
        },
	
	validateResponseJSON: function(){
	      return true;
	},

	showParentMenu: function(el){
		var subMenuData = this.responseJSON.evalJSON();


                var strMenu = '<div id="sm_' + el + '"><ul id="nav1">'; 
		for(i=0,len=subMenuData.length; i<len; ++i){
			var single = subMenuData[i];
			strMenu += '<li><a href="/?cat=' +single.cat + '">' + single.name + '</a></li>';
		}
                strMenu += ' </ul>';

                new Insertion.Bottom(el,strMenu);

                $('sm_' + el).setStyle({
                        'z-index': '999'
                });
	}
}
