
/**
 * File contains JS Library for Category Select Panel Control
 *
 * JavaScript  version 1
 * @category   JavaScript Libraries
 * @author     Eugene A. Kalosha <aristarch@zfort.net>
 * @copyright  (c) 2004-2006 by ZFort Group
 * @version    SVN: $Id: 206$
 * @link       http://www.zfort.net
 * @since      File available since Release 2.3.0
 */
if (typeof(PHP2Controls) == 'undefined') PHP2Controls = new Object();

    String.prototype.trim = function() {
        a = this.replace(/^\s+/, '');
        return a.replace(/\s+$/, '');
    };

    /**
     * PHP2Controls.CategorySelectPanel is the namespace and JS Class for Category Select control.
     *
     * @author   Eugene A. Kalosha <aristarch@zfort.net>
     * @version  $Id: categoryselectpanel.common.js, v 2.3.0 2006/09/19 $
     * @access   public
     * @package  php2
     */
    PHP2Controls.FAQManager = function(wsUrl, arrAnswerId, lnkAnswer, lnkAnswerAll, loadingScript, classInactive, classActive)
    {
        /**
         * Category Select Panel Nesting Lavel
         *
         * @var  integer
         */
        this.wsUrl          = (wsUrl != null) ? wsUrl : '/wsdl/main.ajax.php';
        this.sRootUrl       = '/';
        var currentObject      = this;

        var linkAnswer     = (lnkAnswer != null) ? lnkAnswer : 'lnkAnswer';
        var linkAnswerAll  = (lnkAnswerAll != null) ? lnkAnswerAll : 'lnkAnswerAll';
        this.linkAnswer    = linkAnswer;
        this.linkAnswerAll = linkAnswerAll;
        this.loadingScript = loadingScript;
        this.arrAnswerId   = arrAnswerId;
        for(i in arrAnswerId)
        {
            var currId = arrAnswerId[i];
            document.getElementById(linkAnswer+currId).onclick = function(){currentObject.doShowAnswer(this);}
        }
        document.getElementById(this.linkAnswerAll).onclick = function(){currentObject.doShowAnswer(this);}
        this.classInactive = classInactive;
        this.classActive   = classActive;
    }

    PHP2Controls.FAQManager.prototype.doShowAnswer = function(link)
    {
        var divMessage;
        var setRequest;
        var showAnswerId = new Array(0);
        var objAnswerAll = document.getElementById(this.linkAnswerAll);

        if (link.id == objAnswerAll.id)
        {
            showAnswerId = showAnswerId.concat(this.arrAnswerId);
        }
        else
        {
            var str = new String(link.id);
            var showAnswerId = new Array(1);
            showAnswerId[1] = str.substring(this.linkAnswer.length,str.length);
        }
        setRequest = 0;
        var newVal = '';

        var nCountShow = 0;
        for(i in showAnswerId)
        {
            divMessage = document.getElementById('answer'+showAnswerId[i]);
            questionLink = document.getElementById('lnkAnswer'+showAnswerId[i]);
            
            if (link.id == objAnswerAll.id)
            {
                //alert(objAnswerAll.name);
                if (objAnswerAll.name != null) //'showAll'
                {
                 //alert(objAnswerAll.name);
                  if (objAnswerAll.name == 'hideAll') //'showAll'
                  {
                   newVal = 'show';
                   divMessage.style.display = 'none';
                   questionLink.className = this.classInactive;
                  }
                  else
                  {
                   newVal='hide';
                   divMessage.style.display = 'block';
                   questionLink.className = this.classActive;
                  }
                }
            }
            
            var innerDiv = new String(divMessage.innerHTML);
            // --- Trim space --- //
            innerDiv.trim();
            if (innerDiv.length > 0 && divMessage.innerHTML != this.loadingScript)
            {
                if (link.id != objAnswerAll.id)
                {
                    if (divMessage.style.display == 'none')
                    {
                        nCountShow++;
                        divMessage.style.display = 'block';
                        questionLink.className   = this.classActive;
                    }
                    else
                    {
                        divMessage.style.display = 'none';
                        questionLink.className   = this.classInactive;
                    }
                }
            }
            else setRequest = 1;
        }

        //alert(nCountShow);/////
        if(newVal=='' && nCountShow>0){ newVal =   'hide'; }
        if(newVal!=''){
        	objAnswerAll.name      = newVal + 'All';
        	objAnswerAll.innerHTML = newVal == 'hide' ? 'Hide All' : 'Show All';
			//objAnswerAll.style.background = 'url(../images/btns/btn_'+newVal+'_all.gif) no-repeat scroll left top';
		}
        if (setRequest == 1)
        {
            this.serverResponse = new PHP2Ajax.JSONRequest(this.wsUrl);
            this.serverResponse.call('doShowFAQ');
            this.serverResponse.add("answerId", showAnswerId.join());
            this.serverResponse.setHandler(this.onShowAnswerChecked);
            this.serverResponse.onResponseError = this.onResponseError;
            this.serverResponse.currentObject  = this;
            this.serverResponse.execute();

            for(i in showAnswerId)
            {
                divMessage = document.getElementById('answer'+showAnswerId[i]);
                this.showLoading(divMessage);
            }
        }
        /////////////////////
    }

    /**
     * Loads Responsed HTML Code to the
     *
     * @param AjaxRequest currentObject
     */
    PHP2Controls.FAQManager.prototype.onShowAnswerChecked = function()
    {
        var divMessage;
        var countAnswer = this.response.Response.Answer.CountAnswer;
        for(i = 1; i <= countAnswer; i++)
        {
            eval('id    = this.response.Response.Answer.Id_' + i + ';');
            eval('title = this.response.Response.Answer.Title_' + i + ';');
            divMessage = document.getElementById('answer'+id);
            if (divMessage)
            {
                divMessage.style.display = 'block';
                divMessage.innerHTML = title;
            }
            questionLink = document.getElementById('lnkAnswer'+id);
            if (questionLink)
            {
               questionLink.className = this.currentObject.classActive;
            }
        }
        return true;
    }

    /**
     * Set Skin Area Root Url
     *
     * @param string sRootUrl
     */
    PHP2Controls.FAQManager.prototype.setSRootUrl = function(sRootUrl)
    {
        this.sRootUrl = sRootUrl;
    }

    /**
     * On Response Error Method
     *
     */
    PHP2Controls.FAQManager.prototype.onResponseError = function()
    {
        // alert("Error: " + this.response.Error.Code + ". " + this.response.Error.Message);
        this.currentObject.alert = new PHP2Controls.Alert("Error: " + this.response.Error.Code + ". " + this.response.Error.Message);
        this.currentObject.hideLoading();
    }

    /**
     * Show Loading Div element
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Controls.FAQManager.prototype.showLoading = function(divMessage)
    {
        // --- Setting Loading Data Element --- //
        if (divMessage)
        {
            this.faqManagerLoadingPanel = divMessage;
            this.faqManagerLoadingPanel.innerHTML   = this.loadingScript;
            this.faqManagerLoadingPanel.style.display = 'block';
        }
    }

    /**
     * Hide Loading Div element
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Controls.FAQManager.prototype.hideLoading = function()
    {
        // --- Setting Loading Data Command --- //
        if (this.faqManagerLoadingPanel != null) this.faqManagerLoadingPanel.style.display  = 'none';
    }

    /**
     * Alert Error Message to browser
     *
     * @param  HTMLElement htmlObject
     */
    PHP2Controls.FAQManager.prototype.alert = function(errorMessage, errorCode)
    {
        this.currentAlert = new PHP2Controls.Alert(errorMessage);
    }

    
    // =================== FAQ JQuery functionality extending ===================== //
    var bAllItemsCollapsed = true;
    
    $(function ()
    {
        $('.jqCategoryTitle').click(function ()
        {
            toggleCategoryItem($(this).attr('jq_catid'));
        });
    });
    
        
    function ShowHideAllItems()
    {
        if(bAllItemsCollapsed)
        {
            $(".jqCategoryQuestionBlock:hidden").slideDown('slow');
            $('#idShowAllLink').text('Hide All');
        }
        else
        {
            $(".jqCategoryQuestionBlock:visible").slideUp('slow');
            $('#idShowAllLink').text('Show All');
        }
        document.getElementById('lnkAnswerAll').click();
        bAllItemsCollapsed = !bAllItemsCollapsed;
    }
        
    // show/hide category items    
    function toggleCategoryItem(catID)
    {
            if($(".jqCategoryQuestionBlock[jq_catid*='"+catID+"']:visible").length)
                $(".jqCategoryQuestionBlock[jq_catid*='"+catID+"']:visible").slideUp('slow');
            else
                $(".jqCategoryQuestionBlock[jq_catid*='"+catID+"']:hidden").slideDown('slow');
    }
    
    // show item descr    
    function ShowFaqItem(itemId)
    {
        document.getElementById('lnkAnswer'+itemId).click();
    }
         
    // show item with expanding item category and item descr.
    function ExpandCategoryAndShowFaqItem(itemId)
    {
        var catID = $('#lnkAnswer'+itemId).attr('jq_catid');
        $(".jqCategoryQuestionBlock[jq_catid*='"+catID+"']:hidden").slideDown('slow');
        document.getElementById('lnkAnswer'+itemId).click();
    }
    //END =================== JQuery functionality extending ===================== //     
    
