window.addEvent('domready', function() {

    // Titolo del container che appare //////////////////////////////////////////////////////////////////////////////////////

    var hTitle = $$('.hTitle');

    hTitle.setStyle('width', 0);
    hTitle.set('tween', {duration: '800'});
    hTitle.tween('width', 194);
    hTitle.setStyle('opacity', 0.8);

    // Barre rosse nella pagina dei contatti //////////////////////////////////////////////////////////////////////////////////////

    var contattiOne = $$('.contattiTitleOne');
    var contattiTwo = $$('.contattiTitleTwo');
    var contattiThree = $$('.contattiTitleThree');

    (function(){
	    contattiOne.setStyle('width', 0);
	    contattiOne.tween('width', 580);
    }).delay(100);

    (function(){
    contattiTwo.setStyle('width', 0);
    contattiTwo.tween('width', 580);
    }).delay(300);

    (function(){
    contattiThree.setStyle('width', 0);
    contattiThree.tween('width', 580);
    }).delay(500);

    // Igrandimento imamgini slider /////////////////////////////////////////////////////////////////////////////////////////

    var imgwidth = 140;
    var imgheight= 90;
    var offwidth = 40;
    var offheight= 25;
    var imgs = $$('#sliderz a');
    imgs.each(function(el, index){
        el.addEvent('mouseenter', function(){    
            $$('.gallerythumb').destroy();
            var a = el.clone();
            a.className = 'gallerythumb';
            var i = new Element('img');
            var src = a.get('href').substring(a.get('href').indexOf('/clientfiles'));
            i.set('src', '/Foto.aspx?fname=' + src + '&w=' + imgwidth + '&h=' + imgheight);
            a.empty();
            i.inject(a);
            a.addEvent('mouseleave', function(){ $$('.gallerythumb').destroy(); });
            a.addEvent('click', function(e){ e.stop(); Milkbox.openMilkbox(Milkbox.galleries[0], index); });
            a.inject(document.body);
            var pos = el.getPosition(document.body);
            a.setStyles({
                'top': pos.y - offheight,
                'left': pos.x - offwidth - $('slide').getScroll().x
            });
        });
    });

    // Slider in Home Page //////////////////////////////////////////////////////////////////////////////////////////////////

    var kwicks = $$("#kwicks .kwick");
    if (kwicks.length > 0){
    	
        var szNormal = 237, szSmall  = 0, szFull   = 711;

        var fx = new Fx.Elements(kwicks, {wait: false, duration: 300, transition: Fx.Transitions.Cubic.easeOut});
        kwicks.each(function(kwick, i) {
	        kwick.addEvent("mouseenter", function(event) {
		        var o = {};
		        o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
		        kwicks.each(function(other, j) {
			        if(i != j) {
				        var w = other.getStyle("width").toInt();
				        if(w != szSmall) o[j] = {width: [w, szSmall]};
			        }
		        });
		        fx.start(o);
	        });
        });
         
        $("kwicks").addEvent("mouseleave", function(event) {
	        var o = {};
	        kwicks.each(function(kwick, i) {
		        o[i] = {width: [kwick.getStyle("width").toInt(), szNormal]}
	        });
	        fx.start(o);
        })

    //Chiusura IF (se trova la lista, altrimenti non la esegue)
    }


    // Sottomenu Animato ///////////////////////////////////////////////////////////////////////////////////////////////////

    $$('.subbutton').addEvents({
	    'mouseenter': function(){
		    var childs = this.getChildren('div');
		    this.set('tween', {
			    duration: 500,
			    transition: Fx.Transitions.Cubic.easeOut 
		    }).tween('height', childs[0].getStyle('height').toInt()+45);
	    },
	    'mouseleave': function(){
		    this.set('tween', {}).tween('height', '25px');
	    }
    });

    if ($('slide')) {
        var scroll = new Scroller('slide', { area: 20, velocity: 1});
        $('slide').addEvent ('mouseover', scroll.start.bind(scroll));
        $('slide').addEvent ('mouseout', scroll.stop.bind(scroll));
    }

  // Box News Con Timer ///////////////////////////////////////////////////////////////////////////////////////////////////
    
        // FADE NEWS
    var newsBox = $$('#newsEvidenceBox ul li');
    if (newsBox.length > 1) {
        var mouseOverNews = false;
        var currNews = 0;
        
        newsBox.addEvent('mouseenter', function() {
            mouseOverNews = true;
        }).addEvent('mouseleave', function() {
            mouseOverNews = false;
        });
        
        newsBox.set('tween', { duration: 1000 });
        newsBox.setStyles({'opacity': 0, 'position': 'absolute'});
        newsBox[0].setStyle('opacity', 1);

        var switchNews = function(){
            if (mouseOverNews) { return; }
            var nextNews = (currNews + 1) % newsBox.length;
            newsBox[currNews].tween('opacity', 0);
            newsBox[nextNews].tween('opacity', 1);
            currNews = nextNews;
        }
        
        switchNews.periodical(4000);
    }

// Chiusura del DOMREADY
});
