if(MooTools.version !== '1.11'){
	Element.implement({
		setOpacity:function(opacity){
			this.set('opacity',opacity);
			return this;
		},
		setHTML:function(html){
            this.set('html',html);
            return this;
        }
	});
}

var OPA = new Class({
	options:{
		name:'opa',
		implementation:'internal',
		sizes:{
			collapsed:{
				ad:0,
				button:{
					width:0,
                    height:0,
                    left:0,
                    top:0
				}
			},
			expanded:{
				ad:0,
				button:{
	                width:0,
	                height:0,
	                left:0,
                    top:0
	            }
			}
		},
		assets:{
			collapsed:{
				type:'flash',
				ad:null,
				button:null,
                clickTag:null,
                buttonClickTag:null
			},
			expanded:{
				type:'flash',
				ad:null,
				button:null,
                clickTag:null,
                buttonClickTag:null
			}
		},
		mode:'collapsed',
		duration:{
			expand:800,
			collapse:400
		},
		autoToggle:{
			onInit:false,
			timeout:5
		},
		player:{
			expand:{
				pause:true
			},
			collapse:{
				resume:true
			}
		},
		cancelOnUserInteraction:true
	},
	timer:null,
	flashVars:null,
	hasPreRoll:false,
	videoPlayer:{
		player:null,
		state:null,
		cachedState:null
	},
	initialize:function(options){
		this.setOptions(options);
		this.createDOM();
		this.state = this.options.mode;
		window.addEvents({
			'videoStateChanged':this.videoStateChanged.bind(this),
			'cancelOPAAutoToggle':this.cancelOPAAutoToggle.bind(this),
			'pauseVideo':this.pauseVideo.bind(this),
			'resumeVideo':this.resumeVideo.bind(this)
		});
		$$('.cancelOPAAutoToggle').addEvent('click',this.cancelOPAAutoToggle.bind(this));
		this.run();
	},
	run:function(){
		if(this.options.autoToggle.onInit && this.options.implementation === 'internal'){
			this.timer = this.toggle.delay(this.options.autoToggle.timeout*1000,this);
		}
	},
	cancelOPAAutoToggle:function(ev){
		if(this.timer && this.options.cancelOnUserInteraction){
            $clear(this.timer);
        }
        // if there's a function on the page called cancelOPAAutoToggle call it.
        // used for 3rd party implementation
        if('cancelOPAAutoToggle' in window){
        	try{
        		cancelOPAAutoToggle();
        	}catch(e){}
        }
	},
	videoStateChanged:function(state){
		if(this.flashVars === null){
            this.flashVars = $('flashVars').get('value').toObject();;
            this.hasPreRoll = this.flashVars.preRollAdURL !== null && this.flashVars.preRollAdURL !== "";
        }
        if(this.videoPlayer.player === null){
        	this.videoPlayer.player = document.getElementById('fpObj');
        }
        if(this.videoPlayer.state !== null){
        	this.videoPlayer.cachedState = this.videoPlayer.state;
        }
        this.videoPlayer.state = state;
	},
	createDOM:function(){
		var content = $('Content');
		var strip = $('HubStrip');
		this.container = $('opaContainer');
		if(strip){
			this.container.inject(strip,'after').removeClass('Hidden');
		} else{
            this.container.inject(content,'before').removeClass('Hidden');
		}
		if(this.options.implementation === 'internal'){
			this.container.setStyle('height',this.options.sizes[this.options.mode]['ad'].toInt());
			this.addCollapsedDOM();
			this.addExpandedDOM();
		}
	},
	addCollapsedDOM:function(){
		this.collapsed = $('opaCollapsed');
		this.collapsed.setStyles({
            height:this.options.sizes.collapsed.ad.toInt(),
            overflow:'hidden',
            position:'absolute',
            top:0,left:0
        }).setOpacity(this.options.mode == 'collapsed' ? 1 : 0).removeClass('Hidden');
		var buttonWrap = new Element('div',{
            styles:{
                width:this.options.sizes.collapsed.button.width.toInt(),
                height:this.options.sizes.collapsed.button.height.toInt(),
                left:this.options.sizes.collapsed.button.left.toInt(),
                top:this.options.sizes.collapsed.button.top.toInt(),
                position:'absolute'
            }
        }).inject(this.collapsed);
        var buttonSwf = new Element('span',{id:'opaCollapsedButtonSwf'}).inject(buttonWrap);
		var button = new Element('span',{
			events:{
				'click':this.expand.bind(this)
			},
			styles:{
				width:this.options.sizes.collapsed.button.width.toInt(),
                height:this.options.sizes.collapsed.button.height.toInt(),
				position:'absolute',
				cursor:'pointer',
				left:0,top:0,
				'z-index':10,
				background:'#fff'
			}
		}).setHTML('&nbsp;').setOpacity('0.01').inject(buttonWrap);
        swfobject.embedSWF(this.options.assets.collapsed.button, 'opaCollapsedButtonSwf', this.options.sizes.collapsed.button.width.toInt(), this.options.sizes.collapsed.button.height.toInt(), "9", false, false, swfobject.defaultParams);
	},
	addExpandedDOM:function(){
		this.expanded = $('opaExpanded');
		this.expanded.setStyles({
            height:this.options.sizes.expanded.ad.toInt(),
            overflow:'hidden',
            position:'absolute',
            top:0,left:0
        }).setOpacity(this.options.mode == 'collapsed' ? 0 : 1).removeClass('Hidden');
        var buttonWrap = new Element('div',{
            styles:{
                width:this.options.sizes.expanded.button.width.toInt(),
                height:this.options.sizes.expanded.button.height.toInt(),
                left:this.options.sizes.expanded.button.left.toInt(),
                top:this.options.sizes.expanded.button.top.toInt(),
                position:'absolute'
            }
        }).inject(this.expanded);
        var buttonSwf = new Element('span',{id:'opaExpandedButtonSwf'}).inject(buttonWrap);
        var button = new Element('div',{
            events:{
                'click':this.collapse.bind(this)
            },
            styles:{
                width:this.options.sizes.expanded.button.width.toInt(),
                height:this.options.sizes.expanded.button.height.toInt(),
                position:'absolute',
                cursor:'pointer',
                left:0,top:0,
                'z-index':10,
                background:'#fff'
            }
        }).setHTML('&nbsp;').setOpacity('0.01').inject(buttonWrap);
        swfobject.embedSWF(this.options.assets.expanded.button, 'opaExpandedButtonSwf', this.options.sizes.expanded.button.width.toInt(), this.options.sizes.expanded.button.height.toInt(), "9", false, false, swfobject.defaultParams);
	},
	initFx:function(duration){
		this.container.set('tween',{
            transition: Fx.Transitions.Sine.easeInOut,
            duration:duration,
            onStart:this.onFxStart.bind(this)
        });
        this.collapsed.set('tween',{
        	duration:duration+200,
            transition: Fx.Transitions.Sine.easeInOut
        });
        this.expanded.set('tween',{
        	duration:duration+200,
            transition: Fx.Transitions.Sine.easeInOut
        });
	},
	toggle:function(){
		this[this.state == 'collapsed' ? 'expand' : 'collapse']();
	},
	expand:function(){
		if(this.state === 'expanded'){return;}
		var from = this.options.sizes.collapsed.ad;
		var to = this.options.sizes.expanded.ad;
		this.initFx(this.options.duration.expand.toInt());
		this.container.tween('height',from,to);
		this.pauseVideo();
		this.sendBeacon(this.options.assets.expanded.buttonClickTag);
		this.state = 'expanded';
	},
	collapse:function(){
		if(this.state === 'collapsed'){return;}
		var from = this.options.sizes.expanded.ad;
        var to = this.options.sizes.collapsed.ad;
        this.initFx(this.options.duration.collapse.toInt());
        this.container.tween('height',from,to);
        this.resumeVideo();
        this.sendBeacon(this.options.assets.collapsed.buttonClickTag);
		this.state = 'collapsed';
	},
	onFxStart:function(){
		if(this.state == 'collapsed'){
			this.collapsed.tween('opacity',1,0);
			this.expanded.tween('opacity',0,1);
		} else {
			this.collapsed.tween('opacity',0,1);
            this.expanded.tween('opacity',1,0);
		}
	},
	sendBeacon:function(url){
		if(!url || url.trim() === ''){return;}
		var id = 'opaClickBeacon';
		var cachedBeacon = $(id);
		if(cachedBeacon){
			cachedBeacon[MooTools.version === '1.11' ? 'remove' : 'dispose']();
		}
		var img = new Element('img',{id:id,src:url}).inject($(document.body));
	},
	pauseVideo:function(){
		if(this.options.player.expand.pause && this.videoPlayer.player !== null){
			try{
	            this.videoPlayer.player.pause();
	        }catch(e){}
		}
	},
	resumeVideo:function(){
		if(this.options.player.collapse.resume && this.videoPlayer !== null && this.videoPlayer.cachedState === 'playing'){
			try{
	            this.videoPlayer.player.resume();
	        }catch(e){}
		}
	}
});

OPA.implement(new Options);

if(MooTools.version === '1.11'){
	OPA.implement({
		initFx:function(duration){
			this.fx = {
				container: new Fx.Style(this.container, 'height', {
					transition: Fx.Transitions.Sine.easeInOut,
	                duration:duration,
	                onStart:this.onFxStart.bind(this)
				}),
				collapsed:new Fx.Style(this.collapsed, 'opacity', {
                    transition: Fx.Transitions.Sine.easeInOut,
                    duration:duration+200
                }),
                expanded:new Fx.Style(this.expanded, 'opacity', {
                    transition: Fx.Transitions.Sine.easeInOut,
                    duration:duration+200
                })
			}
	    },
	    onFxStart:function(){
	        if(this.state == 'collapsed'){
	        	this.fx.collapsed.start(1,0);
	        	this.fx.expanded.start(0,1);
	        } else {
	            this.fx.collapsed.start(0,1);
                this.fx.expanded.start(1,0);
	        }
	    },
	    expand:function(){
	    	console.log(this.state);
	        if(this.state === 'expanded'){return;}
	        var from = this.options.sizes.collapsed.ad;
	        var to = this.options.sizes.expanded.ad;
	        this.initFx(this.options.duration.expand.toInt());
	        this.fx.container.start(from,to);
	        this.state = 'expanded';
	        if(this.options.player.expand.pause && this.videoPlayer.player !== null){
	            try{
	            	this.videoPlayer.player.pause();
	            } catch(e){}
	        }
	        this.sendBeacon(this.options.assets.expanded.buttonClickTag);
	    },
	    collapse:function(){
	    	console.log(this.state);
	        if(this.state === 'collapsed'){return;}
	        var from = this.options.sizes.expanded.ad;
	        var to = this.options.sizes.collapsed.ad;
	        this.initFx(this.options.duration.collapse.toInt());
	        this.fx.container.start(from,to);
	        this.state = 'collapsed';
	        if(this.options.player.collapse.resume && this.videoPlayer !== null && this.videoPlayer.cachedState === 'playing'){
	            try{
	            	this.videoPlayer.player.resume();
	            } catch(e){}
	        }
	        this.sendBeacon(this.options.assets.collapsed.buttonClickTag);
	    }
	});
}