
var sections;
var years;

var loading_template = "<div class=\"col1 left\">&nbsp;</div><div class=\"col2 left\">[txt]</div>";

Event.observe(window, 'load', function() {

	/////////////////////////////////////////////////
	// SUB-SECTIONS
	
	sections = $('content').getElementsByClassName('sections');
	
	$A(sections).each( function(el) {
		el.onclick = function() {
			setUpSections(el);
			return false;
		}
		if (el.attachEvent) {
			el.attachEvent("onclick", function() {
				setUpSections(el);
				return false;
			});
		}
	});
	
	/////////////////////////////////////////////////
	// YEARS
	
	years = $('content').getElementsByClassName('years');
	
	$A(years).each( function(year_div) {
		var year_links = year_div.getElementsByTagName('a');
		$A(year_links).each( function(y) {
			y.onclick = function() {
				setUpYears(y);
				return false;
			}
			if (y.attachEvent) {
				y.attachEvent("onclick", function() {
					setUpYears(y);
					return false;
				});
			}
		});
	});
	
	/////////////////////////////////////////////////
	// MONTHS
	
	months = $('content').getElementsByClassName('months');
	
	$A(months).each( function(month_div) {
		var month_links = month_div.getElementsByTagName('a');
		$A(month_links).each( function(m) {
			m.onclick = function() {
				setUpMonths(m);
				return false;
			}
			if (m.attachEvent) {
				m.attachEvent("onclick", function() {
					setUpMonths(m);
					return false;
				});
			}
		});
	});
	
});


function setUpSections(el) {
	var section_id = el.getAttribute('id');
	$A(sections).each(function(s) {
		var cl = s.className;
		s.className = cl.replace(' selected', ' link');
	});
	renderText('');
	if ($('years_'+section_id)) {
		hideAllMonths();
		hideAllYears();
		var cl = el.className;
		el.className = cl.replace(' link', ' selected');
		$('years_'+section_id).style.display="block";
	}
}

function setUpYears(y) {
	var year_id = y.getAttribute('id');
	turnOffSelected(years);
	renderText('');
	if ($('months_'+year_id)) {
		hideAllMonths();
		var cl = y.className;
		y.className = cl.replace('more ', 'selected_bold ');
		$('months_'+year_id).style.display="block";
	}
}

function setUpMonths(m) {
	var h = m.getAttribute('href');
	turnOffSelected(months);
	renderText('Loading...');
	var cl = m.className;
	m.className = cl.replace('more ', 'selected_bold ');
	new Ajax.Request(h+'ajax/', {
		method:'post', 
		onComplete: function(r) {
    		var response = r.responseText || "no response text";
			$('month_list').innerHTML = response;
			initLightbox();
		}			
	});
}

function hideAllYears() {
	$A(years).each(function(y) {
		y.style.display="none";
	});
}

function hideAllMonths() {
	$A(months).each(function(m) {
		m.style.display="none";
	});
}

function turnOffSelected(ar) {
	$A(ar).each(function(div) {
		var links = div.getElementsByTagName('a');
		$A(links).each( function(el) {
			var cl = el.className;
			el.className = cl.replace('selected_bold ', 'more ');
		});
	});
}

function renderText(txt) {
	$('month_list').innerHTML = loading_template.replace("[txt]", txt);
}

