var onloads = new Array();
function bodyOnLoad() {
     for ( var i = 0 ; i < onloads.length ; i++ )
        onloads[i]();
}

var bellelli={
	opened:null,
	init:function() {
		var uri=parseUri(document.URL);
		var id=null;
		switch(uri.fileName) {
			case 'storia.htm':
			case 'certificazioni.htm':
			case 'fiere.htm':
				id="azienda"
			break;
			case 'crash_intro.htm':
			case 'crash_team.htm':
			case 'crash_funzionamento.htm':
			case 'crash_altri.htm':
				id="crash";
			break;
			case 'questionario.cfm':
			case 'contatti.cfm':
			case 'garanzia.htm':
			case 'distributori_agenti.htm':
				id="servizi";
			break;
			case 'rassegna_stampa.htm':
			case 'formazione.cfm':
			case 'nuove_normative.htm':
				id="formazione";
			break;
			
		}
		if (id) {
			var context=this;
			Effect.toggle(id,'appear',{afterFinish:function() { context.opened=id; } });
		}
	},
	showMenu:function(id) {
		var context=this;
		if (!this.opening) {
			if (this.opened) {
				if (this.opened==id) {
					context.opening=true;
					Effect.toggle(id,'appear',{afterFinish:function() { context.opened=null; context.opening=false; } });	
				} else {
					context.opening=true;
					Effect.toggle(context.opened,'appear',{afterFinish:function() { 
						Effect.toggle(id,'appear',{afterFinish:function() { context.opened=id;context.opening=false; } });
					} });
				}
				
			
			} else {
				context.opening=true;
				Effect.toggle(id,'appear',{afterFinish:function() { context.opened=id;context.opening=false; } });	
			}
		}
	}
}
function parseUri(sourceUri){
    var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"];
    var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri);
    var uri = {};
    
    for(var i = 0; i < 10; i++){
        uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
    }
    
    // Always end directoryPath with a trailing backslash if a path was present in the source URI
    // Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key
    if(uri.directoryPath.length > 0){
        uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
    }
    
    return uri;
}

onloads.push(function(){
	bellelli.init();
});

Event.observe(window,'load',bodyOnLoad);
