var Videobox = {

	init: function (options) {
		// init default options
		Videobox.options = Object.extend({
			resizeDuration: 400,	// Duration of height and width resizing (ms)
			initialWidth: 250,		// Initial width of the box (px)
			initialHeight: 250,		// Initial height of the box (px)
			defaultWidth: 425,		// Default width of the box (px)
			defaultHeight: 350,	// Default height of the box (px)
			animateCaption: true,	// Enable/Disable caption animation
			flvplayer: 'swf/flvplayer.swf'
		}, options || {});

		Videobox.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();
          Videobox.click(el);
				}.bind(Videobox));
				Videobox.anchors.push(el);
			}
    }, Videobox);

		Videobox.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(document.body);
		Videobox.center = new Element('div').setProperty('id', 'lbCenter').setStyles({width: Videobox.options.initialWidth+'px', height: Videobox.options.initialHeight+'px', marginLeft: '-'+(Videobox.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);

		Videobox.bottomContainer = new Element('div').setProperty('id', 'lbBottomContainer').setStyle('display', 'none').injectInside(document.body);
		Videobox.bottom = new Element('div').setProperty('id', 'lbBottom').injectInside(Videobox.bottomContainer);
		new Element('a').setProperties({id: 'lbCloseLink', href: '#'}).injectInside(Videobox.bottom).onclick = Videobox.overlay.onclick = Videobox.close.bind(Videobox);
		Videobox.caption = new Element('div').setProperty('id', 'lbCaption').injectInside(Videobox.bottom);
		Videobox.number = new Element('div').setProperty('id', 'lbNumber').injectInside(Videobox.bottom);
		new Element('div').setStyle('clear', 'both').injectInside(Videobox.bottom);

		var nextEffect = Videobox.nextEffect.bind(Videobox);
		Videobox.fx = {
			overlay: Videobox.overlay.effect('opacity', {duration: 500}).hide(),
			center: Videobox.center.effects({duration: 500, transition: Fx.Transitions.sineInOut, onComplete: nextEffect}),
			bottom: Videobox.bottom.effect('margin-top', {duration: 400})
		};

	},

	click: function(link) {
	
     		return Videobox.open (link.href, link.title, link.rel);

	},
	open: function(sLinkHref, sLinkTitle, sLinkRel) {
		Videobox.href = sLinkHref;
		Videobox.title = sLinkTitle;
		Videobox.rel = sLinkRel;
		Videobox.position();
		Videobox.setup();
		Videobox.video(Videobox.href);
		Videobox.top = Window.getScrollTop() + (Window.getHeight() / 15);
		Videobox.center.setStyles({top: Videobox.top+'px', display: ''});
		Videobox.fx.overlay.start(0.8);
		Videobox.step = 1;
		Videobox.center.setStyle('background','#eaeaea');
		Videobox.caption.innerHTML = Videobox.title;
		Videobox.fx.center.start({'height': [Videobox.options.contentsHeight]});
	},

	setup: function(){
		var aDim = Videobox.rel.match(/[0-9]+/g);
		Videobox.options.contentsWidth = (aDim && (aDim[0] > 0)) ? aDim[0] : Videobox.options.defaultWidth;
		Videobox.options.contentsHeight = (aDim && (aDim[1] > 0)) ? aDim[1] : Videobox.options.defaultHeight;

	},

	position: function(){
    Videobox.overlay.setStyles({'top': window.getScrollTop()+'px', 'height': window.getHeight()+'px'});
	},

	video: function(sLinkHref){
		if (sLinkHref.match(/youtube\.com\/watch/i)) {
      Videobox.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('=');
			Videobox.videoID = videoId[1];
			Videobox.so = new SWFObject("http://www.youtube.com/v/"+Videobox.videoID, "flvvideo", Videobox.options.contentsWidth, Videobox.options.contentsHeight, "0");
			Videobox.so.addParam("wmode", "transparent");
		}
		else if (sLinkHref.match(/metacafe\.com\/watch/i)) {
      Videobox.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('/');
			Videobox.videoID = videoId[4];
			Videobox.so = new SWFObject("http://www.metacafe.com/fplayer/"+Videobox.videoID+"/.swf", "flvvideo", Videobox.options.contentsWidth, Videobox.options.contentsHeight, "0");
			Videobox.so.addParam("wmode", "transparent");
		}
		else if (sLinkHref.match(/google\.com\/videoplay/i)) {
      Videobox.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('=');
			Videobox.videoID = videoId[1];
			Videobox.so = new SWFObject("http://video.google.com/googleplayer.swf?docId="+Videobox.videoID+"&hl=en", "flvvideo", Videobox.options.contentsWidth, Videobox.options.contentsHeight, "0");
			Videobox.so.addParam("wmode", "transparent");
		}
		else if (sLinkHref.match(/ifilm\.com\/video/i)) {
		  Videobox.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('video/');
			Videobox.videoID = videoId[1];
			Videobox.so = new SWFObject("http://www.ifilm.com/efp", "flvvideo", Videobox.options.contentsWidth, Videobox.options.contentsHeight, "0", "#000");
			Videobox.so.addVariable("flvbaseclip", Videobox.videoID+"&");
			Videobox.so.addParam("wmode", "transparent");
		}
		else if (sLinkHref.match(/\.mov/i)) {
		  Videobox.flash = false;
			if (navigator.plugins && navigator.plugins.length) {
          Videobox.other ='<object id="qtboxMovie" type="video/quicktime" codebase="http://www.apple.com/qtactivex/qtplugin.cab" data="'+sLinkHref+'" width="'+Videobox.options.contentsWidth+'" height="'+Videobox.options.contentsHeight+'"><param name="src" value="'+sLinkHref+'" /><param name="scale" value="aspect" /><param name="controller" value="true" /><param name="autoplay" value="true" /><param name="bgcolor" value="#000000" /><param name="enablejavascript" value="true" /></object>';
      } else {
        Videobox.other = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="'+Videobox.options.contentsWidth+'" height="'+Videobox.options.contentsHeight+'" id="qtboxMovie"><param name="src" value="'+sLinkHref+'" /><param name="scale" value="aspect" /><param name="controller" value="true" /><param name="autoplay" value="true" /><param name="bgcolor" value="#000000" /><param name="enablejavascript" value="true" /></object>';
      }
		}
		else if (sLinkHref.match(/\.wmv/i) || sLinkHref.match(/\.asx/i)) {
		Videobox.flash = false;
		 Videobox.other = '<object NAME="Player" WIDTH="'+Videobox.options.contentsWidth+'" HEIGHT="'+Videobox.options.contentsHeight+'" align="left" hspace="0" type="application/x-oleobject" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"><param NAME="URL" VALUE="'+sLinkHref+'"><param><param NAME="AUTOSTART" VALUE="false"></param><param name="showControls" value="true"></param><embed WIDTH="'+Videobox.options.contentsWidth+'" HEIGHT="'+Videobox.options.contentsHeight+'" align="left" hspace="0" SRC="'+sLinkHref+'" TYPE="application/x-oleobject" AUTOSTART="false"></embed></object>'
		}
		else if (sLinkHref.match(/\.flv/i)) {
		 Videobox.flash = true;
		 Videobox.so = new SWFObject(Videobox.options.flvplayer+"?file="+sLinkHref, "flvvideo", Videobox.options.contentsWidth, Videobox.options.contentsHeight, "0", "#000");
		}
		else {
		  Videobox.flash = true;
			Videobox.videoID = sLinkHref;
			Videobox.so = new SWFObject(Videobox.videoID, "flvvideo", Videobox.options.contentsWidth, Videobox.options.contentsHeight, "0");
		}
	},

	nextEffect: function(){
		switch (Videobox.step++){
		case 1:
			Videobox.fx.center.start({'width': [Videobox.options.contentsWidth], 'marginLeft': [Videobox.options.contentsWidth/-2]});
			break;
			Videobox.step++;
		case 2:
			Videobox.center.setStyle('background','#fff');
			Videobox.flash ? Videobox.so.write(Videobox.center) : Videobox.center.setHTML(Videobox.other) ;
			Videobox.bottomContainer.setStyles({top: (Videobox.top + Videobox.center.clientHeight)+'px', height: '0px', marginLeft: Videobox.center.style.marginLeft, width: Videobox.options.contentsWidth+'px',display: ''});
			if (Videobox.options.animateCaption){
				Videobox.fx.bottom.set(-this.bottom.offsetHeight);
				Videobox.bottomContainer.style.height = '';
				Videobox.fx.bottom.start(0);
				break;
			}
			Videobox.bottomContainer.style.height = '';
			Videobox.step++;
		}
	},

	close: function(){
		Videobox.fx.overlay.start(0);
		Videobox.center.style.display = Videobox.bottomContainer.style.display = 'none';
		Videobox.center.innerHTML = '';
		return false;
	}

};


window.addEvent('domready', Videobox.init.bind(Videobox));