window.addEvent('domready', function(){

    /*
     *
     * home slide
     *
     */
    try {
        var homeScroll = new Fx.Scroll('home-frame', {
            duration: 1000,
            transition: Fx.Transitions.Quint.easeInOut,
            link: 'chain'
        });
        var buttons = $$('.button');
        var numbers = $$('.number');
        var images = $$('.slide-img');
        var autoplay = new Chain();
        
        function selectItem(btn){
            number = btn.getChildren('span.number');
            target = btn.getProperty('id').replace("btn-", "slide-");
            image = $(btn.getProperty('id').replace("btn-", "img-"));
            homeScroll.toElement(target);
            //fade numeri
            numbers.tween('color', '#eeeeee');
            number.tween('color', '#3a9ccd');
            //fade immagini
            images.tween('opacity', '0');
            image.tween('opacity', 1);
        }
        
        function goHome(){
            image = $("img-kipcast");
            homeScroll.toElement("slide-kipcast");
            //fade numeri
            numbers.tween('color', '#eeeeee');
            //fade immagini
            images.tween('opacity', '0');
            image.tween('opacity', 1);
        }
        
        //inizializzo
        homeScroll.set(0, 0); //(0, 300) per harvest
        images.each(function(el){
            el.setStyle('opacity', 0);
        });
        $('img-kipcast').tween('opacity', 1);
        
        //eventi bottoni
        buttons.each(function(btn){
            btn.addEvent('click', function(){
                autoplay.clearChain(); //blocco autoplay se in esecuzione
                selectItem(btn);
            });
        })
        
        
        //autoplay
        var zero = function(){
            goHome()
        };
        var one = function(){
            selectItem($('btn-harvest'))
        };
        var two = function(){
            selectItem($('btn-process'))
        };
        var three = function(){
            selectItem($('btn-extract'))
        };
        var four = function(){
            selectItem($('btn-publish'))
        };
        var five = function(){
            selectItem($('btn-present'))
        };
        
        autoplay.chain(one);
        autoplay.chain(two);
        autoplay.chain(three);
        autoplay.chain(four);
        autoplay.chain(five);
        autoplay.chain(zero);
        
        
        delay = 5000;
        autoplay.callChain.delay(delay, autoplay); //start one
        autoplay.callChain.delay(delay * 2, autoplay); //two
        autoplay.callChain.delay(delay * 3, autoplay); //three
        autoplay.callChain.delay(delay * 4, autoplay); //four
        autoplay.callChain.delay(delay * 5, autoplay); //five
        autoplay.callChain.delay(delay * 6, autoplay); //zero
    } 
    catch (err) {
    
    }
    
    /*********** links target *****************/
    try {
    
        links = $$('a');
        
        links.each(function(link){
            if (link.getProperty('target')) {
                var target = link.getProperty('target');
                if (target == '_blank') {
                    link.removeProperty('target');
                }
            }
            
            var subs = link.getProperty('href').substring(0, 7);
            if (subs == 'http://') {
                link.setProperty('onclick', 'window.open(this.href); return false;');
            }
            
        });
        
    } 
    catch (err) {
    }
    
    
});
