/*
	Slimbox v1.41 - The ultimate lightweight Lightbox clone
	by Christophe Beyls (http://www.digitalia.be) - MIT-style license.
	Inspired by the original Lightbox v2 by Lokesh Dhakar.
*/


// from http://www.quirksmode.org/viewport/compatibility.html
function getWindowHeight() {
  if (self.innerHeight)  // all except Explorer
      return self.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight)  // Explorer 6 Strict Mode
      return document.documentElement.clientHeight;
  else if (document.body)  // other Explorers
      return document.body.clientHeight;
}

function getScrollTop() {
if (self.pageYOffset)  // all except Explorer
	return self.pageYOffset;
else if (document.documentElement && document.documentElement.scrollTop)  // Explorer 6 Strict
	return document.documentElement.scrollTop;
else if (document.body)  // all other Explorers
	return document.body.scrollTop;
}

var Videobox = {

	init: function(options){
		this.options = $extend({
			resizeDuration: 100,
			resizeTransition: false,	// default transition  // OG Alternativ z.B.:  Fx.Transitions.Back.easeInOut
			initialWidth: 250,
			initialHeight: 250,
			animateCaption: true
		}, options || {});

		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^videobox/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', {'id': 'vbOverlay'}).injectInside(document.body);

		this.center = new Element('div', {'id': 'vbCenter', 'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight, 'marginLeft': -(this.options.initialWidth/2), 'display': 'none'}}).injectInside(document.body);

		this.canvas = new Element('div').setProperty('id', 'vbCanvas').injectInside(this.center);
		
		this.bottomContainer = new Element('div', {'id': 'vbBottomContainer', 'styles': {'display': 'none'}}).injectInside(document.body);
		this.bottom = new Element('div', {'id': 'vbBottom'}).injectInside(this.bottomContainer);
		new Element('a', {'id': 'vbCloseLink', 'href': '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		this.caption = new Element('div', {'id': 'vbCaption'}).injectInside(this.bottom);
        this.descr = new Element('div', {'id': 'vbDescr'}).injectInside(this.bottom);
		new Element('div', {'styles': {'clear': 'both'}}).injectInside(this.bottom);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 200}).hide(),
            center: this.center.effects($extend({duration: this.options.resizeDuration, onComplete: nextEffect}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {})),
			canvas: this.canvas.effect('opacity', {duration: 500, onComplete: nextEffect}),
			bottom: this.bottom.effect('opacity', {duration: 400, onComplete: nextEffect})
		};
	},

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

	open: function(url, title, rel){
		this.href = url;
		this.title = title;
		this.rel = rel;
		this.position();
		this.setup(true);
		this.top = getScrollTop() + 65;
		this.center.setStyles({top: this.top+'px', display: ''});
		this.fx.overlay.start(0.8);    
		return this.loadVideo(url);
	},

	position: function(){
		this.overlay.setStyles({'top': getScrollTop(), 'height': getWindowHeight()});
	},

	setup: function(open){
		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;	

		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;
		});
		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){
		this.step = 1;
		
        this.bottomContainer.style.display = 'none';
        this.fx.canvas.hide();
        this.center.className = 'vbLoading';

        this.type = 'flashobj';
        this.object =  
          '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.contentsWidth + '" height="' + this.contentsHeight + '" id="FlashPlayerId">'+
          ' <param name="movie" value="' + url + '/player.swf" />'+
          ' <param name="play" value="false" />'+
          ' <param name="menu" value="false" />'+
          ' <param name="scale" value="noscale" />'+
          ' <param name="flashvars" value="config=' + url + '/config2.xml" />'+
          ' <!--[if !IE]>-->'+
          ' <object type="application/x-shockwave-flash" data="' + url + '/player.swf" width="' + this.contentsWidth + '" height="' + this.contentsHeight + '">'+
          '  <param name="play" value="false" />'+
          '  <param name="menu" value="false" />'+
          '  <param name="scale" value="noscale" />'+
          '  <param name="flashvars" value="config=' + url + '/config2.xml" />'+
          ' <!--<![endif]-->'+
          '  <a href="http://www.adobe.com/go/getflashplayer">'+
          '   <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" border="0" />'+
          '  </a>'+
          ' <!--[if !IE]>-->'+
          ' </object>'+
          ' <!--<![endif]-->'+
          '</object>';
		  
		this.nextEffect();
		return false;
	},


	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.canvas.style.width = this.bottom.style.width = this.contentsWidth+'px';
			this.canvas.style.height = this.contentsHeight+'px';

 			var title = this.title.split('|');
 			this.caption.setHTML(title[0] || '');
            this.descr.setHTML(title[1] || '');

            if (this.center.clientHeight != this.canvas.offsetHeight){
                this.fx.center.start({height: this.canvas.offsetHeight});
                break;
            }
			this.step++;
		case 2:
            if (this.center.clientWidth != this.canvas.offsetWidth){
                this.fx.center.start({width: this.canvas.offsetWidth, marginLeft: -this.canvas.offsetWidth/2});
                break;
            }
			this.step++;
		case 3:
            this.bottomContainer.setStyles({top: this.top - 55, height: 0, marginLeft: this.center.style.marginLeft, display: ''});
            this.fx.canvas.start(1);
			break;
		case 4:
			this.canvas.setHTML(this.object);
			this.currentObject = document.getElementById('videobox');
			this.center.className = '';
			this.step++; 
		case 5:
			if (this.options.animateCaption){
                this.fx.bottom.hide();
                this.bottomContainer.style.height = '';
				this.fx.bottom.start(1);
				break;
			}
            this.bottomContainer.style.height = '';
		case 6:
			this.step = 0;
		}
	},

	close: function(){
        if (this.type == 'qt' && window.webkit) {
            this.currentObject.Stop(); 
        }
        if (navigator.plugins && navigator.plugins.length) {
            this.canvas.setHTML('');
        } else {
            if (window.ie6) {
                this.canvas.innerHTML = '';
            } else {
                this.canvas.innerHTML = '';
            }
        }
        this.currentObject = null;
        this.currentObject = Class.empty;
        this.type = false;

		if (this.step < 0) return;
		this.step = -1;
		if (this.preload){
			this.preload.onload = Class.empty;
			this.preload = null;
		}
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;
	}
};

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