var videoPlayer = Class.create({

    initialize: function()
    {
        this.positionx = 0;
        this.positiony = 0;
        this.aufloesung = [];
        this.visible = false;
        this.defaultStartImage = 'medien/videos/video.jpg';
        this.div = null;
        this.documentContainer = null;

        Event.observe(document,'dom:loaded', this.getVideoframe.bindAsEventListener(this));
    },

    createDOM: function() {

        if ($('videoplayer_video-container')) {
            return false;
        }

        var video = new Element('div', {id: 'videoplayer_video'});

        var closeButton = new Element ('a', {id: 'videoplayer_videoclosebutton',
               alt: 'Videoanzeige ausblenden (ESC)',
               href: 'javascript: vp.closeVideoframe();'
             }
        ).insert(
            new Element ('img', {src: 'img/videoframe/xbutton.png'})
        );

        var wood = new Element('div', {id: 'videoplayer_video-wood'});
        var holder =  new Element('div', {id: 'videoplayer_video-container', style: 'display:none'}).insert(video);
       
        holder.insert(closeButton);
        holder.insert(wood);

        var abdunkeln = new Element('div', {id: 'videoplayer_abdunkeln', style: 'display: none'});

        var domNode = new Element('div', {id : 'videoplayer_video-holder'}).insert(holder);
        

        document.body.appendChild(domNode);
        document.body.appendChild(abdunkeln);

        this.div = $('videoplayer_video-container');
        this.documentContainer = $('container');

    },

     closeVideoframe : function(){

        $("videoplayer_video").SetVariable("player:jsPause", "");

        //this.div.hide();
        Event.stopObserving(document, 'keyup');

        $('videoplayer_video').hide();
        new Effect.Fade(this.div);
        new Effect.SlideUp(this.div, {
            scaleFrom: this.aufloesung[1]/this.div.getDimensions().height * 100
        });
        new Effect.Fade($('videoplayer_abdunkeln'));

        this.visible = false;


    },

    getVideoframe : function() {

        this.createDOM();
        
        var atags = document.getElementsByTagName('a');

        for(x=0; x < atags.length; x++) {
            if(Element.readAttribute(atags[x], 'rel')) {
                if(Element.readAttribute(atags[x], 'rel').indexOf('videoplayer') != -1) {
                    //Event.observe(atags[x], 'click', this.showVideoframe.bindAsEventListener(this, atags[x].href, atags[x].rel));
                    atags[x].href = 'javascript:vp.showVideoframe("'+atags[x].href+'", "'+atags[x].rel+'");';
                }
            }
        }
    },

    showPlayer : function(href, rel, startimage) {

        // Scriptaculous Blind Down
        /* new Effect.Parallel([ */
            new Effect.BlindDown(this.div, {
                scaleTo: this.aufloesung[1]/Element.getDimensions(this.div).height * 100,
                duration: 0.7
            });
            new Effect.Move(this.div, {
                x: this.positionx ,
                y: this.positiony ,
                mode : 'absolute',
                duration: 3
            });
        /*], {
             duration: 2
        }); */

        Effect.Appear($('videoplayer_abdunkeln'),{delay: 0.3, duration: 3.0, from: 0, to: 0.8})
        $('videoplayer_abdunkeln').style.height = Element.getHeight(this.documentContainer) + 'px';
        Event.observe(window, 'scroll', this.repositiononscrolling.bindAsEventListener(this));
        
        this.visible = true;

        // Close auf ESCAPE Taste

        Event.observe(document, 'keyup', function(e) {
            if(e.which) {
                if (e.which == 27) {
                    this.closeVideoframe();
                }
            } else {
                if (e.keyCode) {
                    if (e.keyCode == 27) {
                        this.closeVideoframe();
                    }
                }
            }
        }.bind(this));
    },

    repositiononscrolling : function() {

        if(this.visible = true) {
            var size = document.viewport.getDimensions();
            var offset = document.viewport.getScrollOffsets();

            this.positiony = offset.top + (size.height/2)-(this.div.getDimensions().height/2);
            this.positionx = size.width/2-this.aufloesung[0]/2;

            new Effect.Move(this.div, {x: this.positionx , y: this.positiony , mode : 'absolute'} );
        }
    },

    getStartImage : function(href, rel) {

        var vpClass = this;
        new Ajax.Request('php/getVideoImage.php?v='+href, {
            onSuccess: function(response){
                if (response.responseText != '') {
                    vpClass.showVideoWithImage(href, rel, response.responseText);
                    return;
                }
                vpClass.showVideoWithImage(href, rel, vpClass.defaultStartImage);
            },
            onException: function() {
                vpClass.showVideoWithImage(href, rel, vpClass.defaultStartImage);
            }
        });
    },

    showVideoframe : function(href, rel) {
        
        this.getStartImage(href, rel);

    },

    showVideoWithImage : function(href, rel, startimage) {

        // Format ermitteln
        var aufloesungen = {
            '4:3' : '320,240',
            '16:9' : '425,240',
            '16:10' : '384,240'
        }

        var seitenverhaeltniss = '4:3';
        if(rel.indexOf('[') != -1) {
            seitenverhaeltniss = rel.substring(rel.indexOf('[')+1,rel.indexOf(']'));
        }

        if(seitenverhaeltniss.indexOf(':') != -1) {
            for(x in aufloesungen) {
                if(x == seitenverhaeltniss) {
                    seitenverhaeltniss = aufloesungen[x];
                }
            }
        }

        this.aufloesung = seitenverhaeltniss.split(',');

        // Div Groesse anpassen
        this.div.style.width = this.aufloesung[0] + 'px';
        this.div.style.height = this.aufloesung[1] + 'px';
        this.div.style.top = 0 + 'px';
        this.div.style.marginTop = 0;
        this.div.style.marginLeft = 0;
        

        // Viewport ermitteln
        var size = document.viewport.getDimensions();
        var offset = document.viewport.getScrollOffsets();

        this.positionx = size.width/2-this.aufloesung[0]/2;
        this.positiony = offset.top + (size.height/2)-(this.div.getDimensions().height/2);

        this.div.style.left = this.positionx + "px";

        this.showPlayer(href, rel, startimage);

        // Auto Play nach ladevorgang
        window.setTimeout(function() {
            this.embedSWF(href, startimage);
        }.bind(this, href, startimage), 3000);

        return false;
    },

    embedSWF: function(href,startimage) {

      // Embed SWF
        var flashvars = {

            // TinyMce Player
            
            flv: href,
            showScaleModes: true,
            startTime: 0,
            scaleMode: 'none',
            hiddenGui: true,
            startimage: startimage,
            bgcolor: 'E6E6E6',
            bgcolor1:'E6E6E6',
            bgcolor2:'E6E6E6',
            showstop:'1',
            showvolume:'1',
            showtime:'1',
            showplayer: 'autohide',
            showfullscreen:'1',
            autoplay: '1'
            
        };
        var params = {
            menu: 'false',
            allowFullScreen: 'true',
            wmode: 'opaque'
        };
        var attributes = {
            id: 'videoplayer_video',
            name: 'videoplayer_video',
            wmode: 'opaque'
        };

        swfobject.embedSWF("swf/videoplayer.swf?"+new Date().getTime(), "videoplayer_video", this.aufloesung[0], this.aufloesung[1], "9.0.0","swf/expressInstall.swf", flashvars, params, attributes,showit);
        swfobject.createCSS("#videoplayer_video","outline:none;");
        
    },

    debug: function(obj)
    {
         for (x in obj) {
            if (typeof(obj[x]) != "function") {
                alert(x + ':' + obj[x]);
            }
        }
    }
});


function showit(e) {

    //$('videoplayer_video').hide();
    //new Effect.Appear($('videoplayer_video'),{duration: 1});
}
var vp = new videoPlayer();
