var currentSection = "";
function toggleSelection(e) {

	var index = 0;
	if(e.target.hasClassName('question')) {
		index = 0;
	} else {
		index = 1;
	}

	var parent = e.target.ancestors()[index];
	var answer = parent.getElementsByClassName("answer")[0];

	// close any previously opened section
	if (currentSection == "") {
		// do nothing
	} else if (currentSection == parent) {
		// do nothing
	} else if (currentSection.hasClassName('selected')) {
		//close previous section 
		currentSection.removeClassName('selected');
		currentSection.getElementsByClassName("answer")[0].show();
	}

	if(parent.hasClassName('selected')) {
		//close it
		parent.removeClassName('selected');
		answer.hide();
	} else {
		//open it
		parent.addClassName('selected');
		answer.show();
	}
	currentSection = parent;

}

function init() {

	if(document.location.toString().indexOf("#") != -1) {
		var anchor = document.location.toString().substring(document.location.toString().indexOf("#")+1,document.location.toString().length);
	}

	$A($(document.body).getElementsByClassName("question")).each(function(s) {
		Event.observe(s, "click", toggleSelection);
		Event.observe(s, "mouseover", function(e) {
			try {
				var parent = e.target.ancestors()[0];
				parent.addClassName("hover");
			} catch (err) {
				// do nothing
			}
		});
		Event.observe(s, "mouseout", function(e) {
			try {
				var parent = e.target.ancestors()[0];
				parent.removeClassName("hover");
			} catch (err) {
				// do nothing
			}
		});
		if(s.ancestors()[0].id == anchor) {
			var obj = new Object();
			obj.target = s;
			toggleSelection(obj);
		}
	});
	

}


function getNodeValue(obj,tag)
{
	return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}


Event.observe(document, 'dom:loaded', function () {

	/*	http://dev.www.warnerbros.com/main/data/WebsiteUsage/Help%20-%20Message%20Board%20FAQ.xml	*/
	/*	../data/messageboard_faq.xml														*/
	/*	'../data/WebsiteUsage/Help%20-%20Message%20Board%20FAQ.xml'							*/
	
	new Ajax.Request (dataXML, {
	  method: 'get',
	  onSuccess: function(transport){
		
			var help = transport.responseXML.getElementsByTagName('help');
			
			
			for (var i=0;i<help.length;i++)
			{
				var descriptionTxt = getNodeValue(help[i],'description');
				var headerTxt = getNodeValue(help[i],'header');
				var idName = 'help' + i;
				
				// create anchor divs for deep linking
				var anchorDIV = document.createElement('a');
				anchorDIV.setAttribute('name', idName);
				anchorDIV.setAttribute('name', idName);	// ie-fix
				
				// create faq, header, arrow and description divs
				var faqDIV = document.createElement('div');
				faqDIV.setAttribute('class', 'faq');
				faqDIV.setAttribute('className', 'faq');	// ie-fix
				faqDIV.setAttribute('id', idName);

				var headerDIV = document.createElement('div');
				headerDIV.innerHTML = headerTxt;				
				headerDIV.setAttribute('class', 'question');
				headerDIV.setAttribute('className', 'question');

				var arrowDIV = document.createElement('div');
				arrowDIV.setAttribute('class', 'arrow-icon');
				arrowDIV.setAttribute('className', 'arrow-icon');	// ie-fix

				var descDIV = document.createElement('div');
				descDIV.innerHTML = descriptionTxt;
				descDIV.setAttribute('class', 'answer');
				descDIV.setAttribute('className', 'answer');		// ie-fix

					
				// insert divs in 'faq-panel'
				new Insertion.Top(faqDIV, headerDIV);
				new Insertion.Top(headerDIV, arrowDIV);
				new Insertion.Bottom(faqDIV, descDIV);
				new Insertion.Bottom('faq-panel', anchorDIV);
				new Insertion.Bottom('faq-panel', faqDIV);
				
			}


		},
	  onComplete: function () {
			init();
		}

	  });	
}); 



