// Start function when DOM has completely loaded 
$(document).ready(function(){ 
	// Open the faqData.xml file
	$.get("faq.xml",{},function(xml){
		myHTMLOutput = '';
		catCounter = '1';
		questionCounter = '1';
		// Walk the XML tree...
		$('categories',xml).each(function() {
			$kids = $(this).children();
			$($kids).each(function() {
				catName = $(this).attr("name");
				catTitle = $(this).attr("title");
				catMessage = $(this).find("message").text();
				myHTMLOutput += buildCategoryText(catName, catTitle, catMessage, catCounter);
				catCounter++;
				$categoryKids = $(this).children();
				$($categoryKids).each(function() {
					$subcategoriesKids = $(this).children();
					$($subcategoriesKids).each(function() {
						subcatName = $(this).attr("name");
						myHTMLOutput += buildSubcatText(subcatName);
						$subcategoryKids = $(this).children();
						$($subcategoryKids).each(function() {
							$questionsKids = $(this).children();
							$($questionsKids).each(function() {
								questionIndex = questionCounter;
								questionValue = $(this).find("value").text();
								questionAnswer = $(this).find("answer").text();				
								// Build HTML data and store in string
								if(questionValue != "") {
								mydata = buildQuestionHTML(questionIndex, questionValue, questionAnswer);
								myHTMLOutput = myHTMLOutput + mydata;
								questionCounter++;
								}
							});
						});
					});
				});
			});
		});	
		// Update the DIV called faqContent with the HTML string
		$("#faqContent").append(myHTMLOutput);
		$("#faqContent").append(tANDc);
		$("p.SubHeader:contains('Credit Card')").attr("id","creditcard");
		$("a:contains('email')").not("a:contains('email preferences')").not("a:contains('email ')").attr("href", "/talk/ttu.html");
		$("a:contains('email us')").attr("href", "/talk/ttu.html");
		//$("a[href='/emailus/servlet/EmailUs?cmd=go']").replaceWith("<a href='/talk/ttu.html'>email us</a>");
		$("#faqContent a").addClass("TextLinkCopyUnderline");
		//$("a[@href^='http']").attr('target','_blank');
		$("#topAnchorLinks").show();
		if (location.hash != "") {
			document.location = location.hash;
		}
		//handle arrow and answer toggles
		$('span.BodyCopy12rightArrow').click(function() {
			var id = $(this).attr('id');
			var host = window.location.hostname;
			var protocol = window.location.protocol;
			var protocolFinal = protocol.split(":");

			var currImage = $('#arrowToggle' + id).css('backgroundImage');
			$('#arrowToggle' + id).css({
				backgroundImage: (currImage == 'url('+protocolFinal[0]+'://' + host + '/images/arrow_red_right.gif)') 
					|| (currImage == 'url("'+protocolFinal[0]+'://' + host + '/images/arrow_red_right.gif")') 
					? 'url('+protocolFinal[0]+'://' + host + '/images/arrow_red_down.gif)' 
					: 'url('+protocolFinal[0]+'://' + host + '/images/arrow_red_right.gif)'
				});
			$('#faqAnswer' + id).toggle();
			return false;
		});
	});
});
  
function buildQuestionHTML(questionIndex, questionValue, questionAnswer){
	// Build HTML string and return
	output = '';
	
	roundedBoxBegin = '<b class="AdModule">';
	roundedBoxBegin += '<b class="AdModule1" ><b class="modline"></b></b>';
	roundedBoxBegin += '<b class="AdModule2"><b></b></b>';
	roundedBoxBegin += '<b class="AdModule3"></b>';
	roundedBoxBegin += '<b class="AdModule4"></b>';
	roundedBoxBegin += '<b class="AdModule5"></b></b>';
	roundedBoxBegin += '<div class="AdModuleBox">';
	roundedBoxBegin += '<span class="BodyCopy12">';
	
	output += '<span class="BodyCopy12rightArrow" id="Q'+ questionIndex +'">';
	output += '<strong>'+ questionIndex + '.</strong>';
	output += ' ' + questionValue + ' ';
	output += '<span id="arrowToggleQ' + questionIndex + '" class="faqClosed">&nbsp;</span></span>';
	output += '<div id="faqAnswerQ' + questionIndex + '" class="faqAnswerContainer">';
	output += roundedBoxBegin;
	output += '<p class="BodyCopy12">' + questionAnswer + '</p><span style="clear: both; height: 0;" />';

	roundedBoxEnd = '</span></div>';
	roundedBoxEnd += '<b class="AdModule">';
	roundedBoxEnd += '<b class="AdModule5"></b>';
	roundedBoxEnd += '<b class="AdModule4"></b>';
	roundedBoxEnd += '<b class="AdModule3"></b>';
	roundedBoxEnd += '<b class="AdModule2"><b ></b></b>';
	roundedBoxEnd += '<b class="AdModule1"><b class="modline"></b></b></b>';
	roundedBoxEnd += '</div><br />';	

	output += roundedBoxEnd;
	return output;
}
function buildCategoryText(catName, catTitle, catMessage, catCounter) {
	output = '';
	if(catCounter > 1) { output += '<br />'; }
	catTitle = catTitle.replace(/Delta and Northwest Merger/i,"");
	output += '<h1 class="PageSubHeader" id="' + catName + '">' +  catTitle + '</h1>'
	output += '<p class="BodyCopy12">' + catMessage + '</p>';
	
	return output;
}
function buildSubcatText(subcatName) {
	output = '';
	output += '<p class="SubHeader">' +  subcatName + '</p>'
	
	return output;
}

var tANDc = '<p class="BodyCopy12"><strong>Terms and Conditions:</strong> All SkyMiles program and SkyMiles program rules apply to SkyMiles and SkyMiles program membership, miles, offers, mile accrual, mile redemption and travel benefits, respectively. To review the rules, please visit <a target="_blank" href="http://www.delta.com/skymiles/about_skymiles/membership_guide_program/index.jsp" class="TextLinkCopyUnderline">delta.com/memberguide</a> and <a id="ext12" target="_blank" href="http://www.nwa.com/skymiles/" class="TextLinkCopyUnderline">nwa.com/worldperks</a>.</p>';