function openepaper( mode, id ){

	var ePaper = $(swfobject.getObjectById(id));

	if( !ePaper ) return;
	
	ePaper.teaserWidth = ePaper.teaserWidth ? ePaper.teaserWidth : $(ePaper.parentNode).getWidth();
	ePaper.teaserHeight = ePaper.teaserHeight ? ePaper.teaserHeight : $(ePaper.parentNode).getHeight();

	if( mode == "teaser" && ePaper.viewer ){
		ePaper.viewer.close();
		delete ePaper.viewer;
	}
	
	if( mode == "epaper" ){
		var viewer = ePaper.viewer = new ePaperViewer( ePaper, {});
	}
}

var ePaperViewer = Class.create({
	
	initialize: function( ePaper, params ){

		this.bgcolor = params.bgcolor || "#000";
		this.bgopacity = params.bgopacity || 0.8;
		this.ePaper = ePaper.parentNode;
		this.ePaperApp = ePaper;
		this.root = $(document.getElementsByTagName('body')[0]);
		this.boundResize = this.resize.bind( this );
		this.boundClose = this.externalClose.bind( this );
		this.overlay = new Element( 'div', { id: 'lightboxBg' }).setStyle( {
			backgroundColor: this.bgcolor,
            position: "absolute",
            left: 0,
            top: 0,
			zIndex: 200
		}).setOpacity(this.bgopacity);
		
		this.overlay.observe( 'click', this.boundClose );
		this.root.observe('keyup', this.handleKey.bind(this));
		this.open();
	},
	handleKey: function( e ){
		if (e.keyCode == Event.KEY_ESC ) return this.close();
	},
	open: function(){
		Event.observe( window, 'resize', this.boundResize );
		this.root.insert( this.overlay );
		this.resize();
	},
	close: function( ){
		Event.stopObserving( window, 'resize', this.boundResize );
		
		this.ePaperApp.setStyle({
			width: this.ePaperApp.teaserWidth + "px",
			height: this.ePaperApp.teaserHeight + "px"
		});
		
		this.ePaper.setStyle({
			width: this.ePaperApp.getWidth() + 'px',
			height: this.ePaperApp.getHeight() + 'px',
			top: '0px',
			left: '0px',
			zIndex: 0
		});
		
		this.overlay.stopObserving( 'click', this.boundClose );
		this.root.stopObserving('keyup', this.handleKey.bind(this));
		if( this.overlay.parentNode ) this.overlay.remove();
		delete this.overlay;
	},
	
	externalClose: function(){
		try{
			this.ePaperApp.showTeaser("random value");
		}
		catch(err){
			if( console ) console.log(err);
		}
	},
	
	getDimensions: function(){
	       
 		var w = 0;
		var h = 0;

		if(!window.innerWidth){
			if(!(document.documentElement.clientWidth == 0)){
				w = document.documentElement.clientWidth;
				h = document.documentElement.clientHeight;
			}
			else{
				w = document.body.clientWidth;
				h = document.body.clientHeight;
			}
		}
		else{
			w = window.innerWidth;
			h = window.innerHeight;
		}
		return {width:w,height:h};
		
	},
	
	resize: function(e){
		
		var margin = 15;
		var offset = this.ePaper.cumulativeOffset();
		var dim = document.body.getDimensions();
		var winDim = document.viewport.getDimensions();
		
		var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body
		var offsetLeft=document.all? iebody.scrollLeft : pageXOffset;
		var offsetTop=document.all? iebody.scrollTop : pageYOffset;
		
		this.overlay.setStyle({width:dim.width + "px", height: dim.height + "px"});
		
		if( e ) return;
		
		this.ePaper.setStyle({
			zIndex:201,
			top: ( offsetTop - offset.top + margin ) + "px",
			left: ( offsetLeft - offset.left + margin)  + "px",
			width: (winDim.width - ( margin * 2 )) + "px",
			height: (winDim.height - ( margin * 2)) + "px"
		});
		
		this.ePaperApp.setStyle({
			width: this.ePaper.getWidth() + "px",
			height: this.ePaper.getHeight() + "px"
		})
	}
});

