	var Videobox = {

	init: function (options) {
		// init default options
		this.options = Object.extend({
			resizeDuration: 1,	// Duration of height and width resizing (ms)
			initialWidth: 810,		// Initial width of the box (px)
			initialHeight: 610,		// Initial height of the box (px)
			defaultWidth: 810,		// Default width of the box (px)
			defaultHeight: 610,	// Default height of the box (px)
			animateCaption: false,	// 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', top:'0px'}).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').setStyle('height', '1px').injectInside(this.bottomContainer);
		new Element('a').setProperties({id: 'lbCloseLink', href: '#'}).setStyle('height', '1px').injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		this.caption = new Element('div').setProperty('id', 'lbCaption').setStyle('height', '1px').injectInside(this.bottom);
		this.number = new Element('div').setProperty('id', 'lbNumber').setStyle('height', '1px').injectInside(this.bottom);
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 0}).hide(),
			center: this.center.effects({duration: 0, transition: Fx.Transitions.sineInOut, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 0})
		};

	},

	click: function(link) {
		//return this.open (link.href, link.title, link.rel);
		//変更
		return this.open (link.href, link.title, link.rel);

	},
	open: function(sLinkHref) {
		this.href = sLinkHref;
		this.title = '';
		this.rel = 'vidbox';
		this.position();
		this.setup();
		this.video(this.href);
		this.top = Window.getScrollTop() + (Window.getHeight() / 14);
		this.center.setStyles({top: this.top+'px', display: ''});
		
		this.fx.overlay.start(0.8);
		this.step = 1;
		//this.center.setStyle('background','#fff url(loading.gif) no-repeat center');
		//this.caption.innerHTML = this.title;
		this.fx.center.start({'height': [this.options.contentsHeight]});
	},

	setup: function(){
		//var aDim = this.rel.match(/[0-9]+/g);
		this.options.contentsWidth = this.options.defaultWidth;
		this.options.contentsHeight = this.options.defaultHeight;

	},

	position: function(){
		//
    	this.overlay.setStyles({'top': '0px', 'height': this.getPageSize()+'px'});
	},

	video: function(sLinkHref){
		this.flash = true;
		this.videoID = sLinkHref;
		this.so = new SWFObject(this.videoID, "popWinexternal", this.options.contentsWidth, this.options.contentsHeight, "8");
		this.so.addParam("allowScriptAccess", "always");
		//Mac FireFoxはwmode使えないです。
      	if(!navigator.userAgent.match("Fire")){
			this.so.addParam("wmode", "transparent");
      	}
	},
	
    //
    getPageSize: function() {
	        
	     var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		return [pageHeight];
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.fx.center.start({'width': '810px', 'marginLeft': [this.options.contentsWidth/-2]});
			break;
			this.step++;
		case 2:
			this.so.write(this.center);
			this.step++;
		}
	},

	close: function(){
		this.fx.overlay.start(0);
    	this.overlay.setStyles({'top': '0px', 'height': '0px'});
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.center.innerHTML = '';
		btnOn();
		return false;
	}

};


window.addEvent('domready', Videobox.init.bind(Videobox));