/*
	Inspired by the original
	Lightbox v2 by Lokesh Dhakar. (http://www.lokeshdhakar.com)
	Slimbox by Christophe Beyls (http://www.digitalia.be)
	Mediabox by John Einselen (http://iaian7.com)
	Customised by Mediamaster (http://www.mediamaster.jp/)
*/
var Mediabox = {

	init: function(options){
		this.options = $extend({
			overlayDuration: 400,
			overlayTransition: Fx.Transitions.Sine.easeInOut,
			resizeDuration: 500,
			resizeTransition: Fx.Transitions.Expo.easeOut,
			defaultWidth: 640,			// Default width (px)
			defaultHeight: 360,			// Default height(px)
			playerpath: '/js/mediaplayer.swf',	// Path to the mediaplayer.swf or flvplayer.swf file
			backcolor:  '0x777777',		// Base color for the controller, color name / hex value (0x000000)
			frontcolor: '0x000000',		// Text and button color for the controller, color name / hex value (0x000000)
			lightcolor: '0x000000',		// Rollover color for the controller, color name / hex value (0x000000)
			fullscreen: 'true'		// Display fullscreen button
		}, options || {});

		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^mediabox/i)){
				el.onclick = this.click.pass(el, this);
				this.anchors.push(el);
			}
		}, this);
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);
		this.overlay = new Element('div').setProperty('id', 'lbOverlay').setStyle('display','none').injectInside(document.body);
		this.center = new Element('div').setProperty('id', 'lbCenter').setStyles('display:none').injectInside(document.body);
		this.canvas = new Element('div').setProperty('id', 'lbImage').injectInside(this.center);
		this.bottom = new Element('div').setProperty('id', 'lbBottom').injectInside(this.canvas);
		this.caption = new Element('span', {'id': 'lbCaption'}).injectInside(this.bottom);
		this.closelink = new Element('a').setProperties({id: 'lbCloseLink', href: 'javascript:void(0);'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);

		/* Build effects */
		this.showMode='hidden';
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: this.options.overlayDuration, transition: this.options.overlayTransition, wait:false, onComplete: this.nextEffect.bind(this)}).hide(),
			center: this.center.effects({duration: this.options.resizeDuration, transition: this.options.resizeTransition, wait:false})
		};
	},

	position: function(){
		this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()+'px','display':'block'});
	},

	click: function(link){
		if(this.showMode=='hidden'){
			this.showMode='visible';
			return this.open(link.href, link.title, link.rel);
		}else{
			return false;
		}
	},

	open: function(url, title, rel){
		this.href = url;
		this.title = title;
		this.rel = rel;
		this.setup(true);
		this.position();
		this.fx.overlay.start(0.9);
		this.center.addClass('lbLoading');
		return this.loadVideo(url,title);
	},

	setup: function(open){
// content size settings from rel
		var aDim = this.rel.match(/[0-9]+/g);
		this.contentsWidth = (aDim && (aDim[0] > 0)) ? aDim[0] : this.options.defaultWidth;
		this.contentsHeight = (aDim && (aDim[1] > 0)) ? aDim[1] : this.options.defaultHeight;

// hide flash content
//	if(window.gecko){
		var elements = $A(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(window.ie ? 'select' : 'embed'));
		elements.each(function(el){
			if (open) el.lbBackupStyle = el.style.visibility;
			el.style.visibility = open ? 'hidden' : el.lbBackupStyle;
		});
//	}

// mediabox settings
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;
	},

	keyboardListener: function(event){
		switch (event.keyCode){
			case 27: case 88: case 67: this.close(); break;
		}
	},

	loadVideo: function(url,title){
		this.step = 1;

// YouTube
		if (url.match(/youtube\.com\/watch/i)) {
			this.type = 'flash';
			var videoId = url.split('=');
			this.videoID = videoId[1];
			this.object = new SWFObject("http://www.youtube.com/v/"+this.videoID+"&autoplay=1", "mediaboxswf", this.contentsWidth, this.contentsHeight, "9", "#000000");
			this.object.write(this.canvas);
// Google Video
		} else if (url.match(/google\.com\/videoplay/i)) {
			this.type = 'flash';
			var videoId = url.split('=');
			this.videoID = videoId[1];
			this.object = new SWFObject("http://video.google.com/googleplayer.swf?docId="+this.videoID+"&autoplay=1&hl=en", "mediaboxswf", this.contentsWidth, this.contentsHeight, "9", "#000000");
			this.object.write(this.canvas);
// Flash .SWF
		} else if (url.match(/\.swf/i)) {
			this.type = 'flash';
			this.object = new SWFObject(url, "mediaboxswf", this.contentsWidth, this.contentsHeight, "6", "#000000");
			this.object.write(this.canvas);
// Flash .FLV
		} else if (url.match(/\.flv/i)) {
			this.type = 'flash';
			this.object = new SWFObject(this.options.playerpath+"?file="+url+"&autostart=true&displayheight="+this.contentsHeight+"&usefullscreen="+this.fullscreen+"&backcolor="+this.options.backcolor+"&frontcolor="+this.options.frontcolor+"&lightcolor="+this.options.lightcolor, "mediaboxswf", this.contentsWidth, this.contentsHeight, "9", "#000000");
			this.object.write(this.canvas);
// Image jpg,gif,png
		} else if (url.match(/\.jpg|\.gif|\.png/i)) {
			this.type = 'image';
			this.object = new Element('img',{'src':url, 'alt':title});
			this.object.injectInside(this.canvas);
// Inline content
		} else if (url.match(/\#mb_/i)) {
			this.type = 'inline';
			var Id = url.split('#');
			this.element = Id[1];
			this.innerObject = $(this.element).innerHTML;
			this.object = new Element('div').setProperty('id',this.element).setStyles({
				width: $(this.element).getStyle('width'),
				height: $(this.element).getStyle('height')
			});
			this.object.setHTML(this.innerObject);
			this.object.injectInside(this.canvas);
// Ajax content
		} else if (url.match(/ajax/i)) {
			this.type = 'ajax';
			this.object = new Element('div').setProperty('id','ajax');
			this.ajax = new Ajax(url, {method: 'get',update: this.object}).request();
			this.object.injectInside(this.canvas);
// iFrame content
		} else {
			this.type = 'iframe';
			this.iframeId = "lbFrame_"+new Date().getTime();
			this.object = new Element('iframe').setProperties({id: this.iframeId, width: this.contentsWidth, height: this.contentsHeight, frameBorder:0, scrolling:'auto', src:url});
			this.object.injectInside(this.canvas);
		}
		this.caption.setHTML(this.title);
		return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.top = window.getSize().scroll.y-this.contentsHeight-44;
			this.center.setStyles({top: this.top, display: 'block'});
			if(this.type == 'image'){
			this.canvas.style.width = this.object.offsetWidth+'px';
			this.canvas.style.height = this.object.offsetHeight+'px';
			} else {
			this.canvas.style.width = this.contentsWidth+'px';
			this.canvas.style.height = this.contentsHeight+'px';
			}
			this.center.style.width = this.canvas.offsetWidth+14+'px';
			this.center.style.height = this.canvas.offsetHeight + this.bottom.offsetHeight+14+'px';
			this.center.style.marginLeft = -this.canvas.offsetWidth/2+'px';
			this.step++;

		case 2:
			this.fx.center.start({
				top: window.getHeight()/2+window.getSize().scroll.y-this.center.offsetHeight/2
			});
			this.step++;

		case 3:
			this.center.removeProperty('class');
			window.addEvent('scroll', this.move.bind(this));
			window.addEvent('resize', this.move.bind(this));
			this.step=0;
			break;
		}
	},

	close: function(){
		this.top = window.getSize().scroll.y-this.center.offsetHeight;
		this.center.effect('top',{duration: this.options.resizeDuration, transition: this.options.resizeTransition, wait:false, fps:60, onComplete: this.reset.bind(this)}).start(this.top);
		return false;
	},

	reset: function(){
		if(this.type=="flash"){
			$('mediaboxswf').remove();
		} else {
			this.object.remove();
		}
		this.type = false;
		this.caption.empty();
		this.canvas.removeProperty('style');
		this.center.setStyles('display:none;');
		this.overlay.effect('opacity',{duration: this.options.overlayDuration, transition: this.options.overlayTransition, wait:false, fps:60, onComplete: this.displayreset.bind(this)}).start(0);
		this.setup(false);
		this.showMode='hidden';
		return false;
	},

	move: function() {
	this.top = window.getHeight()/2+window.getSize().scroll.y-this.center.offsetHeight/2;
	if(this.followingMenu)
		this.Mediabox.stop();
		this.Mediabox = this.center.effects({
			duration: 200,
			wait:false
		});
		this.Mediabox.start.delay(100,this.Mediabox, {'top':this.top});
	},

	displayreset: function(){
		this.overlay.setStyles('display:none; visibility:hidden; opacity:0;');
	}

};

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