﻿/*********************************************************************************************************
 *   FAQ navigation script
 *   Created by Ricardo L. Banate
 *   MRTC
**********************************************************************************************************/
Type.registerNamespace('ETravel');      

ETravel.FAQ = function (showAllButton)
{
    this.Items = new Array();
    this.LinkItems = new Array();
    this.ShowAllButton = showAllButton;
    this.ShowAllText="Show All";
    this.HideAllText="Hide All";
    this.ShowAllPanel="";
    clickCallBack = Function.createCallback(ETravel.FAQ.ShowAll_Clicked,this.Items);
    $addHandler($get(showAllButton), "click", clickCallBack);
}

ETravel.FAQ.prototype = {
    addItem: function(source, faqItem) // prototype for adding buttons that needs to hide/unhide an FAQ item when clicked
    {  
        sourceControl = $get(source);
        Array.add(this.Items, {source: source, item : faqItem, showText: this.ShowAllText, hideText: this.HideAllText, parentPanel: this.ShowAllPanel});
        var clickCallBack = Function.createCallback(ETravel.FAQ.Item_Clicked,this.Items);
        $addHandler(sourceControl, "click", clickCallBack);
    },
    addLinkItem: function(source, imageControl, faqItem) // prototype for adding buttons that needs to hide/unhide an FAQ item when clicked
    {  
        sourceControl = $get(source);
        Array.add(this.LinkItems, {source: source, item : faqItem, control: imageControl, showText: this.ShowAllText, hideText: this.HideAllText, parentPanel: this.ShowAllPanel});
        var clickCallBack = Function.createCallback(ETravel.FAQ.LinkItem_Clicked,this.LinkItems);
        $addHandler(sourceControl, "click", clickCallBack);
    },
    removeItem: function(source, faqItem) // prototype for removing buttons that needs to hide/unhide an FAQ item when clicked. This should be call when the page is being unloaded
    {  
        sourceControl = $get(source);
        var clickCallBack = Function.createCallback(ETravel.FAQ.Item_Clicked,this.Items);
        $removeHandler(sourceControl, "click", clickCallBack );
    },
    removeLinkItem: function(source, faqItem, imageControl) // prototype for removing buttons that needs to hide/unhide an FAQ item when clicked. This should be call when the page is being unloaded
    {  
        sourceControl = $get(source);
        var clickCallBack = Function.createCallback(ETravel.FAQ.LinkItem_Clicked,this.LinkItems);
        $removeHandler(sourceControl, "click", clickCallBack );
    },
    showPanel: function(source, faqItem)
    {
        sourceControl = $get(source);
        //Array.add(this.Items, {source: source, item : faqItem, showText: this.ShowAllText, hideText: this.HideAllText, parentPanel: this.ShowAllPanel});
        var clickCallBack = Function.createCallback(ETravel.FAQ.ShowPanel_Clicked,{source: source, item : faqItem, showText: this.ShowAllText, hideText: this.HideAllText});
        $addHandler(sourceControl, "click", clickCallBack);
    },
    hidePanel: function(source, faqItem)
    {  
        sourceControl = $get(source);
        var clickCallBack = Function.createCallback(ETravel.FAQ.ShowPanel_Clicked,{source: source, item : faqItem, showText: this.ShowAllText, hideText: this.HideAllText});
        $removeHandler(sourceControl, "click", clickCallBack );
    },
    get_ShowAllButton: function() // getter property, returns the name of the button used for showing/hiding all FAQs
    {
        return this.ShowAllButton;
    },
    get_ShowAllText:function()
    {
        return this.ShowAllText;
    },
    set_ShowAllText:function(text)
    {
        this.ShowAllText = text;
    },
    get_HideAllText:function()
    {
        return this.HideAllText;
    },
    set_HideAllText:function(text)
    {
        this.HideAllText = text;
    },
    get_ShowAllPanel:function()
    {
        return this.ShowAllPanel;
    },
    set_ShowAllPanel:function(panelId)
    {
        this.ShowAllPanel = panelId;
    },
    set_ShowAllButton: function(id) // setter property, sets the name of the button used for showing/hiding all FAQs
    {
        /*if(this.ShowAllButton!='')
        {
            try
            {
            sourceControl = $get(this.ShowAllButton);
            var oldclickCallBack = Function.createCallback(ETravel.FAQ.ShowAll_Clicked,this.Items);
            $removeHandler(sourceControl, "click", oldclickCallBack );
            }
            catch(e){}
        }
        else
        {*/
        this.ShowAllButton = id;
        
        sourceControl = $get(id);
        var clickCallBack = Function.createCallback(ETravel.FAQ.ShowAll_Clicked,this.Items);
        $addHandler(sourceControl, "click", clickCallBack);
        
        //}
        
    }
}
    
 ETravel.FAQ.Item_Clicked = function (evt, context)
    {
     found = true;
     for(var faq in context)
        {
            if(context[faq].source == this.id)
            {
                faqDiv = $get(context[faq].item);
                if(faqDiv.style.display=="block")
                {
                    this.src ="../../images/expand.jpg";
                    this.title= "Click to show Answer";
                    faqDiv.style.display="none";
                }
                else
                {
                    faqDiv.style.display="block";
                    this.src = "../../images/collapse.jpg";
                    this.title= "Click to hide Answer";
                }
                found =true;
            }
        }
        
        if(!found)
        {
            alert("No associated FAQ for this button");
        }
    }
 ETravel.FAQ.LinkItem_Clicked = function (evt, context)
    {
     found = true;
     for(var faq in context)
        {
            if(context[faq].source == this.id)
            {
                faqDiv = $get(context[faq].item);
                imageControl = $get(context[faq].control);
                if(faqDiv.style.display=="block")
                {
                    imageControl.src ="../../images/expand.jpg";
                    imageControl.title= "Click to show Answer";
                    faqDiv.style.display="none";
                }
                else
                {
                    faqDiv.style.display="block";
                    imageControl.src = "../../images/collapse.jpg";
                    imageControl.title= "Click to hide Answer";
                }
                found =true;
            }
        }
        
        if(!found)
        {
            alert("No associated FAQ for this button");
        }
    }
 ETravel.FAQ.ShowAll_Clicked = function (evt, context)
 {
    var stateChanged = false;
    for(var faq in context)
        {
            faqDiv = $get(context[faq].item);
            source = $get(context[faq].source);
            
            if(this.value.toLowerCase()!=context[0].showText.toLowerCase())
            {
                faqDiv.style.display="none";
                source.src ="../../images/expand.jpg";
                stateChanged = true;
            }
            else
            {
                faqDiv.style.display="block";
                source.src ="../../images/collapse.jpg";
                stateChanged = true;
            }
        }
    if(stateChanged)
    {
        var parentPanel = $get(context[0].parentPanel);
        if(this.value.toLowerCase()==context[0].showText.toLowerCase())
        {
            this.value = context[0].hideText;
            this.title="Click to hide all Answers";
            
            if(parentPanel!=null)
            {
                parentPanel.style.display="block";
            }
           
            
        }
        else
        {
            this.value = context[0].showText;
            this.title="Click to show all Answers";
            if(parentPanel!=null)
            {
                parentPanel.style.display="none";
            } 
        }
    }
 }

ETravel.FAQ.ShowPanel_Clicked = function (evt, context)
 {
    faqDiv = $get(context.item);
    source = $get(context.source);
    if(this.value.toLowerCase()==context.showText.toLowerCase())
        {
            this.value = context.hideText;
            this.title="Click to hide all Answers";
            faqDiv.style.display="block";
        }
        else
        {
            this.value = context.showText;
            this.title="Click to show all Answers";
            faqDiv.style.display="none";
        }
 }
ETravel.FAQ.registerClass('ETravel.FAQ');
