current_question 	= false;
faq_url				= '/staticfiles/faq/faq.php';
faq_loader_img		= '/images/ajax-loader.gif';

function init_faq(elt) {
	ss 		= new Element('link', { rel: 'stylesheet', type: 'text/css', href: '/staticfiles/faq/styles.css' });
	document.body.appendChild(ss);
		
	qdiv 	= new Element('div',  { 'class': 'questions', 'id': 'questions' });	
	atdiv 	= new Element('div',  { 'class': 'text', 'id': 'answers' }).update('Click on a question to the left to see it\'s answer.');
	ahdiv 	= new Element('div',  { 'class': 'answers', 'id': 'answers_cnt' });	
	abdiv 	= new Element('div',  { 'class': 'bottom' });
	
	tdiv 	= new Element('div',  { 'class': 'linkage', 'id': 'top_linkage' });	
	mdiv 	= new Element('div',  { 'class': 'linkage', 'id': 'middle_linkage' });	
	bdiv 	= new Element('div',  { 'class': 'linkage', 'id': 'bottom_linkage' });	
	
	if(!$('top_linkage')) {		
		document.body.appendChild(tdiv);
		document.body.appendChild(mdiv);
		document.body.appendChild(bdiv);
	} else
		hide_linkages();
		
	current_question = false;
	
	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 	= m_xml.create_doc(o.responseText);			
		items 	= xmlDoc.getElementsByTagName('item');			
		
		ol 		= new Element('ol');
		
		for(x = 0; x < items.length; x++) {
			itm = items[x];
			
			qtxt	= m_xml.get_text(itm, 'question');
			qid		= m_xml.get_text(itm, 'id');
						
			li 		= new Element('li', { 'id': 'q-' + qid }).update(qtxt);
			
			li.onclick 	= function() { get_answer(this); }
			
			ol.appendChild(li);
		}
		
		elt.appendChild(ol);	

		get_answer($$('div.questions ol li').first());
		
		// ie7 fix
		document.body.style.zoom = '1';
	}
	
	//loader(elt, faq_loader_img, 'Loading FAQ...');	
	
	new Ajax.Request(faq_url + '?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.ceil( (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',
		left: 	(hs.qpos.left + hs.qw + 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 = m_xml.create_doc(o.responseText);	
		
		setTimeout(function() { 
			aelt.setStyle({ overflow: 'auto' }); 
			aelt.innerHTML = m_xml.get_text(xmlDoc, 'answer'); 
			link_question();	
		}, 500);	
		
		link_question();			
	}
	
	aelt.innerHTML = '';
	//loader(aelt, faq_loader_img, 'Loading Answer...');
	
	aelt.setStyle({ overflow: 'hidden' });
		
	tid = elt.id.substring(elt.id.indexOf('-') + 1);
	elt.addClassName('active');
	current_question = elt;		
	
	link_question();
	
	new Ajax.Request(faq_url + '?a=answer&q=' + tid + '&j=' + ut(), { 
		method: 'get', 
		onSuccess: ga_handle
	}); 	
}
