var Video = new Class({
    Implements:[Options, Events],
    
    options:
    {
        popupId:"popupVideoContainer",
        popupVideoId:"popupVideo"
    },
    
    element:null,
    overlay:null,
    swf:null, 
    
    initialize:function(options)
    {
        if(options != undefined && options != null)
            this.setOptions(options);
    },
    
    reset:function()
    {
        if(this.overlay != null)
        {
            this.overlay.hide();
            this.overlay = null;
        }
        
        if(this.element != null)
        {
            this.element.dispose();
            this.element = null;
        }
    },
    
    set_swf:function(swf)
    {
        this.swf = swf;
    },
    
    popup:function(title, source)
    {
        this.create_overlay();
        this.create_popup(title, source);
        this.show();
    },
    
    create_overlay:function()
    {
        if(this.overlay == null)
        {
            this.overlay = new Overlay(
            {
                backgroundColor:"#005088", 
                transparency:.31
            });
            
            this.overlay.addEvent("shown", function()
            { 
                this.element.removeClass("hidden");
                $(document.body).grab(this.element, "top");
                
                this.center_popup();
                
            }.bind(this));
            
            this.overlay.addEvent("removed", function()
            {
                this.element.dispose();
            }.bind(this));
        }
    },
    
    center_popup:function()
    {
        var popup = this.element;
        var viewport = Window.getViewport();
        var scroll = Window.getScroll();
        var coords = popup.getCoordinates();
        
        popup.setStyles(
        {
            top: viewport.height / 2 - coords.height / 2 + scroll.y, 
            left: viewport.width / 2 - coords.width / 2 + scroll.x
        });
    },
    
    create_popup:function(title, source)
    {
        if(this.element == null)
        {
            this.element = new Element("div", {"id":this.options.popupId, "class":"popup"});
            
            var closeButton = new Element("a", {"class":"close", "title":"Close"});
            closeButton.set("html", "Close");
            closeButton.addEvent("click", function()
            {
                this.close_popup();
            }.bind(this));
            
            var titleH2 = new Element("h2");
            titleH2.set("html", title);
            
            var movie = new Element("div", {"id":this.options.popupVideoId, "class":"video_container"});
            //movie.set("html", source);
            
            this.element.grab(closeButton);
            this.element.grab(titleH2);
            this.element.grab(movie);
            
            this.element.addClass("hidden");
            
            if(this.swf == null){ return; }
            
            var obj = new Swiff(this.swf, {
                id: 'flash_video_player',
                container: movie,
                width: 480,
                height: 268,
                params: {
                    wmode: 'opaque',
                    bgcolor: '#FFFFFF'
                },
                vars: {
                    "xmlfile":source
                    },
                callBacks: {
                }
            });
        }
    }, 
    
    close_popup:function()
    {
        this.element.dispose();
        this.overlay.hide();
    },
    
    show:function()
    {
        this.overlay.show();
    }
});

var videoController;

if(loader != null) loader.add(function()
{
    videoController = new Video();
});


