$(document).ready(function(){
	
	/* MENU' TOPNAV */
	
	index=$("ul#topnav li").index($("ul#topnav a.active").parent());
	var backgroundStyle = "#333333 url(/export/system/modules/it.banzai.treccani.portale/resources/images/menuRoll.gif) repeat-x bottom center";
	if(index==-1){index=$("ul#topnav li").index($("ul#topnav a.active").parent().parent());main=index;}else{main=index;}
	$("ul#topnav li:eq("+main+")").css({ 'background' : backgroundStyle});
	$("ul#topnav li:eq("+main+") a").css({ 'color' : '#bf4b34'});
    $("ul#topnav li:eq("+main+") span a").css({ 'color' : '#333'});
    $("ul#topnav li:eq("+main+") span a.active").css({ 'color' : '#bf4b34'});
	$("ul#topnav li:eq("+main+")").find("span").show(); //Show the subnav
	$("ul#topnav li").hover(
		function() { //Hover over event on list item
			$("ul#topnav li:eq("+main+")").css({ 'background' : 'none'}); //Ditch the background
			//$("ul#topnav li:eq("+main+")").find("span").hide(); //Hide the subnav
			$(this).css({ 'background' : backgroundStyle}); //Add background color + image on hovered list item
			//$(this).find("span").show(); //Show the subnav
		},
		function(){ //on hover out...
			$(this).css({ 'background' : 'none'}); //Ditch the background
			//$(this).find("span").hide(); //Hide the subnav
			$("ul#topnav li:eq("+main+")").css({ 'background' : backgroundStyle}); //Add background color + image on hovered list item
			//$("ul#topnav li:eq("+main+")").find("span").show(); //Show the subnav
		}
	);
	
	/* TAB DELLA SEARCH - accendi/spegni, scatena la search */
	$(".ct a").click(
    	function(){
           	if ($(this).attr('rel')!=$('#search_where').attr('value')) {
               	$('#search_where').attr('value',$(this).attr('rel'));
				$('.ct a.on').removeClass('on');
                $(this).addClass('on');
                var searchWhat = $('#search_what').attr('value');
				if (searchWhat != '') {
					$('#go_search').submit();
					return false;
				} else {
					return true;
				}
                
			}
		}
	);
	
	if($.fancybox){
		/* MODALE BETA MESS */
		$(".beta").fancybox({
			'titlePosition': 'inside',
			'transitionIn': 'none',
			'transitionOut': 'none',
			'scrolling': 'no',
			'showCloseButton': false
		});
		window.top.jQuery('.fbclose').live('click',
			function(){
				window.top.jQuery.fancybox.close();
				return false;
			}
		);
	}

	/* SCATENA LA SEARCH */
    $('#go_search').submit(
    	function() {
	    	var where = $('#search_where').attr('value');
			var what = encodeURIComponent($('#search_what').attr('value'));
			if (what != '') {
				var go = '';
				switch (where) {
					case 'en':
						go = '/enciclopedia/tag/' + what + '/';
						break;
					case 'vo':
						go = '/vocabolario/tag/' + what + '/';
						break;
					case 'db':
						go = '/enciclopedia/tag/' + what + '/Dizionario_Biografico/';
						break;
				}
			} else {
				switch (where) {
					case 'en':
						go = '/enciclopedia/ricerca/';
						break;
					case 'vo':
						go = '/vocabolario/';
						break;
					case 'db':
						go = '/biografie/';
						break;
				}
			}
            window.top.location.href = go;
            return false;
		}
	);

    $('#search_vocab').submit(
        	function() {
    			var what = encodeURIComponent($('#searchVocabWhat').attr('value'));
                if(what != "" && what != "Vocabolario   "){
                	window.top.location.href = "/vocabolario/tag/" + what + "/";
                }
                return false;
    		}
    	);
    
	/* LINK DEL PATH - accendono il tab selezionato in HP */
	$('#tohp').click(
		function() {
			$('#hp_type').attr('value',$(this).attr('rel'));
			$('#go_home').submit();
			return false;
		}
	);

	$('#tohp2').click(
    	function() {
			$('#hp_type').attr('value',$(this).attr('rel'));
            $('#go_home').submit();
            return false;
		}
	);	
		
});
