<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category   design_default
 * @package    Mage
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<div class="categories-side-col">
    <div class="content-header">
        <h3 class="icon-head head-categories"><?php echo Mage::helper('catalog')->__('Categories') ?></h3>
        <?php if ($this->getRoot()): ?>
            <?php echo $this->getAddRootButtonHtml() ?><br />
            <?php echo $this->getAddSubButtonHtml() ?>
        <?php endif; ?>
    </div>
    <?php echo $this->getStoreSwitcherHtml() ?>
    <div class="tree-actions">
        <?php if($this->getRoot()): ?>
            <?php //echo $this->getCollapseButtonHtml() ?>
            <?php //echo $this->getExpandButtonHtml() ?>
            <a href="#" onclick="tree.collapseTree(); return false;"><?php echo Mage::helper('catalog')->__('Collapse All'); ?></a> <span class="separator">|</span> <a href="#" onclick="tree.expandTree(); return false;"><?php echo Mage::helper('catalog')->__('Expand All'); ?></a>
        <?php endif; ?>
    </div>
    <?php if ($this->getRoot()): ?>
    <div class="tree-holder">
        <div id="tree-div" style="width:100%; overflow:auto;"></div>
    </div>
</div>
<script type="text/javascript">
//<![CDATA[
var tree;

/**
 * Fix ext compatibility with prototype 1.6
 */
Ext.lib.Event.getTarget = function(e) {
    var ee = e.browserEvent || e;
    return ee.target ? Event.element(ee) : null;
};

Ext.tree.TreePanel.Enhanced = function(el, config)
{
    Ext.tree.TreePanel.Enhanced.superclass.constructor.call(this, el, config);
};

Ext.extend(Ext.tree.TreePanel.Enhanced, Ext.tree.TreePanel, {

    loadTree : function(config, firstLoad)
    {
        var parameters = config['parameters'];
        var data = config['data'];

        this.storeId = parameters['store_id'];

        if ( this.storeId != 0) {
            $('add_root_category_button').hide();
        }

        if ((typeof parameters['root_visible']) != 'undefined') {
            this.rootVisible = parameters['root_visible']*1;
        }

        var root = new Ext.tree.TreeNode(parameters);

        this.nodeHash = {};
        this.setRootNode(root);

        if (firstLoad) {
            this.addListener('click', this.categoryClick);
            this.addListener('beforenodedrop', categoryMove.createDelegate(this));
        }

        this.loader.buildCategoryTree(root, data);
        this.el.dom.innerHTML = '';
        // render the tree
        this.render();
        if (parameters['expanded']) {
            this.expandAll();
        } else {
            root.expand();
        }

        var selectedNode = this.getNodeById(parameters['category_id']);
        if (selectedNode) {
            this.currentNodeId = parameters['category_id'];
        }
        this.selectCurrentNode();

        this.cache[parameters['store_id']] = {parameters:parameters, data:data};
    },

    switchStore : function(event, switcher)
    {
        var obj = event.target;

        var newStoreId = obj.value*1;

        var storeParam = newStoreId ? 'store/'+newStoreId + '/' : '';

        if (obj.switchParams) {
            storeParam+= obj.switchParams;
        }
        if (switcher.useConfirm) {
            if (!confirm("<?php echo $this->__('Please confirm site switching. All data that hasn\'t been saved will be lost.') ?>")){
                obj.value = '<?php echo (int) $this->getStoreId() ?>';
                return false;
            }
        }

        if ( newStoreId==0 )    {
            $('add_root_category_button').show();
        }
        else    {
            $('add_root_category_button').hide();
        }


        if (this.cache[this.storeId]) {
            var hash = this.loader.buildHash(this.root);
            this.cache[this.storeId]['data'] = hash['children'];
            this.cache[this.storeId]['parameters']['category_id'] = this.currentNodeId;
        }
        var cache = this.cache[newStoreId];

        if (this.useAjax && !switcher.isSameSore(newStoreId, this.storeId)) {
            if (cache) {
                storeParam = storeParam+'id/'+cache['parameters']['category_id']+'/';
                this.loadTree(cache);
            } else {
                this.storeId = newStoreId;
                this.switchTree(this, this.switchTreeUrl+storeParam, {store:newStoreId});
            }
        } else {
            this.storeId = newStoreId;
            storeParam = storeParam+'id/'+this.currentNodeId+'/';
        }

        updateContent(this.editUrl+storeParam);
    },

    switchTree : function(tree, url, params)
    {
        new Ajax.Request(
            url + (url.match(new RegExp('\\?')) ? '&isAjax=true' : '?isAjax=true' ),
            {
               parameters:  params || {},
               method:      'post',
               onComplete:  tree._switchTree.bind(this)
            }
        );
    },

    _switchTree : function(transport)
    {
        var response = eval('('+transport.responseText+')');
        if (!response['parameters']) {
            return false;
        }

        this.loadTree(response);
    },

    request : function(url, params)
    {
        if (!params) {
            if (category_info_tabsJsTabs.activeTab) {
                var params = {active_tab_id:category_info_tabsJsTabs.activeTab.id};
            }
        }
        var result = new Ajax.Request(
            url + (url.match(new RegExp('\\?')) ? '&isAjax=true' : '?isAjax=true' ),
            {
               parameters:  params || {},
               method:      'post'
            }
        );

        return result;
    },

    selectCurrentNode : function()
    {
        if (this.currentNodeId) {
            var selectedNode = this.getNodeById(this.currentNodeId);
            if ((typeof selectedNode.attributes.path)!='undefined') {
                var path = selectedNode.attributes.path;
                if (!this.storeId) {
                    path = '0/'+path;
                }
                this.selectPath(path);
            } else {
                this.getSelectionModel().select(selectedNode);
            }
        }
    },

    collapseTree : function()
    {
        this.collapseAll();

        this.selectCurrentNode();

        if (!this.collapsed) {
            this.collapsed = true;
            this.loader.dataUrl = '<?php echo $this->getLoadTreeUrl(false) ?>';
            this.request(this.loader.dataUrl, false);
        }
    },

    expandTree : function()
    {
        this.expandAll();
        if (this.collapsed) {
            this.collapsed = false;
            this.loader.dataUrl = '<?php echo $this->getLoadTreeUrl(true) ?>';
            this.request(this.loader.dataUrl, false);
        }
    },

    categoryClick : function(node, e)
    {
        var url = '<?php echo $this->getEditUrl() ?>'+(this.storeId?'store/'+this.storeId+'/':'')+'id/'+node.id+'/';
        this.currentNodeId = node.id;
        this.addNodeTo = false;
        if (!this.useAjax) {
            setLocation(url);
            return;
        }
        if (category_info_tabsJsTabs.activeTab) {
            var params = {active_tab_id:category_info_tabsJsTabs.activeTab.id};
        }
        updateContent(url, params);
    }
});

Ext.onReady(function()
{
    var categoryLoader = new Ext.tree.TreeLoader({
       dataUrl: '<?php echo $this->getLoadTreeUrl() ?>'
    });

    categoryLoader.createNode = function(config) {
        var node;
        var _node = Object.clone(config);
        if (config.children && !config.children.length) {
            delete(config.children);
            node = new Ext.tree.AsyncTreeNode(config);
        } else {
            node = new Ext.tree.TreeNode(config);
        }

        return node;
    };

    categoryLoader.buildCategoryTree = function(parent, config)
    {
        if (!config) return null;

    	if (parent && config && config.length){
            for (var i = 0; i < config.length; i++) {
                var node;
                var _node = Object.clone(config[i]);
                if (_node.children && !_node.children.length) {
                    delete(_node.children);
                    node = new Ext.tree.AsyncTreeNode(_node);
                } else {
                    node = new Ext.tree.TreeNode(config[i]);
                }
                parent.appendChild(node);
                node.loader = node.getOwnerTree().loader;
                if (_node.children) {
                    this.buildCategoryTree(node, _node.children);
                }
            }
        }
    };

    categoryLoader.buildHash = function(node)
    {
        var hash = {};

        hash = this.toArray(node.attributes);

        if (node.childNodes.length>0 || (node.loaded==false && node.loading==false)) {
            hash['children'] = new Array;

            for (var i = 0, len = node.childNodes.length; i < len; i++) {
                if (!hash['children']) {
                    hash['children'] = new Array;
                }
            	hash['children'].push(this.buildHash(node.childNodes[i]));
            }
        }

        return hash;
    };

    categoryLoader.toArray = function(attributes) {
        var data = {};
        for (var key in attributes) {
            var value = attributes[key];
            data[key] = value;
        }

        return data;
    };

    categoryLoader.on("beforeload", function(treeLoader, node) {
        treeLoader.baseParams.id = node.attributes.id;
        treeLoader.baseParams.store = node.attributes.store;
    });

    categoryLoader.on("load", function(treeLoader, node, config) {
        varienWindowOnload();
    });

    tree = new Ext.tree.TreePanel.Enhanced('tree-div', {
        animate:          false,
        loader:           categoryLoader,
        enableDD:         true,
        containerScroll:  true,
		selModel:         new Ext.tree.CheckNodeMultiSelectionModel(),
		rootVisible:      '<?php echo $this->getRoot()->getIsVisible() ?>',
		useAjax:          <?php echo $this->getUseAjax() ?>,
		switchTreeUrl:    '<?php echo $this->getSwitchTreeUrl() ?>',
		editUrl:    '<?php echo $this->getEditUrl() ?>',
		currentNodeId:    <?php echo (int) $this->getCategoryId() ?>,
		addNodeTo:         false,
		cache:            {}
	});

	if ((typeof varienStoreSwitcher)!='undefined') {
	    varienStoreSwitcher.storeSelectorClickCallback = tree.switchStore.bind(tree);
	}

	// set the root node
	var parameters = {
        text:        '<?php echo htmlentities($this->getRoot()->getName()) ?>',
        draggable:   false,
        allowDrop:   <?php if ($this->getRoot()->getIsVisible()): ?>true<?php else : ?>false<?php endif; ?>,
        id:          <?php echo (int) $this->getRoot()->getId() ?>,
        expanded:    <?php echo (int) $this->getIsWasExpanded() ?>,
        store_id:    <?php echo (int) $this->getStore()->getId() ?>,
        category_id: <?php echo (int) $this->getCategoryId() ?>
    };

    tree.loadTree({parameters:parameters, data:<?php echo $this->getTreeJson() ?>},true);

});

function _recursTree( tree, valToDelete )    {
    tree.each(function(item, index) {
        if (item.id == valToDelete) {
            tree = tree.without( item );
        }
        else    {
            if (item.children)  {
                item.children = _recursTree(item.children, valToDelete);
                if (item.children.size()==0)  {
                    delete item.children;
                }
            }
        }

    });
    return tree;
}

function processTreeCache(valToDelete)  {
    var treeCache = this.tree.cache;
    var str = '';
    for (store in treeCache)    {
        treeCache[store].data = _recursTree( treeCache[store].data, valToDelete);
    }
}

function addNew(url, isRoot)
{
    if (isRoot) {
        tree.currentNodeId = tree.root.id;
    }
    tree.addNodeTo = tree.currentNodeId;

    if (/store\/\d+/.test(url)) {
        url = url.replace(/store\/\d+/, "store/" + tree.storeId);
    }
    else    {
        url+= "store/" + tree.storeId + "/";
    }

    url+= 'parent/'+tree.currentNodeId;
    updateContent(url);
}

function categoryMove(obj)
{
    var data = {id: obj.dropNode.id}

    data.point = obj.point;
    switch (obj.point) {
        case 'above' :
            data.pid = obj.target.parentNode.id;
            data.paid = obj.dropNode.parentNode.id;
            if (obj.target.previousSibling) {
                data.aid = obj.target.previousSibling.id;
            } else {
                data.aid = 0;
            }
            break;
        case 'below' :
            data.pid = obj.target.parentNode.id;
            data.aid = obj.target.id;
        break;
        case 'append' :
            data.pid = obj.target.id;
            data.paid = obj.dropNode.parentNode.id;
            if (obj.target.lastChild) {
                data.aid = obj.target.lastChild.id;
            } else {
                data.aid = 0;
            }
        break;
        default :
            obj.cancel = true;
            return obj;
    }

    var success = function(o) {
        try {
            if(o.responseText && o.responseText!=='SUCCESS'){
                alert(o.responseText);
                location.reload();
            }
        }
        catch(e) {

        }
    };
    var failure = function(o) {
        if (console) {
            console.log(o.statusText);
        } else {
            alert(o.statusText);
        }
        location.reload();
    };

    var pd = [];
    for(var key in data) {
        pd.push(encodeURIComponent(key), "=", encodeURIComponent(data[key]), "&");
    }
    pd.splice(pd.length-1,1);
    new Ajax.Request(
        '<?php echo $this->getMoveUrl() ?>',
        {
            method:     'POST',
            parameters: pd.join(""),
            onSuccess : success,
            onFailure : failure
        }
    );
}
//]]>
</script>
<?php endif; ?>