﻿/**
 * the VideoPlayer class
 * replaces link-element with JWplayer object, if required Flash version is present
 *
 * @author Klaas Dieleman, <klaas[AT]efocus.nl>
 * @author ralph Meeuws, <ralph.meeuws[AT]efocus.nl>
 * @since 13 apr 2010
 * @version 1.1
 * @copyright eFocus
 *
 * @uses jQuery 1.4.2, <http://www.jquery.com>
 * @uses SWFobject 2.2, <http://code.google.com/p/swfobject>
 * @uses FlashDetect 1.0.4, <http://www.featureblend.com>
 * @uses JWplayer 4.4, <http://www.longtailvideo.com>
 *
 * @param Element a-tag with url to movie to be replaced by JWplayer
 * @param Object options (optional)
 * @param String unique id for <object>-element (optional but recommended)
 *
 * @todo streaming options
 */
function VideoPlayer(elLink, options, uid){
	
	if(typeof(elLink) == 'object' && FlashDetect.major >= 9) {
		this.elLink = elLink;
		this.uid = uid || 0;
		
		var opt = options || {};
		
		this.options = {
			'width'				: 320,
			'height'			: 200,
			'autoplay'			: true,
			'timesliderprogress': true,
			'fullscreenbutton'	: true,
			'mutebutton'		: true,
			'volumeslider'		: true,
			'playerurl'			: '',
			'skinurl'			: '',
			'controlbarheight'	: 20,
			'videofile'			: '',
			'streaming'			: false,
			'streamingUrl'		: ''
		};
		
		for(i in this.options) {
			if(opt[i] != undefined) {
				this.options[i] = opt[i];
			}
		}
		
		this.initialize();
	} else {
		/**
		* removes the a-tag which wraps the image
		*/
		elLink.children().first().unwrap();
	}
	
};

VideoPlayer.prototype = {
	/**
	 * intializes player with requested options and inserts it
	 */
	initialize: function() {

		this.parseOptions();
		this.buildSkinfileRequest();
		this.createPlaceholder();
		this.insertPlayer();
			
    },
    
    /**
     * reads specified options in the 'rel'-attribute and applies them, overwriting js-requested options
     */
    parseOptions: function() {
		
		var arrGivenOptions = [];
		var arrParsedOptions = [];
		
		arrGivenOptions = String(this.elLink.attr('rel')).replace(/ /g, '').split(';');
		
		for(i in arrGivenOptions) {
			var strOptionName = String(arrGivenOptions[i]).split('=')[0];
			var strOptionValue = String(arrGivenOptions[i]).split('=')[1];
			arrParsedOptions[strOptionName] = strOptionValue;
		}
		
		for(i in this.options) {
			if(arrParsedOptions[i] != undefined) {
				
				var blnOptionFormatInvalid = true;
				
				switch(i) {
					case 'width':
						blnOptionFormatInvalid = isNaN(arrParsedOptions[i]);
						arrParsedOptions[i] = parseInt(arrParsedOptions[i]);
						break;
					case 'height':
						blnOptionFormatInvalid = isNaN(arrParsedOptions[i]);
						arrParsedOptions[i] = parseInt(arrParsedOptions[i]);
						break;
					case 'autoplay':
						if(arrParsedOptions[i] == 'true' || arrParsedOptions[i] == 'false') blnOptionFormatInvalid = false;
						break;
					case 'timesliderprogress':
						if(arrParsedOptions[i] == 'true' || arrParsedOptions[i] == 'false') blnOptionFormatInvalid = false;
						break;
					case 'fullscreenbutton':
						if(arrParsedOptions[i] == 'true' || arrParsedOptions[i] == 'false') blnOptionFormatInvalid = false;
						break;
					case 'mutebutton':
						if(arrParsedOptions[i] == 'true' || arrParsedOptions[i] == 'false') blnOptionFormatInvalid = false;
						break;
					case 'volumeslider':
						if(arrParsedOptions[i] == 'true' || arrParsedOptions[i] == 'false') blnOptionFormatInvalid = false;
						break;
					case 'streaming':
						if(arrParsedOptions[i] == 'true' || arrParsedOptions[i] == 'false') blnOptionFormatInvalid = false;
						break;
					case 'streamingUrl':
						arrParsedOptions[i] = decodeURIComponent(arrParsedOptions[i]);
						blnOptionFormatInvalid = false;
						break;
					default:
						this.options[i] = this.options[i];
				}
				
				if(blnOptionFormatInvalid == false) {
					this.options[i] = arrParsedOptions[i];
				}
				
			}
		}
		
		if(this.options.streaming == 'true') {
			this.options.videofile = this.options.streamingUrl.split('/')[this.options.streamingUrl.split('/').length - 1];
			this.options.streamingUrl = this.options.streamingUrl.replace(this.options.videofile, '');
		} else {
			this.options.videofile = jQuery(this.elLink).attr('href');
		}
		
	},
	
	/**
     * builds url to skin-xml file with options in querystring
     */
	buildSkinfileRequest: function() {
		this.options.skinurl += '?timesliderprogress=' + String(this.options.timesliderprogress);
		this.options.skinurl += '&fullscreenbutton=' + String(this.options.fullscreenbutton);
		this.options.skinurl += '&mutebutton=' + String(this.options.mutebutton);
		this.options.skinurl += '&volumeslider=' + String(this.options.volumeslider);
		this.options.skinurl = encodeURIComponent(this.options.skinurl);
	},

	/**
     * creates placeholder and replaces link element with it
     */
	createPlaceholder: function() {
	
		this.container = jQuery('<span></span>');
		this.container.attr('id', ('videoplayer_' + this.uid));
		this.elLink.after(this.container);
		this.elLink.remove();
		
	},
	
	/**
     * embeds JWplayer on placeholder
     */
	insertPlayer: function() {
	
		var flashvars = {
			'file'		: this.options.videofile,
			'skin'		: this.options.skinurl,
			'autostart'	: this.options.autoplay,
			'stretching': 'fill',
			'volume'	: 100
		};
		
		if(this.options.streaming == 'true') {
			flashvars.streamer = this.options.streamingUrl;
		} else {
			flashvars.type = 'video';
		}
		
		var params = {
			'allowfullscreen'	: true,
			'allowscriptaccess'	: 'always'
		};
		
		var attributes = {
			'id'				: this.container.attr('id'),
			'name'				: this.container.attr('id')
		}

		swfobject.embedSWF(this.options.playerurl, this.container.attr('id'), this.options.width, this.options.height + this.options.controlbarheight, '9.0.0', false, flashvars, params, attributes);
		
	}
}
