JSMenuDir = new Class({

	IMG_HEIGHT: 17,
	SUBMENU_SLIDE_DURATION: 350,
	SEARCH_SLIDE_DURATION: 500,
	tipoCategoria: 0,

	// ----------------------------------------------------------------------------------------------------
	initialize: function(pagina, isLogged, tipoCategoria) {

		if(isNaN(tipoCategoria)) tipoCategoria = 0;
		this.tipoCategoria = tipoCategoria;

		// Arquivo
		$('arquivo').addEvents({
			'mouseover': function() {
				this.src = 'menu_dir/arquivo_over.gif';
			},
			'mouseout': function() {
				this.src = 'menu_dir/arquivo.gif';
			}
		});

		// Array de imagens para precarregar
		this.menuImgArray = new Array();
		this.menuImgArray.push('menu_dir/arquivo.gif');
		this.menuImgArray.push('menu_dir/arquivo_over.gif');

		this.itemObject = {
			baseURL: 'menu_dir/',
			// Pesquisa simples
			pesquisa_simples: {
				element: $('menu_dir_pesquisa_simples'),
				page: 'pesquisa_simples',
				up: 'pesquisa_simples_up.gif',
				over: 'pesquisa_simples_over.gif',
				sel: 'pesquisa_simples_sel.gif'
			},
			// Pesquisa avancada
			pesquisa_avancada: {
				element: $('menu_dir_pesquisa_avancada'),
				page: 'pesquisa_avancada',
				up: 'pesquisa_avan_up.gif',
				over: 'pesquisa_avan_over.gif',
				sel: 'pesquisa_avan_sel.gif'
			},
			// Individuais
			individuais: {
				element: $('menu_dir_individuais'),
				page: 'exposicoes/individuais',
				up: 'individuais_up.gif',
				over: 'individuais_over.gif',
				sel: 'individuais_sel.gif'
			},
			// Colectivas
			colectivas: {
				element: $('menu_dir_colectivas'),
				page: 'exposicoes/colectivas',
				up: 'colectivas_up.gif',
				over: 'colectivas_over.gif',
				sel: 'colectivas_sel.gif'
			},
			// Instalacao
			instalacao: {
				element: $('menu_dir_instalacao'),
				page: 'ver_obras_do_tipo',
				params: {tipo: '1'},
				up: 'instalacao_up.gif',
				over: 'instalacao_over.gif',
				sel: 'instalacao_sel.gif'
			},
			// Video
			video: {
				element: $('menu_dir_video'),
				page: 'ver_obras_do_tipo',
				params: {tipo: '2'},
				up: 'video_up.gif',
				over: 'video_over.gif',
				sel: 'video_sel.gif'
			},
			// Fotografia
			fotografia: {
				element: $('menu_dir_fotografia'),
				page: 'ver_obras_do_tipo',
				params: {tipo: '3'},
				up: 'fotografia_up.gif',
				over: 'fotografia_over.gif',
				sel: 'fotografia_sel.gif'
			},
			// Pintura
			pintura: {
				element: $('menu_dir_pintura'),
				page: 'ver_obras_do_tipo',
				params: {tipo: '4'},
				up: 'pintura_up.gif',
				over: 'pintura_over.gif',
				sel: 'pintura_sel.gif'
			},
			// Desenho
			desenho: {
				element: $('menu_dir_desenho'),
				page: 'ver_obras_do_tipo',
				params: {tipo: '5'},
				up: 'desenho_up.gif',
				over: 'desenho_over.gif',
				sel: 'desenho_sel.gif'
			},
			// Performance
			performance: {
				element: $('menu_dir_performance'),
				page: 'ver_obras_do_tipo',
				params: {tipo: '6'},
				up: 'performance_up.gif',
				over: 'performance_over.gif',
				sel: 'performance_sel.gif'
			},
			// Comissariados
			comissariados: {
				element: $('menu_dir_comissariados'),
				page: 'exposicoes/comissariados',
				up: 'comissariados_up.gif',
				over: 'comissariados_over.gif',
				sel: 'comissariados_sel.gif'
			},
			// Bibliografia
			bibliografia: {
				element: $('menu_dir_bibliografia'),
				page: null,
				up: 'bibliografia_up.gif',
				over: 'bibliografia_over.gif',
				sel: 'bibliografia_sel.gif',
				submenu: {
					options: {
						panel: $('menu_dir_bibliografia_div'),
						maxHeight: 6 * this.IMG_HEIGHT
					},
					monografia: {
						element: $('menu_dir_monografia'),
						page: 'ver_obras_do_tipo',
						params: {tipo: '7'},
						up: 'monografia_up.gif',
						over: 'monografia_over.gif',
						sel: 'monografia_sel.gif'
					},
					livros: {
						element: $('menu_dir_livros'),
						page: 'ver_obras_do_tipo',
						params: {tipo: '8'},
						up: 'livros_up.gif',
						over: 'livros_over.gif',
						sel: 'livros_sel.gif'
					},
					imprensa: {
						element: $('menu_dir_imprensa'),
						page: 'ver_obras_do_tipo',
						params: {tipo: '9'},
						up: 'imprensa_up.gif',
						over: 'imprensa_over.gif',
						sel: 'imprensa_sel.gif'
					},
					publicacoes: {
						element: $('menu_dir_publicacoes'),
						page: 'ver_obras_do_tipo',
						params: {tipo: '10'},
						up: 'publicacoes_up.gif',
						over: 'publicacoes_over.gif',
						sel: 'publicacoes_sel.gif'
					},
					textos_pm: {
						element: $('menu_dir_textos_pm'),
						page: 'ver_obras_do_tipo',
						params: {tipo: '11'},
						up: 'textos_pm_up.gif',
						over: 'textos_pm_over.gif',
						sel: 'textos_pm_sel.gif'
					},
					textos_sobrepm: {
						element: $('menu_dir_textos_sobrepm'),
						page: 'ver_obras_do_tipo',
						params: {tipo: '12'},
						up: 'textos_sobrepm_up.gif',
						over: 'textos_sobrepm_over.gif',
						sel: 'textos_sobrepm_sel.gif'
					}
				}
			},
			// Web
			web: {
				element: $('menu_dir_web'),
				page: 'ver_obras_do_tipo',
				params: {tipo: '13'},
				up: 'web_up.gif',
				over: 'web_over.gif',
				sel: 'web_sel.gif'
			},
			// Outros
			outros: {
				element: $('menu_dir_outros'),
				page: 'ver_obras_do_tipo',
				params: {tipo: '14'},
				up: 'outros_up.gif',
				over: 'outros_over.gif',
				sel: 'outros_sel.gif'
			}
		};

		if(isLogged) {
			this.itemObject.newsletter = {
				element: $('menu_dir_newsletter'),
				page: 'newsletter/newsletter',
				params: null,
				up: 'mail_up.gif',
				over: 'mail_over.gif',
				sel: 'mail_sel.gif'
			}
			this.itemObject.sair = {
				element: $('menu_dir_sair'),
				page: pagina + '&autenticar=0',
				params: null,
				up: 'saida_up.gif',
				over: 'saida_over.gif',
				sel: null
			}
		}

		// Instalar eventos
		for(var n in this.itemObject) {
			if(this.itemObject[n] instanceof Object && this.itemObject[n].element != null) {
				this.installEvents(null, this.itemObject[n], pagina);
			}
		}

		// Pre-carregar imagens do menu
		new Asset.images(
			this.menuImgArray,
			{
				onComplete: function() {
					$('menu_dir_table').setStyles({
						display: 'block',
						cursor: 'default'
					});
				}
			}
		);

		// Cagaco
		(function() {$('menu_dir_table').setStyles({
			display: 'block',
			cursor: 'default'
		})}).delay(6000);

		// Criar o menu de pesquisa
		this.createAdvancedSearch();

		// Pre-carregar imagens da pesquisa
		new Asset.images(
			this.searchImgArray,
			{
				onComplete: function() {
					$('pesquisa_table').setStyle('display', 'block');
				}
			}
		);


	},

	// ----------------------------------------------------------------------------------------------------
	installEvents: function(_parent, node, pagina) {

		// Determinar o URL e instalar funcoes
		var itemPage = node.page;
		var itemURL = '?pagina=' + node.page;
		for(var m in node.params) itemURL += "&" + m + "=" + node.params[m];
		node.element.baseURL = this.itemObject.baseURL;
		node.element.url = itemURL;
		node.element.up = this.itemObject.baseURL + node.up;
		node.element.over = this.itemObject.baseURL + node.over;
		node.element.sel = this.itemObject.baseURL + node.sel;

		// Se nao for a pagina pretendida (o mais provavel)
		if(itemURL != "?pagina=" + pagina) {
			if(node.element != null) {
				node.element.up = this.itemObject.baseURL + node.up;
				this.menuImgArray.push(node.element.up);
				this.menuImgArray.push(node.element.over);
				this.menuImgArray.push(node.element.sel);
				node.element.setStyle('cursor', 'pointer');
				node.element.addEvents({
					'mouseover': function() {
						this.src = this.over;
					},
					'mouseout': function() {
						this.src = this.up;
					}
				});
			}
		// Caso contrario, e a pagina pretendida
		} else if(node.element != null) {
			node.element.up = this.itemObject.baseURL + node.sel;
			node.element.src = this.itemObject.baseURL + node.sel;
			if(_parent != null) _parent.slidePanel.open();
		}

		// Ver se tem submenu
		if(node.submenu != null) {
			var toggler = node.element;
			for(var n in node.submenu) {
				if(n == 'options') {
					var panel = node.submenu[n].panel;
					options = {
						isOpen: false,
						minHeight: 0,
						maxHeight: node.submenu[n].maxHeight,
						duration: this.SUBMENU_SLIDE_DURATION
					}
					toggler.slidePanel = new JSSlidePanel(toggler, panel, options);
				} else this.installEvents(node.element, node.submenu[n], pagina);
			}
		}

	},

	// ----------------------------------------------------------------------------------------------------
	createAdvancedSearch: function() {

		var panel = $('pesquisa_avancada_div');
		options = {
			isOpen: false,
			minHeight: 0,
			maxHeight: 325,
			duration: this.SEARCH_SLIDE_DURATION
		}
		this.advancedSearchPanel = new JSSlidePanel($('bt_categoria'), panel, options);

		$('search_str').isVirgin = true;
		$('search_str').addEvent(
			focus,
			function() {
				if(this.isVirgin) this.value = '';
				this.isVirgin = false;
			}
		);
		$('search_str').onkeydown = function(event) {
			var event = new Event(event);
			if(event.key == 'enter') doSearch();
		}

		$('bt_categoria').controller = this;
		$('bt_categoria').setStyle('cursor', 'pointer');

		$('pesquisa_combo').controller = this;
		$('pesquisa_combo').addEvent(
			'click',
			function() {
				this.controller.advancedSearchPanel.toggle()
			}
		);

		this.searchObject = {
			baseURL: 'menu_dir/pesquisa/',
			// Todas
			todas: {
				element: $('cat_todas'),
				up: 'cat_todas_up.gif',
				over: 'cat_todas_over.gif',
				tipoCategoria: 0,
				nomeCategoria: 'Todas as categorias'
			},
			// Individuais
			indiv: {
				element: $('cat_indiv'),
				up: 'cat_indiv_up.gif',
				over: 'cat_indiv_over.gif',
				tipoCategoria: -1,
				nomeCategoria: 'Exposições Individuais'
			},
			// Colectivas
			colect: {
				element: $('cat_colect'),
				up: 'cat_colect_up.gif',
				over: 'cat_colect_over.gif',
				tipoCategoria: -2,
				nomeCategoria: 'Exposições Colectivas'
			},
			// Noticias
			noticia: {
				element: $('cat_noticia'),
				up: 'cat_noticia_up.gif',
				over: 'cat_noticia_over.gif',
				tipoCategoria: -3,
				nomeCategoria: 'Notícias'
			},
			// Instalacao
			instal: {
				element: $('cat_instal'),
				up: 'cat_instal_up.gif',
				over: 'cat_instal_over.gif',
				tipoCategoria: 1,
				nomeCategoria: 'Instalação'
			},
			// Video
			video: {
				element: $('cat_video'),
				up: 'cat_video_up.gif',
				over: 'cat_video_over.gif',
				tipoCategoria: 2,
				nomeCategoria: 'Vídeo'
			},
			// Fotografia
			foto: {
				element: $('cat_foto'),
				up: 'cat_foto_up.gif',
				over: 'cat_foto_over.gif',
				tipoCategoria: 3,
				nomeCategoria: 'Fotografia'
			},
			// Pintura
			pintura: {
				element: $('cat_pintura'),
				up: 'cat_pintura_up.gif',
				over: 'cat_pintura_over.gif',
				tipoCategoria: 4,
				nomeCategoria: 'Pintura'
			},
			// Desenho
			desenho: {
				element: $('cat_desenho'),
				up: 'cat_desenho_up.gif',
				over: 'cat_desenho_over.gif',
				tipoCategoria: 5,
				nomeCategoria: 'Desenho'
			},
			// Performance
			perform: {
				element: $('cat_perform'),
				up: 'cat_perform_up.gif',
				over: 'cat_perform_over.gif',
				tipoCategoria: 6,
				nomeCategoria: 'Performance'
			},
			// Comissariados
			comissa: {
				element: $('cat_comissa'),
				up: 'cat_comissa_up.gif',
				over: 'cat_comissa_over.gif',
				tipoCategoria: -4,
				nomeCategoria: 'Comissariados'
			},
			// Monografia
			monografia: {
				element: $('cat_monografia'),
				up: 'cat_monografia_up.gif',
				over: 'cat_monografia_over.gif',
				tipoCategoria: 7,
				nomeCategoria: 'Monografia'
			},
			// Livros
			livros: {
				element: $('cat_livros'),
				up: 'cat_livros_up.gif',
				over: 'cat_livros_over.gif',
				tipoCategoria: 8,
				nomeCategoria: 'Livros'
			},
			// Imprensa
			imprensa: {
				element: $('cat_imprensa'),
				up: 'cat_imprensa_up.gif',
				over: 'cat_imprensa_over.gif',
				tipoCategoria: 9,
				nomeCategoria: 'Imprensa'
			},
			// Projectos graficos
			graficos: {
				element: $('cat_graficos'),
				up: 'cat_graficos_up.gif',
				over: 'cat_graficos_over.gif',
				tipoCategoria: 10,
				nomeCategoria: 'Projectos Gráficos'
			},
			// Textos de PM
			cat_de_pm: {
				element: $('cat_de_pm'),
				up: 'cat_de_pm_up.gif',
				over: 'cat_de_pm_over.gif',
				tipoCategoria: 11,
				nomeCategoria: 'Textos de PM'
			},
			// Textos sobre PM
			cat_sobre_pm: {
				element: $('cat_sobre_pm'),
				up: 'cat_sobre_pm_up.gif',
				over: 'cat_sobre_pm_over.gif',
				tipoCategoria: 12,
				nomeCategoria: 'Textos sobre PM'
			},
			// Web
			cat_web: {
				element: $('cat_web'),
				up: 'cat_web_up.gif',
				over: 'cat_web_over.gif',
				tipoCategoria: 13,
				nomeCategoria: 'Web / CD-ROM'
			},
			// Outros projectos
			cat_outros: {
				element: $('cat_outros'),
				up: 'cat_outros_up.gif',
				over: 'cat_outros_over.gif',
				tipoCategoria: 14,
				nomeCategoria: 'Outros projectos'
			}
		}

		var node;
		this.searchImgArray = new Array();
		for(var n in this.searchObject) {
			if(this.searchObject[n] instanceof Object) {
				node = this.searchObject[n];
				node.element.controller = this;
				node.element.baseURL = this.searchObject.baseURL;
				node.element.up = this.searchObject.baseURL + node.up;
				node.element.over = this.searchObject.baseURL + node.over;
				node.element.nomeCategoria = node.nomeCategoria;
				node.element.tipoCategoria = node.tipoCategoria;
				node.element.setStyle('cursor', 'pointer');
				this.searchImgArray.push(node.element.over);
				node.element.addEvents({
					mouseover: function() {
						this.src = this.over;
					},
					mouseout: function() {
						this.src = this.up;
					},
					click: function() {
						this.controller.advancedSearchPanel.close();
						this.controller.tipoCategoria = this.tipoCategoria;
						$('pesquisa_combo').value = ' ' + this.nomeCategoria;
					}
				});
				if(node.tipoCategoria == this.tipoCategoria) $('pesquisa_combo').value = ' ' + node.nomeCategoria;
			}

		}

	}

});

