/// <reference path="jquery-1.4.2.min.js" />
var conNav = function() {
  var pub = {};

  pub.parseQuestions = function($xml) {
    var questionNumber = 1;

    function parseQuestion($xml) {
//      var $xml = $(xml);
		if("console" in window) console.log("Questions: %o; %o", $xml, $xml.children(".question").text());
		
      var question = {};
      question.questionNumber = questionNumber;
      questionNumber++;
      question.value = $xml.children(".question").text();
      question.answers = [];
      $xml.children("ul").children("li").each(function() {
        var $answer = $(this);
        var answer = {};
        answer.value = $answer.children(".answer").add($answer.children("a").children(".answer")).text();
        if ($answer.children("a").size() > 0) {
          answer.href = $answer.children("a").attr("href");
	      if ($answer.children("a").hasClass("findLink")) {
        	answer.itemClass = $answer.children("a").attr("class");
          }
          else {
        	answer.itemClass = '';
          }
      	  if("console" in window) console.log("Answer href = %o", $answer.children("a"));
    	}
        else {
          answer.href = "#question" + parseQuestion($answer.children("ul").children("li"));
          answer.itemClass = '';
        }
        question.answers.push(answer);
      });

      var questionHtml = "<div class='question' id=question" + question.questionNumber + "><h5><a href='#' class='back'>back</a>" + question.value + "</h5><ul>"
      for (var x = 0; x < question.answers.length; x++) {
        questionHtml += "<li><a href='" + question.answers[x].href + "'";
        if (question.answers[x].itemClass.length > 0) {
          questionHtml += " class='" + question.answers[x].itemClass + "'";
        }
        questionHtml += ">" + question.answers[x].value + "</a></li>";
      }
      questionHtml += "</ul></div";

      $("#cNavContent .questions").prepend(questionHtml);
      return question.questionNumber;
    };

    $xml.children("ul").children("li").each(function() {
      parseQuestion($(this));
    });

    $("#cNavContent .questions .question#question1").fadeIn().find("a.back").hide();

    $("#cNavContent .question a.back").click(function(event) {
      event.preventDefault();
      var $this = $(this);
      var $questions = $this.parents(".questions");
      $questions.animate({ "margin-left": (parseInt($questions.css("margin-left").replace("px", "")) + 397) + "px" }, 600, function() { $this.parents(".question").hide(); });
    });

    $("#cNavContent .question li a").click(function(event) {

      var $this = $(this);
      var href = $this.attr("href").replace(window.location, "");
      var itemClass = $this.attr("class");
      if (href.indexOf("#") == 0) {
      	if (itemClass != "findLink") {
        	$(href).show();
        	var $questions = $this.parents(".questions");
	
        	$questions.animate({ "margin-left": (parseInt($questions.css("margin-left").replace("px", "")) - 397) + "px" }, 600);
        	event.preventDefault();
        }
        else {
            event.preventDefault();
            searchModal.resetBox();
            searchModal.filterResults();
            $("#searchpageSearch").modal({ overlayClose: true });
        }
      }
    });
  };

  pub.setup = function() {
//    $.ajax({ type: "GET", url: "questions.xml?d", dataType: "xml", success: pub.parseQuestions });
	pub.parseQuestions($("#umpHeader nav#conversationalNavigation"));
  };

  return pub;
} ();

ahc.Common.onReady(function() {
  conNav.setup();
});

