var loaded = false;
// PageLoad function
// This function is called when:
// 1. after calling $.historyInit(); // no longer called after this... we call it manually.
// 2. after calling $.historyLoad();
// 3. after pushing "Go Back" button of a browser
function pageload(hash) {
	// hash doesn't contain the first # character.
	if (hash) {
		loaded=true;
		// restore ajax loaded state
		if (hash.indexOf('|') != - 1) {
			topic = hash.split("|")[0];
			ques = hash.split("=")[1];
		} else {
			topic = hash;
			ques = null;
		}
		var item = $("td.col-topics span a[rel='" + topic + "']");
		faq.update_selected_topic(item);

		$('td.col-questions').load("/faq/questions/" + topic + "/", function () {
			$('td.col-questions span a').eq(0).addClass('selected');
			$('td.col-questions span a.selected div.white-arrow3').css('visibility', 'visible');

			if (ques != null) {
				$('td.col-questions span a.selected div.white-arrow3').css('visibility', 'hidden');
				$('td.col-questions span a.selected').removeClass('selected');
				$('td.col-questions span a[rel=' + ques + ']').addClass('selected');
				$('td.col-questions span a.selected div.white-arrow3').css('visibility', 'visible');
			}
			faq.load_answers();
		},
		"xml");
	}
}

var faq = {
	landing: true,

	load_answers: function (qid) {
		var selQuestion = $('td.col-questions span a.selected');
		var qid = selQuestion.attr('id');
		var answerPg = selQuestion.attr('rel');

		$('td.col-questions span a').each(function () {
			$('.white-arrow3', this).css('padding-top', (parseInt((parseInt($(this).height()) - 13) / 2)));
		});

		$('.viewAll').removeAttr('rel');

		$('#official-answer').load("/faq/official-answers/get/" + qid + "/", function (data) {
			if (data == '') {
				$('#official-answer').hide();
			} else {
				$('#official-answer').show();
				runExpandText();
			}
		},
		"xml");

		$('#faq-answers').load("/faq/image-answers/get/" + qid + "/", function (data) {
			if (data == '') {
				$('.viewAll').hide();
			} else {
				$('.viewAll').show();
				$('.viewAll').attr('rel', answerPg);
			}
		},
		"xml");

		var ques = '';
		var detail = $(selQuestion).attr('href');
		if (detail.indexOf('|') != -1) {
			topic = detail.replace(/\#|\|.+/g,'').replace('+',' ');
			ques = detail.replace(/.+\=\/faq\/|\//g,'');
		} else {
			topic = detail;
			ques = '';
		}
		s.pageName = 'Mormon.org: FAQ: ' + topic + ' : ' + ques;
		s.prop12 = topic + ' : ' + ((faq.landing) ? 'Landing : ' : '') + ques;
		s.t();

		faq.landing = false;
	},

	update_selected_topic: function (item) {
		$('td.col-topics span a.selected').removeClass('selected');
		$('div.white-arrow').remove();
		item.addClass('selected');
		item.before('<div class="white-arrow" style="position: absolute; width: 10px; height: 15px; margin-left: 179px; margin-top: 9.5px;"><img src="/bc/assets/images/arrow-faq-white.png"/></div>');
		topic = item.attr('rel');
	},

	update_selected_question: function (item) {
		$('td.col-questions span a.selected div.white-arrow3').css('visibility', 'hidden');
		$('td.col-questions span a.selected').removeClass('selected');
		item.addClass('selected');
		$('td.col-questions span a.selected div.white-arrow3').css('visibility', 'visible');
	},

	load_questions: function () {
		topic = $('td.col-topics span a.selected').attr('rel');
		$('td.col-questions').load("/faq/questions/" + topic + "/", function () {
			$('td.col-questions span a').eq(0).addClass('selected');
			$('td.col-questions span a.selected div.white-arrow3').css('visibility', 'visible');
		},
		"xml");
		//faq.load_answers();
	}
};

function FixIE7FirstQuestion(){ FixIE7QuestionHeight($('.col-questions > span > div').eq(0)); }

function FixIE7QuestionHeight(question){
	// hack to make IE7 show the proper height on the first question
	$(question).height(
		$(question).height()
	);
}

$(document).ready(function () {
	// Initialize history plugin.
	// The callback is called at once by present location.hash.
	$.historyInit(pageload);
    
	$('#faq').addClass('selected');

	var hash = location.hash.replace(/\?.*$/, '').replace(/^#/, '');
 	if(hash == ""){
		var defaultTopic = $('td.col-topics span a.selected').attr('rel');
		$('td.col-questions').load("/faq/questions/" + defaultTopic.replace(" ", "+") + "/", function () {
			$('td.col-questions span a').eq(0).addClass('selected');
			faq.load_answers();
			$('td.col-questions span a.selected div.white-arrow3').css('visibility', 'visible');
		},
		"xml");
	}
	else { pageload(hash); }

	if ($.browser.msie && $.browser.version < 8) $('#chat').css('margin-left', '-64px');

	$('.viewAll').live('click', function () {
		location.href = $(this).attr('rel');
	});

	$('td.col-topics span a').live('click', function () {
		var topic = $(this).attr('rel');
		faq.update_selected_topic($(this));
		hash = topic;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page.
		// pageload is called at once.
		// hash don't contain "#", "?"
		$.historyLoad(hash);

		faq.landing = false;
		return false;
	});

	$('td.col-questions span a').live('click', function () {
		if ($.browser.msie && $.browser.version < 8 && !$(this).hasClass('selected')){
			faq.update_selected_question($(this));
			FixIE7QuestionHeight($(this).parent());
			faq.load_answers();
		}
	});
    
    if ($.browser.msie && $.browser.version < 8) setTimeout("FixIE7FirstQuestion();", 250);
});

