var mediaBox = new Class({
    
    Implements:[Options,Events],
    
    options: {
        anchorClass : '.mediaBox'
    },
    
    initialize: function(options){
        this.setOptions(options);
        
        this.mediaPlayerWarp = $('mediaPlayerWarp');
        this.mediaBoxLoader = $('mediaBoxLoader');
        this.mediaBoxSize = this.mediaBoxLoader.getSize();
        
        //this.IE6 = (Browser.Engine.trident && Browser.Engine.version <= 4 ? true : false);
        
        $$(this.options.anchorClass).each(function(linkEl) {
            linkEl.addEvent('click',function(e) {
                e.preventDefault();
                this.mediaBoxInjectToDiv(linkEl);
                $$(this.options.anchorClass).removeClass('selected');
                linkEl.addClass('selected');
            }.bind(this));
        },this);
        
        new Asset.images(['/lib/img/ajax-loader-ec8d0b.gif']);
        
    },
    
    mediaBoxInjectToDiv: function(linkEl) {
        
        this.caption_el = $("caption");
        this.mediaCaption = linkEl.getProperty('title');
        this.mediaSrc = linkEl.getProperty('href');
        this.mediaPoster = linkEl.getProperty('poster');
        this.mediaSrcSplit = this.mediaSrc.split('\.');
        this.mediaType = (this.mediaSrcSplit[1].test('mov|mp4|m4v|flv') ? 'movie' : 'image');
        this.mediaPlayer = this.mediaBoxLoader.getFirst('#mediaPlayer');
        this.mediaImg = this.mediaBoxLoader.getFirst('img');
        // this.mediaSize = (linkEl.getProperty('rel') ? linkEl.getProperty('rel').split(',') : [mediaBoxSize.x,mediaBoxSize.y]);
        // this.mediaWidth = mediaSize[0];
        // this.mediaHeight = mediaSize[1];
        
        this.toggleLoader('on');
        
        if(this.mediaType=='image') {
            
            if($("mediaPlayer")) {
                this.mediaPlayerWarp.removeChild($("mediaPlayer"));
            }
            // if($("jwPlayer")) {
            //     this.mediaPlayerWarp.removeChild($("jwPlayer"));
            // }
            
            var img = new Asset.images([this.mediaSrc],{
                onComplete: function() {
                    //(this.mediaImg!=null ? this.mediaImg.set('src',this.mediaSrc) : img.inject(this.mediaBoxLoader));
                    this.mediaImg!=null && this.mediaImg.dispose();
                    
                    if(!this.caption_el) {
                        new Element('div',{
                            id:"caption",
                            html:this.mediaCaption
                        }).inject(this.mediaBoxLoader);
                    } else {
                        this.caption_el.set("html",this.mediaCaption);
                    }
                    
                    img.inject(this.mediaBoxLoader);
                    this.toggleLoader('off');
                }.bind(this)
            },this);
            
        } else {
            
            this.caption_el!=null && this.caption_el.dispose();
            this.mediaImg!=null && this.mediaImg.dispose();
            
            if(!$("mediaPlayer")) {
                var mediaPlayerObj = new Swiff('/lib/swf/NonverBlaster/NonverBlaster.swf?mediaURL='+this.mediaSrc+'&autoPlay=true&buffer=4',{
                    id: 'mediaPlayer',
                    width: this.mediaBoxSize.x,
                    height: this.mediaBoxSize.y,
                    params:{ wMode:'opaque', swLiveConnect:'false', allowfullscreen:'true' }
                },this).inject(this.mediaPlayerWarp);
            }
            
            // if(!$("jwPlayer")) {
            //     var jwPlayerObj = new Swiff('/lib/swf/jwPlayer.swf?file='+this.mediaSrc+'&image='+this.mediaPoster+'&autostart=true&controlbar=over',{
            //         id: 'jwPlayer',
            //         width: this.mediaBoxSize.x,
            //         height: this.mediaBoxSize.y,
            //         params:{ wMode:'opaque', swLiveConnect:'false', allowfullscreen:'true' }
            //     },this).inject(this.mediaPlayerWarp);
            // }
            
            (function() {this.toggleLoader('off')}).bind(this).delay(1000);
        }
        
    },
    
    toggleLoader: function(action) {
        
        if(action=='on') {
            
            if(!$('loader')) {
                var loader = new Element('div', {
                    id: 'loader',
                    styles: {
                        position: 'absolute',
                        top: (this.mediaBoxSize.y/2)-8,
                        left: (this.mediaBoxSize.x/2)-5,
                        width: 16,
                        height: 11,
                        background: 'transparent url(/lib/img/ajax-loader-ec8d0b.gif) no-repeat center'
                    }
                }).inject(this.mediaBoxLoader);
            } else {
                $('loader').setStyle('display','block');
            }
            
        } else {
            
            if($('loader')) {
                $('loader').setStyle('display','none');
            }
            
        }
        
    },
    
    openMediaBox: function(linkEl) {
        
        var winSize = $(window).getSize();
        var viewPortSize = $(window).getScroll();
        var mediaSize = (linkEl.getProperty('rel') ? linkEl.getProperty('rel').split(',') : [480,270]) ;
        var mediaWidth = mediaSize[0];
        var mediaHeight = mediaSize[1];
        
        var mediaBox = new Element('div').inject(document.body).setStyles({
            opacity:0,
            display: 'block',
            position: (this.IE6==true ? 'absolute' : 'fixed'),
            top: 0,
            left: 0,
            width: '100%',
            height: (winSize.y),
            background: '#000',
            'z-index':99999
        }).addEvent('click',function(e) {
            this.set('tween',{
                onComplete: function() {
                    mediaBox.dispose();
                }.bind(this)
            });
            this.fade(0.7,0);
            mediaBox.dispose();
        });
        
        mediaBox.set('tween',{
            onComplete: function() {
                mediaBox.setStyle('opacity',1);
            }.bind(this)
        });
        mediaBox.fade(0,0.7);
        
        var mediaBox = new Element('div').inject(document.body).setStyles({
            opacity:0,
            display: 'block',
            position: (this.IE6==true ? 'absolute' : 'fixed'),
            left: (winSize.x/2)-(mediaWidth/2),
            width: mediaWidth,
            height: mediaHeight,
            top: (winSize.y/2)-(mediaHeight/2),
            background: '#000',
            'z-index':999999
        });
        
        var swfObj = new Swiff(linkEl.getProperty('href'),{
            width: mediaWidth,
            height: mediaHeight,
            params:{ wMode:'opaque', swLiveConnect:'false', allowfullscreen:'true' }
        },this).inject(mediaBox);
        
    }
    
});
