
/* Merged Plone Javascript file
 * This file is dynamically assembled from separate parts.
 * Some of these parts have 3rd party licenses or copyright information attached
 * Such information is valid for that section,
 * not for the entire composite file
 * originating files are separated by - filename.js -
 */

/* - js_menu/menu.js - */
Ext.onReady(function(){
	
	if(!Ext.getDom('MainMenu')){
		return;
	}
	
	var activeMenu;
	
	function createMenu(name){
		var el = Ext.get(name+'-link');
		var el2 = Ext.get(name+'-menu');
		var tid = 0, menu, doc = Ext.get(document);//Ext.getDoc();
		//el2.scale(150,20);
		//el2.fadeOut();
		
		var handleOver = function(e, t){
			if(t != el.dom && t != menu.dom && !e.within(el) && !e.within(menu)){
				hideMenu();
			}	
		};
				
		var hideMenu = function(){
			if(menu){
				//el2.fadeOut();
				//el2.scale(150,20,{easing: 'easeOut',duration: .35});
				//el2.slideOut();
				menu.hide();
				el.setStyle('text-decoration', '');
				doc.un('mouseover', handleOver);
				doc.un('mousedown', handleDown);
			}
		}
		
		var handleDown = function(e){
			if(!e.within(menu)){
				hideMenu();
			}
		}
		
		var showMenu = function(){
			clearTimeout(tid);
			tid = 0;
			
			if (!menu) {
				menu = new Ext.Layer({shadow:'sides',hideMode: 'display'}, name+'-menu');
			}
			menu.hideMenu = hideMenu;
				
			menu.el = el;
			if(activeMenu && menu != activeMenu){
				activeMenu.hideMenu();
			}
			activeMenu = menu;
			
			if (!menu.isVisible()) {
				menu.show();
				menu.alignTo(el, 'tl-bl?');
				menu.sync();
				//el.setStyle('text-decoration', 'underline');
				
				doc.on('mouseover', handleOver, null, {buffer:150});
				doc.on('mousedown', handleDown);
				//el2.scale(200,200,{easing: 'easeOut',duration: .35});
				//el2.fadeIn();
				if (!el2) { el2 = Ext.get(menu.id);}
				el2.slideIn('t', {easing: 'easeOut',duration: .8,remove: false,useDisplay: false});
				

			}
		}
		
		el.on('mouseover', function(e){
			if(!tid){
				tid = showMenu.defer(150);				
			}
		});
		
		el.on('mouseout', function(e){
			if(tid && !e.within(el, true)){
				clearTimeout(tid);
				tid = 0;				
			}
		});
	}
	
	createMenu('raccolta');
	createMenu('comunicati');
	createMenu('progetti');
	createMenu('tariffe');
	createMenu('documenti');
	createMenu('contatti');

	// expanders
	Ext.getBody().on('click', function(e, t){
		t = Ext.get(t);
		e.stopEvent();
		
		var bd = t.next('div.expandable-body');
		bd.enableDisplayMode();
		var bdi = bd.first();
		var expanded = bd.isVisible();
		
		if(expanded){
			bd.hide();
		}else{
			bdi.hide();
			bd.show();
			bdi.slideIn('l', {duration:.2, stopFx: true, easing:'easeIn'});	
		}
		
		t.update(!expanded ? 'Hide details' : 'Show details');
			
	}, null, {delegate:'a.expander'});
	
	var gs = Ext.get('gsearch-box');
	if(gs){
		gs.on('focus', function(){
			if(gs.getValue() == 'Search with google'){
				gs.dom.value = '';
				gs.up('div').addClass('gs-active');
			}
		});
		
		gs.on('blur', function(){
			if(gs.getValue() == ''){
				gs.dom.value = 'Search with google';
				gs.up('div').removeClass('gs-active');
			}
		});
		
		if(gs.getValue() == ''){
			gs.dom.value = 'Search with google';
		}
	}
	
    var hd = Ext.get('top');
    if(hd){
        hd.createChild({tag:'a', cls:'extcon', href: 'http://extjs.com/conference/'});
    }
    
});


/* - carusel/carusel.js - */
// What is $(document).ready ? See: http://flowplayer.org/tools/using.html#document_ready
jQuery(function($) {

    var resizeCarousel = function(carousel, scrollable, elems) {
        // Adjust height of the carousel to the max height of the elements.
        var base_height = Math.max.apply(null,
            $(elems).map(function() { return $(this).height() }).get()
        );
        $(elems).height(base_height);
        
        // Re-size .scrollable. Since all .tileItem lements have equal height 
        // by now, we can rely on the first element in the set.
        var scrollable_height = $(elems).eq(0).outerHeight(true);
        $(scrollable).height(scrollable_height);
        
        // Re-size .carousel.
        var outer_height = $(scrollable).outerHeight(true) + $(".navi").outerHeight(true);
        var $carousel = $(carousel);
        if ($carousel.height() < outer_height) {
            // 'resized.carousel' is a custom trigger that 3rd-party code can use for 
            // binding events to the moment when a carousel is resized. 'carousel' namespace 
            // minimizes chances of conflicting with any other custom trigger of the same 'resized'
            // name.
            // In your custom JS code to bind an event to the moment when a carousel has been 
            // completely loaded and resized use something like this:
            // $("#my-special-case .carousel").bind('resized.carousel', function(event, newheight) {
            //     your custom handler for resized.carousel' event
            // });
            // This is helpful if you need to have more than 1 carousel in the same row
            // and want all of them to be the same height - then you bind risizing function to this
            // 'resized.carousel' trigger.
            $carousel.height(outer_height).trigger('resized.carousel', [outer_height]);
        }
    };
    
    $(".toolBar").hide();
    var carousels = $(".carousel");
	var carousel1 = $(".carousel1");
	var carousel2 = $(".carousel2");
	var carousel_box = $(".carousel_box");
    //alert(carousels);
    carousels.each( function(i) {
        var carousel = this;
        var scrollable = $(this).find(".scrollable").eq(0);        
        var elems = $(scrollable).find('.tileItem');
        
        // Set width of all carousel items so they wrap and have correct widths
        scrollable_width = $(scrollable).width();         
        for (i=0; i<elems.length; i++) {   
            $(elems[i]).css( {width: scrollable_width } );
        };
        
        // Use setTimeout here to give other code a chance to bind events.
        // setTimeout 0 causes code to run right after the jQuery load event has finished.
        // setTimeout(function() { resizeCarousel(carousel, scrollable, elems); }, 0);
        setTimeout(function() { resizeCarousel(carousel, scrollable, elems) }, 0);        
        $(scrollable).find("img").load(function(event) {
            // If an image is loaded later we need to resize the whole carousel to fit it          
            resizeCarousel(carousel, scrollable, elems);          
        });            
    })    
    carousel1.each( function(i) {
        var carousel = this;
        var scrollable = $(this).find(".scrollable").eq(0);        
        var elems = $(scrollable).find('.tileItem');
        
        // Set width of all carousel items so they wrap and have correct widths
        scrollable_width = $(scrollable).width();         
        for (i=0; i<elems.length; i++) {   
            $(elems[i]).css( {width: scrollable_width } );
        };
        
        // Use setTimeout here to give other code a chance to bind events.
        // setTimeout 0 causes code to run right after the jQuery load event has finished.
        // setTimeout(function() { resizeCarousel(carousel, scrollable, elems); }, 0);
        setTimeout(function() { resizeCarousel(carousel, scrollable, elems) }, 0);        
        $(scrollable).find("img").load(function(event) {
            // If an image is loaded later we need to resize the whole carousel to fit it          
            resizeCarousel(carousel, scrollable, elems);          
        });            
    })    	
    carousel2.each( function(i) {
        var carousel = this;
        var scrollable = $(this).find(".scrollable").eq(0);        
        var elems = $(scrollable).find('.tileItem');
        
        // Set width of all carousel items so they wrap and have correct widths
        scrollable_width = $(scrollable).width();         
        for (i=0; i<elems.length; i++) {   
            $(elems[i]).css( {width: scrollable_width } );
        };
        
        // Use setTimeout here to give other code a chance to bind events.
        // setTimeout 0 causes code to run right after the jQuery load event has finished.
        // setTimeout(function() { resizeCarousel(carousel, scrollable, elems); }, 0);
        setTimeout(function() { resizeCarousel(carousel, scrollable, elems) }, 0);        
        $(scrollable).find("img").load(function(event) {
            // If an image is loaded later we need to resize the whole carousel to fit it          
            resizeCarousel(carousel, scrollable, elems);          
        });            
    })    

    carousel_box.each( function(i) {
        var carousel = this;
        var scrollable = $(this).find(".scrollable").eq(0);        
        var elems = $(scrollable).find('.tileItem');
        
        // Set width of all carousel items so they wrap and have correct widths
        scrollable_width = $(scrollable).width();         
        for (i=0; i<elems.length; i++) {   
            $(elems[i]).css( {width: scrollable_width } );
        };
        
        // Use setTimeout here to give other code a chance to bind events.
        // setTimeout 0 causes code to run right after the jQuery load event has finished.
        // setTimeout(function() { resizeCarousel(carousel, scrollable, elems); }, 0);
        setTimeout(function() { resizeCarousel(carousel, scrollable, elems) }, 0);        
        $(scrollable).find("img").load(function(event) {
            // If an image is loaded later we need to resize the whole carousel to fit it          
            resizeCarousel(carousel, scrollable, elems);          
        });            
    })    

    
    // doesn't make sense to enable autoscrolling if more than one 
    // carousel is on a page - this just distracts and annoys
    var ap = (carousels.length == 1) ? true : false;  
    
    // initialize scrollable 
    var api = $("div.scrollable").scrollable({
        size: 1,  
        clickable: false,      
        loop: true
    }).circular().autoscroll({autoplay: ap,steps:1,interval:15000}).navigator({api:true});
      
    // Show toolBar when hovering over a carousel
    $(".carousel").hover(
        function(){
            $(this).find(".toolBar").eq(0).slideToggle('fast').show();
        },
        function(){
            $(this).find(".toolBar").eq(0).slideToggle('fast').hide();
        }
    );
})

