/*
**	ifla.js - Main javascript file for IFLA; uses mootools v1.11
**	Lodewijk Schutte ~ Low | Eend
**	Last edit: 2008.08.21
*/

var IFLA = new Hash;

// --- Adds double arrow to menu items ------------------------------

/*
IFLA.set('Raquos', {
	raquo: ' <strong>&raquo;</strong>',
	add: function(a) {
		a.innerHTML += this.raquo;
	},
	init: function() {
		$$('#mainnav a').each(function(el){
			this.add(el);
		},this);
		$$('#secondary .item').each(function(el){
			if (el.id == 'themes') return;
			el.getElements('li li li a').each(function(a){	// SL: added
				if (a.hasClass('toggle')) return;
				a.addClass('subsubsub');
				this.add(a);
			},this);
			el.getElements('li li a').each(function(a){	// SL: added
				if (a.hasClass('toggle')) return;
				if (a.hasClass('subsubsub')) return;
				a.addClass('subsub');
				this.add(a);
			},this);
			el.getElements('li a').each(function(a){
//				if (a.hasClass('external')) return;
				if (a.hasClass('toggle')) return;
				if (a.hasClass('subsubsub')) return;
				if (a.hasClass('subsub')) return;	// SL: added
				this.add(a);
			},this);
		},this);
	}
});
*/

// --- Session time tables toggler ----------------------------------

IFLA.set('Sessions', {
	elements: undefined,
	toggle: function() {
		if (this.hasClass('open')) {
			this.removeClass('open');
			this.addClass('closed');
		} else {
			this.removeClass('closed');
			this.addClass('open');
		}
	},
	init: function() {
		this.elements = $$('.session');
		if (!this.elements.length) return;
		this.elements.each(function(el){
			var h3 = el.getElement('h3');
			if (h3.getNext()) {
				h3.addEvent('click',this.toggle.bind(el));	
				h3.addEvent('mouseover',function(){h3.addClass('hover')});
				h3.addEvent('mouseout',function(){h3.removeClass('hover')});
			}
		}, this);
	}
});

// --- Show/hide archives in sidebar --------------------------------

IFLA.set('Archives', {
	items: undefined,
	toggle: function(e) {
		new Event(e).stop();
		if (this.hasClass('open')) {
			this.removeClass('open');
			this.addClass('closed');
		} else {
			this.removeClass('closed');
			this.addClass('open');
		}
	},
	init: function() {
		this.items = $$('#archives li.year');
		if (!this.items.length) return;
		
		this.items.each(function(el){
			if (!el.getElement('ul')) return;
			el.getElement('a').addEvent('click',this.toggle.bindAsEventListener(el));
		},this);
		
	}
});

// --- Show/hide activity on home page ------------------------------

IFLA.set('Activity', {
	foot: undefined,
	toggle: function(e) {
		new Event(e).stop();
		this.foot.toggleClass('show');
	},
	init: function() {
		this.foot = $('prefoot');
		var show = $('show-activity');
		var hide = $('hide-activity');
		if (!this.foot || !show || !hide) return;
		
		show.addEvent('click',this.toggle.bindAsEventListener(this));
		hide.addEvent('click',this.toggle.bindAsEventListener(this));
	}
});

// --- Show/hide descriptions in table cells <td class="description"><span>text</span></td> ------------------------------

IFLA.set('Celldescriptions', {
	init: function() {
		var descCells = $$('td.description');
		if (!descCells.length) return;
		descCells.each(function(el){
			var span = el.getElement('span');
			span.style.position = 'absolute';
			span.style.marginTop = '-1.7em';
			span.style.display = 'none';
			span.style.padding = '.5em .7em';
			span.style.background = '#f6f6f6';
			span.style.border = 'solid 1px #666';
			
			spanLabel = 'Show';
			spanClass = span.className;
			if (spanClass) {
				spanTitle = spanClass.replace('-', '&#160;');
				spanLabel = spanTitle;
			}
			
			var minwidth = 200;
			var maxwidth = 400;
			var maxleft = 700;
			var a1 = document.createElement('a');
			a1.className = 'js-toggle';
			a1.style.display = 'inline';
			a1.style.cursor = 'pointer';
			a1.innerHTML = spanLabel;
			var a2 = document.createElement('a');
			a2.className = 'js-toggle';
			a2.style.display = 'block';
			a2.style.cssFloat = 'right';	//standards compliant browsers
			a2.style.styleFloat = 'right';	// (old) IEs
			a2.style.cursor = 'pointer';
			a2.innerHTML = 'Hide';
			var showDesc = function() { 
										a1.style.display = 'none'; 
										span.style.display = 'block'; 
										if ((span.clientWidth * 1) < minwidth) {
											//var leftmargin = span.clientWidth - minwidth;
											span.style.width = minwidth +'px';
											span.style.marginLeft = '0px';
										}
										if ((span.clientWidth * 1) > maxwidth) {
											//var leftmargin = maxwidth - span.clientWidth;
											span.style.width = maxwidth +'px';
											span.style.marginLeft = '0px';
										}
										if ((span.offsetLeft * 1) > maxleft) {
											span.style.width = minwidth +'px';
											span.style.marginLeft = '-'+ (minwidth - 20) +'px';
										}
									};
			var hideDesc = function() { 
										span.style.display = 'none'; 
										a1.style.display = 'inline'; 
									};
			a1.onmouseover = showDesc;
			span.onmouseover = showDesc;
			span.onmouseout = hideDesc;
			span.onclick = hideDesc;
			
			var innerspan = document.createElement('span');
			innerspan.style.display = 'block';
			innerspan.innerHTML = span.innerHTML;
			if (spanTitle) {
				innerspan.innerHTML = '<strong class="js-title">'+ spanTitle +'</strong><br />'+ innerspan.innerHTML
			}
			span.innerHTML = '';
			
			el.insertBefore(a1, span);
			span.appendChild(a2);
			span.appendChild(innerspan);
		});
	}
});

// --- Clickable items in masthead ----------------------------------

IFLA.set('Masthead', {
	init: function() {
		var items = $$('#masthead .item');
		if (!items.length) return;
		items.each(function(el){
			el.addEvent('click', function(){ location.href = el.getElement('a').href; });
			el.addEvent('mouseover',function(){ el.addClass('hover'); });
			el.addEvent('mouseout',function(){ el.removeClass('hover') });
		});
	}
});

// ----- INITIATE HASH ----------------------------------------------

IFLA.each(function(obj,key){
	window.addEvent('domready',obj.init.bind(obj));
});
