var Videobox = {

	init: function (options) {
		// init default options
		this.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 || {});

		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.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(document.body);
		this.center = new Element('div').setProperty('id', 'lbCenter').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', 'lbBottomContainer').setStyle('display', 'none').injectInside(document.body);
		this.bottom = new Element('div').setProperty('id', 'lbBottom').injectInside(this.bottomContainer);
		new Element('a').setProperties({id: 'lbCloseLink', href: '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		this.caption = new Element('div').setProperty('id', 'lbCaption').injectInside(this.bottom);
		this.number = new Element('div').setProperty('id', 'lbNumber').injectInside(this.bottom);
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);

		this.overlay.fade('hide');
		this.fx = {
			center: new Fx.Morph(this.center, { duration: 'short', link: 'chain' }),
			bottom: new Fx.Morph(this.bottom)
		};

	},

	click: function(link) {
	
     		return this.open (link.href, link.title, link.rel);

	},
	open: function(sLinkHref, sLinkTitle, sLinkRel) {
		this.href = sLinkHref;
		this.title = sLinkTitle;
		this.rel = sLinkRel;
		this.setup();
		this.video(this.href);
		this.top = Window.getScrollTop() + (Window.getHeight() / 15);
		this.center.setStyles( {top: this.top+'px', display: 'block'});
		this.overlay.fade(0.75);
		this.step = 1;
		this.center.setStyle('background','#fff url(loading.gif) no-repeat center');
		this.caption.innerHTML = this.title;
		this.movie = new Element('div').setProperty('id', 'lbMovie').injectInside(this.center);
		this.fx.center.start({'height': [this.options.contentsHeight]})
		this.fx.center.start({'width': [this.options.contentsWidth], 'marginLeft': [this.options.contentsWidth/-2]});
		this.fx.center.start({chainComplete: this.nextEffect.bind(this)});
	},

	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;

	},

	video: function(sLinkHref){
		if (sLinkHref.match(/youtube\.com\/watch/i)) {
      this.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('=');
			this.videoID = videoId[1];
			this.embedURL = "http://www.youtube.com/v/"+this.videoID;
		}
		else if (sLinkHref.match(/vimeo\.com/i)) {
      this.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('/');
			this.videoID = videoId[3];
			this.embedURL = "http://vimeo.com/moogaloop.swf?clip_id="+this.videoID+"&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&ampcolor=00ADEF&amp;fullscreen=1&amp;autoplay=1&amp;loop=0";
		}
		else if (sLinkHref.match(/metacafe\.com\/watch/i)) {
      this.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('/');
			this.videoID = videoId[4];
			this.embedURL = "http://www.metacafe.com/fplayer/"+this.videoID+"/.swf";
		}
		else if (sLinkHref.match(/google\.com\/videoplay/i)) {
      this.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('=');
			this.videoID = videoId[1];
			this.embedURL = "http://video.google.com/googleplayer.swf?docId="+this.videoID+"&hl=en";
		}
		else if (sLinkHref.match(/ifilm\.com\/video/i)) {
		  this.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('video/');
			this.videoID = videoId[1];
			this.embedURL = "http://www.ifilm.com/efp";
		}
		else if (sLinkHref.match(/flickr\.com\/photos\/\w+\/sets/i)) {
		  this.flash = true;
			var hRef = sLinkHref;
			var videoId = hRef.split('/');
			this.videoID = videoId[6];
			var page_show_url = encodeURIComponent(hRef.split('flickr.com')[1])
			this.embedURL = "http://www.flickr.com/apps/slideshow/show.swf?v=71649&offsite=true&lang=en-us&page_show_url="+page_show_url+"&page_show_back_url="+page_show_url+"&set_id="+this.videoID;
		}
		else {
		  this.flash = true;
			this.videoID = sLinkHref;
			this.embedURL = this.videoID;
		}
	},

	nextEffect: function(){
		this.center.setStyle('background','#fff');
		var params = { bgcolor: "#000", allowScriptAccess:"always", allowFullScreen:"true", wmode:"transparent" };
		swfobject.embedSWF(this.embedURL, 'lbMovie', this.options.contentsWidth+'px', this.options.contentsHeight+'px', '9', false, false, params);
		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){
			this.fx.bottom.set({'margin-top': -this.bottom.offsetHeight});
			this.bottomContainer.style.height = '';
			this.fx.bottom.start({'margin-top': 0});
		} else {
			this.bottomContainer.style.height = '';
		}
	},

	close: function(){
		this.overlay.fade('out');
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.center.innerHTML = '';
		return false;
	}

};


window.addEvent('domready', Videobox.init.bind(Videobox));

