// NOSE Screencast Lightbox
// Copyright: NOSE AG Design Intelligence 2009
// Author: Sebastian Wohlrab
// Requires MooTools 1.2 and SWFObject 2.1

var moviepre = '/screencast/vid/';
var imagepre = '/screencast/img/';
var playerpre = '/screencast/flash/';

var argstr = playerpre + "player.swf";
var atts = {};

window.addEvent('domready', function() {
    $$('.screencast').each(function(element) {
        element.addEvent('click', function(e) {
            e.stop();
            injectLightbox( $(element).get('id') );
        });
    });
});

function injectLightbox(contentdata) {
    var lightboxBack = new Element('div', {'id' : 'lightboxBack'}).inject( $('container') );
    var flashBox = new Element('div', {'id' : 'flashBox'}).inject( $('container') );
    var flashPlayer = new Element('div', {'id' : 'flashPlayer'} ).inject( flashBox );
    
    var closeimg = 'close.png';
    var closeimgie = 'close.gif';

    if (Browser.Engine.trident == true && Browser.Engine.version <= 4) {
        var scrollsize = $('container').getScrollSize();
        
        $('lightboxBack').set('styles',
        {
            'position' : 'absolute',
            'padding-top' : scrollsize.y,
            'height' : '0',
            'line-height' : '0'
        });
        closeimg = closeimgie;
    }

    var close = new Element('a', {'id' : 'close', 'href' : '#'}).inject('flashBox');
    var closeimg = new Element('img', {'src' : imagepre + closeimg, 'alt' : 'close'}).inject(close);
    
    close.addEvent('click', function(e) {
        e.stop();
        fadeIt($('lightboxBack'), 'out', 0.8);
        fadeIt($('flashBox'), 'out', 1);
    });

    lightboxBack.setStyles({ 'opacity' : 0, 'visibility' : 'hidden' });
    flashBox.setStyles({ 'opacity' : 0, 'visibility' : 'hidden' });

    var params = {
        allowfullscreen: 'true',
        allowscriptaccess: 'always',
        flashvars: '&file=' + moviepre + contentdata + '.flv&controlbar=over&stretching=exactfit&image=' + imagepre + contentdata + '.png'
    };

    fadeIt(lightboxBack, 'in', 0.8);
    fadeInWithFlash(flashBox, params);
}
function fadeInWithFlash(element, params) {
    var tween = new Fx.Tween(element);
    tween.start('opacity', 0, 1);
    tween.start('filter', 'alpha(opacity=0)', 'alpha(opacity=1)');
    tween.addEvent('complete', function(e) {
        swfobject.embedSWF( argstr , "flashPlayer", "640px" , "480px" , "8" , null , null , params , atts );
    });
}
function fadeIt(element, direction, weight) {
    var tween = new Fx.Tween(element);
    switch(direction) {
        case 'in':
            tween.start('opacity', 0, weight);
            tween.start('filter', 'alpha(opacity=0)', 'alpha(opacity='+ weight +')');
        break;

        case 'out':
            tween.start('opacity', weight, 0);
            tween.start('filter', 'alpha(opacity='+ weight +')', 'alpha(opacity=0)');
            tween.addEvent('complete', function(e) { 
                element.destroy();
            });
        break;
    }
}
