function smart_resize() {
	viewport_width = document.documentElement.clientWidth;

	body_width = 764;
	MIN_WIDTH = 700;
	MAX_WIDTH = 1200;


	if (viewport_width >=MIN_WIDTH && viewport_width <= MAX_WIDTH) {
		body_width = viewport_width - 100;
	}
	else if (viewport_width <MIN_WIDTH) {
		body_width = viewport_width - 10;
	}
	else if (viewport_width > MAX_WIDTH) {
		body_width = MAX_WIDTH - 100;
	}


// Allargo il body
	$('body').css('width', body_width);

// Allargo le sezioni Ateneo, Ricerca, Didattica
	margin_sezioni = parseInt($('#sezioni').css('margin-left'));
	if (isNaN(margin_sezioni)) {
		margin_sezioni = 12;
	}

	column = (body_width - margin_sezioni * 2) / 5;


//	document.title = "Win: " + viewport_width + " - Body: " + body_width + " - Sezione: " + column;

	$('#sezioni li').css('width', column);


	ricerca_width = column * 3 - parseInt($('#ricerca').css('padding-left')) - parseInt($('#ricerca').css('padding-right'));


// Aumentare l'altezza

// Altezza della lista "accesso personalizzato"

	personalizzato_height = column * 3 * 183 / 462 - 75;

	li_utenti_height = personalizzato_height / 4 - 7 - 1 * $.browser.msie;
	li_utenti_padding = (li_utenti_height - column/13) / 2 + 2;


	$('#utenti').css('width', ricerca_width);
	$('.home #utenti').css('width', '');

	$('.home #utenti li').css({'height': li_utenti_height - li_utenti_padding + 2, 'padding-top': li_utenti_padding, 'font-size':column / 13, 'padding-right': (personalizzato_height + 40) * 0.656 - 10});
	$('.home #porta').css({'height': personalizzato_height + 40});

        if (body_width >= 750) {
        //Impostazione banner per layout dei centri
        $('.dipartimento #logo').css({'width': body_width - 190});
        //$('.dipartimento #top-dipartimento').css({'padding-bottom': document.getElementById("logo").clientHeight-60});
        //$('.dipartimento  #link-home').css({'height': document.getElementById("logo").clientHeight});
             if (body_width <= 950) {
             $('.dipartimento #logo').css({'bottom':-20});
             }else{
             $('.dipartimento #logo').css({'bottom':0});
             }
        }
        else{
        $('.dipartimento #logo').css({'width': 470});
        $('.dipartimento #logo').css({'bottom':-20});
        }


	$('#ricerca').css('width', ricerca_width);


	$('.home #ricerca').css('width', ricerca_width - column);
	$('.centro #ricerca').css('width', '');



	$('#foto-home-sezione').css('width', column * 3);
	$('.home-sezione h1').css({'width': column * 3});



// Se non restringo troppo la finestra ridimensiono la colonna principale e quella a destra in rapporto 3 a 2
//	if (body_width >= 600) {
	if (body_width >= 100) {
		$('#content[class!=large]').css({'width': column * 3, 'float':''});
		$('#main.home #content[class!=large]').css({'width': column * 3 - 20, 'float':''});
		$('.facolta #content[class!=large]').css({'width': '', 'float':''});

		$('.centro #content').css({'width': body_width - 520});
		$('.centro #content[class^=large]').css('width', '');

                $('.dipartimento #content').css({'width': body_width - 450});
		$('.dipartimento #content[class^=large]').css('width', '');

		$('body.home #content[class*=search-results]').css({'width': column * 5 - 24});

		$('#navigation').css({'width':column * 2 - 20});

		browserVersion = parseInt($.browser.version.substring(0,1));
		badBrowser = $.browser.msie && browserVersion < 7;

		$('.home #navigation').css({'width': column * 2 - 12 - 18 * badBrowser});

// Dimensione dei caratteri dei titoli di accesso personalizzato
		$('#navigation.home-sezione h2').css('font-size', (column / 130) + 'em');

// Nei centri la colonna contenuto e' larga come il body MENO la colonna dei menu e MENO la colonna destra di navigazione (uguali tra loro e pari a 250 px circa)		
//		$('.centro #navigation').css({'margin-left': body_width - 500, 'width': 240});
		$('.centro #navigation').css({'width': 240});
		$('.dipartimento #navigation').css({'width': 240});
		$('.facolta #navigation').css({'margin-left': '', 'width':''});

// Devo impostare l'altezza del blocco fotohome perche' funzioni il crossfade, anche la larghezza !!
		$('#fotohome').css({'height': (column * 3 - 2) * 61/154, 'width': column * 3});
		$('#fotohome img').css('width', column * 3 - 2);
// Video Promo unimc
		$('#video-prom object').css({'height': ((column * 3 - 2) * 61/154) - 4, 'width':  (column * 3) * 0.5643});
		$('#video-prom embed').css({'height': ((column * 3 - 2) * 61/154) - 4, 'width':  (column * 3) * 0.5643});
 
// Impostazione altezza slideshow
                $('.facolta .slideshow .images').css({'height': ((column * 3 - 2) * 61/154)-35});
                $('.centro .slideshow .images').css({'height': ((column * 3 - 2) * 61/154)-23});
                $('.dipartimento .slideshow .images').css({'height': ((column * 3 - 2) * 61/154)-8});

		$('#porta').css('display', '');
		$('img.large').css({'width': column * 3});
		$('h1').css('margin-right', '');
		$('body').css('font-size', '');
		$('#page-actions li').css({'width': column * 2 / 3 - 5});
		
		if ($('#page-actions li img').width() >= column * 2 / 3 - 20) {
			$('#page-actions li img').css({'width': column * 2 / 3 - 20});	
		} else {
			$('#page-actions li img').css('width', '');
		}

		$('#accesso-rapido select').css({'width': column * 4 / 3 - 10});
		$('#utenti select').css({'width': column * 5 / 3 - 129});

		$('.news .description').css({'font-size': ((8 + column / 50) / 12) + 'em'});


		
	// viene eliminata la scritta "apri-chiudi sezione" dal menu a sx
		if (body_width < 730) {
			$('.home-sezione h2 span').hide();
		} else {
			$('.home-sezione h2 span').show();	
		}
		
	} else {
// altrimenti espando la colonna contenuti e metto l'altra sotto
		$('#content').css({'width':'auto', 'float':'none'});
		$('#navigation').css('margin-left', 12);
		$('#porta').css('display', 'none');
		$('h1').css('margin-right', 0);
		$('body').css('font-size', '.7em');

		// Allargo le tre sezioni perche' si prendano TUTTO lo spazio disponibile
		$('#sezioni li').css('width', (body_width - margin_sezioni * 2 - 3) / 3);
		$('img.large').css({'width': body_width - margin_sezioni * 2 - 3});

	}


}






function aprimenu(link, id) {
	$('#' + id).slideToggle('fast');

	html = $(link).html();
	text = $(link).text();

	if (text.indexOf('chiudi') != -1) {
		html = html.replace('chiudi', 'apri').replace('meno', 'piu');
	} else {
		html = html.replace('apri', 'chiudi').replace('piu', 'meno');
	}


	$(link).html(html);

	return false;
}




function setFontSize(offset) {
	currentFontSize = parseFloat($('body').css('fontSize'));
	newFontSize = currentFontSize + offset * 1.6;
	$('body').css('fontSize', newFontSize + 'px');

/* il cookie va salvato in em, NON in px !!!	*/
	emFontSize = newFontSize / 16;

	setCookie('fontsize', emFontSize, 1);
}



/*
 * Azioni eseguite al caricamento
 */


$(document).ready(function() {

// Apre il menu facolta al clic
	$("#menu-facolta > li:has(ul) > a").click( function() {
		$(this).next('ul').toggle();
		return false;
	} );
	
// Menu di accesso rapido, nasconde il tasto di submit ed esegue un redirect al link della <select> all'onchange
	$('#accesso-rapido input[@type=submit]').hide();
	$('#accesso-rapido select').change( function() {
		$('#accesso-rapido').submit();
	} );

// Apre i link con rel="external" in una nuova finestra (Xhtml strict compliant)
	$("a[rel=external]").click( function() {
		window.open(this.href)
		return false;
	} );

// Aggancia il javascript alle icone per modificare la dimensione carattere
	if (!$.browser.msie) {
		$("#fontsizeup").click(function() {
			setFontSize(1);
			return false;
		});
		$("#fontsizedown").click(function() {
			setFontSize(-1);
			return false;
		});
	}

// Colora a righe alterne le tabelle
	$('table.alterne tr:even').addClass('pari');


// Mette la width a 99% nelle tabelle impostate a 100% pubblicate con l'editor, altrimenti con IE6 non ci stanno
	$('#content table[width=100%]').attr('width', '99%');



// Apre i menu delle sezioni principali
	$('a.slideMenu').click(function() {
		$(this).next().slideToggle();
		imageArrow = $(this).children('img').eq(0);
		srcImage = imageArrow.attr('src');

		if (srcImage.substring(srcImage.length - 8, srcImage.length) == 'down.gif') {
			srcImage = srcImage.replace('down.gif', 'up.gif');
		} else {
			srcImage = srcImage.replace('up.gif', 'down.gif');
		}

		imageArrow.attr('src', srcImage)
		
		return false;
	})


// Mette la manina sui titoli cliccabili
	$('h2[onclick]').css('cursor', 'pointer');


// Nasconde le parti da mostrare tramite azioni Javascript
	$('.toHide').hide();
	$('.toShow').show();


// Cambio sfondo all'elemento <html> nei centri
	$('html:has(body.dipartimento)').css('background','#fff');
        $('html:has(body.centro)').css('background','#0B425A');


	smart_resize();
})

/*
 * End azioni eseguite all'avvio
 */


/*
 * Al resize della finestra ricalcolo le larghezze
 */

$(window).resize(smart_resize);







/*
 * Funzioni per gestire i Cookie
 */

function setCookie(sNome, sValore, iGiorni) {
	var dtOggi = new Date();
	var dtExpires = new Date();
	//dtExpires.setTime(dtOggi.getTime() + 24 * iGiorni * 3600000);
        dtExpires.setTime(dtOggi.getTime() + 1 * iGiorni * 7200000);
	document.cookie = sNome + "=" + escape(sValore) + "; expires=" + dtExpires.toGMTString() + ";path=/";
}

// restituisce il valore del cookie sNome
function getCookie(sNome) {
	// genera un array di coppie "Nome = Valore"
	// NOTA: i cookies sono separati da ';'
	var asCookies = document.cookie.split("; ");
	// ciclo su tutti i cookies
	for (var iCnt = 0; iCnt < asCookies.length; iCnt++) {
	// leggo singolo cookie "Nome = Valore"
		var asCookie = asCookies[iCnt].split("=");
		if (sNome == asCookie[0]) {
			return (unescape(asCookie[1]));
		}
	}
	// SE non esiste il cookie richiesto
	return("");
}


function toggleMenuCentro(link) {
	$(link).next('ul').toggle();
	linkText = $(link).text();
/*	if (linkText.indexOf('\u2193') != -1) {
		newText = linkText.replace('\u2193', '\u2191');
	} 
	else if (linkText.indexOf('\u2191') != -1) { // &darr;
		newText = linkText.replace('\u2191', '\u2193');
	}


	$(link).text(newText);
*/
	return false;
}


function addFixedToLinks() {
    links = $('a');
    jQuery.each(links, function() {
          $(this).attr('href', $(this).attr('href') + '?fixed');
    });
}



function openSearchResults(link) {
	if (link.tagName == 'A') {
//		menu = $(link).parent().next('ul');
		$(link).parent().click();
	} else {
		menu = $(link).next('ul');
	}

	html = $(link).html();

	if (html.indexOf('chiudi') != -1) {
		html = html.replace('chiudi', 'apri').replace("meno", "piu");
		menu.hide('fast');
	} else {
		html = html.replace('apri', 'chiudi').replace("piu", "meno");
		menu.show('fast');
	}

	$(link).html(html);
	
	return false;
}


