/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/LICENSE-M1.txt
 *
 * @category   AW
 * @package    AW_Advancedmenu
 * @copyright  Copyright (c) 2009-2011 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/LICENSE-M1.txt
 */

/* [@pack@] */

var isArray = function(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
};

var AWVerticalFolding = Class.create();
AWVerticalFolding.prototype = {
    initialize: function(indyItems, foldingDepth, expandCurrent, uniqId){
        this.depth = foldingDepth;
        this.indy = indyItems;
        this.collClick = false;
        this.categories = new Array();
        this.expand = expandCurrent;
        this.uniqId = uniqId;
        document.observe("dom:loaded", (function(event) {
            if (this.expand){
                var current = this.getCurrentCategory();
                if ((current !== null) && (current.parent_id != 0)){

                    var parent = this.getCategory(current.parent_id);
                    while ( parent && parent.category_id ){
                        var parentColl = $(this.uniqId + '_collapser_' + parent.category_id);
                        parentColl.removeClassName('collapsed');
                        parentColl.addClassName('expanded');
                        this.setChildrenVisibility(parent.category_id, 'block', true);
                        parent = this.getCategory(parent.parent_id);
                    }
                }
            }
        }).bind(this) );    
    },
    registerCategory: function(category_id, parent_id, is_parent, is_expanded, current){
        var catData = {
            category_id: category_id,
            parent_id: parent_id,
            parent: is_parent,
            expanded: is_expanded,
            current: current
        }
        this.categories.push(catData);
    },
    collapser: function(categoryId){
        this.collClick = true;
        var obj = $(this.uniqId + '_collapser_' + categoryId);
        if (typeof(obj) != 'undefined'){
            if (obj.hasClassName('collapsed')){
                if (this.getCategory(categoryId).parent_id == 0){
                    if (!this.indy){
                        var rootCats = this.getAllChildren(0);
                        for (var i = 0; i < rootCats.length; i++){
                            var rootCat = $(this.uniqId + '_collapser_' + rootCats[i].category_id);
                            if (rootCat){
                                if (rootCat.hasClassName('expanded')){

                                    rootCat.removeClassName('expanded');
                                    rootCat.addClassName('collapsed');
                                    this.setChildrenVisibility(rootCats[i].category_id, 'none');
                                    this.getCategory(rootCats[i].category_id).expanded = false;
                                }
                            }
                        }
                    }
                }
                obj.removeClassName('collapsed');
                obj.addClassName('expanded');
                this.setChildrenVisibility(categoryId, 'block');
                this.getCategory(categoryId).expanded = true;
            } else {
                obj.removeClassName('expanded');
                obj.addClassName('collapsed');
                this.setChildrenVisibility(categoryId, 'none');
                this.getCategory(categoryId).expanded = false;
            }
        }

    },
    setChildrenVisibility: function(parent_id, display, oneLevel){
        var children = this.getAllChildren(parent_id);
        if ( isArray(children) && (children.length > 0) ){
            for (var i = 0; i < children.length; i++){

                if (typeof(oneLevel) == 'undefined'){
                    if (children[i].parent){
                        if ( ((display == 'block') && children[i].expanded) || (display == 'none') ){
                            this.setChildrenVisibility(children[i].category_id, display);
                        }
                    } else {
                        this.setChildrenVisibility(children[i].category_id, display);
                    }
                }

                var li = $(this.uniqId + '_li_category_' + children[i].category_id);
                if (li){
                    li.style.display = display;
                }
            }
        }     
    },
    getAllChildren: function(parent_id){
        var children = new Array();

        for (var i = 0; i < this.categories.length; i++){
            if (this.categories[i].parent_id == parent_id){
                children.push(this.categories[i]);
            }
        }
        return children;
    },
    getCategory: function(category_id){
        for (var i = 0; i < this.categories.length; i++){
            if (this.categories[i].category_id == category_id){
                return this.categories[i];
            }
        }
        return null;
    },
    liClick: function(url){
        if (!this.collClick){
            document.location = url;
        }
        this.collClick = false;
    },
    getCurrentCategory: function(){
        for (var i = 0; i < this.categories.length; i++){
            if (this.categories[i].current){
                return this.categories[i];
            }
        }
        return null;
    }
}






