current_question = false;

function init_faq(elt) {
	qdiv 	= $(document.createElement('div'));	// question div
	
	atdiv 	= $(document.createElement('div'));	// answer text div
	ahdiv	= $(document.createElement('div'));	// answer header div
	abdiv	= $(document.createElement('div'));	// answer bottom div
	
	tdiv 	= $(document.createElement('div'));	// top linkage div
	mdiv 	= $(document.createElement('div'));	// middle linkage div
	bdiv 	= $(document.createElement('div'));	// bottom linkage div
	
	if(!$('top_linkage')) {
		tdiv.addClassName('linkage'); 	tdiv.id = 'top_linkage';
		mdiv.addClassName('linkage'); 	mdiv.id = 'middle_linkage';
		bdiv.addClassName('linkage'); 	bdiv.id = 'bottom_linkage';
		
		document.body.appendChild(tdiv);
		document.body.appendChild(mdiv);
		document.body.appendChild(bdiv);
	} else
		hide_linkages();
		
	current_question = false;
		
	qdiv.addClassName('questions');	qdiv.id = 'questions';	
	
	ahdiv.addClassName('answers');	ahdiv.id = 'answers_cnt';
	abdiv.addClassName('bottom');		
	atdiv.addClassName('text');		atdiv.id = 'answers';
	
	atdiv.innerHTML = "Click on a question to the left to see it's answer.";
	
	ahdiv.appendChild(atdiv);	// text div and bottom div child of header div
	ahdiv.appendChild(abdiv);	
	
	elt.appendChild(ahdiv);		// header div is the container for the answer, append to elt
	elt.insert({ 'top' : ahdiv });
	elt.appendChild(qdiv);	
	
	load_questions(qdiv);		
}

function load_questions(elt) {
	gq_handle = function(o) {
		elt.innerHTML = '';
		
		xmlDoc = create_xml_doc(o.responseText);	
		
		items 	= xmlDoc.getElementsByTagName('item');	
		
		ol 		= $(document.createElement('ol'));
		
		for(x = 0; x < items.length; x++) {
			itm = items[x];
			
			qtxt	= getText(itm, 'question');
			qid		= getText(itm, 'id');
			
			li 				= $(document.createElement('li'));
			li.id 			= 'q-' + qid;
			li.innerHTML 	= qtxt;
			
			li.onclick 		= function() { 				
				get_answer(this);
			}
			
			ol.appendChild(li);
		}
		
		elt.appendChild(ol);	

		get_answer($$('div.questions ol li')[0]);
		
		// ie7 fix
		document.body.style.zoom = '1';
	}
	
	loader(elt, 'images/loader.gif', 'Loading FAQ...');	
	
	new Ajax.Request('staticfiles/faq/faq.php?a=questions&j=' + ut(), { 
		method: 'get', 
		onSuccess: gq_handle 
	}); 
}

function link_question() {
	aelt = $('answers');	
	qelt = $(current_question);
		
	hs = { 
		'qw' 	: qelt.getWidth(), 
		'qh' 	: qelt.getHeight(), 
		'qpos'	: qelt.cumulativeOffset(),
		'aw'	: aelt.getWidth(),
		'ah'	: aelt.getHeight(),
		'apos'	: aelt.cumulativeOffset()
	};
	
	/*
	ofs = $('answers_cnt').cumulativeScrollOffset().top - hs.qpos.top - (hs.ah + 128);	
	$('answers_cnt').setStyle({
		marginTop: (ofs > 0 ? ofs : 16) + 'px'
	});	
	*/
	
	vert_width	= 4;	
	horiz_width = Math.floor( (hs.apos.left - (hs.qpos.left + hs.qw)) / 2 );	

	tdiv = $('top_linkage');		
	mdiv = $('middle_linkage');
	bdiv = $('bottom_linkage');
	
	top_linkage_pos 	= hs.qpos.top + Math.floor(hs.qh / 2 - vert_width / 2);
	bottom_linkage_pos 	= hs.apos.top + Math.floor(hs.ah / 2 - vert_width / 2);
	
	tdiv.setStyle({
		width: 	horiz_width + 'px',
		height: vert_width + 'px',
		left: 	(hs.qpos.left + hs.qw) + 'px',
		top: 	top_linkage_pos + 'px',
		zIndex: '999'
	});

	mdiv.setStyle({
		width: 	vert_width + 'px',
		height:	( Math.abs(top_linkage_pos - bottom_linkage_pos) + vert_width ) + 'px',
		left: 	(hs.qpos.left + hs.qw + horiz_width) + 'px',
		top: 	(top_linkage_pos > bottom_linkage_pos ? bottom_linkage_pos : top_linkage_pos) + 'px',
		zIndex: '999'
	});

	bdiv.setStyle({
		width: 	horiz_width + 'px',
		height: vert_width + 'px',
		left: 	(hs.apos.left - horiz_width) + 'px',
		top: 	bottom_linkage_pos + 'px',
		zIndex: '999'
	});
}

function hide_linkages() {
	if($('top_linkage')) {
		$('top_linkage').hide(); $('middle_linkage').hide(); $('bottom_linkage').hide();	
	}
}

function show_linkages() {
	if($('top_linkage')) {
		$('top_linkage').appear(); $('middle_linkage').appear(); $('bottom_linkage').appear();	
	}
}

function destroy_linkages() {
	if($('top_linkage')) {
		$('top_linkage').parentNode.removeChild($('top_linkage')); 
		$('middle_linkage').parentNode.removeChild($('middle_linkage')); 
		$('bottom_linkage').parentNode.removeChild($('bottom_linkage')); 
	}
}

function get_answer(elt) {
	if(current_question)
		$(current_question).removeClassName('active');
	else
		show_linkages();	

	aelt = $('answers');
	
	ga_handle = function(o) {
		xmlDoc = create_xml_doc(o.responseText);			
		setTimeout(function() { 
			aelt.setStyle({ overflow: 'auto' }); aelt.innerHTML = getText(xmlDoc, 'answer'); 
		}, 500);		
		link_question();			
	}
	
	aelt.innerHTML = '';
	loader(aelt, 'images/loader.gif', 'Loading Question...');
	
	aelt.setStyle({ overflow: 'hidden' });
		
	tid = elt.id.substring(elt.id.indexOf('-') + 1);
	elt.addClassName('active');
	current_question = elt;		
	
	link_question();
	
	new Ajax.Request('staticfiles/faq/faq.php?a=answer&q=' + tid + '&j=' + ut(), { 
		method: 'get', 
		onSuccess: ga_handle
	}); 	
}
