// Common behaviours for all pages in the site

(function($) {

  // get a fade duration based on the browser (IE has redraw issues with, but fades look great in other browsers!) 
  $.fadeDuration = function(duration) {
    return ($.browser.msie && $.browser.version < 8) ? 0 : duration || "fast";
  };
  
	// code in here can safely use $, knowing that it will reference jQuery and not Prototype


	$(document).ready( function() {
		
		// this replaces the old "handleEnter" function 
		$('#txtRicerca').keypress( function(event) {
	
			var field = event.target;
			var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
			
			if (keyCode == 13) {
			
				var i;
				
				for (i = 0; i < field.form.elements.length; i++)
					if (field == field.form.elements[i])
						break;
				
				i = (i + 1) % field.form.elements.length;
				field.form.elements[i].focus();
				event.preventDefault();
			
			} else {
				return true;
			}
			
		});
		
		
		// addbookmark replacement (footer)
		
		$('#linkAddBookmark').click( function(event) {

      if (window.sidebar) {
          // versione FF
          window.sidebar.addPanel(document.title, location.href,'');
      } else if( window.external ) {
          // versione IE
          window.external.AddFavorite( location.href, document.title); 
      }

		  event.preventDefault();
	  });
		
		
		// set up navigation
		$("#nav>li").hover(
    	function(){jQuery("ul",this).stop(true, true).slideDown("fast");},
    	function(){jQuery("ul",this).stop(true, true).slideUp("fast");}
  	);

		// setup any slideshows
		$('.slideshow')
		  .slideshow()
		  .find(".carousel").carouselPause();
	
		// replace the <select> at the top with a rich version
		jQuery('#myselectbox').selectbox();

    // setup "window" links to "window.open" the link instead
    
    $('a.windowopen').windowopen();
    
    
    // setup show form box behaviours
    $('.formBox').click( function(event) {
      
      var c = $(this).metadata().c;
      var totaleVideo = $(this).metadata().totaleVideo;
      
      for (i=0; i<totaleVideo; i++) {
      	$('#boxVideo-'+i).hide();
    	}
    	
    	$('#boxVideo-'+c).show();
    	
    	event.preventDefault();
    });
       

    // setup login form behaviours (removed "onclick" attributes)
    $('#linkAccedi').click( function(event) { 
      if ($('#theFormLogin').is(":visible")) {
        $('#theFormLogin').fadeOut($.fadeDuration()); 
      } else {
        $('#theFormRegistrazione').fadeOut($.fadeDuration());
        $('#theFormLogin').fadeIn($.fadeDuration()); 
      }
      
      event.preventDefault();
    });
    
    $('#linkHideLogin').click( function(event) {
      $('#theFormLogin').fadeOut($.fadeDuration()); 
      event.preventDefault();  
    });
    
    $('#linkRegistrati').click( function(event) {
      if ($('#theFormRegistrazione').is(":visible")) {
        $('#theFormRegistrazione').fadeOut($.fadeDuration());
      }
      else {
        $('#theFormRegistrazione').fadeIn($.fadeDuration());
        $('#theFormLogin').fadeOut($.fadeDuration()); 
      }
      
      event.preventDefault();  
    });

    $('#linkHideRegistration').click( function(event) {
      $('#theFormRegistrazione').fadeOut($.fadeDuration());
    });    

	});
	
	$(window).load( function() {
		$('.slideshow')
		  .find(".carousel").carouselPlay(6000);
  });
	
})(jQuery);