$JQ(function() {

function scrollPage(w_scroll)
{
	var lastElem = $JQ('#L').offset().top - 98;
	if (apple)
	{	
		if (w_scroll > lastElem + 228) w_scroll = lastElem + 228;
		$JQ('#contenitore_menu').animate({top:w_scroll+"px"}, 325);
		
	} else {
	
		if (w_scroll > lastElem + 113)
		{	
			$JQ('#contenitore_menu').css(
			{
				top: lastElem - w_scroll + 113
			});
		} else {			
			$JQ('#contenitore_menu').css(
			{
				top: 16
			});
		}
	}
}

var apple = (navigator.platform.indexOf("iPhone") != -1) || 
			(navigator.platform.indexOf("iPod") != -1) ||
			(navigator.platform.indexOf("iPad") != -1);
			
if (apple) 
{
	setInterval(function() 
	{
			scrollPage($JQ(window).scrollTop());
	}, 1000);

} else {

	$JQ(window).scroll(function()
	{
		scrollPage($JQ(window).scrollTop());
	});
}

$JQ('#menu a').add('a.scroll').click(function ()
{
	var target = $JQ(this.hash);
	
	if (target.length)
	{
        var targetOffset = target.offset().top;
		if (this.hash == '#A')
		{
			targetOffset = apple ? -127 : -16;
		} 
		
		if (apple || navigator.appName != 'Microsoft Internet Explorer')
		{
			if ($JQ(this).attr("short") == 'true') targetOffset += 140;
		}
		
		if (apple)
		{
			targetOffset += 90 + 40;
			$JQ('#contenitore_menu').animate({top:targetOffset+"px"}, 325);
		} 
		
		$JQ('html,body').animate({scrollTop: targetOffset}, 650);
		
		return false;
	}
});

// The height of the content block when it's not expanded
var adjustheight = 100;
// The "more" link text
var moreText = '<strong>[SEGUE...]</strong>';
// The "less" link text
var lessText = '<strong>[...CHIUDI]</strong>';

// Sets the .more-block div to the specified height and hides any content that overflows
$JQ(".more-less .more-block").css('height', adjustheight).css('overflow', 'hidden');

// The section added to the bottom of the "more-less" div
$JQ(".more-less").append('<a href="#" class="adjust"></a>');

$JQ("a.adjust").html(moreText);

$JQ(".adjust").toggle(function() {
		$JQ(this).parents("div:first").find(".more-block").css('height', 'auto').css('overflow', 'visible');
		$JQ(this).parents("div:first").find("p.continued").css('display', 'none');
		$JQ(this).html(lessText);
	}, function() {
		var pre = $JQ(this).parents("div:first").height();
		$JQ(this).parents("div:first").find(".more-block").css('height', adjustheight).css('overflow', 'hidden');
		$JQ(this).parents("div:first").find("p.continued").css('display', 'block');
		$JQ(this).html(moreText);
		var post = $JQ(this).parents("div:first").height();
		scrollStep(pre,post);
});

});	
