/*
Script: sharetab.js
	Contains <ShareTab>

Author:
	Alan Roemen
	June 22, 2010

Class: ShareTab
	<No Description>

Options:
	baseURL : URL of script. Default: false
	shareIMG : URL of Image. Default: false
	imgAlign : Image alignment around text. Options 'off' or 'both' or 'right' or 'left'. Default: 'off'
	timeout: Amount of time sharethis window will remain open. Default: false (false means it will timeout)
	duration: Amount of time to transition pop window opening. Default: 500
	offsets: PopUp offsets. Default: {'x': 0, 'y': 18}
	defaults: If set to true, will use page title and page url. Default: false
	shareTXT : Text to display in WebPage. Default: 'Share This Page'
	subject: Message default subject. Default: 'Check out this website'
	message: Message default message body. Default: 'I found a website that I thought you might find interesting. '
	className : Class name to CSS class. Default: 'ShareThis'

Events:
	onShow - optionally you can alter the default onShow behaviour with this option (like displaying a fade in effect);
	onHide - optionally you can alter the default onHide behaviour with this option (like displaying a fade out effect);
*/

var ShareTab = ShareThis.extend({

	options: {
		defaults: true,
		globalClass: false,
		shareTXT: 'Share This Page',
		duration: 0,
		offsets: {
			x: -63,
			y: 21
		},
		onHide: function(el){
			el.setStyles({'visibility': 'hidden', 'opacity': 0});
		},
		className: 'share-drop',
		email_subject: 'Check out this BMG Product',
		sharing: 'product'
	},

	initialize: function(access_check, options) {
		this.access_check = access_check || false;
		this.parent('a#share-tab', options);
		this.sharetabbed = false;
	},

	do_check: function() {
		if ($('share-tab').getProperty('rel') == 'public') return true;
		$$('div[class^=make-public]').each(function(el){
			el.effect('background-color', {duration: 750, transition: Fx.Transitions.linear})
				.start('#ffedcf', '#ffffff')
		});
		return false;
	},

	start: function(id) {
		if (this.access_check && !this.do_check()) return;
		this.parent(id);
	},

	build: function() {
		$('share').addClass('active');
		if (this.sharetabbed) {
			this.sharebox.empty();
			this.wrapper = new Element('div').injectInside(this.sharebox);
		}
		this.parent();

		this.sharedrop = {};
		this.sharedrop.center = this.sharebox.getChildren()[0];
		this.sharedrop.center.addClass('share-drop-center');
		this.sharedrop.wrap = new Element('div', {'id': 'share-wrap'}).injectBefore(this.sharedrop.center);
		this.sharedrop.wrap.adopt(this.sharedrop.center);
		new Element('div', {'class': 'share-drop-top'}).injectBefore(this.sharedrop.wrap);
		new Element('div', {'class': 'share-drop-bottom'}).injectAfter(this.sharedrop.wrap);

		this.sharedrop.tabs = this.sharebox.getElement('div.head');
		this.sharedrop.tabs.removeClass('head').setProperty('id', 'share-tabs');
		this.sharedrop.close = this.sharedrop.tabs.getElement('a');
		this.sharedrop.close.empty().setProperty('id', 'share-close').removeProperty('class');
		this.sharedrop.close.injectTop(this.sharedrop.center);

		this.sharedrop.email = new Element('a', {'class': 'social-tabs social-tab-active'}).setHTML('Email').inject(this.sharedrop.tabs);
		this.sharedrop.social_media = new Element('a', {'class': 'social-tabs'}).setHTML('Social Media').inject(this.sharedrop.tabs);
		this.sharedrop.email.cloneEvents(this.sharedrop.tabs.getElements('li')[0]);
		this.sharedrop.social_media.cloneEvents(this.sharedrop.tabs.getElements('li')[1]);
		this.sharedrop.tabs.getElement('ul').remove();

		this.sharedrop.content = {};
		this.sharedrop.content.email = this.sharebox.getElement('div.body div.email');
		this.sharedrop.content.social_media = this.sharebox.getElement('div.body ul.social');
		this.sharedrop.content.social_media.injectAfter(this.sharedrop.tabs);
		this.sharedrop.content.email.injectAfter(this.sharedrop.tabs);
		this.sharebox.getElement('div.body').remove();
		this.sharedrop.content.email.removeProperty('class').setProperty('id', 'share-email');
		this.sharedrop.content.social_media = new Element('div', {
			'id': 'share-social',
			'styles': {'display': 'none'}
		}).injectAfter(this.sharedrop.content.email).adopt(this.sharedrop.content.social_media).empty();

		// Email
		this.sharedrop.content.email_center = new Element('div', {'class': 'share-center', 'id': 'share-email-form'})
			.adopt(this.sharedrop.content.email.getChildren())
			.inject(this.sharedrop.content.email);
		new Element('div', {'class': 'share-top'}).injectTop(this.sharedrop.content.email);
		new Element('div', {'class': 'share-bottom'}).inject(this.sharedrop.content.email);
		this.sharedrop.content.email.getElement('fieldset').remove();
		this.sharedrop.content.email_inputs = this.sharedrop.content.email_center.getElements('input[type=text]');
		this.sharedrop.content.email_message = this.sharedrop.content.email_center.getElement('textarea');
		this.sharedrop.content.email_center.empty();
		new Element('div', {'class': 'input-identification'}).setHTML('Recipient(s) Address')
			.inject(this.sharedrop.content.email_center)
			.adopt(new Element('div', {'class': 'share-input-title'}).adopt([
				$(this.sharedrop.content.email_inputs[0]).addClass('share-input'),
				new Element('p').setHTML('(Multiple: friend@site.com, friend2@site.com)')
			]));
		new Element('div', {'class': 'input-identification'}).setHTML('Your Email Address')
			.inject(this.sharedrop.content.email_center)
			.adopt(new Element('div', {'class': 'share-input-title'}).adopt(
				$(this.sharedrop.content.email_inputs[2]).addClass('share-input')
			));
		new Element('div', {'class': 'input-identification-small'}).setHTML('Message')
			.inject(this.sharedrop.content.email_center)
			.adopt(new Element('div', {'class': 'share-input-title'}).adopt(
				$(this.sharedrop.content.email_message).addClass('share-input-large')
			));
		new Element('div', {'class': 'input-identification-short'}).setHTML('Enter <strong>only</strong> the <strong>black</strong> characters in the image')
			.inject(this.sharedrop.content.email_center)
			.adopt(new Element('div', {'class': 'share-input-title-short'}).adopt([
				$(this.sharedrop.content.email_inputs[4]).setProperty('class', 'share-input-short'),
				$(this.captchaIMG).addClass('captcha').addEvent('click', function(e){
					new Event(e).stop();
					this.reloadCAPTCHA();
				}.bind(this))
			]));
		new Element('div', {'class': 'clear'}).inject(this.sharedrop.content.email_center)
		this.sharedrop.content.email_send = new Element('span', {'class': 'button-input input-primary'})
			.inject(this.sharedrop.content.email_center)
			.adopt(new Element('input', {
				'class': 'button input-button-primary',
				'type': 'button',
				'value': 'Send',
				'events': {'click': this.sendEmail.bind(this)}
			}))
		new Element('div', {'class': 'clear'}).inject(this.sharedrop.content.email_center)

		// Social Media
		this.sharedrop.content.social_center = new Element('div', {'class': 'share-center'}).inject(this.sharedrop.content.social_media);
		this.sharedrop.content.social_left = new Element('ul').inject(this.sharedrop.content.social_center);
		this.sharedrop.content.social_right = new Element('ul').inject(this.sharedrop.content.social_center);
		new Element('div', {'class': 'share-top'}).injectTop(this.sharedrop.content.social_media);
		new Element('div', {'class': 'share-bottom'}).inject(this.sharedrop.content.social_media);
		new Element('li').adopt(new Element('a', {	// TWITTER
			'class': 'twitter',
			'href': 'http://twitter.com/home?status=Check out this quality Christian entertainment! ' + this.fields[this.active].link,
			'target': '_blank'
		}).setHTML('Twitter')).inject(this.sharedrop.content.social_left);
		new Element('li').adopt(new Element('a', {	// FACEBOOK
			'class': 'facebook',
			'href': 'http://www.facebook.com/sharer.php?u=' + this.fields[this.active].link + '&t=' + this.fields[this.active].title,
			'target': '_blank'
		}).setHTML('Facebook')).inject(this.sharedrop.content.social_left);
		new Element('li').adopt(new Element('a', {	// DIGG
			'class': 'digg',
			'href': 'http://digg.com/submit?phase=2&url=' + this.fields[this.active].link + '&title=' + this.fields[this.active].title,
			'target': '_blank'
		}).setHTML('Digg')).inject(this.sharedrop.content.social_left);
		new Element('li').adopt(new Element('a', {	// REDDIT
			'class': 'reddit',
			'href': 'http://reddit.com/submit?url=' + this.fields[this.active].link + '&title=' + this.fields[this.active].title,
			'target': '_blank'
		}).setHTML('Reddit')).inject(this.sharedrop.content.social_left);
		new Element('li').adopt(new Element('a', {	// LINKED IN
			'class': 'linkedin',
			'href': 'http://www.linkedin.com/shareArticle?mini=true&url=' + this.fields[this.active].link + '&title=' + this.fields[this.active].title,
			'target': '_blank'
		}).setHTML('Linked In')).inject(this.sharedrop.content.social_left);
		new Element('li').adopt(new Element('a', {	// STUMBLEUPON
			'class': 'stumbleupon',
			'href': 'http://www.stumbleupon.com/submit?url=' + this.fields[this.active].link + '&title=' + this.fields[this.active].title,
			'target': '_blank'
		}).setHTML('StumbleUpon')).inject(this.sharedrop.content.social_right);
		new Element('li').adopt(new Element('a', {	// YAHOO!
			'class': 'yahoo',
			'href': 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u=' + this.fields[this.active].link + '&t=' + this.fields[this.active].title,
			'target': '_blank'
		}).setHTML('Yahoo!')).inject(this.sharedrop.content.social_right);
		new Element('li').adopt(new Element('a', {	// DEL.ICIO.US
			'class': 'delicious',
			'href': 'http://del.icio.us/post?url=' + this.fields[this.active].link + '&title=' + this.fields[this.active].title,
			'target': '_blank'
		}).setHTML('del.icio.us')).inject(this.sharedrop.content.social_right);
		new Element('li').adopt(new Element('a', {	// GOOGLE BUZZ
			'class': 'googlebuzz',
			'href': 'http://www.google.com/buzz/post?url=' + this.fields[this.active].link,
			'target': '_blank'
		}).setHTML('Google Buzz')).inject(this.sharedrop.content.social_right);
		new Element('div', {'class': 'clear'}).inject(this.sharedrop.content.social_center);
		this.sharedrop.content.social_center.getElements('li').addEvent('click', this.end.bind(this));

		this.sharetabbed = true;
	},

	activeTab: function(id){
		this.sharedrop.tabs.getElement('a.social-tab-active').removeClass('social-tab-active');
		this.sharedrop.tabs.getElements('a.social-tabs')[id].addClass('social-tab-active');
		if (id == 0) {	// Email
			this.sharedrop.content.email.setStyle('display', 'block');
			this.sharedrop.content.social_media.setStyle('display', 'none');
		} else { // Social Media
			this.sharedrop.content.email.setStyle('display', 'none');
			this.sharedrop.content.social_media.setStyle('display', 'block');
		}
	},
	
	sendEmail: function(){
		if(!window.ie){
			this.sharedrop.content.email.setStyle('opacity', 0.5);
		}
		var str = [];
		var query = {
			to: {name: 'A recipient\'s address', value: this.toAddress.value},
			name: {name: 'Your Email Address', value: this.senderAddress.value},
			from: {name: 'Your Email Address', value: this.senderAddress.value},
			url: {name: 'URL', value: this.fields[this.active].link},
			subject: {name: 'Subject', value: this.options.email_subject},
			message: {name: 'Message', value: this.message.value},
			captcha: {name: 'Image Key', value: this.captcha.value},
			sharing: {name: 'Sharing', value: this.options.sharing}
		}
		for(var i in query) if(query[i].value.trim() == '') { this.showError(query[i].name); return; }
		for(var name in query) str.push(name+'='+encodeURIComponent(query[name].value));
		str = str.join('&');
		new Ajax(this.ajaxURL, {
			method: 'post',
			data: 'ax=send&'+str,
			onComplete: function(response){
				this.emailSent(response);
			}.bind(this)
		}).request();
	},

	showError: function(desc){
		if(!window.ie){
			this.sharedrop.content.email.setStyle('opacity', 1);
		}
		this.reloadCAPTCHA();
		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.sharedrop.content.email_send);
	},

	emailSent: function(response){
		if(response == 'Email Sent') {
			if(this.alert_msg) { this.alert_msg.remove(); this.alert_msg=false; }
			if(!window.ie){
				this.bodydivs[0].setStyle('opacity','1');
			}
			this.alert_msg = new Element('p',{'class':'alert_msg ok'})
				.setHTML(response)
				.injectBefore(this.sharedrop.content.email_send);

			//Empty Input Fields
			this.toAddress.value = '';
			this.reloadCAPTCHA();
			(function(){
				if (this.alert_msg !== false)
					this.alert_msg.remove();
				this.alert_msg=false;
			}).delay(5000, this);
		} else this.showError(response);
	},

	end: function() {
		$('share').removeClass('active');
		this.alert_msg = false;
		this.active = false;
		$clear(this.timer);
		this.fireEvent('onHide', [this.sharebox]);
	}

});
