var EngineLoad = new Class({

	/* initialize needed methods
	 ======================================================================= */
	initialize: function() {
		this.globalSearchEl 	= $('hd-search-text');
		this.timeline 			= $('cell-container');
		this.graphicTimeline 	= $('graphic-timeline');
		this.growlBasket 		= $('growlbasket');
		this.popups 			= $$('.externalWindow, .flashPopupTour, .flashPopup, .blank');
		this.selfSubmits 		= $$('.singleSelect, .selfSubmit, #filter-by select');
		this.membershipType 	= $$('.membershipType');
		this.membershipTypeDesc = $$('.membershipTypeDesc');
		this.oc 				= $$('.oc');
		this.noc 				= $$('.noc');
		this.thumbLinks 		= $$('.thumbb a');
		this.parallellives 		= $$('#fhi .imageHomeContainer');
		this.radioOther			= $$('input.other');
		this.addToBasketSubmits	= $$('.addToBasket');
		this.limitedTextareas	= $$('textarea.maxlength');
		// find template stuff
		this.fundRadio			= $$('#donation-method_wrapper input[type=radio]');

		this.start();
	},

	/* attach events and add the initially fired methods
	 ======================================================================= */
	start: function() {	
		this.openclose();
		this.woaOpenClose();
		if (this.fundRadio.length) {
			this.initFundTemplate();
		}
	},
	
	
	
	/* 
	 ======================================================================= */
	woaOpenClose: function(){
		var togglers = $$('#section-switcher a');
		var woaCont = $$('.woaCont');
		
		togglers.each(function(toggler, i){
			toggler.addEvents({
				click: function(e){
					e.stop();
					woaCont.addClass('hidden').removeClass('show');
					woaCont[i].addClass('show').removeClass('hidden');
										
					togglers.getParent().removeClass('active');
					this.getParent().addClass('active');
				}
			});
		});
	},
	
	
	openclose: function() {
		this.oc.each(function(el) {
			var title 		= el.getElement('p');	
			var prevTitle 	= title.get('html');
			title.set('html', ' <a rel="show" class="func" title="Show description" href="javascript:void(0);">Open</a>' + prevTitle);
			var titleDIV 	= el.getElement('div.itemDescription');
			var prevTitleDIV = titleDIV.get('html');
			titleDIV.set('html', prevTitleDIV);

			if(!el.hasClass('nioc')) titleDIV.setStyle('display', 'none');
		});

		this.oc.getElement('a.func').each(function(el) {
			var mainCont = el.getParent().getParent('div');
			if(mainCont.hasClass('nioc')) {
				el.set('html', 'Close');
				el.set('rel', 'hide');
			}

			el.addEvent('click', function(e) {
				var tttt = el.getParent().getParent().getElement('div.itemDescription');

				if(el.getProperty('rel') === 'show') {
					el.set('html', 'Close');
					el.set('rel', 'hide');
					tttt.reveal();
				} else {
					el.set('html', 'Open');
					el.set('rel', 'show');
					tttt.dissolve();
				}
			});
		});

		this.oc.getElement('.fancy_oc').each(function(el) {
			var mainCont = el.getParent('div'), 
				toggle = el.getElement('a.func'), 
				title = el.getElement('span');

			if(mainCont.hasClass('nioc')) {
				toggle.set('html', 'Close');
				toggle.set('rel', 'hide');
			}

			title.addEvent('click', function(e) {
				var tttt = el.getParent().getElement('div.itemDescription');

				if(toggle.getProperty('rel') === 'show') {
					toggle.set('html', 'Close');
					toggle.set('rel', 'hide');
					tttt.reveal();
				} else {
					toggle.set('html', 'Open');
					toggle.set('rel', 'show');
					tttt.dissolve();
				}
			});
		});

		this.noc.each(function(el) {
			var titleP = el.getElement('a');
			titleP.dissolve();

			var titleDIV = el.getElement('div.itemDescription');
			titleDIV.reveal();
		});
		
		$$('.imOpenclose a').each(function(el){
			el.addEvent('click', function(e){
				if (e) {
					e.stop();
				}
				var moreInfoCont = this.getParent('.inlineMedia').getElement('.imMoreInfo');
				var moreInfoContFx = new Fx.Reveal(moreInfoCont);
				moreInfoContFx.toggle();

				if(el.getProperty('rel') === 'Close info') {
					el.set('html', 'Close info');
					el.set('rel', 'More info');
				} else {
					el.set('html', 'More info');
					el.set('rel', 'Close info');
				}
			});
		});

		// Hide anc change the toggle value for the MoreInfo elements inside the "change_my_value" wrapper
		$$('.change_my_value .imOpenclose a.func').each(function(el){
			el.fireEvent('click', [null]);
		});
	},
	
	initFundTemplate: function() {
		this.fundDonateDiv	= $('membership');
		this.fundForm		= $('donation-form');
		
		// store the original height
		this.fundDonateDiv.store('original-height', this.fundDonateDiv.getStyles('height').height);
		this.fundDonateDiv.setStyles({
			'display': 'none',
			'height': '0px'
		});
		
		// selecting a donation opens the gift aid section
		this.fundRadio.each(function(el) {
			el.addEvent('click', function(instance) {
				$$('#donation-form input[name=recurring]')[0].set('value', this.hasClass('recurring') ? 1 : 0);
				instance.fundDonateDiv.morph({
					'display': 'block',
					'height': instance.fundDonateDiv.retrieve('original-height')
				});
				
			}.bind(el, this));
		}.bind(this));
		
		// openclose for the sections (acquisitions, exhibitions, education etc)
		// Normally they are all open, but it can be the case that all but one are closed
		var section = document.location.hash;
		if (section) {
			section = section.substr(2);
			sectionEl = $(section);
			if ($chk(sectionEl)) {
				var toHide = $$('.soc').filter(function(a) {
					return this[0] !== a.get('href').substr(1);
				}.bind([section]));
				toHide.each(function(el) {
					el.fireEvent('click');
				});
			}
		}
	}
});
