togglers = new Array();

function faqRegisterToggler(img)
{
    togglers.push(img);
    togglers = togglers.unique();
}

function faqExpandAll()
{
    for(i=0; i<togglers.length; i++)
    {
    	node = togglers[i].id.replace('faq_subcat_plusminus_', '');
        faqExpandNode(node);
    }
}

function faqCollapseAll()
{
    for(i=0; i<togglers.length; i++)
    {
        node = togglers[i].id.replace('faq_subcat_plusminus_', '');
        faqCollapseNode(node);
    }
}

function faqToggleQuestion(id)
{
    var question = jq('#faq_question_'+id);
    var link     = jq('#faq_question_'+id+'_toggler');
    
    question.toggle();
    link.toggle();
    
    positionFooter();
}

function faqToggleNode(id)
{
    if(jq('#faq_subcat_plusminus_'+id).attr('src').match(/plus\.gif/))
    {
        faqExpandNode(id);
    }
    else
    {
        faqCollapseNode(id);
    }
}

function faqExpandNode(id)
{
    jq('#faq_subcat_plusminus_'+id).attr('src', '/site/images/faq/minus.gif');
    jq('#faq_subcat_'+id).show();
}

function faqCollapseNode(id)
{
	jq('#faq_subcat_plusminus_'+id).attr('src', '/site/images/faq/plus.gif');
    jq('#faq_subcat_'+id).hide();
}

FaqUpdater = Class.create();
Object.extend(FaqUpdater.prototype, {

    initialize: function(id)
    {
        this.id           = id;
        this.qWrapper     = $("faq_"+id+"_q");
        this.aWrapper     = $("faq_"+id+"_a");
        this.qText        = this.qWrapper.innerHTML;
        this.aText        = this.aWrapper.innerHTML;
        this.editLink     = $("faq_"+id+"_edit");
        this.saveLink     = $("faq_"+id+"_save");
        this.errorWrapper = $("faq_"+this.id+"_errors");
    },
    
    displayErrors: function(output)
    {
        Element.update(this.errorWrapper, output);
        Element.show(this.errorWrapper);
    },

    edit: function()
    {
        Element.toggle(this.editLink);
        Element.toggle(this.saveLink);

        while(this.qWrapper.firstChild) this.qWrapper.removeChild(this.qWrapper.firstChild);    
        while(this.aWrapper.firstChild) this.aWrapper.removeChild(this.aWrapper.firstChild);    
    
        var fqHiddenId = document.createElement("input");
        fqHiddenId.setAttribute("type", "hidden");
        fqHiddenId.setAttribute("name", "id");
        fqHiddenId.setAttribute("value", this.id);
        this.qWrapper.appendChild(fqHiddenId);

        var fqTextInput = document.createElement("input");
        fqTextInput.setAttribute("type", "text");
        fqTextInput.setAttribute("name", "q");
        fqTextInput.setAttribute("id", "faq_"+this.id+"_form_q");
        fqTextInput.setAttribute("size", "50");
        fqTextInput.setAttribute("value", this.qText);
        this.qWrapper.appendChild(fqTextInput);
    
        var faTextArea = document.createElement("textarea");
        faTextArea.setAttribute("name", "a");
        faTextArea.setAttribute("id", "faq_"+this.id+"_form_a");
        faTextArea.setAttribute("rows", "5");
        faTextArea.setAttribute("cols", "80");
        this.aWrapper.appendChild(faTextArea);
        faTextArea.appendChild(document.createTextNode(this.aText));    	
    },
    
    revert: function()
    {
        while(this.qWrapper.firstChild) this.qWrapper.removeChild(this.qWrapper.firstChild);
        this.qWrapper.appendChild(document.createTextNode(this.qText));
        
        while(this.aWrapper.firstChild) this.aWrapper.removeChild(this.aWrapper.firstChild);
        this.aWrapper.appendChild(document.createTextNode(this.aText));
        
        Element.toggle(this.editLink);
        Element.toggle(this.saveLink);
    },

    save: function()
    {        
        Element.hide(this.errorWrapper);

        var form   = $("faq_"+this.id+"_form");
        this.qText = form.q.value;
        this.aText = form.a.value;
        
        /* Send the request */
        new Ajax.Request(
            '/ajax/Faq/edit/', 
            {
                parameters:     Form.serialize(form),
                onSuccess:      this.submitSuccessHandler.bind(this),
                onFailure:      this.submitErrorHandler.bind(this),
                onException:    this.submitExceptionHandler.bind(this)
            }
        );
    },
    
    submitSuccessHandler: function(request)
    {
        if(request.responseText == '1')
        {
            this.revert();
        }
        else
        {
            this.displayErrors(request.responseText);
        }
    },
    
    submitErrorHandler: function(request, json)
    {
        alert('A javascript error occured. Please notify the webmaster at webmaster@fracturedatlas.org.');
    },
    
    submitExceptionHandler: function(request, exception)
    {
        alert('A javascript exception occured. Please notify the webmaster at webmaster@fracturedatlas.org.');
    }
});
