
/*********************************************************************/
/** Please make sure this page encoding matches the site encoding   **/
/** (ISO-8859-1 at the time of writing)                             **/
/*********************************************************************/

window.addEvent('domready', function () {
	//eligibility form
	if ($('frmEligibility')) {
		$('frmEligibility').addEvent('submit', function(evt) {
			if ($('jsChecked')) {
				$('jsChecked').value = 1;
			}
			var isEligible = $('fldE1Yes').checked && $('fldE2Yes').checked && $('fldE3Yes').checked && $('fldE4No').checked;
			if (!isEligible) {
				$ans = confirm("Unfortunately you are not eligible to enter the protégé competition - however you will still be considered for our graduate/intern and summer placement schemes, but you will not have the chance to enter the final stages of the protégé competition. If you still wish to proceed with an application, please click the 'OK' button. If you do not wish to proceed with an application, please click the 'Cancel' button.");
				if (!$ans) {
					new Event(evt).stop();
					window.location = 'rlb_not_for_me.php';
				}
			}
			else {
				alert("Congratulations, you have confirmed that you are eligible to enter the protégé competition. Please click the 'OK' button below to continue with your application (Please note that all responses on this application will be verified so please complete accurately.)");
			}
		});
	}
	
	//"reveal more" text blocks
	var triggers = $$('a.revealMore');
	var blocks = $$('div.revealMore');
	var nbOfTriggers = triggers.length;
	var nbOfBlocks = blocks.length;
	for (var i = 0; i < nbOfTriggers; i++) {
		if (i < nbOfBlocks) {
			new RevealMore(triggers[i], blocks[i]);
		}
	}
	
	// Ajax forms (for demographic survey appearing in popup)
	if (typeof jpForm != 'undefined') {
		var frmDemographic = $('frmDemographic');
		if (frmDemographic) {
			frmDemographic = new jpForm(frmDemographic, {
				autoFocus: false,
				ajax: true,
				submittingMessage: 'Submitting, please wait...',
				onSuccess: function() {
					if (typeof parent.Mediabox != 'undefined') {
						// If opened from a pop up, close it.
						parent.Mediabox.close();
					}
				}
			});
			if (typeof parent.Mediabox != 'undefined') {
				parent.handleMediaboxClosure(frmDemographic);
			}
			// <button>s value won't get submitted with AJAX
			var fldAlreadySubmitted = $('fldAlreadySubmitted');
			if (fldAlreadySubmitted) {
				fldAlreadySubmitted.addEvent('click', function(evt) {
					var button = $(this);
					new Element('input', {
						type: 'hidden',
						name: button.get('name'),
						value: button.get('value')
					}).injectAfter(button);
				});
			}
		}
	}
	
	// demographic form on apply_application_form.php
	var fldConsentInfoHeldYes = $('fldConsentInfoHeldYes');
	var fldConsentInfoHeldNo = $('fldConsentInfoHeldNo');
	if (fldConsentInfoHeldYes) {
		fldConsentInfoHeldYes.addEvent('click', function(evt) {
			if (this.checked) {
				$$('.show-if-consent-info-held').removeClass('hidden');
			}
		});
	}
	if (fldConsentInfoHeldNo) {
		fldConsentInfoHeldNo.addEvent('click', function(evt) {
			if (this.checked) {
				$$('.show-if-consent-info-held').addClass('hidden');
			}
		});
		if (fldConsentInfoHeldNo.checked) {
			$$('.show-if-consent-info-held').addClass('hidden');
		}
	}
});

/**
 * Wrapper to a lightbox (such as Mediabox). Useful so that the lightbox
 * can be called from the page, but started only once everything is ready.
 * @param string url URL to open
 */
function openInLightbox(url, title) {
	window.addEvent('domready', Mediabox.open.bind(Mediabox, [url, title, '960 700']));
}

/**
 * Submit demographic form when media box is closed
 */
function handleMediaboxClosure(frmDemographic) {
	Mediabox.eventHandler.removeEvents('close');
	Mediabox.eventHandler.addEvent('close', function(evt) {
		if (!frmDemographic.loading && !frmDemographic.posted) {
			// post the empty form
			frmDemographic.submit();
		}
		else {
			Mediabox.close(null, true);
		}
	}.bindWithEvent(Mediabox, [frmDemographic]));
}

var RevealMore = new Class({
	Implements: Options,
	trigger: null,
	block: null,
	triggerCopy: null,
	blockBottomMargin: 0,
	originalHeight: 0,
	options: {
		duration: 250
	},
	hFx: null,
	oFx: null,
	revealing: false,
	initialize: function(trigger, block, options) {
		this.setOptions(options);
		
		this.trigger = $(trigger);
		this.block = $(block);
		
		this.triggerCopy = this.trigger.get('html');
		this.blockBottomMargin = this.block.getStyle('margin-bottom');
		this.originalHeight = this.block.getSize().y;
		
		
		this.trigger.setStyles({
			outline: 'none'
		});
		this.block.setStyles({
			height: '0px',
			overflow: 'hidden',
			'margin-bottom': '0'
		});
		if (!/MSIE\s6/.test(navigator.userAgent)) {
			this.block.setStyle('opacity', 0);
		}
		
		this.trigger.addEvent('click', this.toggle.bindWithEvent(this));
	},
	toggle: function(evt) {
		evt.stop();
		
		if (this.hFx) this.hFx.cancel();
		if (this.oFx) this.oFx.cancel();
		
		var currentHeigth = parseInt(this.block.getStyle('height'));
		var currentOpacity = this.block.getStyle('opacity');
		if (!this.revealing) { //reveal
			this.revealing = true;
			if (!/MSIE\s6/.test(navigator.userAgent)) {
				//this.oFx = new Fx.Style(this.block, 'opacity', this.options).start(currentOpacity, 1);
				var options = this.options;
				options.property = 'opacity';
				this.oFx = new Fx.Tween(this.block, options).start(currentOpacity, 1);
			}
			//this.hFx = new Fx.Style(this.block, 'height', this.options).start(currentHeigth, this.originalHeight);
			var options = this.options;
			options.property = 'height';
			this.hFx = new Fx.Tween(this.block, options).start(currentHeigth, this.originalHeight);
			
			this.trigger.set('html', 'Hide');
			this.trigger.addClass('revealMoreActive');
			this.block.setStyle('margin-bottom', this.blockBottomMargin);
		}
		else { //hide
			this.revealing = false;
			if (!/MSIE\s6/.test(navigator.userAgent)) {
				//this.oFx = new Fx.Style(this.block, 'opacity', this.options).start(currentOpacity, 0);
				var options = this.options;
				options.property = 'opacity';
				this.oFx = new Fx.Tween(this.block, options).start(currentOpacity, 0);
			}
			//this.hFx = new Fx.Style(this.block, 'height', this.options).start(currentHeigth, 0);
			var options = this.options;
			options.property = 'height';
			this.hFx = new Fx.Tween(this.block, options).start(currentHeigth, 0);
			
			this.trigger.set('html', this.triggerCopy);
			this.trigger.removeClass('revealMoreActive');
			this.block.setStyle('margin-bottom', 0);
		}
	}
});

