function activate_news_expanders(parent_element)	{
	if(parent_element == null)
		var news = $('news');
	else
		var news = parent_element;
	var news_story;
	var expand_link;
	
	clean_text_nodes(news);
	
	for(var i = 0; i < news.childNodes.length; i++)	{
		if(	news.childNodes[i].tagName == 'DIV' &&
			(news.childNodes[i].styleClass == 'article_summary' || news.childNodes[i].className == 'article_summary')
		)	{
			news_story = news.childNodes[i];	
			
			expand_link = document.createElement('a');
			expand_link.styleClass = 'expand_link';
			expand_link.className = 'expand_link';
			expand_link.innerHTML = 'expand';
			expand_link.href = 'javascript:void(0);';
			expand_link.show_fx = new Fx.Styles(news_story.childNodes[1], {duration: 500, transition: Fx.Transitions.circInOut});
			expand_link.hide_summary = function()	{
				this.element.nextSibling.style.display = 'none';	
			}
			expand_link.hide_fx = new Fx.Styles(news_story.childNodes[1], {duration: 500, transition: Fx.Transitions.circInOut, onComplete: expand_link.hide_summary});
			expand_link.target_height = news_story.childNodes[1].offsetHeight;
			
			expand_link.onclick = function()	{
				// show the one we clicked
				this.show_fx.clearTimer();
				this.style.display = 'none';
				this.show_fx.custom({
					'height' : [1, this.target_height],
					'opacity' : [0, 1],
					'padding' : [0, 5]
				});
				
				// hide the ones we didn't click				
				for(var i = 0; i < this.parentNode.parentNode.childNodes.length; i++)	{
					if(	this.parentNode.parentNode.childNodes[i].tagName == 'DIV' &&
						this.parentNode.parentNode.childNodes[i] != this.parentNode &&
						(this.parentNode.parentNode.childNodes[i].styleClass == 'article_summary' || this.parentNode.parentNode.childNodes[i].className == 'article_summary')
					)	{
						this.parentNode.parentNode.childNodes[i].childNodes[1].show_fx.clearTimer();
						this.parentNode.parentNode.childNodes[i].childNodes[1].style.display = 'inline';
						source_height = this.parentNode.parentNode.childNodes[i].childNodes[2].offsetHeight;
						this.parentNode.parentNode.childNodes[i].childNodes[1].show_fx.custom({
							'height' : [source_height, 1],
							'opacity' : [1, 0],
							'padding' : [0, 0]
						})
					}
				}
				
				// take the focus off the link
				this.blur();
			}
			
			news_story.insertBefore(expand_link, news_story.childNodes[1]);
		}
	}
	
	// activate the first news link
	for(var i = 0; i < news.childNodes.length; i++)	{
		if(	news.childNodes[i].tagName == 'DIV' &&
			(news.childNodes[i].styleClass == 'article_summary' || news.childNodes[i].className == 'article_summary')
		)	{
			news.childNodes[i].childNodes[1].onclick();
			break;
		}
	}		
}

function show_login_form()	{
	var login_form = document.createElement('div');
	
	login_form.innerHTML = '<h1>Login</h1>' +
							'<form>' +
							'<span>Username</span>' +
							'<input name="user_name" type="text"/><br/>' +
							'<span>Password</span>' +
							'<input name="password" type="password"/><br/>' +
							'<input name="action" type="hidden" value="login" style="display: none"/>' +
							'<div style="text-align: right"><img id="login_form_loader" style="visibility: hidden" src="images/ajax-loader.gif"/></div>' +							
							'<span>' +
							'<input id="login_input_button" type="button" value="login">' +
							'<input type="button" value="cancel">' +
							'</span>' +
							'<div>' +
							'<span>Don\'t have an account?</span>' +
							'<a href="register.php"> Register!</a>' +
							'</div>' +
							'</form>';

	var inputs = login_form.getElementsByTagName('input');
	for(var i = 0; i < inputs.length; i++)	{
		var input_element = inputs[i];
		input_element.onkeydown = function(evt)	{
			var key = window.event ? window.event.keyCode : evt.keyCode;
			
			if(key == 13)
				$('login_input_button').click();
		}	
	}
	
	login_form.lastChild.lastChild.previousSibling.firstChild.nextSibling.onclick = function()	{
		hide_popup(this.parentNode.parentNode.parentNode);
	};
	login_form.lastChild.lastChild.previousSibling.firstChild.onclick = function()	{
		if(this.parentNode.parentNode.getElementsByTagName('input')[0].value.strip() == '' || this.parentNode.parentNode.getElementsByTagName('input')[1].value.strip() == '')	{
			alert('Please enter your username and password.');
			this.parentNode.parentNode.getElementsByTagName('input')[0].focus();
			return false;
		}
		else	{
			$('login_form_loader').style.visibility = 'visible';
			var myAjax = new Ajax.Request(
				'services/process_login.php', 
				{
					method: 'post', 
					parameters: Form.serialize(this.parentNode.parentNode), 
					onComplete: function(ajax)	{
						if(ajax.responseText == 'Empty profile.')	{
							var profile_form = document.createElement('div');
							
							profile_form.innerHTML = '<h1>Update Your Profile</h1>' +
													'<form>' +
													'<span>Your profile is empty! You can update your profile by clicking on ' +
													'the <b>My Profile</b> link in the left menu and selecting <b>Edit ' +
													' Profile</b> or by clicking <a href="manage_profile.php">here</a>.' +
													'</span>' +
													'<div style="text-align: right">' +
													'<input class="button" type="button" value="ok">' +
													'</div>' +
													'</form>';
							
							profile_form.lastChild.lastChild.firstChild.onclick = function()	{
								window.location.reload();	
							}
							
							show_popup(profile_form);
						}
						else
							window.location.reload();
					}
				});		
				
			hide_popup(this.parentNode.parentNode.parentNode);				
		}			
	};	
	
	show_popup(login_form);
	
	// login_form.getElementsByTagName('input')[0].focus();	
}

function activate_gallery(start_index)	{
	var gallery = $('gallery');
	
	clean_text_nodes(gallery);
	
	if(gallery.gallery_data == null)	{
		var gallery_data = eval('(' + gallery.firstChild.innerHTML + ')');
		gallery.gallery_data = gallery_data;
	}
	else	{
		var gallery_data = gallery.gallery_data;
		gallery.parentNode.removeChild(gallery.nextSibling);
		gallery.parentNode.removeChild(gallery.nextSibling);
	}
	
	if(gallery_data.length == 0)
		gallery.innerHTML = 'No uploaded images.';
	else	{
		while(gallery.hasChildNodes())
			gallery.removeChild(gallery.lastChild);
		
		if(start_index == null)
			start_index = 0;
			
		for(var i = start_index; i < gallery_data.length && i < start_index + 10; i++)	{
			img = document.createElement('img');
			img.path_to_thumb = gallery_data[i].image_path + 'thumbs/' + gallery_data[i].image_filename;
			img.path_to_image = gallery_data[i].image_path + gallery_data[i].image_filename;
			img.image_id = gallery_data[i].image_id;
			img.src = gallery_data[i].image_path + 'thumbs/' + gallery_data[i].image_filename;
			img.style.width = '40%';
			// img.style.height = '100px';
			img.style.margin  = '1%';
			img.path_to_show = gallery_data[i].image_path + gallery_data[i].image_filename;
			img.path_to_thumb = gallery_data[i].image_path + 'thumbs/' + gallery_data[i].image_filename;
			img.label = gallery_data[i].image_label;
			img.onload = adjust_image_height;
			img.onclick = show_image_path;
			img.ondblclick = set_article_summary_image;
			img.onmousedown = swap_image;
			gallery.appendChild(img);
		}
	}
	
	var gallery_controls = document.createElement('div');
	gallery_controls.style.margin = '0px';
	gallery_controls.style.padding = '0px';
	gallery_controls.innerHTML = '';
	if(start_index > 0)
		gallery_controls.innerHTML += '<a style="float: left" href="javascript:activate_gallery(' + (start_index - 10) + ');">previous 10</a>';
	if(gallery_data.length - 1 > start_index + 10)
		gallery_controls.innerHTML += '<a style="float: right" href="javascript:activate_gallery(' + (start_index + 10) + ');">next 10 (of ' + gallery_data.length + ' total)</a>';
	
	var br = document.createElement('br');
	
	gallery.parentNode.insertBefore(br, gallery.nextSibling)
	gallery.parentNode.insertBefore(gallery_controls, gallery.nextSibling)
	
	gallery.scrollTop = '0px';
	
}

function swap_image()	{
	img = this;
	if(img.src.indexOf(img.path_to_thumb) != -1)
		img.src = img.path_to_show;
	else
		img.src = img.path_to_thumb;
}

function adjust_image_height()	{
	return 0;
	
	this.style.height = '';
	this.height = '';
	this.style.height = this.style.width;
}

function activate_spotlights()	{
	var spotlights_container = $('spotlights');
	var spotlights_content = $('spotlight_contents');
	clean_text_nodes($('spotlights'));
	var spotlight_tab;
	
	for(var i = 0; i < spotlights_content.childNodes.length; i += 2)	{

		spotlight_header = spotlights_content.childNodes[i];
		spotlight_header.style.cursor = 'pointer';
		spotlight_header.slide_fx = new Fx.Styles(spotlight_header.nextSibling, {duration: 750, transition: Fx.Transitions.circInOut});
		
		spotlight_header.color_fx = new Fx.Color(spotlight_header, 'color', {duration: 1500});
		spotlight_header.background_fx = new Fx.Color(spotlight_header, 'backgroundColor', {duration: 750});
		
		spotlight_header.target_height = spotlight_header.nextSibling.offsetHeight;
		spotlight_header.onclick = show_spotlight;
		spotlight_header.nextSibling.style.overflow = 'hidden';
	}
	
	spotlights_content.childNodes[0].onclick();
}

function show_spotlight()	{
	if(this.active)
		return 0;
		
	// do all the other spotlights
	for(var i = 0; i < this.parentNode.childNodes.length; i += 2)	{
		this.parentNode.childNodes[i].slide_fx.clearTimer();
		this.parentNode.childNodes[i].slide_fx.custom({
			'height' : [this.parentNode.childNodes[i + 1].offsetHeight - 10, 0],
			'opacity' : [1, 0]
		});
		
		this.parentNode.childNodes[i].color_fx.clearTimer();
		if(this.parentNode.childNodes[i].active)
			this.parentNode.childNodes[i].color_fx.custom('990000', '7F7F7F');
		else
			this.parentNode.childNodes[i].style.color = '7F7F7F';
		this.parentNode.childNodes[i].active = false;
	}
	
	// do this spotlight
	this.slide_fx.clearTimer();
	this.slide_fx.custom({
		'height' : [this.nextSibling.offsetHeight, this.target_height],
		'opacity' : [0, 1]
	});
	this.color_fx.clearTimer();
	this.color_fx.custom('7F7F7F', '990000');
	this.active = true;
	
	// for manage section
	if($('spotlights_select_id') != null)	{
		$('spotlights_select_id').value = this.nextSibling.id;
		show_spotlight_data();
	}
}

function answer_question(answer_container, question_id)	{
	clean_text_nodes(answer_container.parentNode.parentNode);
	var previous_answers = answer_container.parentNode.previousSibling;
	
	answer_form = document.createElement('div');
	
	answer_form.innerHTML = '<h1>Answer</h1>' +
							'<form enctype="multipart/form-data" method="post" action="services/process_answer.php">' +
							'<span>Name</span>' +
							'<input name="answerer" type="text"/><br/>' +
							'<span>Answer</span>' +
							'<textarea name="answer" rows=5></textarea>' +
							'<input name="action" type="hidden" value="answer" style="display: none"/>' +
							'<input name="question_id" type="hidden" value="' + 
							question_id + 
							'" style="display: none"/>' +
							'<span>' +
							'<input type="button" value="answer">' +
							'<input type="button" value="cancel">' +
							'</span>' +
							'</form>';
							
	
	answer_form.lastChild.lastChild.firstChild.onclick = function()	{
		var answerer = this.parentNode.parentNode.getElementsByTagName('input')[0];
		var answer = this.parentNode.parentNode.getElementsByTagName('textarea')[0];
		
		if(answerer.value.strip() == '')	{
			answerer.focus();
			alert('Please enter your name.');
			return 0;
		}
		if(answer.value.strip() == '')	{
			answer.focus();
			alert('Please enter an answer.');
			return 0;
		}
		
		this.parentNode.parentNode.submit();
	};
	
	answer_form.lastChild.lastChild.lastChild.onclick = function()	{
		hide_popup(this.parentNode.parentNode.parentNode);
	};

	show_popup(answer_form);	
}

function populate_articles(start_index)	{
	var article_picker = $('article_picker');
	if(article_picker == null)
		return 0;
	
	if(start_index == null)
		start_index = 0;
		
	// get rid of the loading gif
	if(article_picker.hasChildNodes())
		article_picker.removeChild(article_picker.firstChild);

	// load the json data
	if(article_picker.article_data == null)	{
		var article_json = $('article_data').innerHTML;
		var article_data = eval('(' + article_json + ')');
		article_picker.article_data = article_data;
	}
	else	{
		var article_data = article_picker.article_data;
		article_picker.parentNode.removeChild(article_picker.nextSibling);
		article_picker.parentNode.removeChild(article_picker.nextSibling);
	}
	
	if(article_data.length == 0)	{
		article_picker.innerHTML = '<div style="text-align: center">There aren\'t any articles.</div>';
	}
	
	// create the table for the picker
	var article_table = document.createElement('table');
	
	for(var i = start_index; i < article_data.length && i < start_index + 20; i++)	{
		row = article_table.insertRow(article_table.rows.length)
		cell = row.insertCell(row.cells.length);
		cell.innerHTML = (i + 1) + '.';
		cell.width = '5px';
		cell = row.insertCell(row.cells.length);
		cell.innerHTML = '<input style="width: 25px; border: none" name="selected_article" onclick="set_selected_article(this.value);" value="' + article_data[i].article_id + '" type="radio"/>';
		cell.width = '5px';
		cell = row.insertCell(row.cells.length);
		cell.innerHTML = '<span>' + article_data[i].article_title + '</span>'
		cell = row.insertCell(row.cells.length);
		cell.innerHTML = article_data[i].article_submit_date + '</span>'
		cell = row.insertCell(row.cells.length);
		cell.innerHTML = '<span class="article_author">' + article_data[i].user_name + '</span>'
	}
	
	article_picker.appendChild(article_table);
	
	var article_picker_controls = document.createElement('div');
	article_picker_controls.style.margin = '0px';
	article_picker_controls.style.padding = '0px';
	article_picker_controls.innerHTML = '';
	if(start_index > 0)
		article_picker_controls.innerHTML += '<a style="float: left" href="javascript:populate_articles(' + (start_index - 20) + ');">previous 20 articles</a>';
	if(article_data.length - 1 > start_index + 20)
		article_picker_controls.innerHTML += '<a style="float: right" href="javascript:populate_articles(' + (start_index + 20) + ');">next 20 articles (of ' + article_data.length + ' total)</a>';
	
	var br = document.createElement('br');
	
	article_picker.parentNode.insertBefore(br, article_picker.nextSibling)
	article_picker.parentNode.insertBefore(article_picker_controls, article_picker.nextSibling)
	
	article_picker.scrollTop = '0px';
}

function activate_article_expanders()	{
	var articles = $('articles');
	var article;
	var expand_link;
	clean_text_nodes(articles);
	
	for(var i = 0; i < articles.childNodes.length; i++)	{
		if(	articles.childNodes[i].tagName == 'DIV' &&
			(articles.childNodes[i].styleClass == 'article_summary' || articles.childNodes[i].className == 'article_summary')
		)	{
			article = articles.childNodes[i];
			article.style.overflow = 'hidden';
			
			article_header = article.childNodes[0];
			
			article_header.show_fx = new Fx.Styles(article.childNodes[1], {duration: 500, transition: Fx.Transitions.circInOut});
			article_header.target_height = article_header.nextSibling.offsetHeight;
			
			article_header.onmouseover = activate_article_expanders_show_article;
			
			// article.insertBefore(expand_link, article.childNodes[1]);
		}
	}
	
	// activate the first news link
	for(var i = 0; i < articles.childNodes.length; i++)	{
		if(	articles.childNodes[i].tagName == 'DIV' &&
			(articles.childNodes[i].styleClass == 'article_summary' || articles.childNodes[i].className == 'article_summary')
		)	{
			articles.childNodes[i].childNodes[0].onmouseover();
			break;
		}
	}		
}

function activate_article_expanders_show_article()	{
	if(this.active)
		return 0;

	// show the one we clicked			
	this.show_fx.clearTimer();
	this.show_fx.custom({
		'height' : [1, this.target_height],
		'opacity' : [0, 1],
		'padding' : [0, 5]
	});
	this.active = true;
	
	// hide the ones we didn't click				
	for(var i = 0; i < this.parentNode.parentNode.childNodes.length; i++)	{
		if(	this.parentNode.parentNode.childNodes[i].tagName == 'DIV' &&
			this.parentNode.parentNode.childNodes[i] != this.parentNode &&
			(this.parentNode.parentNode.childNodes[i].styleClass == 'article_summary' || this.parentNode.parentNode.childNodes[i].className == 'article_summary') &&
			this.parentNode.parentNode.childNodes[i].childNodes[1].offsetHeight != 0
		)	{
			this.parentNode.parentNode.childNodes[i].childNodes[0].active = false;
			this.parentNode.parentNode.childNodes[i].childNodes[0].show_fx.clearTimer();
			this.parentNode.parentNode.childNodes[i].childNodes[0].show_fx.custom({
				'height' : [this.parentNode.parentNode.childNodes[i].childNodes[1].offsetHeight, 1],
				'opacity' : [1, 0],
				'padding' : [0, 0]
			})
		}
	}
}

function show_previous_question_answers(question_id, element)	{
	if($('previous_questions').call_in_progress)	{
		alert('Previous answers for a question are loading.');
		return 0;	
	}
	
	var myAjax = new Ajax.Request(
	'services/html.previous_question_answers.php', 
	{
		method: 'get', 
		parameters: 'question_id=' + question_id, 
		onComplete: process_previous_answers
	});		
	
	$('previous_questions').call_in_progress = true;
	$('previous_questions').element_to_update = element.parentNode;
	
	element.parentNode.innerHTML = '<img src="images/ajax-loader.gif"/>';
}

function process_previous_answers(ajax)	{
	$('previous_questions').element_to_update.innerHTML = ajax.responseText;
	$('previous_questions').element_to_update.style.textAlign = '';
	$('previous_questions').call_in_progress = false;
}

function activate_feed_dump_viewer()	{
	if($('feed_dumps') == null)
		return 0;
		
	clean_text_nodes($('feed_dumps'));
	
	var feed_dumps = $('feed_dumps').childNodes;
	
	for(var i = 0; i < feed_dumps.length - 3; i += 4)	{
		// feed_dumps[i].nextSibling.show_fx = new Fx.Styles(feed_dumps[i].nextSibling, {duration: 500, transition: Fx.Transitions.backOut});		
		// feed_dumps[i].nextSibling.hide_fx = new Fx.Styles(feed_dumps[i].nextSibling, {duration: 500, transition: Fx.Transitions.backIn, onComplete: feed_dumps[i].nextSibling.hide});
		
		var feed_url = feed_dumps[i + 1].innerHTML;
		var feed_id = feed_dumps[i].id;
		
		var myAjax = new Ajax.Request(
		'services/json.rss_reader.php', 
		{
			method: 'get', 
			parameters: 'feed_url=' + feed_url + '&feed_id=' + feed_id,
			onComplete: process_feed_items
		});		
	}	
}

function process_feed_items(ajax)	{
	var feed_data = eval('(' + ajax.responseText + ')');
	$(feed_data.feed_id).nextSibling.nextSibling.innerHTML = feed_data.feed_items_markup;
	$(feed_data.feed_id).nextSibling.nextSibling.style.padding = '0px';
	$(feed_data.feed_id).nextSibling.nextSibling.style.margin = '0px';
	$(feed_data.feed_id).style.display = 'none';
	$(feed_data.feed_id).innerHTML = feed_data.feed_title;
	activate_news_expanders($(feed_data.feed_id).nextSibling.nextSibling);
}
