// JavaScript Document




function doUpdate(transport) {
answersText = transport.responseText.evalJSON();

if (answersText['all']['count'] < 10) {
$('NextLink').style.display = 'none';
} else {
$('NextLink').style.display = '';
}
var count = 0;
window.scroll(0,0);
 $('answerbox').removeChild($('answerbox').firstChild);
showing = document.createElement('p');
showingB = document.createElement('strong');
showingB.appendChild(document.createTextNode('Viewing '+(parseInt(curPage)+1)+'-'+(parseInt(curPage)+answersText['all']['count'])+' of Hundreds'));
showing.appendChild(showingB);
 $('answerbox').appendChild(showing);
answersText['all']['questions'].each(function(question) {
count++;
mainDiv = document.createElement('div');
mainDiv.addClassName('QuestionBox');
userDiv = document.createElement('div');
userDiv.addClassName('UserBox');
userImg = document.createElement('img');
if ( question['UserPhotoURL'] == '')  question['UserPhotoURL'] = 'http://l.yimg.com/a/i/identity/nopic_48.gif';
userImg.setAttribute('src', question['UserPhotoURL']);
userP = document.createElement('p');
userP.appendChild(document.createTextNode(question['UserNick']));
userDiv.appendChild(userImg);
userDiv.appendChild(userP);
mainDiv.appendChild(userDiv);
questDiv =  document.createElement('div');
questDiv.addClassName('QuestionText');
questH = document.createElement('h2');
questH.appendChild(document.createTextNode(question['Subject'].escapeHTML()));

text = '<p>'+question['Content'].escapeHTML().gsub(/http:\/\/[^\s\"^\r^\n]+/,'<a href="#{0}">#{0}</a>').gsub('\r','<br />')+'</p>';
questDiv.appendChild(questH);
new Insertion.Bottom(questDiv, text);
//questDiv.appendChild(questP);
ansBar = document.createElement('div');
ansBar.addClassName('AnswerBar');
ansA = document.createElement('a');
ansA.setAttribute('href', './?start='+curPage+'&showAnswer='+question['id']);
ansA.addClassName('reveal');
ansA.id = 'Reveal'+count;
ansP = document.createElement('p');
ansImg = document.createElement('img');
ansImg.setAttribute('src','images/reveal.gif');
ansImg.setAttribute('alt','Reveal Answer');
ansA.appendChild(ansImg);
ansP.appendChild(ansA);
new Insertion.Bottom(ansP,'&nbsp;&nbsp; Best of '+question['NumAnswers']+' answers');
//ansP.insert('&nbsp;&nbsp; Best of '+question['NumAnswers']+' answers');

ansBar.appendChild(ansP);
questDiv.appendChild(ansBar);
ansText = document.createElement('div');
ansText.addClassName('AnswerText');
ansText.id = 'Answer'+count;
ansText.style.display = "none";
ansClose = document.createElement('div');
ansClose.addClassName('CloseBox');
closeImg = document.createElement('img');
closeImg.setAttribute('src','images/close.gif');

closeImg.id = 'close'+count;
ansClose.appendChild(closeImg);
ansText.appendChild(ansClose);

//ansTextP = document.createElement('p');
//ansTextP.insert(question['ChosenAnswer'].gsub('\r','<br />'));
//ansText.appendChild(ansTextP);
new Insertion.Bottom(ansText,'<p>'+question['ChosenAnswer'].escapeHTML().gsub(/http:\/\/[^\s^\"^\r^\n]+/,'<a href="#{0}">#{0}</a>').gsub('\r','<br />')+'</p>');

questDiv.appendChild(ansText);
clearDiv = document.createElement('div');
clearDiv.addClassName('clearBox');

questDiv.appendChild(clearDiv);
mainDiv.appendChild(questDiv);

$('answerbox').appendChild(mainDiv);


});
initAnswers();
}





function initAnswers() {

	$$('.reveal').each(function(button) {
	
		button.observe('click',function(event) {
Event.stop(event);
			$$('.AnswerText').each(function(answers) {
			
				if (answers.style.display != 'none' && answers.id != 'Answer'+button.id.sub('Reveal','')) {
					Effect.BlindUp(answers);
				
				}


			});
			if ($('Answer'+button.id.sub('Reveal','')).style.display == 'none') {
				Effect.BlindDown('Answer'+button.id.sub('Reveal',''));
	//$('Answer'+button.id.sub('Reveal','')).style.display = '';
			}

			return false;
		});
		
	});
	
	$$('.CloseBox img').each(function(button) {
		button.observe('click',function(event) {
			Effect.BlindUp('Answer'+this.id.sub('close',''));
		});
	});
	
}

function initAll() {
initAnswers();

 $('PrevLink').observe('click',function(event) {
Event.stop(event);
curPage -= 10;
if (curPage <= 0) { curPage = 0; $('PrevLink').style.display = 'none'; } 
while ($('answerbox').firstChild) {
 $('answerbox').removeChild($('answerbox').firstChild);
}
loading = document.createElement('img');
loading.setAttribute('src','images/loading.gif');
loading.setAttribute('alt','loading');
$('answerbox').appendChild(loading);

new Ajax.Request('request.php',{
  method: 'get',
  parameters: 'start='+curPage+'&results=10',
  onSuccess: function(transport) { doUpdate(transport); } });


});

$('NextLink').observe('click',function(event) {
$('PrevLink').style.display = '';
Event.stop(event);
curPage += 10;
while ($('answerbox').firstChild) {
 $('answerbox').removeChild($('answerbox').firstChild);
}
loading = document.createElement('img');
loading.setAttribute('src','images/loading.gif');
loading.setAttribute('alt','loading');
$('answerbox').appendChild(loading);

new Ajax.Request('request.php',{
  method: 'get',
  parameters: 'start='+curPage+'&results=10',
  onSuccess: function(transport) { doUpdate(transport); } });


}); 

}

Event.observe(window, 'load', function() { initAll(); });

