//FAQ

//STATIC HTML/TEXT
var loadingQuestionsHtml = "<span>Loading Frequently Asked Questions...</span><br /><br /><div><img src='scripts/loader.gif' width='32' height='32' /></div>";
var loadingTopicTitleHtml = "<div><img src='scripts/loader_small.gif' width='24' height='24' /></div>";
//formatitemstring-parameter for WS call
//{0}-Topic ID
//{1}-Topic Name
//{2}-Topic Description
var formatItemString=
"<div class=\"category\">\n" +
"<div class=\"icon\" style=\"float:left; width:36px; margin-right:12px; padding-bottom:10px;\">\n" +
"<img src=\"http://www.phila.gov/311/IMGs/iconsLRG/{1}.gif\" onclick=\"displayQuestions('{0}','{1}');\"/></div>\n" +
"<div class=\"topicNameAndDescrip\" style=\"float:left; width:175px; padding-left:10px;\">\n" +
"<div class=\"head\" onclick=\"displayQuestions('{0}','{1}');\">{1}</div>\n" +
"<div class=\"topicDetails\">{2}</div>\n" +
"</div>\n" +
"<div class=\"clear\"></div>\n" +
"</div>"
;
var topicTitleFormatItemString=
"<img style=\"display:inline\" src=\"http://www.phila.gov/311/IMGs/iconsSM/{1}.gif\" />\n" + 
"<h1 class=\"\" style=\"display:inline\">{1}</h1>"
;


//CONTAINER ELEMENT IDs
var topicContainerElementId = "topics";
var titleContainerElementId = "topicTitle";
var questionsContainerElementId = "questions";

//WEB SERVICE URLs
var baseUrl = "http://www.phila.gov/311/AJAX/";
var faqServiceUrl =  baseUrl + "AjaxFaqService.asmx";
var topicServiceUrl = baseUrl + "AjaxTopicService.asmx";
var ticketServiceUrl=baseUrl + "AjaxTicketService.asmx";

//REQUESTs
var reqTopicList = false;
var reqTopicTitle = false;
var reqQuestions = false;


//SET ACCESSORS 
function setTopicContainerElementId(value)
{
	topicContainerElementId = value;
}

function setTopicTitleContainerElementId(value)
{
	titleContainerElementId = value;
}

function setFaqsContainerElementId(value)
{
	questionsContainerElementId = value;
}

//CORE FUNCTIONS
function getTopicDivs(containerElementId, className)
{
    topicContainerElementId = containerElementId;

    var url = topicServiceUrl + "/GenerateTopicListActiveAsCustomDivs"
        + "?ClassName=" + className
        + "&AttributesFormatString=" + "onclick=\"displayQuestions('{0}','{1}');\"";
    
    reqTopicList = openAjaxRequest("GET", url, renderTopicDivs, null);
}

function getTopicDivsUsingFormatString(containerElementId)
{
    topicContainerElementId = containerElementId;
    var url = topicServiceUrl + "/GenerateTopicListActive"
        + "?formatItemString=" + formatItemString;
    
    reqTopicList = openAjaxRequest("GET", url, renderTopicDivs, null);

}

function displayTopicTitle(topicId, htmlTagName, className, attributesFormatString)
{ 
    document.getElementById(titleContainerElementId).innerHTML = loadingTopicTitleHtml;
    
    var url = topicServiceUrl + "/GenerateTopicById"
        + "?TopicId=" + topicId
        + "&FormatItemString=" + topicTitleFormatItemString
	;

    reqTopicTitle = openAjaxRequest("GET", url, renderTopicTitle, null);

}

function displayQuestions(topicId,topicName)
{
    displayTopicTitle(topicId, "h1", "", "");

    document.getElementById(questionsContainerElementId).innerHTML = loadingQuestionsHtml;
    
    var url = faqServiceUrl + "/GenerateFaqListByTopicIdAsDivs"
        + "?TopicId=" + topicId;
    
    reqQuestions = openAjaxRequest("GET", url, renderQuestions, null);
    pageTracker._trackEvent('311 faqs', 'Select Topic', topicName);
    try {
        var pageTracker2 = _gat._getTracker("UA-860026-1");
        pageTracker2._initData();
        pageTracker2._trackEvent('311 faqs', 'Select Topic', topicName);
    } catch (ex) {
        //
        var testBlock = document.getElementById("testBlock");
        testBlock.style.cursor = "pointer";
    }
}


//RENDER CONTAINER ELEMENT CONTENTS
function renderTopicDivs()
{
    if (reqTopicList.readyState == 4)
    {
        document.getElementById(topicContainerElementId).innerHTML =
           HtmlDecode(reqTopicList.responseText);

	reqTopicList = false;
 }
}

function renderTopicTitle()
{
    if (reqTopicTitle.readyState == 4)
    {
        document.getElementById(titleContainerElementId).innerHTML =
           HtmlDecode(reqTopicTitle.responseText);

	reqTopicTitle = false;
     }
}

function renderQuestions()
{
    if (reqQuestions.readyState == 4)
    {
        document.getElementById(questionsContainerElementId).innerHTML =
           HtmlDecode(reqQuestions.responseText);

	reqQuestions = false;
     }
}


