/*
 Ajuntament de Barcelona - Museu Picasso
 www.bcn.es/museupicasso
 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: jquery-menu-picasso.js | nº7, 2007-10-26 13:00h, alex royo (capgemini)
*/

$(document).ready ( function() {
	$('body').addClass('js');
	$.InterfaceNavigation.Init();
});


$.InterfaceNavigation = {
	
	options : {            
		quin: '.menu'
	},
	
	Init : function(options) {
		// Comprovem si es passen els paràmetres de configuració
		// i en cas negatiu agafem els paràmetres per defecte
		if (options)
			$.InterfaceNavigation.options = $.extend($.InterfaceNavigation.options, options);

		// Detectem en quina pàgina hi som
		var currentPage = $.InterfaceNavigation.CurrentPage();

		// Marquem l'item seleccionat i extreiem els seus 3r i 4t nivell
		$($.InterfaceNavigation.options.quin+' a[href*='+currentPage+']').each ( function() {
			$(this).parents('li').addClass('select');
			$(this).html('<strong>'+$(this).text()+'</strong>');
			$.InterfaceNavigation.SecondMenu($(this));
		});

		// Ordre de tabulació i comportament dels items del primer nivell
		$($.InterfaceNavigation.options.quin+' ul li span').each ( function() {
			$(this).html('<span class="spanmenu" tabindex="0">'+$(this).text()+'</span>');
			$.InterfaceNavigation.ItemEvents($(this), $(this).parents('map').attr('id'));
		});

		// Amaguem els items del segon nivell
		$($.InterfaceNavigation.options.quin+' ul li ul').hide();

		// Esborrem els items de tercer i quart nivell
		$($.InterfaceNavigation.options.quin+' ul li ul li ul').remove();
		
		// Mostrem l'item seleccionat i els seus germans
		$($.InterfaceNavigation.options.quin+' a[href*='+currentPage+']').parents().show();
		$($.InterfaceNavigation.options.quin+' ul.selected').show();
	},

	CurrentPage : function() {
		var cp = String(document.location.href);
		var dir = '';
		var page = '';
		var pageReg = /\d+,\d+,(\d+_\d+_\d)_\d+/;	// /(\d+,\d+,\d+_\d+_\d)_\d+/;
		
		if (cp.indexOf('#')!=-1) cp = cp.substring(0, cp.indexOf('#'));
		if (cp.indexOf('?')!=-1) cp = cp.substring(0, cp.indexOf('?'));
		
		cp		= cp.split('/');
		dir 	= cp[cp.length-2];
		page	= cp[cp.length-1];
		
		if (page.match(pageReg) != null) {
			page = page.replace(pageReg, '$1');
			return (page);
		} else {
			//return('');
			if (page!='') return (dir+'/'+page);
		}
	},
	
	SecondMenu : function(e) {
		var groupJump = $('#menu-principal').find('p').clone();
		var className = String($.InterfaceNavigation.options.quin).substring(1, $.InterfaceNavigation.options.quin.length);
		var content = '<map id="menu-contingut" class="'+className+'"></map>';
		var positionTree = $(e).parents('ul').length;
		
		$(e).parents('ul').addClass('selected');

		switch(positionTree) {
			case 2:
				// Segon nivell
				if ($(e).parent().children('ul').text()) {
					$($(e).parent().children('a')).html(''+$(e).parent().children('a').text()+'');
					Process($(e).parent().children('a').text());
					$(e).parent().children('ul').appendTo('#menu-contingut');
				}
				break;
			case 3:
				// Tercer nivell
				if ($(e).parent().parent().parent().children('ul').text()) {
					$($(e).parent().parent().parent().children('a')).html(''+$(e).parent().parent().parent().children('a').text()+'');
					Process($(e).parent().parent().parent().children('a').text());
					$(e).parent().parent().parent().children('ul').appendTo('#menu-contingut');
				}
				break;
			case 4:
				// Quart nivell
				if ($(e).parent().parent().parent().parent().parent().children('ul').text()) {
					$(e).parent().parent().parent().parent().parent().children('a').html('<strong>'+$(e).parent().parent().parent().parent().parent().children('a').text()+'</strong>');
					Process($(e).parent().parent().parent().parent().parent().children('a').text());
					$(e).parent().parent().parent().parent().parent().children('ul').appendTo('#menu-contingut');
				}
				break;
		}
		
		function Process(groupTitle) {
			var language = $('html').attr('lang');
			
			switch(language) {
				case 'es':
					$('#menu-principal').find('p a').text('Ve al menú de '+groupTitle);
					break;
				case 'en':
					$('#menu-principal').find('p a').text('Go to '+groupTitle+' menu');
					break;
				default:
					$('#menu-principal').find('p a').text('Vés al menú de '+groupTitle);
					break;
			}
			$('#menu-principal').find('p a').attr('href', '#menu-contingut');
			$('#menu-principal').after(content);

			if (language!='en') $('#menu-contingut').append('<h2>Menú de '+groupTitle+'</h2>');
			else $('#menu-contingut').append('<h2>'+groupTitle+' menu</h2>');
			$('#menu-contingut h2').after(groupJump);
		}
	},
	
	ItemEvents : function(e, id) {
		$(e).css('cursor','pointer');
	
		$(e).click ( function() {
			// Si l'ítem clicat no és el seleccionat...
			if (!$(this).parents('li').children('ul').hasClass('selected')) {
				// Esborrem la selecció de l'antic ítem
				$('#'+id+' ul li ul').removeClass('selected');
				// Marquem el nou ítem com a seleccionat
				$(this).parents('li').children('ul').addClass('selected');
				// I amaguem i mostrem els ítems corresponents
				$('#'+id+' ul li ul').hide('slow');
				$(this).parent().find('ul').toggle('slow');
			}
		});
		$(e).keypress ( function(event) {
			// Si la tecla clicada es l'ENTER...
			if ($.InterfaceNavigation.KeyNum(event)==13) {
				if (!$(this).parents('li').children('ul').hasClass('selected')) {
					// Esborrem la selecció de l'antic ítem
					$('#'+id+' ul li ul').removeClass('selected');
					// Marquem el nou ítem com a seleccionat
					$(this).parents('li').children('ul').addClass('selected');
					// I amaguem i mostrem els ítems corresponents
					$('#'+id+' ul li ul').hide('slow');
					$(this).parent().find('ul').toggle('slow');
				}
			}
		});
	},
	
	KeyNum : function(e) {
		var keynum;
		
		if (window.event) keynum = e.keyCode; 	// IE
		else if (e.which) keynum = e.which; 	// Estandars
		return(keynum);
	}
	
}
