/**
 *
 * @access public
 * @return void
 **/
function toggle_section(e) {
	var container = Element.nextSiblings(e)[0];
	if(container.classNames().toString().indexOf('memorize') >= 0) {
		var name = container.id;
		var panel_cookie = GetCookie('panel_stats')
		var panel_stats = (panel_cookie)? panel_cookie.split('|') : [];

		var term = panel_stats.detect(function(value,index){if(value.split(':')[0] == name) return true;});
		var status_exists = (term != null);

		var status = 0;
		if(status_exists) status = term.split(':')[1];
		status = (status == 0)? 1 : 0;
		term = name+':'+status;

		if(status_exists) {
			panel_stats.detect(function(value,index){if(value.split(':')[0] == name) panel_stats[index] = term;});
		}
		else {
			panel_stats[panel_stats.length] = term;
		}
		SetCookie('panel_stats', panel_stats.join('|'), null, '/');

	}
	else {
		var images    = $A(e.getElementsByTagName('img'));
		var img       = images[images.length-1];
		status = (img.src.indexOf('plus') != -1)? 1 : 0;
	}
	set_section(container, status);
}

/**
 *
 * @access public
 * @return void
 **/
function set_section(container, status, fast) {
	if(typeof container == 'string') container = $(container);
	if(!container) return;

	var section_bar = container.previousSiblings()[0];
	var images    = $A(section_bar.getElementsByTagName('img'));
	var img       = images[images.length-1];

	if(status == 1) {
		if(fast) {
			container.show();
		}
		else {
			new Effect.BlindDown(container, {duration:0.3});
		}
		img.src = img.src.replace('plus', 'minus');
	}
	else {
		if(fast) {
			container.hide();
		}
		else {
			new Effect.BlindUp(container, {duration:0.3});
		}
		img.src = img.src.replace('minus', 'plus');
	}
}
function sec_set(bar, status, fast) {

	var container = bar.next();
	var img = bar.down('.sec_expd_img');

	if(status == 1) {
		if(fast) container.show();
		else     new Effect.BlindDown(container, {duration:0.3});

		img.src = img.src.replace('plus', 'minus');
	}
	else {
		if(fast) container.hide();
		else     new Effect.BlindUp(container, {duration:0.3});

		img.src = img.src.replace('minus', 'plus');
	}
}

/**
 *
 * @access public
 * @return void
 **/
function sec_toggle(e) {
	var bar = e.target;
	var img = bar.down('.sec_expd_img');
	var status = (img.src.indexOf('plus') != -1)? 1 : 0;
	sec_set(bar, status, false);
}

var sections_animated = false;
/**
 *
 * @access public
 * @return void
 */
function animate_sections() {
	// for some unknown reason this function gets called twice, so this flag
	// prevents it from happening (should be analyzed and removed)
	if(sections_animated) return;

	$$('.seccion').each(function(e) {

		if(!e.hasClassName('expand')) return;

		var bar = e.down('.sec_bar');
		var img = new Element('img', {className: 'sec_expd_img', src: '/images/minus.gif'});
		bar.down().insert({before: img});
		bar.setStyle({cursor: 'pointer'});
		bar.observe(
			'click',
			sec_toggle.bind(bar)
		);
	});
	sections_animated = true;
}

Event.observe(window, 'load', function() {
	animate_sections();
});
