Array.prototype.max = function() {
	var max = this[0];
	var len = this.length;
	for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
	return max;
}

var openSubMenu = false;
var arrowimages = { down: ['downarrowclass', 'down.gif', 23], right: ['rightarrowclass', 'right.gif'] }
var lockMenu = false;
var jqueryslidemenu = {

animateduration: {over: 200, out: 100}, // duration of slide in-out animation, in milliseconds

buildmenu:function(menuid, arrowsvar){
	jQuery(document).ready(function($)
	{
		if(jQuery(".errormessagevisible").length > 0) openLogin();
		
		if(jQuery("#dinamic_alert").length > 0)
		{
			jQuery("#dinamic_alert").css('visibility', 'hidden');	
			jQuery("#dinamic_alert").show();
			$(".dinamic_alert_header a.dinamicAlertClose").each(
				function()
				{
					$(this).hover(
						function() 
						{ 
							if(navigator.platform.indexOf("iPad") == -1) jQuery('.dinamic_alert_header a').addClass('overCloseAlert');
						}, 
						function() 
						{  
							if(navigator.platform.indexOf("iPad") == -1) jQuery('.dinamic_alert_header a').removeClass('overCloseAlert');
						}
					);
				});
			jQuery("#dinamic_alert").hide();
			jQuery("#dinamic_alert").css('visibility', 'visible');	
		}
		
		jQuery("#layer_opacity_grey").click( 
			function() 
			{ 
				if(jQuery("#login_annotation").length > 0 && jQuery("#login_annotation").css('display') != "none")
					closeAnnotationLogin();
				if(jQuery("#insert_panel_annotation").length > 0 && jQuery("#insert_panel_annotation").css('display') != "none")
					closeAnnotationLogin();
			});

		jQuery("#layer_opacity_transparent").click( 
			function() 
			{ 
				if(jQuery("#dinamic_alert").length > 0 && jQuery("#dinamic_alert").css('display') != "none")
					closeDinamicAlert("dinamic_alert");
			});
		
		jQuery(".layer_opacity_comments").click( 
			function() 
			{ 
				if(jQuery("#comments_view_container").length > 0 && (jQuery("#comments_view_container").css('display') != "none" || jQuery("#comments_delete_panel").css('display') != "none"))
					closeCommentsView();
			});
		
		if(jQuery.browser.msie && jQuery.browser.version <= 7)
		{
			if(jQuery(".loginuserblock").length > 0)
			{
				var width = jQuery(".loginuserblock").children("span").width() + parseInt(jQuery(".loginuserblock").children("span").css('margin-right')) + jQuery(".loginuserblock").children("a").width();
				jQuery(".loginuserblock").css('width', (width+15)+'px');
			}
		}
		
		jQuery(document).click( function(event) 
			{ 
				if(jQuery(".jNiceSelectWrapper").children('ul').length > 0)
				{
					var ulWrapper = jQuery(".jNiceSelectWrapper").children('ul');
					for(var i = 0; i < ulWrapper.length; i++)
					{
						if(jQuery(ulWrapper[i]).css('display') != "none")
							jQuery(ulWrapper[i]).hide();
					}
				}
			});	
		
		jQuery("#user").val('');
		jQuery("#pwd").val('');
		jQuery("#email").val('');
		
		jQuery("#loginuserpanel, .signup, #loginuserblock").click( function(event) { event.stopImmediatePropagation(); });
		jQuery(".login_btn").click( function(event) 
			{ 
				event.stopImmediatePropagation();
				if(jQuery(this).hasClass("login_btn_open"))
				{
					jQuery("#user").parent("div").attr('style', 'border:1px solid #CCCCCC;');
					jQuery("#pwd").parent("div").attr('style', 'border:1px solid #CCCCCC;');
					jQuery("#email").parent("div").attr('style', 'border:1px solid #CCCCCC;');
					jQuery("#user").val('');
					jQuery("#pwd").val('');
					jQuery("#email").val('');
					jQuery(this).removeClass("login_btn_open");
					jQuery("#loginuserpanel").hide();
				}else
				{
					jQuery(this).addClass("login_btn_open");
					jQuery("#loginuserpanel").show();
				}
			});
		/****** eliminato 01/012*/
		/*jQuery(".user_block, .btn_invia_commento_no_loggato").click( function(event) 
			{ 
				event.stopImmediatePropagation();
				var newH = jQuery(".user_block").children("span").height() + parseInt(jQuery(".user_block").children("span").css('padding-top')) + parseInt(jQuery(".user_block").children("span").css('padding-bottom'));
				jQuery(".user_block").css('height', newH+'px');
				jQuery(".user_block").css('-moz-opacity', '1');
				jQuery(".user_block").css('opacity', '1');
				jQuery(".user_block").css('filter', 'alpha(opacity=100)');
				jQuery(".user_block").parent().children('textarea').css('visibility', 'hidden');
				jQuery(".commentsaddtextbox").css('height', jQuery(".user_block").children('span').height()+'px');
				if(jQuery(".commentsadduserblock").length > 0)
				{ jQuery(".commentsadd").hide(); jQuery(".commentsadduserblock").show(); }
				jQuery(".user_block").children('span').css('visibility', 'visible');
				//if(jQuery(".login_btn").length > 0)	openLogin();
				if(jQuery("#login_annotation").length > 0)	openAnnotationLogin('login_annotation');
				
			});*/
			
		jQuery(".commentsaddtextbox").click( function(event) 
			{ 
				event.stopImmediatePropagation();
				if(jQuery("#insert_panel_annotation").length > 0) openAnnotationLogin('insert_panel_annotation');
				jQuery("#panelcomment").focus();
			});
		
		// Dario: no autocomplete sulla textbox di ricerca
		//$("#searchvalue").autocomplete("searchHelp.jsp", {delay: 1});
		
		if(jQuery.browser.msie && jQuery.browser.version <= 6)
		{
			jQuery("#insert_panel_annotation").css('position', 'absolute');
			jQuery("#login_annotation").css('position', 'absolute');
			
			$("#header").css("left", $("#maincontainer").offset().left);
			$("#header").animate({marginTop: "0px"}, 0, "linear");
			
			if(jQuery("#layer_opacity").length > 0) jQuery("#layer_opacity").css('height', (jQuery("#header").height()+jQuery("#maincontainer").height())+'px');
			if(jQuery("#layer_opacity_transparent").length > 0) jQuery("#layer_opacity_transparent").css('height', (jQuery("#header").height()+jQuery("#maincontainer").height())+'px');
			if(jQuery("#layer_opacity_grey").length > 0) jQuery("#layer_opacity_grey").css('height', (jQuery("#header").height()+jQuery("#maincontainer").height())+'px');
			jQuery(document).pngFix(); 
		}
		
		if(navigator.platform.indexOf("iPad") != -1)
		{
			jQuery("#insert_panel_annotation").css('position', 'absolute');
			jQuery("#login_annotation").css('position', 'absolute');
			
			jQuery("#layer_opacity").css('filter', 'alpha(opacity=70)');
			jQuery("#layer_opacity").css('-moz-opacity', .70);
			jQuery("#layer_opacity").css('opacity', .70);
			
			jQuery("#layer_opacity").css('position', 'absolute');
			jQuery("#layer_opacity").css('height', (jQuery("#header").height()+jQuery("#maincontainer").height())+'px');
			jQuery("#layer_opacity").css('width', (jQuery("#totalcontainer").width() + 20)+'px');
			
			jQuery("#layer_opacity_transparent").css('position', 'absolute');
			jQuery("#layer_opacity_transparent").css('height', (jQuery("#header").height()+jQuery("#maincontainer").height())+'px');
			jQuery("#layer_opacity_transparent").css('width', (jQuery("#totalcontainer").width() + 20)+'px');
			
			jQuery("#layer_opacity_grey").css('position', 'absolute');
			jQuery("#layer_opacity_grey").css('height', (jQuery("#header").height()+jQuery("#maincontainer").height())+'px');
			jQuery("#layer_opacity_grey").css('width', (jQuery("#totalcontainer").width() + 20)+'px');
		}
		
		var $mainmenu = $("#"+menuid);
		var $headers = $mainmenu.find("ul").parent();
		
		$headers.each(function(i)
		{			
			var visible = 0;
			var $curobj = $(this);
			var $subul = $(this).find('ul:eq(0)');
			this._dimensions = {w: this.offsetWidth, h: this.offsetHeight, subulw: $subul.outerWidth(), subulh: $subul.outerHeight()};
			this.istopheader = $curobj.parents("ul").length == 1 ? true : false;
			
			var $linkHeader = $(this).find('a:eq(0)');
			
			height_menu = 52; 
			positionTop = height_menu;
			$subul.css({top: this.istopheader ? (positionTop)+"px" : height_menu+"px"}); // TOP ????
			$curobj.click( 
				function(e)
				{
					var $targetul = $(this).children("ul:eq(0)");
					this._offsets = {left:$(this).offset().left, top:$(this).offset().top};
					$subul.css({left:($(this).offset().left - $("ul#headercontainer").offset().left -5)+"px"})
					var menuleft = this.istopheader ? 0 : this._dimensions.w;
					menuleft = (this._offsets.left + menuleft + this._dimensions.subulw > $(window).width()) ? 
							 		(this.istopheader ? -this._dimensions.subulw + this._dimensions.w : -this._dimensions.w) : menuleft;
					
					if ($targetul.children("li").length >= 1 && $targetul.css('display') == 'none') //if 1 or less queued animations
					{
						openSubMenu = true;
						visible = 1;
						if (visible == 1 && $targetul.children("li").length >= 1 && $targetul.css('display') == 'none')
						{
							var tmpHeight = $subul.height();
							$subul.height(0);
							if(!(jQuery.browser.msie && jQuery.browser.version <= 7)) $subul.css("display","block");
							$subul.animate({height: tmpHeight}, 350);
						}
					}
				}
			);
			$curobj.hover(
				function(e){},		  
				function(e)
				{
					visible = 0;
					$subul.stop(false,true);
					if($subul.css('display') != 'none')
					{
						var tmpHeight = $subul.height();
						$subul.animate({height: 0}, 100, 
							function()
							{
								openSubMenu = false;
								$subul.css("display","none");
								$subul.height(tmpHeight);
							}
						);
					}
				}
			); //end hover
		}) //end 
		$mainmenu.find("ul").css({display:'none', visibility:'visible'});
	}) //end document.ready
}
}

//build menu with ID="myslidemenu" on page:
jqueryslidemenu.buildmenu("headercontainer", arrowimages);

function openLogin()
{
	if(jQuery(".login_btn").hasClass("login_btn_open")) return;
	changeTab('recoverypwdpanel', 'loginpanel');
	jQuery(".login_btn").addClass("login_btn_open");
	jQuery("#user").parent("div").attr('style', 'border:1px solid #CCCCCC;');
	jQuery("#pwd").parent("div").attr('style', 'border:1px solid #CCCCCC;');
	jQuery("#email").parent("div").attr('style', 'border:1px solid #CCCCCC;');
	jQuery("#user").val('');
	jQuery("#pwd").val('');
	jQuery("#email").val('');
	jQuery("#loginuserpanel").show();
}

var count_error_login = 0;
function checkLogin()
{
	count_error_login = 0;
	if(jQuery.trim(jQuery("#user").val()) == "")
	{
		count_error_login = count_error_login + 1;
		jQuery("#user").parent("div").attr('style', 'border:1px solid #3399ff;');
	}
	
	if(jQuery.trim(jQuery("#pwd").val()) == "")
	{
		count_error_login = count_error_login + 1;
		jQuery("#pwd").parent("div").attr('style', 'border:1px solid #3399ff;');
	}
	
	if(count_error_login == 0) jQuery("#formLogin").submit();
}

function resetBorderInput(idField)
{
	jQuery("#"+idField).parent("div").attr('style', 'border:1px solid #CCCCCC;');
}

function changeTab(tabToClose, tabToOpen)
{
	jQuery("#user").parent("div").attr('style', 'border:1px solid #CCCCCC;');
	jQuery("#pwd").parent("div").attr('style', 'border:1px solid #CCCCCC;');
	jQuery("#emailrec").parent("div").attr('style', 'border:1px solid #CCCCCC;');
	jQuery("#"+tabToClose).hide();
	jQuery("#"+tabToOpen).show();
}

function checkRecoveryPassword()
{	
	count_error_login = 0;
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/; 
	
	var email = jQuery.trim(jQuery("#emailrec").val());
	if (!email_reg_exp.test(email) || (email == "") || (email == "undefined"))
	{
		count_error_login = count_error_login + 1;
		jQuery("#emailrec").parent("div").attr('style', 'border:1px solid #3399ff;');
	}
	
	if(count_error_login == 0) jQuery("#formRecoveryPwd").submit();
}

function openDinamicAlert(id, title, text)
{
	jQuery("#dinamic_alert_title").html(title);
	jQuery("#dinamic_alert_text").html(text);
	setTopAlert(id);
	jQuery('#layer_opacity_transparent').show();
	jQuery('#'+id).show();
}

function closeDinamicAlert(id)
{
	jQuery('#layer_opacity_transparent').hide();
	jQuery('#'+id).hide();
}

function getTopAlert(id, top)
{
	var heightPanel = jQuery("#"+id).height();
	var heightWin = jQuery(window).height();
	if(heightPanel >= heightWin) top = Math.ceil(heightWin * 0.5) * -1;
	else if(Math.floor(Math.abs((heightWin - heightPanel)) * 0.5) < Math.abs(top)) top = Math.floor((heightPanel * 0.5)) * -1;
	return top;
}

function setTopAlert(id)
{
	var top = Math.ceil(jQuery("#"+id).height() / 2) * -1;
	id = jQuery.trim(id);
	top = getTopAlert(id, top);
	var Y = jQuery(window).scrollTop();
	var pannello_y_default = top;
	document.getElementById(id).style.marginTop = pannello_y_default+'px';
	if(Y > 0)
	{
		var newTop = pannello_y_default + Y;
		pannello_y_default = newTop;
		document.getElementById(id).style.marginTop = pannello_y_default+'px';
	}
}

jQuery(window).scroll(
	function () 
	{
		var animationTime = 100;
		if(jQuery.browser.msie) animationTime = 20;
		var idAlert = getOpenAlertID();
		var heightBrowser = jQuery(window).height();
		var scrollTop = jQuery(window).scrollTop();
		if(idAlert.length > 0)
		{
			var tmpID;
			var heightAlert;
			var topAlertDefault;
			
			for(var i = 0; i < idAlert.length; i++)
			{
				tmpID = idAlert[i];
				heightAlert = jQuery("#"+tmpID).height();
				if(heightAlert < heightBrowser)
				{
					topAlertDefault = Math.ceil(heightAlert / 2) * -1;
					topAlertDefault = getTopAlert(tmpID, topAlertDefault);
					jQuery("#"+tmpID).animate({marginTop: topAlertDefault + scrollTop}, animationTime, "linear");
				}
			}
		}
	}
);

function getOpenAlertID()
{
	var alertID = new Array();
	if(jQuery("#dinamic_alert").css('display') == 'block' || jQuery("#dinamic_alert").css('display') == 'inline') alertID.push("dinamic_alert");
	return alertID;
}

function resetTextSearch(id, defaultText, errorText, color)
{
	document.getElementById(id).style.color = color;
	var value = jQuery.trim(document.getElementById(id).value);
	if(value == defaultText || value == errorText)
	{
		document.getElementById(id).value = "";
	}
	if(value == "")
	{
		document.getElementById(id).value = defaultText;
	}
}

var count_error_login_annotation = 0;
function openAnnotationLogin(id)
{
	var scrollTop = jQuery(window).scrollTop();
	if(jQuery.trim(jQuery("#loginuserpanel").css('display')) != 'none')
	{
		jQuery(".login_btn").removeClass("login_btn_open");
		jQuery("#loginuserpanel").hide();
	}
	
	jQuery("#"+id).css('visibility', 'hidden');
	jQuery("#"+id).show();
	var top = parseInt(jQuery(".commentspanelinsert").offset().top);
	var left = parseInt(jQuery("#comments").offset().left) - jQuery("#"+id).width(); //+ jQuery("#comments").width()
	
	if(jQuery.browser.msie && jQuery.browser.version <= 6)
	{
		jQuery("#"+id).css('left', (left+80)+'px');
		if(jQuery.trim(jQuery("#"+id).css('position')) == 'absolute')
			jQuery("#"+id).css('top', (top-18)+'px');
		else
			jQuery("#"+id).css('top', '142px');
	}else
	{
		jQuery("#"+id).css('left', left+'px');
		if(jQuery.trim(jQuery("#"+id).css('position')) == 'absolute')
		{
			if((top + jQuery("#"+id).height() + PADDIN_BOTTOM_PANEL) < jQuery(window).height())
			{
				jQuery("#"+id).css('top', top+'px');
			}else
				jQuery("#"+id).css('top', jQuery(window).scrollTop() + jQuery(window).height() - jQuery("#"+id).height() - PADDIN_BOTTOM_PANEL+'px');
		}else
		{
			if((defaultTopAddComment + jQuery("#"+id).height() + PADDIN_BOTTOM_PANEL) < jQuery(window).height())
				jQuery("#"+id).css('top', defaultTopAddComment+'px');
			else
				jQuery("#"+id).css('top', jQuery(window).height() - jQuery("#"+id).height() - PADDIN_BOTTOM_PANEL+'px');
		}
	}
	jQuery("#layer_opacity_grey").show();
	jQuery("#"+id).css('visibility', 'visible');
}

function closeAnnotationLogin()
{
	jQuery("#layer_opacity_grey").hide();
	if(jQuery("#login_annotation").length > 0 && jQuery("#login_annotation").css('display') != "none") jQuery("#login_annotation").hide();
	else jQuery("#insert_panel_annotation").hide();
}

function checkLoginAnnotation()
{
	count_error_login_annotation = 0;
	if(jQuery.trim(jQuery("#user").val()) == "")
	{
		count_error_login_annotation = count_error_login_annotation + 1;
		jQuery("#user_login").attr('style', 'border:1px solid #d0372f;');
	}
	
	if(jQuery.trim(jQuery("#pwd_login").val()) == "")
	{
		count_error_login_annotation = count_error_login_annotation + 1;
		jQuery("#pwd_login").attr('style', 'border:1px solid #d0372f;');
	}
	
	if(count_error_login_annotation == 0) jQuery("#formLoginAnnotation").submit();
}

function resetBorderInput(idField)
{
	jQuery("#"+idField).attr('style', 'border:1px solid #D4D4D4;');
}

function changeTabAnnotation(tabToClose, tabToOpen)
{
	jQuery("#user_login").attr('style', 'border:1px solid #D4D4D4;');
	jQuery("#pwd_login").attr('style', 'border:1px solid #D4D4D4;');
	jQuery("#emailrec_login").attr('style', 'border:1px solid #D4D4D4;');
	jQuery("#"+tabToClose).hide();
	jQuery("#"+tabToOpen).show();
}

function checkRecoveryPasswordAnnotation()
{	
	count_error_login_annotation = 0;
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/; 
	
	var email = jQuery.trim(jQuery("#emailrec_login").val());
	if (!email_reg_exp.test(email) || (email == "") || (email == "undefined"))
	{
		count_error_login_annotation = count_error_login_annotation + 1;
		jQuery("#emailrec_login").attr('style', 'border:1px solid #3399ff;');
	}
	
	if(count_error_login_annotation == 0) jQuery("#formRecoveryPwdAnnotation").submit();
}

function checkCommentsPanel()
{
	
}
