function positionTabSx(animation, scroll) {
	var aiPage        = jQuery('.aiPage')
	  , aiMainContent = jQuery('.aiMainContent')
	  , myTab         = jQuery('.aiTabSx')
	  , isMsie        = jQuery.browser.msie
	  , browserVers   = jQuery.browser.version
		;
	var pageOffset    = aiPage.offset()
	  , myTabOffset   = myTab.offset()
	  , myTabHeight   = myTab.outerHeight(true)
	  , contentOffset = aiMainContent.offset()
	  , contentHeight = aiMainContent.outerHeight()
		;
	myTab.each(function(e) {
		var e = (myTabHeight - 10)*e;
		if (!isMsie) { jQuery(this).data('startOpacity', .4);}
		if (isMsie && browserVers < '7') {
			jQuery(this).show();
			jQuery(this).css('position', 'absolute');
			jQuery(this).css('top', (contentOffset.top - 10) + e + 'px');
			jQuery(this).css('left', (pageOffset.left - 25) + 'px');
		} else {
			jQuery(this).css('position', 'fixed');
			jQuery(this).css('top', (contentOffset.top - 10) + e + 'px');
			jQuery(this).css('left', (pageOffset.left - 25) + 'px');
	
			if (animation && !isMsie) {
				jQuery(this).fadeTo(1500, jQuery(this).data('startOpacity'));
			} else {
				jQuery(this).show();
			}
		}
	});
}

function centerPage() {
	var page = jQuery('.aiPage')
	    docWidth = jQuery(document).width()
	    pageWidth = page.width()
		newLeft = ((docWidth - pageWidth) / 2)
	;
	page.css('float', 'left');
	page.css('left', newLeft + 'px');
}

function noMenu() {
	if (jQuery('.aiColSx').css('display') == 'none' || jQuery('.aiColSx').length == 0) {
		jQuery('.aiThirdFrame').css('width', '910px');
		jQuery('.aiContent').css('width', '702px');
	} else {
		jQuery('.aiThirdFrame').css('width', '732px');
		jQuery('.aiContent').css('width', '524px');
	}
}

jQuery(document).ready(function() {
	var isMsie        = jQuery.browser.msie
	  , browserVers   = jQuery.browser.version
	  ;
	// centratura pagina
	centerPage();

	if (jQuery('.slideBtBox').length > 0
	 && jQuery('.aiBtBox').length > 0) {
		jQuery('.aiBtBox:last').css('margin-right', 0)
		jQuery('.slideBtBox').hide();
		jQuery('.aiBtBox').hover(
			function () {
				jQuery('.slideBtBox', this).slideDown('fast');
				//$('.slideBtBox', this).fadeIn();
			},
			function () {
				jQuery('.slideBtBox', this).slideUp('slow');
				//$('.slideBtBox', this).fadeOut();
			}
		);
	}

	// gestione dimensione colonne contenuti
	if (jQuery('.aiColSx').length == 0) {
		noMenu();
		/*jQuery('.aiThirdFrame').css('width', '732px');
		jQuery('.aiContent').css('width', '524px');*/
		
		/*jQuery('.aiThirdFrame').css('width', '910px');
		jQuery('.aiContent').css('width', '702px');*/
	}
	if (jQuery('.aiColDx').length == 0) {
		jQuery('.aiThirdFrame').css('width', '732px');
		jQuery('.aiContent').css('width', '702px');
	}
	if (jQuery('.aiColSx').length == 0 && jQuery('.aiColDx').length == 0) {
		jQuery('.aiThirdFrame').css('width', '910px');
		jQuery('.aiContent').css('width', '870px');
	}

	// gestione barra verticale menu principale e servizi
	jQuery('.aiMainMenu li:last').css('border-right', 'solid 1px #fff');
	jQuery('.aiServicesMenu li:last').css('border-right', 'none');

	// gestione posizionamento aiTabSx (tab sinistra)
	if (jQuery('.aiTabSx').length > 0) {
		positionTabSx(true);
		var left = jQuery('.aiTabSx').css('left');
		if (!isMsie) {
			jQuery('.aiTabSx').hover(
					function () {
						jQuery(this).stop().animate({left: '-=10', opacity: 1});
					}, 
					function () {
						jQuery(this).stop().animate({left: left, opacity: jQuery(this).data('startOpacity')});
					}
			);
		}

		var dimensions = { 0:"76", 1:"100", 2:"120" };
		jQuery('ul.aiDimTesto li').each(function(e) {
			jQuery(this).css('font-size', '1' + (e*4) + 'px');
			jQuery(this).addClass(dimensions[e]);
			jQuery(this).click(function(es) {
				jQuery('body').css('font-size', jQuery(this).attr('class')+'%');
			});
		});
	}

	if (jQuery('.toggleMenu').length > 0) {
		jQuery('.toggleMenu').click(function() {
			jQuery('.aiColSx').toggle();
			jQuery('.menuHeader').toggle();
			noMenu();
			if (jQuery('.aiColSx').css('display') == 'none') {
				jQuery('img', this).attr('src', '/img/2010/ico-menu_espandi.png');
			} else {
				jQuery('img', this).attr('src', '/img/2010/ico-menu_contrai.png');
			}
		});
	} 

	// gestione top della pagina
	jQuery('a[href=#top]').each(function() {
		jQuery(this).click(function() {
			jQuery('html, body').animate({scrollTop:0}, 'slow');
			return false;
		});
	});

	// gestione Hover su .aiSecondaryMenu e livelli oltre il secondo
	if (jQuery('.aiSecondaryMenu').length > 0) {
		//jQuery('.aiSecondaryMenu li.selected li a').css({'background-color':'#fff', 'border':'solid 1px red'});
		/*{'background':'#f3c200 url(../img/2010/ico-arrowright_on.gif) no-repeat 8px 0','border-bottom':'solid 1px #f3c200','background-color':'#f3c200 !important'}*/
		jQuery('.aiMainContent ul.aiSecondaryMenu li.selected').children('a').css({'background':'#f3c200 url(/img/2010/ico-arrowright_on.gif) no-repeat 3px -2px', 'border-bottom':'solid 1px #f3c200', 'background-color':'#f3c200'});
		
		jQuery('.aiMainContent ul.aiSecondaryMenu .level2 li.selected').children('a').css({'background-position':'15px 0'});
		
		
		
		jQuery('.aiSecondaryMenu li a').mouseover(function() {
			jQuery(this).parent('li').css('background-color', '#f3c200');
		}).mouseout(function() {
			if (jQuery(this).parent('li').hasClass('selected')) {
			} else {
				jQuery(this).parent('li').css('background-color', '#fff');
			}
		});
	}

	// gestione box news
	if (jQuery('.aiNewsBox').length > 0) {
		jQuery('.aiNewsBox').each(function(e) {
			if (parseInt((e+1) / 3)) {
				jQuery(this).css('margin', '0');
			}
		});
	}

	// gestione Hover su .aiBox e bordo del fondo
	if (jQuery('.aiColDx .aiBox ul li').length > 0) {
		jQuery('.aiColDx .aiBox.grey ul li a').mouseover(function() {
			jQuery(this).parent('li').css('background-color', '#ededee');
		}).mouseout(function() {
			jQuery(this).parent('li').css('background-color', '#dcdcdc');
		});
		jQuery('.aiColDx .aiBox.yellow ul li a').mouseover(function() {
			jQuery(this).parent('li').css('background-color', '#fae83a');
		}).mouseout(function() {
			jQuery(this).parent('li').css('background-color', '#f3c200');
		});

		// serve a mostrare la chiusura del box
		if (!isMsie) {
			jQuery('.aiColDx .aiBox ul').css('float','right');
		}
	}


	// gestione Hover su .aiBox e bordo del fondo
	if (jQuery('.aiColSx .aiBox ul li').length > 0) {
		jQuery('.aiColSx .aiBox.grey ul li a').mouseover(function() {
			jQuery(this).parent('li').css('background-color', '#ededee');
		}).mouseout(function() {
			jQuery(this).parent('li').css('background-color', '#dcdcdc');
		});
		jQuery('.aiColSx .aiBox.yellow ul li a').mouseover(function() {
			jQuery(this).parent('li').css('background-color', '#fae83a');
		}).mouseout(function() {
			jQuery(this).parent('li').css('background-color', '#f3c200');
		});

		// serve a mostrare la chiusura del box
		if (!isMsie) {
			jQuery('.aiColSx .aiBox ul').css('float','right');
		}
	}	
	
	
	
	// gestione menu aiFooterServices
	jQuery('.aiPrintPage').click(function() {
		window.print();  
		return false;  
	});
	if (jQuery.browser.webkit) {
		jQuery('.aiAdd2Favs').parent('li').hide();
	} else {
		jQuery('.aiAdd2Favs').click(function() {
			return false;  
		});
	}
	if (jQuery('.aiTitSlidingBox').length > 1 && jQuery('.aiSlidingBox').length > 1) {
		
		jQuery('.aiSlidingBox').hide();
		jQuery('.aiTitSlidingBox').css('cursor', 'pointer');
		jQuery('.aiTitSlidingBox').click(function() {
			jQuery(this).next('div.aiSlidingBox').slideToggle();
		});
		jQuery('.aiTitSlidingBox').toggle(
			function () { jQuery(this).css('background','url(/img/2010/sf-titSlidingBox_open.png) no-repeat 0 0'); }
		  , function () { jQuery(this).css('background','url(/img/2010/sf-titSlidingBox.png) no-repeat 0 0'); }
		 );
	}

	//gestione altezza aiIndexBox
	/*
	if (jQuery('.aiIndexBox').length > 0) {
		var allChildren = jQuery('.aiIndexBox').children();
		allChildren.each(function() {
			alert(jQuery(this).height());
		});
	}
	*/

	//gestione stili per liste ordinate
	if (jQuery('#contentArea ol').length > 0) {
		jQuery('#contentArea ol li').css('list-style', 'none');

		jQuery('.aiContent ol li').each(function(e) {
			jQuery(this).wrapInner('<span class="olTxt" />');
			jQuery(this).prepend('<span class="olNum">' + (e+1) + '.</span>');
		});
	}

	//gestione stili per tabelle
	if (jQuery('.aiTable').length > 0) {
		var table      = jQuery('.aiTable')
		  , th         = jQuery('.aiTable th')
		  , td         = jQuery('.aiTable td')
		  , tr         = jQuery('.aiTable tr')
		  , trLast     = jQuery('.aiTable tr:last')
		  , tdFirst    = jQuery('.aiTable tr td:first-child')
		  , tdNotFirst = jQuery('.aiTable tr td:not(:first-child)')
		  , tdNotFirstSum = jQuery('.tableSum tr td:not(:first-child)')
		  , tdLast     = jQuery('.aiTable tr td:last-child')
		  , trLastTdNotFirst = jQuery('.tableSum tr:last td:not(:first-child)')
		  , tdEven     = jQuery('.aiTable tr td:even')
		  , tdOdd      = jQuery('.aiTable tr td:odd')
			;

			table.css('width', (table.width() - 1) + 'px');
			tdFirst.css({'background-color': '#ededed', 'border-right': 'solid 1px #fff', 'border-bottom': 'solid 1px #fff'});
			tdLast.css('border-right', 'none');
			tdNotFirst.css({'background-color': '#fcee45', 'border-bottom': 'solid 1px #fff'});
			tdNotFirstSum.css({'text-align': 'right'});
			trLastTdNotFirst.css({'background-color': '#f6d419', 'border-bottom': 'solid 1px #fff', 'font-weight': 'bold'});
			//th.wrapInner('<span class="thTitle" />');
			th.wrapInner('<span class="thTxt" />');
			th.prepend('<span class="thTitle">&nbsp;</span>');
			
			trLast.children('td').css('border-bottom', 'none');
	}

	// gestione spostamento voce menu 'area riservata'
	if (jQuery('.aiHeader ul.aiMainMenu li a.areariservata').length > 0) {
		var adminLi  = jQuery('.aiHeader ul.aiMainMenu li a.areariservata').parent('li')
		  , where    = jQuery('.aiServicesMenu')
		  , onlyHtml = jQuery('.aiHeader ul.aiMainMenu li a.areariservata').parent('li').html()
			;

		//jQuery('.aiHeader').append(onlyHtml);
		jQuery('.aiSecondaryMenu').before('<div class="dislocatedBt">' + onlyHtml + '</div>');
		if (adminLi.hasClass('selected')) {
			jQuery('.dislocatedBt').addClass('selected');
		} else {
			jQuery('.dislocatedBt').removeClass('selected');
		}
		//jQuery('.dislocatedBt').css({'top': jQuery('.aiSecondaryMenu').height() + 20 + 'px'});
			//jQuery('a.areariservata').css({'position':'absolute', 'top':'3px', 'left':'34px', 'width':'140px'});
			jQuery('a.areariservata').css({'display':'block', 'position':'relative', 'left':'34px', 'width':'140px'});
		adminLi.remove();
			jQuery('.aiSecondaryMenu').css({'margin-top':'28px', 'border-top-color':'#fff'});
		//jQuery('.aiMainMenu li:last').css('border-right', 'solid 1px #fff');
			//where.append(adminLi);
	}
});
jQuery(window).resize(function() {
	// centratura pagina
	centerPage();

	// gestione posizionamento aiTabSx (tab sinistra)
	if (jQuery('.aiTabSx').length > 0) {
		positionTabSx(false);
	}
});
jQuery(window).scroll(function() {
	// gestione posizionamento aiTabSx (tab sinistra)
	if (jQuery('.aiTabSx').length > 0) {
		positionTabSx(false, true);
	}
});
