(function(){


    /**
    * Display youtube effect.
    */
    aura.provide('s100g.effect.youtube');

    var effect = aura.namespace('s100g.effect.youtube');

    effect.process = function(node)
    {

        var o = {
            node: node,
            videoUrl: aura.dom.getAttribute(node, 'videoUrl'),
            videoTitle: aura.dom.getAttribute(node, 'videoTitle')
        };

        o.panel = new YAHOO.widget.Panel("youtubeVideo-popup",{
            width: '435px',
            height: '505px',
            fixedcenter: true,
            close: true,
            draggable: false,
            modal: true,
            visible: false,
            effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5}
        });

        o.panel.setHeader(o.videoTitle);
        o.panel.setBody('Loading...');

        aura.event.on(node, 'click', function(e)
        {
            aura.event.stop(e);
            o.panel.render(aura.body());
            effect.show(o);
        });

        o.panel.beforeHideEvent.subscribe(function(){ o.panel.setBody(''); });

    };

    effect.show = function(o)
    {
        o.panel.setBody('loading...');
        o.panel.show();
        YAHOO.util.Connect.asyncRequest('GET', o.videoUrl, {
            success: function(response)
            {
                o.panel.setBody(response.responseText);
                o.panel.render(aura.body());
            },
            failure: function(response)
            {
                alert('Failure!');
            }
        });
    };


    /*
    effect.bodyStart = '<div class="youtubeVideo-popupBody">';
    effect.bodyStart = '</div>';

    effect.process = function(node)
    {
        var o = {
            node: node,
            videoUrl: aura.dom.getAttribute(node, 'videoUrl'),
            panel: null
        };
        aura.event.on(node, 'click', function(e)
        {
            aura.event.stop(e);
            effect.show(o);
        });
    };

    effect.show = function(o)
    {
        if ( o.panel !== null ) effect.showFinal(o);
        else effect.createPopup(o);
    };

    effect.showFinal = function(o)
    {
        o.panel.show();
    };

    effect.createPopup = function(o)
    {
        o.panel = effect.createPanel(o);
        o.panel.render(aura.body());
        //o.panel.render();
        o.panel.setBody(effect.bodyStart + 'loading...' + effect.bodyEnd);
        effect.showFinal(o);
        YAHOO.util.Connect.asyncRequest('GET', o.videoUrl, {
            success: function(response)
            {
                o.panel.setBody(effect.bodyStart + response.responseText + effect.bodyEnd);
                o.panel.render(aura.body());
            },
            failure: function(response)
            {
                alert('Failure!');
            }
        });
    };


    effect.createPanel = function(o)
    {

        var youtube = new YAHOO.widget.Panel("youtube-video-" + o.videoId,{
            width: '500px',
            height: '500px',
            fixedcenter: true,
            close: true,
            draggable: false,
            modal: true,
            visible: false,
            effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5}
        });

        youtube.setHeader('Video!');
        return youtube;

    };
    */

    /**
    * Register us with the effects manager.
    */
    aura.effect.registerEffect(
        's100g.effect.youtube',
        effect.process
    );


})();
