
var Email = {
	message: '',
	ajaxURL: 'http://'+ document.domain +'/cfc/core/emailer.cfc?method=article',
	init: function() {
		if (!$('emailForm')) return;
		// Form submission
		$$('form#emailForm a.nosubmit')[0].observe('click',Email.stopSubmit);
		
		var x = 'abc@abc.com';
			// Email link
		$('emailForm').getFirstAncestor('li').select('a')[0].observe('click',Email.activate);
		// Cancel Button
		$('emailForm').getFirstAncestor('li').select('a')[2].observe('click',Email.deactivate);
		// Submit Button
		$('emailForm').getFirstAncestor('li').select('a')[1].observe('click',Email.validate);
		
		$('formThankYou').hide();
		Email.fixErrorMessage();
	},
	fixErrorMessage: function() {
		$('emailErrorMsg').removeClassName('errorMessage');
		$('emailErrorMsg').setStyle({ 'color': '#CA0000', 'margin-top': '5px' });
		$('emailErrorMsg').hide();
	},
	stopSubmit: function(event) {
		// Stop the form from being submitted
		event.stop();
	},
	validate: function() {
		var errors = 0;

		$$('form#emailForm input.emailInput').each(function(inp) {
			if (!$(inp).value.isEmail()) {
				errors++;
				$$('LABEL[for="'+ $(inp).getAttribute('id') +'"]')[0].addClassName("error");
				$(inp).addClassName("error");
			} else {
				$$('LABEL[for="'+ $(inp).getAttribute('id') +'"]')[0].removeClassName("error");
				$(inp).removeClassName("error");
			}
		});
		
		Email.message = 'Email address(es) invalid or missing.';
		if (errors>0) Email.showError();
		else Email.submitForm();
	},
	showError: function() {
		$('emailErrorMsg').removeAllChildren();
		$('emailErrorMsg').appendChild(document.createTextNode(Email.message));
		$('emailErrorMsg').show();
	},
	submitForm: function() {
		new Ajax.Request(Email.ajaxURL,{
			method: 'get',
			parameters: $('emailForm').serialize(true),
			onSuccess: function(transport) {
				var res = transport.responseText.strip().evalJSON();
				if (res.sent == true) {
					Email.successful();
				} else {
					Email.message = 'Errors: '+ res.reason;
					showError();
				}
			}
		});
	},
	successful: function() {
		$('formThankYou').show();
		$('emailForm').hide();
		setTimeout(Email.deactivate,6000);
	},
	deactivate: function(event) {
		try { event.preventDefault(); } catch(er) {};
		$('emailForm').getFirstAncestor('div').hide();
		$('emailForm').getFirstAncestor('li').removeClassName('active');
		
		$('formThankYou').hide();
		$('emailForm').show();
		$('emailErrorMsg').hide();
	},
	activate: function() {
		$('emailForm').getFirstAncestor('li').addClassName('active');
		$('emailForm').getFirstAncestor('div').show();
	}       
};

var Share = {
	st: null,
	url: null,
	headline: null,
	init: function() {
		// Uncomment for production
		return;
		if ($$('ul.shareTools').length == 0) return;
		Share.setVars();
		Share.setupLinks();
	},
	setVars: function() {
		Share.st		= $$('ul.shareTools')[0];
		Share.url		= encodeURI(window.location.href);
		Share.headline	= encodeURI($$('div.article h1')[0].childNodes[0].nodeValue);

	},
	setupLinks: function() {
		$A($(Share.st).select('a')).each(function(a) {
			$(a).observe('click',Share.handleLink);
		});
	},
	handleLink: function(event) {
		// Keep from hitting the #
		event.stop();
		var callr = Event.element(event);
		var txt = callr.childNodes[0].nodeValue;
		var url = '';
		switch(txt) {
			case 'Del.icio.us':
				url = 'http://del.icio.us/login/?url='+ Share.url +'&title='+ Share.headline;
				break;
			case 'Digg':
				url = 'http://digg.com/submit?phase=2&url='+ Share.url +'&title='+ Share.headline +'&bodytext=';
				break;
			case 'Facebook':
				url = 'http://www.facebook.com/sharer.php?u='+ Share.url;
				break;
			case 'My Space':
				url = 'http://www.myspace.com/Modules/PostTo/Pages/?t='+ Share.headline +'&c=%20&u='+ Share.url +'&l=2';
				break;
			case 'Reddit':
				url = 'http://reddit.com/submit?url='+ Share.url +'&title='+ Share.headline;
				break;
			case 'Stumbleupon':
				url = 'http://www.stumbleupon.com/submit?url='+ Share.url +'&title='+ Share.headline;
				break;
			default: break;
		}
		window.location.href = url;
	}
};

var HeaderTools = {
	Init: function(){
		// Return if no headerTools found.
		if (!$(document.body).selectFirst('.article .headerTools')) return;
		
		// Inits for specific tools
		Email.init();
		Share.init();
		
		// if the header tools exists (on the article page) then we'll need to init some functionality
		var headerTools = $(document.body).selectFirst('.article .headerTools');
		
		// use a generic reset so that we if we decide to change how we reset "active", it's in one spot
		$(headerTools).resetActive = function() {
			$(headerTools).select('li').invoke('removeClassName', 'active');
		}
		
		//go through each drop down element
		headerTools.select('.dropdown').each(function(ele){
		
			// go get the links for the tools
			ele.up().selectFirst('a.tool').observe('click', function(){
			
				// reset active
				headerTools.resetActive();
				
				// show the item that we clicked on
				ele.up().addClassName('active');
			});
			
			// if the cancel button is clicked, then we need to reset the form
			// and hide the element
			ele.selectFirst('.cancel').observe('click', function(){
				ele.up().removeClassName('active');
				
				// if there's a form in this element, then it needs to be reset
				var formEle = ele.up().selectFirst('form');
				if (formEle) {
					// reset the form
					formEle.reset();
					
					// hide the error messages
					if (formEle.selectFirst('.errorMessage')) formEle.selectFirst('.errorMessage').hideElement();
					
					// reset the labels so they aren't red anymore
					if (formEle.select('.error')) formEle.select('.error').invoke('removeClassName', 'error');
				}
			});
		});
		var printEle = headerTools.selectFirst('.print')
		if (printEle) {
			printEle.observe('click', function(){
				// reset active
				headerTools.resetActive();
				window.print();
				return false;
			});
		}
	}
};

document.observe( 'dom:loaded', HeaderTools.Init );

