/*
Script: mp.js
	Contains <VideoPlayer>

Author:
	Alan Roemen

Class: VideoPlayer
	Exention of MediaPlayer, a simple javascript lightbox for global media player
	http://www.longtailvideo.com/players/jw-flv-player/
*/

var VideoPlayer = MediaPlayer.extend({

	options: {
		login_page: false,
		cssFile: false,
		className: 'video-pop',
		player_offset: {x: 0, y: 60},
		showClose: false,
		tabEffect: {duration: 300},
		flash_vars: {
			autostart: true,
			screencolor: '939598',
			backcolor: 'FFFFFF',
			skin: '/images/bmg-popup-skin/bmg-popup-skin.xml',
			smoothing: false,
			stretching: 'uniform',
			bufferlength: 9
		}
	},

	createObject: function(){
		if(!this.width) this.width = this.options.setWidth;
		if(!this.height) this.height = this.options.setHeight;
		this.width = this.width.toInt();
		this.height = this.height.toInt();

		this.str = '<embed id="video-player" name="video-player" ';
			this.str += 'height="' + (this.height + this.options.player_offset.y) + '" ';
			this.str += 'width="' + (this.width + this.options.player_offset.x) + '" ';
			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" ';
			this.str += 'allowfullscreen="true" ';
			this.str += 'wmode="transparent" ';
			this.str += 'quality="high" ';
			this.str += 'src="/scripts/player-licensed.swf" ';
			this.str += 'type="application/x-shockwave-flash" />';

		this.showContents();
	},

	email_link: function(type) {
		var el;
		if (!$chk(this.share_form))
			this.share_form = {length: 0};

		switch (type) {
			case 'to':
				this.share_form.length++;
				el = new Element('input', {'type': 'text', 'class': 'share-input', 'tabindex': this.share_form.length});
				break;
			case 'from':
				this.share_form.length++;
				el = new Element('input', {'type': 'text', 'class': 'share-input', 'tabindex': this.share_form.length});
				break;
			case 'message':
				this.share_form.length++;
				el = new Element('textarea', {'class': 'share-input-large', 'tabindex': this.share_form.length});
				break;
			case 'captcha':
				this.share_form.length++;
				el = new Element('input', {'type': 'text', 'class': 'share-input-short', 'tabindex': this.share_form.length});
				break;
			case 'captcha_img':
				el = new Element('img', {
					'class': 'captcha',
					'styles': {'cursor': 'pointer'},
					'src': '/scripts/sharethis/images/captcha.php',
					'events': {'click': this.reload_captcha.bind(this)},
					'title': 'Click on the image to obtain a new phrase'
				});
				break;
			case 'submit':
				this.share_form.length++;
				el = new Element('input', {
					'type': 'submit',
					'class': 'button input-button-primary',
					'value': 'Send',
					'events': {'click': this.share_send.bindWithEvent(this)},
					'tabindex': this.share_form.length
				});
				break;
		}

		this.share_form[type] = el;
		return el;
	},

	reload_captcha: function() {
		var src = this.share_form.captcha_img.getProperty('src').replace(/\?.*?$/, '');
		this.share_form.captcha.setProperty('value', '');
		this.share_form.captcha_img.setProperty('src', src + '?' + (new Date()).getTime());
	},

	share_send: function() {
		$('video-email').setStyle('opacity', 0.5);
		var str = [];
		var query = {
			to: {name: 'A recipient\'s address', value: this.share_form.to.value},
			name: {name: 'Your Email Address', value: this.share_form.from.value},
			from: {name: 'Your Email Address', value: this.share_form.from.value},
			url: {name: 'URL', value: location.href},
			subject: {name: 'Subject', value: 'Check out this video'},
			message: {name: 'Message', value: this.share_form.message.value},
			captcha: {name: 'Image Key', value: this.share_form.captcha.value}
		}
		for(var i in query) if(query[i].value.trim() == '') { this.show_error(query[i].name); return; }
		for(var name in query) str.push(name+'='+encodeURIComponent(query[name].value));
		str = str.join('&');
		new Ajax('/scripts/sharethis/sharethis.php', {
			method: 'post',
			data: 'ax=send&'+str,
			onComplete: function(response){
				this.email_sent(response);
			}.bind(this)
		}).request();
	},

	show_error: function(desc){
		$('video-email').setStyle('opacity', 1);
		this.reload_captcha();
		if(this.alert_msg) { this.alert_msg.remove(); this.alert_msg=false; }
		this.alert_msg = new Element('p', {'class':'alert_msg'})
			.setHTML('Error: ' + desc + ' is missing or invalid')
			.injectBefore(this.share_form.submit.getParent());
	},

	email_sent: function(response){
		if(response == 'Email Sent') {
			if(this.alert_msg) { this.alert_msg.remove(); this.alert_msg=false; }
			$('video-email').setStyle('opacity','1');
			this.alert_msg = new Element('p', {'class':'alert_msg ok'})
				.setHTML(response)
				.injectBefore(this.share_form.submit.getParent());

			//Empty Input Fields
			this.share_form.to.value = '';
			this.reload_captcha();
			(function(){
				this.alert_msg.remove();
				this.alert_msg = false;
			}).delay(5000, this);
		} else this.show_error(response);
	},

	social_link: function(class_name, text) {
		var href, link = location.href, title = $$('title')[0].getText().replace(/\s\-\s.+?$/, '');
		switch (class_name) {
			case 'twitter': href = 'http://twitter.com/home?status=Check out this quality Christian entertainment! ' + link; break;
			case 'facebook': href = 'http://www.facebook.com/sharer.php?u=' + link + '&t=' + title; break;
			case 'digg': href = 'http://digg.com/submit?phase=2&url=' + link + '&title=' + title; break;
			case 'reddit': href = 'http://reddit.com/submit?url=' + link + '&title=' + title; break;
			case 'linkedin': href = 'http://www.linkedin.com/shareArticle?mini=true&url=' + link + '&title=' + title; break;
			case 'stumbleupon': href = 'http://www.stumbleupon.com/submit?url=' + link + '&title=' + title; break;
			case 'yahoo': href = 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u=' + link + '&t=' + title; break;
			case 'delicious': href = 'http://del.icio.us/post?url=' + link + '&title=' + title; break;
			case 'googlebuzz': href = 'http://www.google.com/buzz/post?url=' + link; break;
		}

		return new Element('li').adopt(
			new Element('a', {
				'href': href,
				'target': '_blank',
				'class': class_name
			}).setHTML(text)
		);
	},

	build_video_wrap: function() {
		var styles = {'height': this.height.toInt() + this.options.player_offset.y + 14};
		this.video_wrap = new Element('div', {'id': 'video-wrap'})
			.adopt([
				new Element('a', {
					'href': '#',
					'id': 'video-share-tab',
					'events': {'click': this.tab_switch.bindWithEvent(this, 'share')}
				}).setHTML('Share'),
				new Element('a', {
					'href': '#',
					'class': 'video-close',
					'events': {'click': this.tab_switch.bindWithEvent(this, 'close')}
				}),
				new Element('div', {'id': 'video-left', 'styles': styles}).setHTML('<div></div><div></div>'),
				new Element('div', {'id': 'video-right', 'styles': styles}).setHTML('<div></div><div></div>'),
				new Element('div', {'id': 'video-popup'})
			])
			.inject(this.container);
	},

	build_video_share: function() {
		this.video_share = new Element('div', {'class': 'dialog-box-share', 'id': 'video-share-pop'})
			.adopt([
				new Element('a', {
					'href': '#',
					'id': 'back-to-video-tab',
					'events': {'click': this.tab_switch.bindWithEvent(this, 'video')}
				}).setHTML('Back to Video'),
				new Element('a', {
					'href': '#',
					'class': 'video-close',
					'events': {'click': this.tab_switch.bindWithEvent(this, 'close')}
				}),
				new Element('div', {'id': 'video-share-left'}),
				new Element('div', {'id': 'video-share-center'}).adopt([
					new Element('div', {'id': 'video-social-media'}).adopt([
						new Element('p').setHTML('Social Media'),
						new Element('div', {'id': 'social-box-top'}),
						new Element('div', {'id': 'social-box-center'}).adopt([
							new Element('ul').adopt([
								this.social_link('twitter', 'Twitter'),
								this.social_link('facebook', 'Facebook'),
								this.social_link('digg', 'Digg'),
								this.social_link('reddit', 'Reddit'),
								this.social_link('linkedin', 'Linked In'),
								this.social_link('stumbleupon', 'StumbleUpon'),
								this.social_link('yahoo', 'Yahoo!'),
								this.social_link('delicious', 'del.icio.us'),
								this.social_link('googlebuzz', 'Google Buzz')
							]),
							new Element('div', {'class': 'clear'})
						]),	// </#social-box-center
						new Element('div', {'id': 'social-box-bottom'}),
						new Element('div', {'id': 'back-to-video-button'}).adopt(
							new Element('a', {'href': '#', 'events': {'click': this.tab_switch.bindWithEvent(this, 'video')}}).setHTML('Back to Video')
						)
					]),	// </#video-social-media
					new Element('div', {'id': 'video-email'}).adopt([
						new Element('div', {'id': 'video-email-title'}).setHTML('Email'),
						new Element('div', {'id': 'video-email-top'}),
						new Element('div', {'id': 'video-email-center'}).adopt([
							new Element('div', {'class': 'input-identification'}).setHTML('Recipient(s) Address').adopt(
								new Element('div', {'class': 'share-input-title'}).adopt([
									this.email_link('to'),
									new Element('p').setHTML('(Multiple: friend1@site.com, friend2@site.com)')
								])
							),
							new Element('div', {'class': 'input-identification'}).setHTML('Your Email Address').adopt(
								new Element('div', {'class': 'share-input-title'}).adopt(this.email_link('from'))
							),
							new Element('div', {'class': 'input-identification-small'}).setHTML('Message').adopt(
								new Element('div', {'class': 'share-input-title'}).adopt(this.email_link('message'))
							),
							new Element('div', {'class': 'input-identification-short'}).setHTML('Enter only the black characters in the image').adopt(
								new Element('div', {'class': 'share-input-title-short'}).adopt([
									this.email_link('captcha'),
									this.email_link('captcha_img')
								])
							),
							new Element('div', {'class': 'clear'}),
							new Element('span', {'class': 'button-input input-primary'}).adopt(this.email_link('submit'))
						]),	// </#video-email-center
						new Element('div', {'id': 'video-email-bottom'})
					])	// </#video-email
				]),	// </#video-share-center
				new Element('div', {'id': 'video-share-right'})
			])
		.inject(this.container);
	},

	showContents: function() {
		this.background.removeProperty('class')
			.removeProperty('style')
			.setProperty('id', 'overlay')
			.setStyle('opacity', 0.5);

		this.container.addClass('dialog-box')
			.removeClass('video-pop')
			.setProperty('id', 'video-pop')
			.removeProperty('style')
			.setStyles({
				'overflow': 'visible',
				'opacity': 0
			});
		this.build_video_wrap();
		this.build_video_share();

		if(this.options.overlay) this.background.inject(document.body);
		this.container.setStyles({'width': this.width, 'height': this.height.toInt() + 60, 'margin-left': this.width / 2 * -1});
		this.container.inject(document.body);


		//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({
			'opacity': 1
		}).chain(function(){
			if(this.str != '') this.container.getElement('div#video-popup').setHTML(this.str);
		}.bind(this));
	},

	tab_switch: function(e, tab) {
		if (e) new Event(e).stop();
		if(this.alert_msg) { this.alert_msg.remove(); this.alert_msg=false; }
		if (tab == 'close') { this.hide(); return; }

		var vp = $('video-player');
		try { vp.sendEvent('PLAY', false); } catch (e) {}
		this['tab_' + tab]();
	},

	tab_share: function() {
		this.video_wrap.setStyle('display', 'none');
		this.video_share.setStyle('display', 'block');
		this.video_wrap.setStyle('visibility', 'hidden');
		this.video_share.setStyle('visibility', 'visible');
		this.container.setStyles({'width': 534, 'margin-left': 534 / 2 * -1});
	},

	tab_video: function() {
		this.video_share.setStyle('display', 'none');
		this.video_wrap.setStyle('display', 'block');
		this.video_share.setStyle('visibility', 'hidden');
		this.video_wrap.setStyle('visibility', 'visible');
		this.container.setStyles({'width': this.width, 'margin-left': this.width / 2 * -1});
	},

	hide: function() {
		this.container.setHTML();
		this.parent();
	}
});
