
//------------------------------------------------------------------------------

var mdl = new Modalizer();

var shareLinkPlan = {
	dialogInclude : '/_includes/code/dialogs/share_dialog.inc',
	init : function() {
		$('recipents').focus();
		$('urlTitle').value = window.document.title;
		$('url').value = window.location;
	},
	methods : {
		dialogSendButton : function() {
			$$('.dialogStep2').setStyle('display','inline');
	
			$('shareDialog').get('send',{
				method: 'post',
				onSuccess: function(wsResult){
					var jsonResult = JSON.decode(wsResult);
					if(!jsonResult.success) {
						new Hash(jsonResult.messages).each( function(value, key) {
							if(value=='') {
								$(key).empty();
							} else {
								$(key).set('html',value);
							} // if..else
						}); // each
		
						$$('.dialogStep2').setStyle('display','none');
						$('dialogSendButton').set('src', '/_media/images/common/send_button_green.gif');
						$('dialogCancelButton').set('src', '/_media/images/common/cancel_button_green.gif');
						$$('.dialogStep1 input', '.dialogStep1 textarea').set('disabled', false);
					} else {
						$$('.dialogStep1').setStyle('display','none');
						$$('.dialogStep3').setStyle('display','block');
					} // if..else
				}
			}).send();
	
			//-- disable AFTER asynchronous XHR request is sent
			$('dialogSendButton').set('src', '/_media/images/common/send_button_green_disabled.gif');
			$('dialogCancelButton').set('src', '/_media/images/common/cancel_button_green_disabled.gif');
			$$('.dialogStep1 input', '.dialogStep1 textarea').set('disabled', true);
		}, // dialogSendButton
		dialogCancelButton : function() {
			$('dialogContainer').empty();
			mdl.modalHide();
		}, // dialogCancelButton
		dialogCloseButton : function() {
			$('dialogContainer').empty();
			mdl.modalHide();
		}, // dialogCloseButton
		dialogSendAnotherLink : function() {
			$('dialogContainer').empty();
			dialogManager(shareLinkPlan);
		} // dialogSendAnotherLink
	} // methods
} // shareLinkPlan

var newsletterSignupLinkPlan = {
	dialogInclude : '/_includes/code/dialogs/newsletter_signup_dialog.inc',
	init : function() {
		$('sender_name').focus();
	},
	methods : {
		dialogSendButton : function() {
			$$('.dialogStep2').setStyle('display','inline');
	
			$('shareDialog').get('send',{
				method: 'post',
				onSuccess: function(wsResult){
					var jsonResult = JSON.decode(wsResult);
					if(!jsonResult.success) {
						new Hash(jsonResult.messages).each( function(value, key) {
							if(value=='') {
								$(key).empty();
							} else {
								$(key).set('html',value);
							} // if..else
						}); // each
		
						$$('.dialogStep2').setStyle('display','none');
						$('dialogSendButton').set('src', '/_media/images/common/send_button_green.gif');
						$('dialogCancelButton').set('src', '/_media/images/common/cancel_button_green.gif');
						$$('.dialogStep1 input').set('disabled', false);
					} else {
						$$('.dialogStep1').setStyle('display','none');
						$$('.dialogStep3').setStyle('display','block');
					} // if..else
				}
			}).send();
	
			//-- disable AFTER asynchronous XHR request is sent
			$('dialogSendButton').set('src', '/_media/images/common/send_button_green_disabled.gif');
			$('dialogCancelButton').set('src', '/_media/images/common/cancel_button_green_disabled.gif');
			$$('.dialogStep1 input').set('disabled', true);
		}, // dialogSendButton
		dialogCancelButton : function() {
			$('dialogContainer').empty();
			mdl.modalHide();
		}, // dialogCancelButton
		dialogCloseButton : function() {
			$('dialogContainer').empty();
			mdl.modalHide();
		} // dialogCloseButton
	} // methods
} // newsletterSignupLinkPlan

var applicationsLoginLinkPlan = {
	dialogInclude : '/_includes/code/dialogs/application_login_dialog.inc',
	init : function() {
		$('login_username').focus();
	},
	methods : {
		dialogSendButton : function() {
			$$('.dialogStep2').setStyle('display','inline');
	
			$('shareDialog').get('send',{
				method: 'post',
				onSuccess: function(wsResult){
					var jsonResult = JSON.decode(wsResult);
					if(!jsonResult.success) {
						new Hash(jsonResult.messages).each( function(value, key) {
							if(value=='') {
								$(key).empty();
							} else {
								$(key).set('html',value);
							} // if..else
						}); // each
		
						$$('.dialogStep2').setStyle('display','none');
						$('dialogSendButton').set('src', '/_media/images/common/login_button_green.gif');
						$('dialogCancelButton').set('src', '/_media/images/common/cancel_button_green.gif');
						$$('.dialogStep1 input').set('disabled', false);
					} else {
						window.location = '/applications/index.php';
					} // if..else
				}
			}).send();
	
			//-- disable AFTER asynchronous XHR request is sent
			$('dialogSendButton').set('src', '/_media/images/common/login_button_green_disabled.gif');
			$('dialogCancelButton').set('src', '/_media/images/common/cancel_button_green_disabled.gif');
			$$('.dialogStep1 input').set('disabled', true);
		}, // dialogSendButton
		dialogCancelButton : function() {
			$('dialogContainer').empty();
			mdl.modalHide();
		} // dialogCancelButton
	} // methods
} // applicationsLoginLinkPlan

//------------------------------------------------------------------------------

function dialogManager( plan ) {
	mdl.modalShow({ 'hideOnClick': false, 'modalStyle': { 'opacity':.50, 'background-color':'#000000', 'z-index': 1000 } });
	var req = new Request.HTML({url : plan.dialogInclude, 
		onSuccess: function(html) {
			$('dialogContainer').set('text', '').adopt(html);

			plan.init();
			new Hash(plan.methods).each( function(methodFunction, key) {
				$(key).onclick = $lambda(methodFunction);
			}); // each
		} // onSuccess
	}).send();
} // function..dialogManager

//------------------------------------------------------------------------------

var toggleStateEmail = 'off';
var toggleStateLogin = 'off';

function rethinkBehaviors() {
	//-- apply smooth scrolling to inner anchor tags
	//var mySmoothScroll = new SmoothScroll({ links: '.bvSmoothAnchor' });

	//-- provide XHTML valid method for target=_blank for <a>'s
	$$('.bvLinkTargetBlank').each( function(el) {
		el.onclick = function() {
			var newWindow = window.open(this.getAttribute('href'), '_blank');
			newWindow.focus();
			return false;
		} // onclick
	}); // bvLinkTargetBlank

	//-- Browser "Print this page" method
	$$('.bvPrintLink').each( function(el) {
		el.onclick = function() {
			window.print(); return false;
		} // onclick
	}); // bvPrintLink

	//-- AJAX dialog for "Email link" method
	$$('.bvShareLink').each( function(el) {
		el.onclick = function() { dialogManager(shareLinkPlan); }
	}); // bvShareLink

	$$('.bvNewsletterSignupLink').each( function(el) {
		el.onclick = function() { dialogManager(newsletterSignupLinkPlan); }
	}); // bvNewsletterSignupLink

	$$('.bvApplicationsLoginLink').each( function(el) {
		el.onclick = function() { dialogManager(applicationsLoginLinkPlan); }
	}); // bvApplicationsLoginLink

	$$('.bvFirstField').each( function(el) {
		el.focus(); //-- this REALLY only makes sense if there is only one instance of .bvFirstField
	}); // bvFirstField

	$('query').addEvents({
		'focus' : function(ev) { this.value = (this.value=="Search") ? "" : this.value; },
		'blur'	: function(ev) { this.value = (this.value=="") ? "Search" : this.value; }
	}); // query

} // function..rethinkBehaviors

//------------------------------------------------------------------------------

window.addEvent('domready', function() {
	rethinkBehaviors();
}); // domready

//------------------------------------------------------------------------------
