window.addEvent('domready', function(){
   $('divCredits').hide();
   var credits = $('divCredits').clone();
   credits.show();
   var popup = new CosMooPopup({'content':credits, 'width':400, 'classPopup':'creditsPopup', rootPath:myRootPath, destroyOnClose: false});
   $$('a#credits').each(function(link){link.addEvent('click', function(ev){ev.preventDefault();})});
   popup.attach('a#credits');
   
   $$('#menu > li').each(function(el){
	   var sub = el.getElements('ul.sub');
	   if (!sub.length) return;
	   sub = sub[0];
	   el.addEvent('mouseenter', function(ev){sub.setStyle('display','block');});
	   el.addEvent('mouseleave', function(ev){sub.setStyle('display','none');});
	   
   })


	function checkCerca(form){
		if(form.chiave.value==""){
			StickyWin.alert("Attenzione","Indicare almeno una chiave di ricerca!");
			return false;
		}
		return true;
	}
   
   $("cercaSubmit").addEvent("click", function(){
	   if(checkCerca($("cerca")))
           $("cerca").submit();
    });
   $("cerca").addEvent("submit", function(ev){
	   if(!checkCerca(this))
		   ev.stop();
   });
   $("registrati").addEvent("submit", function(ev){
	   if(this.username.value=="" || this.password.value=="" ||
			(this.username.value=="username" && this.password.value=="password")){
			ev.stop();
		   	StickyWin.alert("Attenzione","Specificare username e password");
	   }
   });
   if ($("RSSubmit")) $("RSSubmit").addEvent("click", function(){
	   if(checkCerca($("ricercaSemplice")))
           $("ricercaSemplice").submit();
    });
   if ($("ricercaSemplice")) $("ricercaSemplice").addEvent("submit", function(ev){
	   if(!checkCerca(this))
		   ev.stop();
   });

   function checkAvanzata(){
		if($("avanzate").cod.value=="" && $("avanzate").codfor.value=="" && $("avanzate").desc.value==""){
			StickyWin.alert("Attenzione","Indicare almeno un parametro di ricerca!");
			return false;
		}
		return true;
	}
  
   if ($("RASubmit")) $("RASubmit").addEvent("click", function(){
	   if(checkAvanzata())
		   $("avanzate").submit();
   });
   if ($("avanzate")) $("avanzate").addEvent("submit", function(ev){
	   if(!checkAvanzata())
		   ev.stop();
   });
   if ($("avanzate")) $("avanzate").addEvent("keydown", function(ev){
	   if( ev.key == 'enter')
		      this.submit();
   });
	
   var visible = null; var active=null;
   $$('.letter').each(function(l){
		l.addEvent('click', function(ev){
			if(active) active.removeClass("active");
			this.addClass("active");
			ev.stop();
			var parent = this.getParent();
			if (parent.getStyle('overflow')=='hidden')
			{
				parent.setStyle('position', 'relative');
				parent.setStyle('height',parent.getCoordinates().height-1);
				parent.setStyle('overflow','visible');
			}
			if (visible) visible.hide();
			var next = this.getNext();
			var coords = this.getCoordinates(this.getParent());
			next.setStyles({
				top: coords.top + coords.height,
				left: coords.left
			});
			next.show();
			active=this;
			visible = next;
		});
   });
   if ($$('.letterSearch').length>0) $$('.letterSearch')[0].addEvent('mouseleave', function(ev){
		if (visible) visible.hide();
		if(active) active.removeClass("active");
   });
   
   
   if ($('infoPopup'))
   {
	   $('sfogliatore').setStyle('display','none');
	   var popup = new CosMooPopup({'rootPath':myRootPath, 'content':$('infoPopup'), 'width':860, 'height':603, 'classPopup':'contPopup benessere'});
	   popup.show();
   }
	   
});
function showSfogliatore()
{
	var cnt = $('infoPopup').getParent();
	$('infoPopup').destroy();
	$('sfogliatore').inject(cnt);
	$('sfogliatore').setStyle('display','block');
	return false;
}
