/*
Script: mp.js
	Contains <MediaPlayer>

Author:
	Alan Roemen

Class: MediaPlayer
	A simple javascript lightbox for global media player
	http://www.longtailvideo.com/players/jw-flv-player/

Options:
	baseURL: Base directory for script. Default: false
	scriptName: Name of javascript file. Default: 'slidingtabs'
	cssFile: Name of css file. Set to name of CSS file or false to use none. Default: 'audio_clips',
	className: CSS class name for script. Set to false for no CSS. Default: 'SlidingTabs'
	lightboxRel: Default value for REL attribute. Default: 'lightbox'
	initialWidth: Initial width of container. Default: '10px'
	initialHeight: Initial height of container. Default: '10px'
	movieWidth: Set width of flash videos. Default: 380
	movieHeight: Set height of flash videos. Default: 400
	screenMin: Minimum size allowed for screen. Set false to disable. Default: { x: '450', y: '450' }
	showEffect: options for effect used to animate the sliding, see Fx.Base in mootools docs. Default: duration: 300 // half a second
	hideEffect: options for effect used to animate the sliding, see Fx.Base in mootools docs. Default: duration: 300 // half a second
	showClose: Shows the close button for the container. Default: true
	backgroundClose: Close container when background is clicked. Default: false
	overlay: Display background overlay. Default: true
	login_page: Set to name of CSS & PHP file used or set to false to not require login. Default: 'default'
*/

var MediaPlayer = new Class({
	options: {
		baseURL: false,
		scriptName: 'mp',
		cssFile: 'clips',
		className: 'MediaPlayer',
		lightboxRel: 'lightbox',
		initialWidth: '10px',
		initialHeight: '10px',
		setWidth: 380,
		setHeight: 400,
		screenMin: { x: '450', y: '450' },
		showEffect: {	duration: 300	},
		hideEffect: {	duration: 300	},
		showClose: true,
		backgroundClose: false,
		overlay: true,
		type: 'swf',
		login_page: 'default',
		flash_vars: { autostart: 'true' },
		closeOffset: { x: 0, y: 0 }
	},

	initialize: function(links, options) {
		this.setOptions(options);
		links = links || this.options.lightboxRel;
		this.links = document.getElements('a[rel^='+links+']');
		if(!this.links) return;
		
		// Setup Flash Vars
		var temp = '';
		for(var key in this.options.flash_vars)
			temp = temp + '&' + key + '=' + this.options.flash_vars[key];
		this.options.flash_vars = temp;
		
		//IE Problems
		if(window.ie6){
			this.options.showClose = false;
			this.options.backgroundClose = true;
			this.options.overlay = true;
		}
		
		// Get script base path
		if(!this.options.baseURL) {
			var elements = document.getElementsByTagName('script');
			for (var i=0; i<elements.length; i++) {
				if (elements[i].src && (elements[i].src.indexOf(this.options.scriptName+'.js') != -1)) {
					var src = elements[i].src;
					this.options.baseURL = src.substring(0, src.lastIndexOf('/'));
					break;
				}
			}
			// Get document base path
			this.documentBasePath = document.location.href;
			if (this.documentBasePath.indexOf('?') != -1)
				this.documentBasePath = this.documentBasePath.substring(0, this.documentBasePath.indexOf('?'));
			this.documentBasePath = this.documentBasePath.substring(0, this.documentBasePath.lastIndexOf('/'));
			if (this.options.baseURL.indexOf('://') == -1 && this.options.baseURL.charAt(0) != '/')
				this.options.baseURL = this.documentBasePath + "/" + this.options.baseURL;
		}
		
		// Adds Stylesheet
		if(this.options.cssFile !== false) new Asset.css(this.options.baseURL +'/'+ this.options.cssFile +'.css');
		
		// Setup Links & Link Images
		this.images = new Array();
		this.links.each(function(el, i){
			el.options = false;
			if(el.rel.indexOf('|') != -1) this.getOptions(el);
			el.addClass(this.options.className);
			el.addEvent('click', function(e){
				e = new Event(e).stop();
				if(this.active) return;
				this.active = el;
				this.show();
			}.bindWithEvent(this));
		}.bind(this));
		
		// Check Logon
		if(this.options.login_page === false){
			this.loggedon = true;
			return;
		}
		this.login_content = false;
		new Ajax(this.options.baseURL+'/logon_check.php', {
			method: 'get',
			onComplete: function(logon){ this.loggedon = eval(logon); }.bind(this)
		}).request();
	},

	getOptions: function(el){
		var obj = {};
		var options = el.rel.substring(el.rel.indexOf('|')+1);
		options = options.split(',');
		for(var i=0; i<options.length; i++){
			var temp = options[i].split(':');
			if(temp.length != 2) continue;
			obj[temp[0]] = temp[1];
		}
		var check='';for(var i in obj)check+=i+': '+obj[i]+'\n'; if(check=='') return;
		el.options = obj;
	},

	checkScreenSize: function(){
		if(!this.options.screenMin) return true;
		var size = window.getSize().size;
		var aSize = { x: window.screen.availWidth.toInt(), y: window.screen.availHeight.toInt() };
		var check = true;
		if(size.x < this.options.screenMin.x) check = false;
		if(size.y < this.options.screenMin.y) check = false;
		if(check) return true;
		
		if(aSize.x<this.options.screenMin.x || aSize.y<this.options.screenMin.y){
			alert('Error! Your browser does not support the needed size of '+this.options.screenMin.y+'x'+this.options.screenMin.x);
			return false;
		}
		
		window.moveTo(0,0);
		window.resizeTo(aSize.x,aSize.y);
		return true;
	},

	logon: function(){
		this.active = false;
		
		if(this.login_content === false){
			this.background = new Element('div', {
			'styles': {
				'background-color': '#000000',
				'opacity': '0.7',
				'position': 'fixed',
				'top': '0',
				'left': '0',
				'width': '100%',
				'height': '100%',
				'z-index': '60'
			}
		}).inject(document.body);
			this.container = new Element('div', {
			'styles': {
				'background': 'transparent',
				'position': 'fixed',
				'top': '0',
				'left': '0',
				'width': '100%',
				'height': '100%',
				'z-index': '61'
			}
		});
			new Asset.css(this.options.baseURL +'/login_pages/'+ this.options.login_page +'.css');
			new Ajax(this.options.baseURL+'/login_pages/'+this.options.login_page+'.php', {
				method: 'get',
				onComplete: function(html){
					this.login_content = html;
					this.container.setHTML(html);
					this.container.inject(document.body);
					if($('mp_close')) $('mp_close').addEvent('click', function(e){
						e = new Event(e).stop();
						this.container.remove();
						this.background.remove();
					}.bind(this));
				}.bind(this)
			}).request();
		} else {
			this.background.inject(document.body);
			this.container.inject(document.body);
		}
	},

	show: function(){
		if(!this.loggedon) { this.logon(); return; }
		if(!this.checkScreenSize()) return;	
		
		//Create Element
		this.background = new Element('div', {
			'class': 'background',
			'styles': {
				'width': '100%',
				'height': '100%',
				'opacity': '0.7',
				'cursor': (this.options.backgroundClose?'pointer':'auto')
			},
			'events': {
				'click': (function(e){
					e = new Event(e).stop();
					if(this.options.backgroundClose) this.hide();
				}).bindWithEvent(this)
			}
		});
		this.container = new Element('div', {
			'class': this.options.className,
			'styles': {
				'width': this.options.initialWidth,
				'height': this.options.initialHeight,
				'overflow': 'hidden'
			}
		});
		this.close = false;
		
		this.width = false;
		this.height = false;
		if(this.active.options && this.active.options.width) this.width = this.active.options.width.toInt();
		if(this.active.options && this.active.options.height) this.height = this.active.options.height.toInt();
		this.createObject();
	},

	showContents: function(){
		if(this.options.overlay) this.background.inject(document.body);
		this.container.inject(document.body);
		
		var size = this.container.getSize().size;
		this.container.setStyles({
			'margin-top': (size.y/2).toInt()+'px',
			'margin-left': (size.x/2).toInt()+'px'
		});

		//IE6 fixed positioning & positioning hack
		if(window.ie6){
			new Ajax(this.options.baseURL+'/ie7-fixed.js', {
				method: 'get',
				evalScripts: true
			}).request();
		}
		
		// Show Effect
		var cords = this.container.getCoordinates();
		if(window.ie6) this.container.setStyles({'top': cords.top.toInt() - (this.height/2), 'left': cords.left.toInt() - (this.width/2)});
		this.fx = new Fx.Styles(this.container, this.options.showEffect);
		this.fx.start({
			'height': this.height,
			'margin-top': (this.height/2) * -1
		}).chain(function(){
			this.fx.start({
				'width': this.width,
				'margin-left':(this.width/2) * -1
			}).chain(function(){
				if(this.str != '') this.container.innerHTML = this.str;
				if(this.options.showClose) this.showClose();
			}.bind(this));
		}.bind(this));
	},

	showClose: function(){
		if(this.close) this.close.remove();
		var cords = this.container.getCoordinates();
		this.close = new Element('div', {
			'class': 'close',
			'styles': {
				'cursor': 'pointer',
				'position': 'fixed',
				'top': '50%',
				'left': '50%',
				'margin-top': ((cords.height.toInt()/2) * -1) + this.options.closeOffset.y,
				'margin-left': ((cords.width.toInt()/2) - 10) + this.options.closeOffset.x
			},
			'events': {
				'click': (function(e){
					e = new Event(e).stop();
					this.hide();
				}).bindWithEvent(this)
			}
		}).setHTML(this.options.closeText).inject(document.body);
	},

	hide: function(){
		this.container.empty();
		var cords = this.container.getCoordinates();
		if(this.options.showClose) this.close.remove();
		// Hide Effects
		this.fx = new Fx.Styles(this.container, this.options.hideEffect);
		this.fx.start({
			'height': 10,
			'margin-top': -5
		}).chain(function(){
			this.fx.start({
				'width': 10,
				'margin-left': -5
			}).chain(function(){
				this.remove();
			}.bind(this));
		}.bind(this));
	},

	remove: function(){
		this.container.remove();
		if(this.options.overlay) this.background.remove();
		this.active = false;
	},

	createObject: function(){
		if(!this.width) this.width = this.options.setWidth;
		if(!this.height) this.height = this.options.setHeight;
		
		if(this.options.type == 'swf'){
			this.str = '<embed id="ply" ';
				this.str += 'height="'+this.height+'" ';
				this.str += 'width="'+this.width+'" ';
				this.str += 'flashvars="file=' + this.active.getProperty('href') + this.options.flash_vars;
			if(this.active.options.preview)
				this.str += '&image=' + this.active.options.preview;
				this.str += '" ';	//end of flashvars
				this.str += 'allowscriptaccess="always" wmode="transparent" allowfullscreen="true" quality="high" bgcolor="#FFFFFF" name="ply" ';
				this.str += 'src="'+this.options.baseURL+'/mediaplayer.swf" ';
				this.str += 'type="application/x-shockwave-flash" style="" />';
		} else if(this.options.type == 'obj') {
			this.str = '<object type="application/pdf" wmode="transparent" ';
				this.str += 'width="' + this.width + '" ';
				this.str += 'height="' + this.height + '" ';
				this.str += 'data="' + this.active.getProperty('href') + '">';
			this.str += '</object>';
		} else if(this.options.type == 'pdf') {
			this.str = '<iframe src ="' + this.active.getProperty('href') + '#page=1&zoom=100&pagemode=none&navpanes=0&toolbar=1" ';
				this.str += 'height="' + this.height + '" ';
				this.str += 'width="' + this.width + ' ';
				this.str += 'frameborder="0" scrolling="no"></iframe>';
		} else if(this.options.type == 'flashVideo'){
			this.str = '<object id="csSWF" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
				this.str += 'width="' + this.width + '" ';
				this.str += 'height="' + this.height + '" ';
				this.str += 'codebase="http://active.macromedia.com/flash7/cabs/ swflash.cab#version=9,0,28,0">';
				this.str += '<param name="allowFullScreen" value="true"/>';
  			this.str += '<param name="wmode" value="transparent">';
				this.str += '<param name="src" value="' + this.active.getProperty('href') + '"/>';
				this.str += '<param name="bgcolor" value="#1a1a1a"/>';
				this.str += '<param name="quality" value="best"/>';
				this.str += '<param name="allowScriptAccess" value="always"/>';
				this.str += '<param name="scale" value="showall"/>';
				this.str += '<param name="flashVars" value="autostart=false"/>';
				this.str += '<embed name="csSWF" src="' + this.active.getProperty('href') + '" allowFullScreen="true" wmode="transparent" ';
				this.str += 'width="' + this.width + '" ';
				this.str += 'height="' + this.height + '" ';
				this.str += 'bgcolor="#1a1a1a" quality="best" allowScriptAccess="always" scale="showall" flashVars="autostart=false" ';
				this.str += 'pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>';
			this.str += '</object>';
		} else {
			this.str = '<iframe src ="' + this.active.getProperty('href') + '" ';
				this.str += 'height="' + this.height + '" ';
				this.str += 'width="' + this.width + ' ';
				this.str += 'frameborder="0" scrolling="no"></iframe>';
		}
		
		this.showContents();
	}
});

MediaPlayer.implement(new Options, new Events);