jQuery.noConflict();
jQuery(document).ready(function($){
	//start var
	int_DSLI = null;
	navContainerH_closed = $('#nav_container').height();
	navContainerH_open = 360;
	menuOpen = false;
	newsH_closed = 93;
	
	// assegno la classe 'sel' alle voci di menu
	checkSel();
	
	//compilo le briciole di pane
	for( var i = 0; i<$('#nav .sel').length; i++) {
		var element = $('#nav .sel > a').eq(i).text();
		$('#breadcrumb').append(
			'<div class=\"item\">'+element+'</div>'
		);
	}
	
	//coperture
	$('#nav').append('<div id=\"nav_cover\"></div>');
	$('#breadcrumb').append('<div id=\"breadcrumb_cover\"></div>');
	
	// apri/chiudi menu
	$('#nav_container').hover(openMenu,closeMenu);
	
	//baco ie
	if ($.browser.msie) {
		$('a','#nav').hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		);
	}

	//vale per tutte le voci di menu
	$('#nav li a').click(
		function() {
			//solo se clicco su una voce non ancora "highlight"
			if( $( '> a.highlight', $(this).parent() ).length == 0) {
				//controllo se nel livello in cui mi trovo, esiste un elemento selezionato
				//se sì, lo deseleziono, compresi gli eventuali figli
				if( $( '> li a.highlight', $(this).parent().parent() ).length > 0 ) {
					deselectNavItem(
						$( '> li a.highlight', $(this).parent().parent() )
					);
					
				}
				//controllo se nel livello che sto per aprire ho già qualcosa di aperto
				//se sì, lo nascondo, compresi gli eventuali figli
				if( $( '> li ul:visible', $(this).parent().parent() ).length > 0 ) {
					$( 'li ul:visible', $(this).parent().parent() ).hide();
				}
				//seleziono la voce cliccata
				var selectedItem = $(this);
				selectNavItem(selectedItem);
				//controllo se ho un livello interno
				if( $( '> ul', $(this).parent() ).length > 0 ) {
					//mostro il secondo livello
					var ulToShow = $( '> ul', $(this).parent() );
					showNavListItems(ulToShow);
				}
			}
			
			//resetto il menu sul click per evitare di trovarlo aperto se faccio back da browser
			if( $(this).attr('href') != undefined && $(this).attr('href') != "" && $(this).attr('href') != '#'){
				closeMenu();
			}
			
		}
	);
	/*
	
	NEWS
	
	*/
	$('.col_2_3 .open-news').click(
		function() {
			$(this).hide();
			$('.close-news', $(this).parent()).show();
			
			$('.ShortNews', $(this).parent().parent() ).css({'height': 'auto'});
			var tempH = $('.ShortNews', $(this).parent().parent() ).css('height');
			$('.ShortNews', $(this).parent().parent() ).css({'height': newsH_closed});

			$('.ShortNews', $(this).parent().parent() ).stop(true,false).animate({
			height: tempH
			}, 600, 'easeOutCubic');
		}
	);
	$('.col_2_3 .close-news').click(
		function() {
			$(this).hide();
			$('.open-news', $(this).parent()).show();
			
			$('.ShortNews', $(this).parent().parent() ).stop(true,false).animate({
			height: newsH_closed
			}, 600, 'easeOutCubic');
		}
	);
	
	//assegno il colore alla pagina
	setBodyColor();
	
	//force scrollbar on whole page
	forceScrollbar();
	/*
	
	
	FUNCTIONS
	
	
	*/
	function setBodyColor() {
		if($('body.nocolor').length == 0) {
			var nColors = 4;
			var randomnumber=Math.floor(Math.random()*nColors)+1;
			var randomcolor = 'body_color_'+randomnumber;
			$('body').addClass(randomcolor);
		}
	}
	function forceScrollbar() {
		var OSName="Unknown OS";
		if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
		if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
		if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
		if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
		if (OSName=="MacOS") {
			//document.getElementsByTagName('body').item(0).className = 'scroll_y';
			$('body').addClass('scroll_y');
		}
	}
	function checkSel() {
		//leggo l'id da ricercare che è scritto in un campo hidden
		var val = $('#v').val();
		//se val esiste procedo
		if(val != ""){
			//il div che ha come id lo stesso valore
			var obj = $('#'+val);
			
			//se esiste, procedo
			if(obj.length > 0) {
				assignSel(obj);
			}
		}
	}
	function assignSel(obj) {
		$(obj).addClass("sel");
		var newObj = $(obj).parent().parent();
			
		if( $(newObj).get(0).tagName == 'LI' ){
			assignSel(newObj);
		}
	}
	function selectNavItem(a) {
		// l'elemento passato è l'A contenuto nell'LI
		a.addClass('highlight');
		a.animate({
		marginLeft: 0
		}, 350, 'easeOutCubic');
	}
	function deselectNavItem(a) {
		// l'elemento passato può essere singolo o multiplo A
		a.removeClass('highlight');
		a.animate({
		marginLeft: -26
		}, 350, 'easeOutCubic');
	}
	function showNavListItems(ul) {
		//nascondo gli LI all'interno, poi li mostro in maniera progressiva
		$('li', ul).hide();
		
		//mostro l'UL
		ul.show();
		
		//mostro in maniera progressiva gli LI
		if(int_DSLI){
			clearInterval(int_DSLI);
		}
		nListItemsToShow = $('> li', ul).length;
		listItemToShow = 0;
		
		currentUlToShow = ul;
		doShowNavListItems();
		
	}
	function doShowNavListItems() {
		if(int_DSLI){
			clearInterval(int_DSLI);
		}
		
		var showingItem = $('> li', currentUlToShow).eq(listItemToShow);
		showingItem.show();
		// baco ie con opacity
		if ($.browser.msie) {
			//IE
			showingItem.css({'margin-left':26});
			showingItem.animate({
			marginLeft: 0
			}, 350, 'easeOutCubic',
				function() {
  					$(this).css('filter', 'none');
				}
			);
		} else {
			//non IE
			showingItem.css({'margin-left':26, 'opacity':0});
			showingItem.animate({
			marginLeft: 0,
			opacity: 1
			}, 350, 'easeOutCubic');
		}
		
		listItemToShow++;
		if(listItemToShow < nListItemsToShow){
			int_DSLI = setInterval(doShowNavListItems, 100);
		}
	
	}
	function openMenu(){
		if(menuOpen == false){
			menuOpen = true;
			//resetto il menu
			$('#nav_cover').stop(true,true);
			
			//nascondo le briciole di pane
			$('#breadcrumb_cover').css({'display':'block', 'opacity':0});
			$('#breadcrumb_cover').stop(true,false).animate({
			opacity: 1
			}, 300);
			
			//evidenzio gli elementi con classe .sel
			$('#nav .sel > a').addClass('highlight');
			$('#nav .sel > a').css({'margin-left':0});
			//mostro gli UL che contengono LI con classe .sel
			$('.sel').parent().show();
			//
			$('#nav_container').stop(true,false).animate({
			height: navContainerH_open
			},
			{
				step: function(now, fx) {
					//baco ie 7
					if ($.browser.msie && $.browser.version < 8) {
						$('#col_sx').css({'top':+(now)+'px'});
					}
  				}
			},
			500, 'easeInOutCubic');
			
			$('#nav_cover').stop(true,false).animate({
			opacity: 0
			}, 500,
			function(){
				$(this).hide();
			}
			);
		}
	}
	function closeMenu(){
		if(menuOpen==true){
			menuOpen = false;
			//mostro le briciole di pane
			$('#breadcrumb_cover').stop(true,false).animate({
			opacity: 0
			}, 300);
			
			$('#nav_container').stop(true,false).animate({
			height: navContainerH_closed
			},
			{
				step: function(now, fx) {
					//baco ie 7
					if ($.browser.msie && $.browser.version < 8) {
						$('#col_sx').css({'top':+(now)+'px'});
					}
  				}
			},
			500, 'easeInOutCubic');
			
			$('#nav_cover').stop(true,false).animate({
			opacity: 1
			}, 300,
			//onComplete
			function (){
				//resetto gli elementi
				$('#nav ul').hide();
				$('.highlight').css({'margin-left':-26})
				$('.highlight').removeClass('highlight');
			}
			);
			$('#nav_cover').show();
		}
	}
	
	jQuery.extend( jQuery.easing, {
	
		easeOutCubic: function (x, t, b, c, d) {
			return c*((t=t/d-1)*t*t + 1) + b;
		},
		easeInOutCubic: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t + b;
			return c/2*((t-=2)*t*t + 2) + b;
		}
	
	});
});
