var Videobox = {

	init: function (options) {
		// init default options
		this.options = Object.extend({
			resizeDuration:500,	// Duration of height and width resizing (ms)
			initialWidth:100,		// Initial width of the box (px)
			initialHeight:100,		// Initial height of the box (px)
			defaultWidth:400,		// Default width of the box (px)
			defaultHeight:320,	// Default height of the box (px)
			animateCaption:true,	// Enable/Disable caption animation
			includeTranscript:true, // Enable/Disable display of transcript
			flvplayer:'/ergo/flash/player.swf'
		}, options || {});

		this.anchors = [];
		$A($$('a')).each(function(el){
			if(el.rel && el.href && el.rel.test('^vidbox', 'i')) {
				el.addEvent('click', function (e) {
          e = new Event(e);
          e.stop();
          this.click(el);
				}.bind(this));
				this.anchors.push(el);
			}
    }, this);
    this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);

		this.overlay = new Element('div').setProperty('id', 'vlbOverlay').injectInside(document.body);
		this.center = new Element('div').setProperty('id', 'vlbCenter').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);

		this.bottomContainer = new Element('div').setProperty('id', 'vlbBottomContainer').setStyle('display', 'none').injectInside(document.body);
		this.bottom = new Element('div').setProperty('id', 'vlbBottom').injectInside(this.bottomContainer);
		this.closeLink = new Element('a').setProperties({id: 'vlbCloseLink', href: '#'}).injectInside(this.bottom);
		this.caption = new Element('div').setProperty('id', 'vlbCaption').injectInside(this.bottom);
		this.number = new Element('div').setProperty('id', 'vlbNumber').injectInside(this.bottom);
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);

		this.overlay.onclick = this.close.bind(this);

		this.transcriptContainer = new Element('div').setProperty('id', 'vlbTranscriptContainer').setStyle('display', 'none').injectInside(document.body);
		this.transcript = new Element('div').setProperty('id', 'vlbTranscript').injectInside(this.transcriptContainer);

		var nextEffect = this.nextEffect.bind(this);
		var displayOverflow = this.displayOverflow.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			center: this.center.effects({duration: 500, transition: Fx.Transitions.sineInOut, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 500}),
			transcript: this.transcript.effect('margin-left', {duration: 500, onComplete: displayOverflow})
		};

		if (window.innerHeight && window.scrollMaxY) // Firefox
    {
		  this.docHeight = window.innerHeight + window.scrollMaxY;
		  this.docWidth  = window.innerWidth + window.scrollMaxX;
	  }
	  else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
	  {
  		this.docHeight = document.body.scrollHeight;
		  this.docWidth  = document.body.scrollWidth;
	  }
	  else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
	  {
  		this.docHeight = document.body.offsetHeight;
		  this.docWidth  = document.body.offsetWidth;
  	}

  	this.overlay.setStyles({'top': "0px", 'height': this.docHeight});

  	this.position();
	},

	click: function(link) {
     		return this.open (link.href, link.title, link.rel, link.rev);
	},

	open: function(sLinkHref, sLinkTitle, sLinkRel, sLinkRev) {
	  this.href = sLinkHref;

	  var version = deconcept.SWFObjectUtil.getPlayerVersion();
		if (document.getElementById && version["major"] > 0)
		{
			majVersion = version['major'];

			if (majVersion < 8)
			{
        //this.center.setStyle('visibility','hidden');
			  alert("You need to upgrade your Flash Player to watch these videos properly.\nGet it at http://adobe.com/products/flashplayer/");
			  this.href = "";
			}
		}
		else
		{
		  alert("You need to install Flash Player to watch these videos.\nYou can download it at http://adobe.com/products/flashplayer/");
		}

		this.title = sLinkTitle;
		this.rel = sLinkRel;
		this.rev = sLinkRev;
		this.position();
		this.setup();
		this.video(this.href);
		this.top = Window.getScrollTop() + (Window.getHeight() / 15);
		this.center.setStyles({top: this.top+'px', display: ''});
		this.fx.overlay.start(0.8);
		this.step = 1;
		this.center.setStyle('background','#fff url(/ergo/js/videobox/loading.gif) no-repeat center');
		this.caption.innerHTML = this.title;
		this.fx.center.start({'height': [this.options.contentsHeight]});
		this.transcript.setHTML('');

    if (window.gecko) this.closeLink.onmouseover  = function () { doTooltip2('', '<p>Close video</p>'); };
		else this.closeLink.onmouseover  = function () { doTooltip2(event, '<p>Close video</p>'); };
		this.closeLink.onmouseout   = function () { hideTip(); };
		this.closeLink.onclick = this.close.bind(this);

		var fn = open ? 'addEvent' : 'removeEvent';
	  document[fn]('keydown', this.eventKeyDown);
	},

	setup: function() {
		var aDim = this.rel.match(/[0-9]+/g);
		this.options.contentsWidth  = (aDim && (aDim[0] > 0)) ? aDim[0] : this.options.defaultWidth;
		this.options.contentsHeight = (aDim && (aDim[1] > 0)) ? aDim[1] : this.options.defaultHeight;
	},

	position: function() {
    this.overlay.setStyles({'top': '0px', 'height': this.docHeight+'px'});
	},

	video: function(sLinkHref){
		if (sLinkHref.match(/\.flv/i)) {
		 this.flash = true;
		 this.so = new SWFObject(this.options.flvplayer+"?file="+sLinkHref, "flvvideo", this.options.contentsWidth, this.options.contentsHeight, "0", "#000");
		}
		else {
		  this.flash = true;
			this.videoID = sLinkHref;
			this.so = new SWFObject(this.videoID, "flvvideo", this.options.contentsWidth, this.options.contentsHeight, "0");
		}
	},

	nextEffect: function() {
		switch (this.step++) {
		case 1:
		  if (this.options.includeTranscript) {
			  this.fx.center.start({'width': [this.options.contentsWidth], 'marginLeft': [0 - this.options.contentsWidth - 12]});
			}
			else {
			  this.fx.center.start({'width': [this.options.contentsWidth], 'marginLeft': [this.options.contentsWidth/-2]});
			}
			break;
			this.step++;
		case 2:
			this.center.setStyle('background','#fff');
			this.flash ? this.so.write(this.center) : this.center.setHTML(this.other);

			if (this.options.includeTranscript) {
			  this.bottomContainer.setStyles({top:(this.top + this.center.clientHeight)+'px', height:'0px', marginLeft:this.center.style.marginLeft, width:this.options.contentsWidth+'px', display:''});
			  this.transcriptContainer.setStyles({top:(this.top)+'px', left:this.bottomContainer.offsetLeft, width:'0px', marginLeft:'436px', height:(this.options.contentsHeight - 0 + this.bottom.offsetHeight + 10)+'px', display:''});
			  this.transcript.setStyles({height:(this.options.contentsHeight - 0 + this.bottom.offsetHeight + 10)+'px', overflow:'hidden'});
			}
			else {
			  this.bottomContainer.setStyles({top: (this.top + this.center.clientHeight)+'px', height: '0px', marginLeft: this.center.style.marginLeft, width: this.options.contentsWidth+'px',display: ''});
			}

			if (this.options.animateCaption) {
				if (window.webkit) this.fx.bottom.set(0);
				else this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
				if (this.options.includeTranscript) {
				  if (window.webkit) this.fx.transcript.set(0);
				  else this.fx.transcript.set(-this.transcript.offsetWidth);
  				this.transcriptContainer.style.width = '';
				  this.fx.transcript.start(0);
				}
				break;
			}
			this.bottomContainer.style.height = '';
			this.step++;
		}
	},

	displayOverflow: function() {
	  this.transcript.setStyles({overflowY:'scroll'});
	  var tmpCaption = eval("vidText"+this.rev);
		this.transcript.setHTML(tmpCaption || '');
	},

	keyboardListener: function(event) {
	  this.close();
		//switch (event.keyCode){
		//	case 27: case 88: case 67: this.close(); break;
		//}
	},

	close: function() {
		this.fx.overlay.start(0);
		this.center.style.display = this.bottomContainer.style.display = this.transcriptContainer.style.display = 'none';
		this.center.innerHTML = '';
		Tooltip.hide();
		return false;
	}

};

window.addEvent('domready', Videobox.init.bind(Videobox));
