/**
* AFW module Glossary
* 
*/
afw.module.glossary = 
{
    terms : {}
    ,categories : {}
    ,aliases : {}
    ,linkTableCatTerm : { 0 : [] }
    
    ,settings : 
    {
        categoryRender : null
        ,useTransition : false
        ,multipleCategories : false
        ,suggestTerm : false
    }
    
    ,dom : 
    {
        glossaryContainer : null
        ,termsCategoriesContainer : null
        ,termsListContainer : null
        ,termsList : null
        ,termListCategory : null
    }
    
    ,filter : 
    {
        categories : [0]
    }
    
    ,functions : 
    {
        initiate : function()
        {
            afw.module.glossary.dom.glossaryContainer = afw.common.methods.getElement('afw_glossary_container');
            afw.module.glossary.dom.termsCategoriesContainer = afw.common.methods.getElement('afw_glossary_category_container');
            afw.module.glossary.dom.termsListContainer = afw.common.methods.getElement('afw_glossary_list_container');
            afw.module.glossary.dom.termsList = afw.common.methods.getElement('afw_glossary_list');
            afw.module.glossary.dom.termListCategory = afw.common.methods.getElement('afw_glossary_active_category_title');
            
            var newCat = 
            {
                0 : afw.module.glossary.categories[0]
                ,4 : afw.module.glossary.categories[4]
                ,1 : afw.module.glossary.categories[1]
                ,2 : afw.module.glossary.categories[2]
                ,3 : afw.module.glossary.categories[3]
            };
            afw.module.glossary.categories = newCat;
            
            if(afw.module.glossary.settings.categoryRender == 'js')
            {
                afw.module.glossary.functions.renderCategories();
            }
        }
        
        ,changeCategory : function(oElement)
        {
            var categories, i, m, dom, categoryId;
            if(arguments.length == 0 || afw.common.methods.isEvent(arguments[0]))
                oElement = this;
            categoryId = afw.module.glossary.methods.getCategoryId(oElement);
            categories = 
            {
                oldList : afw.module.glossary.filter.categories
                ,newList : []
                ,all : afw.common.methods.getElement('glossary_categories_all')
            };
            if(!afw.module.glossary.settings.multipleCategories)
            {
                categories.newList.push(categoryId);
                dom = afw.common.methods.getElement('afw_glossary_category_container');
                if(dom)
                {
                    dom = dom.getElementsByTagName('INPUT');
                    for(i = 0, m = dom.length; i < m; ++i)
                        dom[i].checked = false;
                }
                oElement.checked = true;
            }
            else if(oElement.checked && categoryId == 0)
            {
                dom = afw.common.methods.getElement('afw_glossary_category_container');
                if(dom)
                {
                    dom = dom.getElementsByTagName('INPUT');
                    for(i = 0, m = dom.length; i < m; ++i)
                        if(dom[i].checked && afw.module.glossary.methods.getCategoryId(categoryId) != 0)
                            dom[i].checked = false;
                }
                categories.newList = [0];
            }
            else if(oElement.checked)
            {
                categories.all.checked = false;
                for(i = 0, m = categories.oldList.length; i < m; ++i)
                    if(categories.oldList[i] != 0)
                        categories.newList.push(categories.oldList[i]);
                categories.newList.push(categoryId);
            }
            else
            {
                for(i = 0, m = categories.oldList.length; i < m; ++i)
                    if(categories.oldList[i] != categoryId)
                        categories.newList.push(categories.oldList[i]);
                if(!categories.newList.length)
                {
                    categories.newList.push(0);
                    categories.all.checked = true;
                }
            }
            afw.module.glossary.filter.categories = categories.newList;
            afw.module.glossary.functions.filter.terms();
        }
        
        ,renderCategories : function()
        {
            var count = 0, dom = 
            {
                div : afw.module.glossary.dom.termsCategoriesContainer
                ,ul : document.createElement('UL')
                ,item : null
                ,lastItem : null
            };
            for(var categoryId in afw.module.glossary.categories)
            {
                count++;
                dom.item = 
                {
                    li : document.createElement('LI')
                    ,label : document.createElement('LABEL')
                    ,input : document.createElement('INPUT')
                };
                
                dom.item.input.id = 'category_' + categoryId;
                dom.item.input.value = categoryId;
                dom.item.input.name = 'afw_glossary_categories_selection';
                dom.item.input.type = (afw.module.glossary.settings.multipleCategories ? 'checkbox' : 'radio');
                dom.item.input.onclick = afw.module.glossary.functions.changeCategory;
                if(categoryId == 0)
                {
                    dom.item.input.id = 'glossary_categories_all';
                }
                if(count == 1)
                    dom.item.li.className = 'first';
                dom.lastItem = dom.item.li;
                dom.item.label.innerHTML = afw.module.glossary.categories[categoryId].title;
                dom.item.label.appendChild(dom.item.input);
                dom.item.li.appendChild(dom.item.label);
                dom.ul.appendChild(dom.item.li);
                setTimeout(function()
                {
                    var dom = afw.common.methods.getElement('glossary_categories_all');
                    if(dom)
                        dom.checked = true;
                }, 30);
            }
            if(dom.lastItem !== null)
                dom.lastItem.className = 'last';
            dom.div.appendChild(dom.ul);
            afw.module.glossary.dom.termsListContainer.className += ' glossary_right';
            afw.module.glossary.dom.glossaryContainer.insertBefore(dom.div, afw.module.glossary.dom.termsListContainer);
        }
        
        ,filter : 
        {
            terms : function()
            {
                var i, m, i2, m2, newList, dom, filter, linkTable, categoryId, term;
                newList = afw.module.glossary.dom.termsList.cloneNode(false);
                filter = afw.module.glossary.filter;
                for(i = 0, m = filter.categories.length; i < m; ++i)
                {
                    categoryId = filter.categories[i];
                    linkTable = afw.module.glossary.linkTableCatTerm[categoryId];
                    if(!afw.module.glossary.settings.multipleCategories && i == 0)
                        afw.module.glossary.dom.termListCategory.innerHTML = afw.module.glossary.categories[filter.categories[i]].title;
                    for(i2 = 0, m2 = linkTable.length; i2 < m2; ++i2)
                    {
                        term = afw.module.glossary.terms[linkTable[i2]];
                        dom = 
                        {
                            li : document.createElement('LI')
                            ,title : document.createElement('DIV')
                            ,description : document.createElement('DIV')
                            ,descriptionLong : document.createElement('DIV')
                        };
                        
                        dom.title.className = 'title';
                        dom.description.className = 'description';
                        dom.descriptionLong.className = 'descriptionLong';
                        
                        dom.title.innerHTML = term.title;
                        dom.description.innerHTML = term.description;
                        dom.descriptionLong.innerHTML = term.descriptionLong;
                        
                        dom.li.appendChild(dom.title);
                        dom.li.appendChild(dom.description);
                        if(term.descriptionLong != '' && term.descriptionLong != term.description)
                            dom.li.appendChild(dom.descriptionLong);
                        
                        newList.appendChild(dom.li);
                    }
                }
                if(afw.module.glossary.settings.useTransition === false)
                {
                    afw.module.glossary.dom.termsListContainer.replaceChild(newList, afw.module.glossary.dom.termsList);
                    afw.module.glossary.dom.termsList = newList;
                }
                else
                {
                    switch(afw.module.glossary.settings.useTransition)
                    {
                        case 'fade':
                            afw.module.glossary.transitions.fade(afw.module.glossary.dom.termsList, 100, -8, function()
                            {
                                afw.common.methods.setOpacity(newList, 0);
                                afw.module.glossary.dom.termsListContainer.replaceChild(newList, afw.module.glossary.dom.termsList);
                                afw.module.glossary.dom.termsList = newList;
                                afw.module.glossary.transitions.fade(afw.module.glossary.dom.termsList, 0, 8);
                            });
                        break;
                    }
                }
            }
        }
        
        ,addTerm : function(iId, sName, sLanguage, sTitle, sShortDesc, sLongDesc)
        {
            afw.module.glossary.terms[iId] = 
            {
                id : iId
                ,name : sName
                ,language : sLanguage
                ,title : sTitle
                ,description : sShortDesc
                ,descriptionLong : sLongDesc
                ,aliases : []
                ,categories : []
            };
        }
        
        ,addCategory : function(iId, sTitle, sDescription)
        {
            if(afw.module.glossary.categories[iId] === undefined)
            {
                afw.module.glossary.categories[iId] = 
                {
                    id : iId
                    ,title : sTitle
                    ,description : sDescription
                };
            }
        }
        
        ,addAlias : function(iId, sLanguage, sTitle)
        {
            if(afw.module.glossary.aliases[iId] === undefined)
            {
                afw.module.glossary.aliases[iId] = 
                {
                    id : iId
                    ,language : sLanguage
                    ,title : sTitle
                };
            }
        }
        
        ,bindCategory : function(iTermId, iCategoryId)
        {
            afw.module.glossary.terms[iTermId].categories.push(iCategoryId);
            if(afw.module.glossary.linkTableCatTerm[iCategoryId] === undefined)
                afw.module.glossary.linkTableCatTerm[iCategoryId] = [];
            afw.module.glossary.linkTableCatTerm[0].push(iTermId);
            afw.module.glossary.linkTableCatTerm[iCategoryId].push(iTermId);
        }
        
        ,bindAlias : function(iTermId, iAliasId)
        {
            afw.module.glossary.terms[iTermId].aliases.push(iAliasId);
        }
    }
    
    ,transitions : 
    {
        fade : function(oElement, iOpacity, iStep, callback)
        {
            var fade = 
            {
                opacity : (iOpacity + iStep > 100 ? 100 : iOpacity + iStep < 0 ? 0 : iOpacity + iStep)
                ,done : (iOpacity + iStep >= 100 || iOpacity + iStep <= 0 ? true : false)
            };
            afw.common.methods.setOpacity(oElement, fade.opacity);
            if(!fade.done)
                setTimeout(function()
                {
                    afw.module.glossary.transitions.fade(oElement, fade.opacity, iStep, callback);
                }, 30);
            else
            {
                if(fade.opacity == 100)
                    oElement.style.filter = '';
                if(callback !== undefined)
                    callback();
            }
        }
    }
    
    ,methods : 
    {
        getCategoryId : function(o){ var id = o.id.toString().replace(/[^0-9]*/g, ''); return (id == '' ? 0 : parseInt(id) ); }
    }
};

AFW_addEvent(window, 'load', afw.module.glossary.functions.initiate);
