// Fade between elements
//
var Carousel= PeriodicAnimation.makeSubclass();
Carousel.prototype._init= function(elements, each, fade) {
    PeriodicAnimation.prototype._init.call(this, (each+fade)*elements.length);
    this.elements= elements;
    this.fade= fade/(each+fade);
    this.slide= null;
    elements.forEach(function(element) {
        element.style.visibility= 'hidden';
    });
};
Carousel.prototype.set= function(d) {
    d*= this.elements.length;
    var slide0= Math.floor(d);
    d-= slide0;
    d= Math.min(d/this.fade, 1);
    var slide1= (slide0+1)%this.elements.length;
    var style0= this.elements[slide0].style;
    var style1= this.elements[slide1].style;

    if (slide0!==this.slide) {
        if (this.slide!==null)
            this.elements[this.slide].style.visibility= 'hidden';
        this.slide= slide0;
        style0.visibility=style1.visibility= 'visible';
        style0.zIndex= '1';
        style1.zIndex= '2';
        if ('opacity' in style0)
            style0.opacity= '1';
        else
            style0.filter= 'alpha(opacity=100)';
    }
    if ('opacity' in style1)
        style1.opacity= ''+d;
    else
        style1.filter= 'alpha(opacity='+Math.floor(d*100)+')';
};

var clients= Node_getElementsByClassName(document.getElementById('clients'), 'client');
var clientcarousel= new Carousel(Array.fromList(clients), 5000, 1000);
clientcarousel.start();


// Pop-up video window
//
var PopupAnimation= SwitchAnimation.makeSubclass();
PopupAnimation.prototype._init= function(pop, video) {
    this.pop= pop;
    this.video= video;
    SwitchAnimation.prototype._init.call(this, 750, 0);
};
PopupAnimation.prototype.show= function(d) {
    if (d===0) {
        clientcarousel.start();
        if (window.mastthrobber)
            mastthrobber.start();
    }
    if (d===1 && !autostarted) {
        autostarted= true;
        var v= document.getElementById('video-html5');
        if (v && 'play' in v) {
            setTimeout(function() { v.play(); }, 0);
        } else {
            v= document.getElementById('player_api');
            if (v && 'fp_play' in v)
                v.fp_play();
        }
    }
    var w= 'innerWidth' in window? window.innerWidth : document.documentElement.offsetWidth;
    var s= this.pop.style;
    s.zIndex= d===0? '2' : '4';
    s.visibility= d===1? 'hidden' : 'visible';
    document.getElementById('mast-video').style.left= d===1? '0' : '-4096px';
    s.top= -160*d+120+'px';
    s.right= (w-900)/2*d+116*(1-d)+'px';
    s.width= 780*d+120+'px';
    s.height= 556*d+140+'px';
    if ('opacity' in s)
        s.opacity= ''+d;
    else
        s.filter= 'alpha(opacity='+Math.floor(d*100)+')';
};


if (document.getElementById('mast-pop')) {
    var popswitch= new PopupAnimation(document.getElementById('mast-pop'), document.getElementById('mast-video'));
    var autostarted= false;
    document.getElementById('mast-close').onclick= function() {
        popswitch.set(0);
        var v= document.getElementById('video-html5');
        if (v && 'pause' in v) {
            v.pause();
        } else {
            v= document.getElementById('player_api');
            if (v && 'fp_pause' in v)
                v.fp_pause();
        }
    };
    document.getElementById('mast-screen').onclick= function() {
        popswitch.set(1);
        clientcarousel.stop();
        if (window.mastthrobber)
            mastthrobber.stop();
        return false;
    };
}


