/*
	Mediabox version 0.6.8 - John Einselen (http://iaian7.com)
	updated 15.12.07

	tested in OS X 10.5/10.4 using FireFox 2, Flock 2, Opera 9, Safari 3, and Camino 1.5
	tested in Windows Vista using Internet Explorer 7, FireFox 2, Opera 9, and Safari 3
	loads flash, flv, quicktime, wmv, and html content in a Lightbox-style window effect.

	based on Slimbox version 1.4 - Christophe Beyls (http://www.digitalia.be)
			 Slimbox Extended version 1.3.1 - Yukio Arita (http://homepage.mac.com/yukikun/software/slimbox_ex/)
			 Videobox Mod version 0.1 - Faruk Can 'farkob' Bilir (http://www.gobekdeligi.com/videobox/)
			 DM_Moviebox.js - Ductchmonkey (http://lib.dutchmoney.com/)
			(licensed same as originals, MIT-style)

	inspired by the grandaddy of them all, Lightbox v2 - Lokesh Dhakar (http://www.huddletogether.com/projects/lightbox2/)

	distributed under the MIT license, terms:
	copyright (c) 2007 dutchmoney llc
	
	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/
var Mediabox = {
	init: function(options){
		var wht = (window.getHeight() == 0) ? window.getScrollHeight() : window.getHeight();
		var wwd = (window.getWidth() == 0) ? window.getScrollWidth() : window.getWidth();

		this.options = Object.extend({
			resizeDuration: 500,
			resizeTransition: Fx.Transitions.Sine.easeInOut,
			topDistance: 15,			// Divisor of the total visible window height, higher number = higher Mediabox placement
										// If you wish to change this to an absolute pixel value, scroll down to lines 107 and 108 and swap the commenting slashes one for the other
			initialWidth: '70px',
			initialHeight: '70px',
			initialLeftMargin: '40px',
			defaultWidth: (wwd-10),			// Default width (px) ph was 640
			defaultHeight: (wht-20),			// Default height(px) ph was 360
		// Quicktime and Windows Media settings
			autoplay: 'true',			// Automatically play movie, true / false
			bgcolor: 'blue',			// Background color, name / hex value (plh default was 'black')
			controller: 'true',			// Show controller, true / false
		// Mediaplayer or FLVplayer settings
			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: 'false',		// Display fullscreen button
		// Animation controls
			animateCaption: false		// This is not smooth animation in IE 6 with XML prolog. (plh default true)
										// If your site is XHTML strict with XML prolog, disable this option.
		}, options || {});

		if(window.ie6 && document.compatMode=="BackCompat"){ this.options.animateCaption = false; }	// IE 6 - XML prolog problem

		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').injectInside(document.body);
		this.center = new Element('div').setProperty('id', 'lbCenter').setStyles({top: 0, width: this.options.initialWidth, height: this.options.initialHeight, marginLeft: this.options.initialLeftMargin, display: 'none'}).injectInside(document.body);
		this.canvas = new Element('div').setProperty('id', 'lbImage').injectInside(this.center);
		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', {'id': 'lbCaption'}).injectInside(this.bottom);
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);

		/* Build effects */
		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: this.options.resizeDuration, transition: this.options.resizeTransition, onComplete: nextEffect}).hide(), // from hidden to shown
			center: this.center.effects({duration: this.options.resizeDuration, transition: this.options.resizeTransition, onComplete: nextEffect}), // expand
			content: this.canvas.effect('opacity', {duration: 0, onComplete: nextEffect}), // show content 
			bottom: this.bottomContainer.effect('height', {duration: 0, onComplete: nextEffect}) //expand bottom
		};
		if (0) {  //!Cookie.get('SLTy_SawLogo')) {
			Cookie.set('SLTy_SawLogo','true', {duration: 36*30}); // don't watch again for 36 months
			if (!Cookie.get('SLTy_SawLogo')) {
				alert('Warning! You have cookies disabled, so our logo animation will appear on every page.');
			}
			var myloc = window.location.href.split("/");
			if (myloc[2]=='localhost') {
				myloc = '/k/e107_themes';
			} else {
				myloc = '/SLTy/t';
			}
			this.open(myloc+'/SLTy7/images/logo_animation_overlay.swf' , '', 'mediabox');
		};
	},

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

	open: function(url, title, rel){
		this.href = url;
		this.title = 'Stop and consider... what is Spirit-Led Technology? (To view again, click animated logo on our site.)';
		this.rel = rel;
		this.position();
		this.setup(true);
		var wh = (window.getHeight() == 0) ? window.getScrollHeight() : window.getHeight();
		var st = document.body.scrollTop  || document.documentElement.scrollTop;
//		this.top = st + (wh / this.options.topDistance);	// comment this code out and uncomment the line below to switch to absolute positioning
		this.top = 0;										// this is the code needed for an absolute position (do not add "px", numerical only!) ph switched to this
		this.center.setStyles({top: '50px', display: ''}); // starting point (ph this was this.top+'px')
//		this.center.style.borderColor = 'yellow';
		this.center.className = 'lbLoading';
		this.fx.overlay.start(0.8); // get overlay and zoom going
		this.fx.center.start({ 'height': (this.contentsHeight-4)+'px', 'width': (this.contentsWidth-4)+'px', 'marginLeft': (-this.contentsWidth/2)+'px', 'top': '0'});
		return false; //this.loadVideo(url);
	},

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

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

		var elements = $A(document.getElementsByTagName('object'));								// hide page content
		elements.extend(document.getElementsByTagName(window.ie ? 'select' : 'embed'));
		elements.each(function(el){
			//alert('saving visibility='+el.style.visibility);
			if (open) el.lbBackupStyle = el.style.visibility;
			el.style.visibility = open ? 'hidden' : (el.lbBackupStyle ? el.lbBackupStyle : 'visible'); // avoid error if never set
		});

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

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

	loadVideo: function(url){
	//	this.step = 2; // reset display animation
		var myObject = null;

// YouTube
		if (url.match(/youtube\.com\/watch/i)) {
			this.type = 'flash';
			var videoId = url.split('=');
			this.videoID = videoId[1];
			myObject = new SWFObject("http://www.youtube.com/v/"+this.videoID+"&autoplay=1", "sfwvideo", this.contentsWidth, this.contentsHeight, "9", "#000000", "wmode", "transparent");
// DailyMotion
		} else if (url.match(/dailymotion\.com/i)) {
			this.type = 'flash';
			myObject = new SWFObject(url, "sfwvideo", this.contentsWidth, this.contentsHeight, "9", "#000000");
// Metacafe
		} else if (url.match(/metacafe\.com\/watch/i)) {
			this.type = 'flash';
			var videoId = url.split('/');
			this.videoID = videoId[4];
			myObject = new SWFObject("http://www.metacafe.com/fplayer/"+this.videoID+"/.swf", "sfwvideo", this.contentsWidth, this.contentsHeight, "9", "#000000", "wmode", "transparent");
// Google Video
		} else if (url.match(/google\.com\/videoplay/i)) {
			this.type = 'flash';
			var videoId = url.split('=');
			this.videoID = videoId[1];
			myObject = new SWFObject("http://video.google.com/googleplayer.swf?docId="+this.videoID+"&autoplay=1&hl=en", "sfwvideo", this.contentsWidth, this.contentsHeight, "9", "#000000"); /* , "wmode", "transparent"); */
			myObject.addParam("wmode", "transparent");
// Flash .SWF
		} else if (url.match(/\.swf/i)) {
			this.type = 'flash'; /* plh version was 9 now 8 below */
					
/*			window.addEvent('load', function(){
				myObject = new SWFObject(url, "SLTyMovie", this.contentsWidth, this.contentsHeight, "8", "#ffcccc", "autohigh" ); /* was: "wmode", "transparent"); */
/*				myObject.addParam("wmode", "transparent");
				myObject.addParam("allowScriptAccess", "always");
				myObject.addParam("swliveconnect", "true");
				sTmp = url.split("/");
				sTmp[sTmp.length-1] = 'expressinstall.swf';
				sTmp = sTmp.join("/");
				//alert(sTmp);
				myObject.useExpressInstall(sTmp);
				//myObject.setAttribute('xiRedirectUrl', 'http://www.example.com/upgradefinished.html'); // must be the absolute URL to your site
			});
*/

			var flashvars = {};
			var params = {};
			params.quality = "autohigh";
			params.wmode = "transparent";
			params.bgcolor = "#ffcccc";
			params.swliveconnect = "true";
			params.allowscriptaccess = "always";
			var attributes = {};
			attributes.id = "SLTyMovie";
			sTmp = url.split("/");
			sTmp[sTmp.length-1] = 'expressinstall.swf';
			sTmp = sTmp.join("/");
			swfobject.embedSWF(url, "lbImage", this.contentsWidth, this.contentsHeight, "8.0.0", sTmp, flashvars, params, attributes);

				
// Flash .FLV
		} else if (url.match(/\.flv/i)) {
			this.type = 'flash';
			myObject = 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, "flvvideo", this.contentsWidth, this.contentsHeight, "9", "#000000", "wmode", "transparent");
// Quicktime .MOV
		} else if (url.match(/\.mov/i)) {
			this.type = 'qt';
			if (this.options.controller=='true') {this.contentsHeight = (this.contentsHeight*1)+16};
			if (navigator.plugins && navigator.plugins.length) {
				myObject = '<object id="mediabox" standby="loading quicktime..." type="video/quicktime" codebase="http://www.apple.com/qtactivex/qtplugin.cab" data="'+url+'" width="'+this.contentsWidth+'" height="'+this.contentsHeight+'"><param name="src" value="'+url+'" /><param name="scale" value="aspect" /><param name="controller" value="'+this.options.controller+'" /><param name="autoplay" value="'+this.options.autoplay+'" /><param name="bgcolor" value="'+this.options.bgcolor+'" /><param name="enablejavascript" value="true" /></object>';
			} else {
				myObject = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" standby="loading quicktime..." codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="'+this.contentsWidth+'" height="'+this.contentsHeight+'" id="mediabox"><param name="src" value="'+url+'" /><param name="scale" value="aspect" /><param name="controller" value="'+this.options.controller+'" /><param name="autoplay" value="'+this.options.autoplay+'" /><param name="bgcolor" value="'+this.options.bgcolor+'" /><param name="enablejavascript" value="true" /></object>';
			}
// Windows Media .WMV
		} else if (url.match(/\.wmv/i)) {
			this.type = 'qt';
			if (this.options.controller=='true') {this.contentsHeight = (this.contentsHeight*1)+16};
			if (navigator.plugins && navigator.plugins.length) {
				myObject = '<object id="mediabox" standby="loading windows media..." type="video/x-ms-wmv" data="'+url+'" width="'+this.contentsWidth+'" height="'+this.contentsHeight+'" /><param name="src" value="'+url+'" /><param name="autoStart" value="'+this.options.autoplay+'" /></object>';
			} else {
				myObject = '<object id="mediabox" standby="loading windows media..." classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" type="video/x-ms-wmv" data="'+url+'" width="'+this.contentsWidth+'" height="'+this.contentsHeight+'" /><param name="filename" value="'+url+'" /><param name="showcontrols" value="'+this.options.controller+'"><param name="autoStart" value="'+this.options.autoplay+'" /><param name="stretchToFit" value="true" /></object>';
			}
// iFrame content
		} else {
			this.type = 'iframe';
			this.iframeId = "lbFrame_"+new Date().getTime();	// Safari would not update iframe content that had a static id.
			myObject = new Element('iframe').setProperties({id: this.iframeId, width: this.contentsWidth, height: this.contentsHeight, frameBorder:0, scrolling:'auto', src:url});
		}
		this.object = myObject;

	//	this.nextEffect();
		return false;
	},

	nextEffect: function(url){
		switch (this.step++){
		case 0:
//			this.center.style.borderColor = 'red';
//			this.canvas.style.width = this.bottom.style.width = this.contentsWidth+'px';
//			this.canvas.style.height = this.contentsHeight+'px';
			this.caption.innerHTML = this.title;

//			if (this.center.clientHeight != this.canvas.offsetHeight){
//				this.fx.center.start({ 'height': (this.contentsHeight-4)+'px', 'width': (this.contentsWidth-4)+'px', 'marginLeft': (-this.contentsWidth/2)+'px', 'top': '0'});
//				this.fx.center.start({ 'height': this.canvas.offsetHeight, 'width': this.canvas.offsetWidth, 'marginLeft': -this.canvas.offsetWidth/2, 'top': '0em'});
				break;
//			} else if (this.center.clientWidth != this.canvas.offsetWidth){
//				this.fx.center.start({'height': this.canvas.offsetHeight, 'width': this.canvas.offsetWidth, marginLeft: -this.canvas.offsetWidth/2});
//				break;
//			}
			this.step++;
		case 1:
//			this.center.style.borderColor = 'yellow';
			this.loadVideo(this.href);
	//		break;
			this.step++;
		case 2:
//			this.center.style.borderColor = 'green';
			this.bottomContainer.setStyles({'top': (this.top + this.center.clientHeight)+'px', 'height':'0px', 'marginLeft': this.center.style.marginLeft, 'width':this.center.style.width, 'display': ''});
			//this.fx.content.start(1);
			//break;
			this.step++;

		case 3:
//			this.center.style.borderColor = 'black';
			if (this.type == 'flash'){
				//this.object.write(this.canvas); ph already done in SWFobject 2
			} else if (this.type == 'iframe'){
				this.object.injectInside(this.canvas)
			} else {
				this.canvas.setHTML(this.object);
			}
			this.currentObject = document.getElementById('mediabox');
			this.center.className = '';
	//		break;
			this.step++;

		case 4:
//			this.center.style.borderColor = 'orange';
			if (this.options.animateCaption){
				this.fx.bottom.start(0,this.bottom.offsetHeight);
				break;
			}
			this.bottomContainer.style.height = (this.bottom.offsetHeight)+'px';

		case 5:
			this.step = 0;
		}
	},

	close: function(){
			if (this.type == 'qt' && window.webkit) {
				this.currentObject.Stop();	// safari needs to call Stop() to remove the object's audio stream...
			}
			if (navigator.plugins && navigator.plugins.length) {
				this.canvas.setHTML('');
			} else {
				if (window.ie6) {
//					this.currentObject.style.display = 'none';
					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;

		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;
	}
};

function SLTyMovie_DoFSCommand(command, args) {
  if (command == "close")
  {
    Mediabox.close(); // NOP for now - some browsers close??!!
  }
}
window.addEvent(window.ie ? 'load' : 'domready',Mediabox.init.bind(Mediabox));
//window.addEvent(window.ie ? 'domready' : 'domready', function(){Mediabox.init.bind(Mediabox);});
// moved to theme.php window.addEvent(window.ie ? 'load' : 'domready', Mediabox.init.bind(Mediabox));
