MooTools.More={version:"1.2.4.4",build:"6f6057dc645fdb7547689183b2311063bd653ddf"};Fx.Elements=new Class({Extends:Fx.CSS,initialize:function(b,a){this.elements=this.subject=$$(b);
this.parent(a);},compute:function(g,h,j){var c={};for(var d in g){var a=g[d],e=h[d],f=c[d]={};for(var b in a){f[b]=this.parent(a[b],e[b],j);}}return c;
},set:function(b){for(var c in b){var a=b[c];for(var d in a){this.render(this.elements[c],d,a[d],this.options.unit);}}return this;},start:function(c){if(!this.check(c)){return this;
}var h={},j={};for(var d in c){var f=c[d],a=h[d]={},g=j[d]={};for(var b in f){var e=this.prepare(this.elements[d],b,f[b]);a[b]=e.from;g[b]=e.to;}}return this.parent(h,j);
}});Fx.Accordion=new Class({Extends:Fx.Elements,options:{display:0,show:false,height:true,width:false,opacity:true,alwaysHide:false,trigger:"click",initialDisplayFx:true,returnHeightToAuto:true},initialize:function(){var c=Array.link(arguments,{container:Element.type,options:Object.type,togglers:$defined,elements:$defined});
this.parent(c.elements,c.options);this.togglers=$$(c.togglers);this.previous=-1;this.internalChain=new Chain();if(this.options.alwaysHide){this.options.wait=true;
}if($chk(this.options.show)){this.options.display=false;this.previous=this.options.show;}if(this.options.start){this.options.display=false;this.options.show=false;
}this.effects={};if(this.options.opacity){this.effects.opacity="fullOpacity";}if(this.options.width){this.effects.width=this.options.fixedWidth?"fullWidth":"offsetWidth";
}if(this.options.height){this.effects.height=this.options.fixedHeight?"fullHeight":"scrollHeight";}for(var b=0,a=this.togglers.length;b<a;b++){this.addSection(this.togglers[b],this.elements[b]);
}this.elements.each(function(e,d){if(this.options.show===d){this.fireEvent("active",[this.togglers[d],e]);}else{for(var f in this.effects){e.setStyle(f,0);
}}},this);if($chk(this.options.display)||this.options.initialDisplayFx===false){this.display(this.options.display,this.options.initialDisplayFx);}if(this.options.fixedHeight!==false){this.options.returnHeightToAuto=false;
}this.addEvent("complete",this.internalChain.callChain.bind(this.internalChain));},addSection:function(e,c){e=document.id(e);c=document.id(c);var f=this.togglers.contains(e);
this.togglers.include(e);this.elements.include(c);var a=this.togglers.indexOf(e);var b=this.display.bind(this,a);e.store("accordion:display",b);e.addEvent(this.options.trigger,b);
if(this.options.height){c.setStyles({"padding-top":0,"border-top":"none","padding-bottom":0,"border-bottom":"none"});}if(this.options.width){c.setStyles({"padding-left":0,"border-left":"none","padding-right":0,"border-right":"none"});
}c.fullOpacity=1;if(this.options.fixedWidth){c.fullWidth=this.options.fixedWidth;}if(this.options.fixedHeight){c.fullHeight=this.options.fixedHeight;}c.setStyle("overflow","hidden");
if(!f){for(var d in this.effects){c.setStyle(d,0);}}return this;},detach:function(){this.togglers.each(function(a){a.removeEvent(this.options.trigger,a.retrieve("accordion:display"));
},this);},display:function(a,b){if(!this.check(a,b)){return this;}b=$pick(b,true);if(this.options.returnHeightToAuto){var d=this.elements[this.previous];
if(d&&!this.selfHidden){for(var c in this.effects){d.setStyle(c,d[this.effects[c]]);}}}a=($type(a)=="element")?this.elements.indexOf(a):a;if((this.timer&&this.options.wait)||(a===this.previous&&!this.options.alwaysHide)){return this;
}this.previous=a;var e={};this.elements.each(function(h,g){e[g]={};var f;if(g!=a){f=true;}else{if(this.options.alwaysHide&&((h.offsetHeight>0&&this.options.height)||h.offsetWidth>0&&this.options.width)){f=true;
this.selfHidden=true;}}this.fireEvent(f?"background":"active",[this.togglers[g],h]);for(var j in this.effects){e[g][j]=f?0:h[this.effects[j]];}},this);
this.internalChain.chain(function(){if(this.options.returnHeightToAuto&&!this.selfHidden){var f=this.elements[a];if(f){f.setStyle("height","auto");}}}.bind(this));
return b?this.start(e):this.set(e);}});var Accordion=new Class({Extends:Fx.Accordion,initialize:function(){this.parent.apply(this,arguments);var a=Array.link(arguments,{container:Element.type});
this.container=a.container;},addSection:function(c,b,e){c=document.id(c);b=document.id(b);var d=this.togglers.contains(c);var a=this.togglers.length;if(a&&(!d||e)){e=$pick(e,a-1);
c.inject(this.togglers[e],"before");b.inject(c,"after");}else{if(this.container&&!d){c.inject(this.container);b.inject(this.container);}}return this.parent.apply(this,arguments);
}});

window.addEvent('domready', function(){

	// left hand side accordion
	new Fx.Accordion($$('div.accordion h3'), $$('div.accordion div.ac-content'), { display: -1, alwaysHide: true })
	.addEvent('active', function(t, e){
		e.addClass('active');
		t.addClass('active');
		t.getElement('img.off').setStyle('display', 'none');
		t.getElement('img.on').setStyle('display', 'inline');
	})
	.addEvent('background', function(t, e){
		e.removeClass('active');
		t.removeClass('active');
		t.getElement('img.off').setStyle('display', 'inline');
		t.getElement('img.on').setStyle('display', 'none');		
	});
	
	// list of courses
	new Fx.Accordion($$('ul.course-list h2'), $$('ul.course-list li.more'), { display: -1, alwaysHide: true })
	.addEvent('active', function(t, e){
		e.addClass('active');
		t.addClass('active');
		t.getElement('img.off').setStyle('display', 'none');
		t.getElement('img.on').setStyle('display', 'inline');
	})
	.addEvent('background', function(t, e){
		e.removeClass('active');
		t.removeClass('active');
		t.getElement('img.off').setStyle('display', 'inline');
		t.getElement('img.on').setStyle('display', 'none');		
	});
	
	// search filtering
	filter = function(){
		
		// need to work out what we want to see
	
		// awards
		awards = [];
		$('filter-form').getElements('p input[name="awards"]')
		.each(function(i){ 
			if (i.get('checked')) awards.push(i.get('value'));
		});
		
		// subjects
		subjects = [];
		$('filter-form').getElements('p input[name="subjects"]')
		.each(function(i){ 
			if (i.get('checked')) subjects.push(i.get('value'));
		});
		
		// body
		body = [];
		$('filter-form').getElements('p input[name="body"]')
		.each(function(i){ 
			if (i.get('checked')) body.push(i.get('value'));
		});
		
		// entry
		entry = [];
		$('filter-form').getElements('p input[name="entry"]')
		.each(function(i){ 
			if (i.get('checked')) entry.push(i.get('value'));
		});
		
		// modulecount
		modulecount = [];
		$('filter-form').getElements('p input[name="modulecount"]')
		.each(function(i){ 
			if (i.get('checked')) modulecount.push(i.get('value'));
		});
		
		// location
		loc = [];
		$('filter-form').getElements('p input[name="location"]')
		.each(function(i){ 
			if (i.get('checked')) loc.push(i.get('value'));
		});
		
		// lecturer
		lecturer = [];
		$('filter-form').getElements('p input[name="lecturer"]')
		.each(function(i){ 
			if (i.get('checked')) lecturer.push(i.get('value'));
		});
		
		// fee
		//fee = [];
		//$('filter-form').getElements('p input[name="fee"]')
		//.each(function(i){ 
		//	if (i.get('checked')) fee.push(i.get('value').replace('£','&pound;'));
		//});
		
		// ptlength
		ptlength = [];
		$('filter-form').getElements('p input[name="ptlength"]')
		.each(function(i){ 
			if (i.get('checked')) ptlength.push(i.get('value'));
		});
		
		// ftlength
		ftlength = [];
		$('filter-form').getElements('p input[name="ftlength"]')
		.each(function(i){ 
			if (i.get('checked')) ftlength.push(i.get('value'));
		});

		// length
		llength = [];
		$('filter-form').getElements('p input[name="llength"]')
		.each(function(i){ 
			if (i.get('checked')) llength.push(i.get('value'));
		});
				
		// title
		title = $('ctitle').get('value').toLowerCase();
		if (title == 'course title') title = '';
		
		// description
		ldescription = $('cdescription').get('value').toLowerCase();
		if (ldescription == 'search') ldescription = '';
		
		// loop over courses		
		$$('ul.course-list li').each(function(c, ind){
		
			// get course json
			json = JSON.decode(c.retrieve('info'));
			
			// only if json
			if (json){
																
				// awards
				if ($type(json.award) != 'array'){
					json.award = json.award.split(',');
				}
							
				found = false;
				if (awards.length > 0){
					awards.each(function(a){ 
						if (json.award.contains(a)) found = true;
					}, this);
				}
				if (!found){
					c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
					return;
				}
												
				// subjects
				if ($type(json.award) != 'array'){
					json.subjects = json.subjects.split(',');
				}
							
				found = false;
				if (subjects.length > 0){
					subjects.each(function(a){ 
						if (json.subjects.contains(a)) found = true;
					}, this);
				}
				if (!found){
					c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
					return;
				}
												
				// body
				if ($type(json.body) != 'array'){
					json.body = json.body.split(',');
				}
											
				found = false;
				if (body.length > 0){
					body.each(function(a){ 
						if (json.body.contains(a)) found = true;
					}, this);
				}
				if (!found){
					c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
					return;
				}
								
				// entry
				if ($type(json.entry) != 'array'){
					json.entry = json.entry.split(',');
				}
											
				found = false;
				if (entry.length > 0){
					entry.each(function(a){ 
						if (json.entry.contains(a)) found = true;
					}, this);
				}
				if (!found){
					c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
					return;
				}
				
				// module count
				if ($type(json.modulecount) != 'array'){
					json.modulecount = json.modulecount.split(',');
				}
											
				found = false;
				if (modulecount.length > 0){
					modulecount.each(function(a){ 
						if (json.modulecount.contains(a)) found = true;
					}, this);
				}
				if (!found){
					c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
					return;
				}
												
				// location
				if ($type(json.location) != 'array'){
					json.location = json.location.split(',');
				}
											
				found = false;
				if (loc.length > 0){
					loc.each(function(a){ 
						if (json.location.contains(a)) found = true;
					}, this);
				}
				if (!found){
					c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
					return;
				}
											
				// lecturer
				if ($type(json.lecturer) != 'array'){
					json.lecturer = json.lecturer.split(',');
				}
											
				found = false;
				if (lecturer.length > 0){
					lecturer.each(function(a){ 
						if (json.lecturer.contains(a)) found = true;
					}, this);
				}
				if (!found){
					c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
					return;
				}
								
				// fee
				//if ($type(json.fee) != 'array'){
				//	json.fee = json.fee.split(',');
				//}
											
				//found = false;
				//if (fee.length > 0){
				//	fee.each(function(a){ 
				//		if (json.fee.contains(a)) found = true;
				//	}, this);
				//}
				//if (!found){
				//	c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
				//	return;
				//}
								
				// ptlength
				if ($type(json.ptlength) != 'array'){
					json.ptlength = json.ptlength.split(',');
				}
											
				found = false;
				if (ptlength.length > 0){
					ptlength.each(function(a){ 
						if (json.ptlength.contains(a)) found = true;
					}, this);
				}
				if (!found){
					c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
					return;
				}
				
				// ftlength
				if ($type(json.ftlength) != 'array'){
					json.ftlength = json.ftlength.split(',');
				}
											
				found = false;
				if (ftlength.length > 0){
					ftlength.each(function(a){ 
						if (json.ftlength.contains(a)) found = true;
					}, this);
				}
				if (!found){
					c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
					return;
				}
								
				// length
				if ($type(json.clength) != 'array'){
					json.clength = json.clength.split(',');
				}
											
				found = false;
				if (llength.length > 0){
					llength.each(function(a){ 
						if (json.clength.contains(a)) found = true;
					}, this);
				}
				if (!found){
					c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
					return;
				}
												
				// title
				if (title.trim() != '' && c.getElement('h2').get('text').toLowerCase().indexOf(title) == -1){
					c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
					return;
				}
				
				// description
				if (ldescription.trim() != '' && c.getNext('li.more').getElement('td.description').get('text').toLowerCase().indexOf(ldescription) == -1){
					c.setStyle('display', 'none').getNext('li.more').setStyle('display', 'none');
					return;
				}
								
				// show c
				c.setStyle('display', 'block').getNext('li.more').setStyle('display', 'block');;
			
			}
		
		});
	
	};
	
	
	// add events to checkboxes
	$('filter-form').getElements('p input').each(function(el){
		if (el.getProperty('type') == 'checkbox')
			el.addEvent('click', filter);
		else
			el.addEvent('keyup', filter);
	});
	$('filter-form').getElements('select').addEvent('change', filter);
	$('filter-form').getElements('textarea').addEvent('keyup', filter);
		
	$('ctitle')
	.addEvent('blur', function(ev){ if (this.get('value') == '') this.set('value','Course Title'); })
	.addEvent('focus', function(ev){ if (this.get('value') == 'Course Title') this.set('value',''); });
	
	$('cdescription')
	.addEvent('blur', function(ev){ if (this.get('value') == '') this.set('value','Search'); })
	.addEvent('focus', function(ev){ if (this.get('value') == 'Search') this.set('value',''); });

	// checkboxes in course list
	$$('ul.course-list input[type="checkbox"]').addEvent('change', function(ev){
	
		if (this.get('checked')){
				
			if ($$('ul.course-list input[type="checkbox"]:checked').length > 3){
				ev.stop();
				this.set('checked', false);
			}
		
		}
	
	});
	
	// compare button
	$$('div.right input.compare-button')
	.addEvent('click', function(ev){
		if ($$('ul.course-list input[type="checkbox"]:checked').length > 1){
			location.href = '/study/compare.php?compare=' + $$('ul.course-list input[type="checkbox"]:checked').get('value').join('|');
		}
	});
	
	// reset filters
	$$('div.left input[type="reset"]')
	.addEvent('click', function(){
		$('filter-form').getElements('input[type="checkbox"]').set('checked', true);
		$('filter-form').getElements('input[type="text"]').set('value', '').fireEvent('blur');
		$('filter-form').getElements('textarea').set('value', '').fireEvent('blur');
		$('filter-form').getElements('select').set('value', '').fireEvent('change');
		filter();
	});
	
	// links with extras
	var extras = $$('div.extra')
	.each(function(el){
		
		el.getElements('a')
		.addEvent('click', function(ev){
		
			if (this.getProperty('href').indexOf('#') == -1) return;
		
			if (ev) ev.stop();
			
			var h = this.getProperty('href').split('#');
			
			switch (h[1]){
			
				case 'close':
					this.getParent('div.extra').setStyles({
						'display': 'none'
					});
				break;
			
			}
		
		});
		
		if (el.getParent('div.box')){
		
			(function(){
			
				el.setStyles({
					'height': 0,
					'width':240,
					'display': 'block',
					'margin-left': 12,
					'margin-top': -2,
					'overflow': 'hidden'
				})
				.tween('height', 620);
			
			}.delay(900));
			
		}
	
	});

});
