if (typeof Mxm == 'undefined') {
    var Mxm = {};
}

if (Mxm.Collect == undefined) {
    Mxm.Collect = {};
}

if (Mxm.Collect.Shopping == undefined) {
    Mxm.Collect.Shopping = {};
}

Mxm.Collect.Shopping.CategoriesSelector = {
    init : function () {

    },

    generateNextLevel : function (selectElmId, nextLevelIdx) {
        var currentElm = $(selectElmId);
        var rootCategId = currentElm.options[currentElm.selectedIndex].value;

        var url = "/shopping/category/children/" + rootCategId + "/" + nextLevelIdx;

        var baseName = 'shop-nav-categ-';

        var i = nextLevelIdx;
        // If the changed value is not at the last level displayed
        // we have to delete all the next levels.
        while (i < 10) {
            if ( $(baseName + i) ) {
                var toDelete = $(baseName + i);
                var formElm = currentElm.parentNode;
                formElm.removeChild(toDelete);
            } else {
                break;
            }
            ++i;
        }

        // Ajax request to get children of chosen category.
        var getChildren = new Ajax.Request(
            url,
            {
                asynchronous:  false,
                method: 'get',
                parameters: '',
                onComplete: function(transport) {
                    if (transport.status == 200) {
                        ;
                    }
                },
                onSuccess: function(response) {
                    if (response.responseText.indexOf('URL') !== 0 && response.responseText.length > 0) {
                        // The list is returned as :
                        // SubCategName1|SubCategId1##SubCategName2|SubCategId2
                        var listSub = response.responseText.split('##');
                        var newSelect = new Element('select');
                        newSelect.setAttribute('id', (baseName + nextLevelIdx));
                        newSelect.setAttribute('name', (baseName + nextLevelIdx));
                        newSelect.setAttribute('onchange', 'return false;');
                        newSelect.setAttribute('class', 'shoppingSubCat');

                        Element.insert(currentElm, {'after' : newSelect});

                        newSelect.observe('change', function(event) {
                            if (newSelect.selectedIndex !== 0) {
                                Mxm.Collect.Shopping.CategoriesSelector.generateNextLevel(newSelect.id, (++nextLevelIdx));
                            }
                        });

                        var makeChoiceElm = new Element('option');
                        makeChoiceElm.setAttribute('value', '');
                        makeChoiceElm.update('Make a choice');
                        Element.insert(newSelect, {'bottom' : makeChoiceElm});


                        for (var i = 0; i < listSub.length; ++i) {
                            // Differentiate between subcategory name and ID.
                            // The ID is set as the value of the option.
                            var tmpData = listSub[i].split('|');
                            try {
                                var tmpElm = new Element('option');
                                tmpElm.setAttribute('value', tmpData[1]);
                                tmpElm.update(tmpData[0]);
                                Element.insert( newSelect, {'bottom' : tmpElm} );
                            } catch(ex) {
                                // If we catch an exception we log it into firebug's
                                // console (if enabled, shouldn't happen though)
                                console.log(ex);
                            }
                        }

                        // fix for loading div .. probably needs a check IE .. @todo
                        $('loading').hide();
                    }
                    else {
                        if (response.responseText.indexOf('URL') !== 0) {
                            return ;
                        }

                        // redirect to page .
                        var list = response.responseText.split('|');
                        window.location.href = list[1];
                    }
                }
            });
    }
};

