<?php
/**
 * Maho
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2024 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/** @var Mage_Adminhtml_Block_Store_Switcher $this */
?>
<?php $_websiteCollection = $this->getWebsiteCollection() ?>
<?php if ($_websiteCollection->getSize()): ?>
<div id="store_switcher_container">
<p class="switcher"><label for="store_switcher"><?= $this->__('Choose Store View:') ?></label>
<select name="store_switcher" id="store_switcher" class="left-col-block">
    <option value=""><?= $this->escapeHtml($this->getDefaultStoreName()) ?></option>
    <?php foreach ($_websiteCollection as $_website): ?>
        <?php $showWebsite=false; ?>
        <?php foreach ($this->getGroupCollection($_website) as $_group): ?>
            <?php $showGroup=false; ?>
            <?php foreach ($this->getStoreCollection($_group) as $_store): ?>
                <?php if ($showWebsite == false): ?>
                    <?php $showWebsite = true; ?>
                    <optgroup label="<?= $this->escapeHtml($_website->getName()) ?>"></optgroup>
                <?php endif ?>
                <?php if ($showGroup == false): ?>
                    <?php $showGroup = true; ?>
                    <optgroup label="&nbsp;&nbsp;&nbsp;<?= $this->escapeHtml($_group->getName()) ?>">
                <?php endif ?>
                <option group="<?= $_group->getId() ?>" value="<?= $_store->getId() ?>"<?php if($this->getStoreId() == $_store->getId()): ?> selected="selected"<?php endif ?>>&nbsp;&nbsp;&nbsp;&nbsp;<?= $this->escapeHtml($_store->getName()) ?></option>
            <?php endforeach ?>
            <?php if ($showGroup): ?>
                </optgroup>
            <?php endif ?>
        <?php endforeach ?>
    <?php endforeach ?>
</select>
</p>
</div>
<script type="text/javascript">
//<![CDATA[
var varienStore = new Class.create();

varienStore.prototype = {
    initialize : function(containerId, storeSwitcher, url, useAjax, useConfirm){

        this.containerId    = containerId;
        this.storeSwitcher  = storeSwitcher
        this.url            = url;
        this.useAjax        = useAjax;
        this.useConfirm     = useConfirm;

        this.storeSelectorClickCallback = false;

        this.selectorOnChange = this.optionOnChange.bindAsEventListener(this);

        this.storesGroup = {};

        this.initSwitcher();
    },

    optionOnChange : function (event)
    {
        if (this.storeSelectorClickCallback) {
            try {
                this.storeSelectorClickCallback(event, this);
            }
            catch (e) {}
        }
    },

    initSwitcher : function()
    {
        if ($(this.storeSwitcher)) {
            this.options = $$('#'+this.containerId+' option');
            for (var option=0; option<this.options.length; option++) {
                if (option%2==0) {
                    Element.addClassName(this.options[option], 'even');
                }
                var id = this.options[option].value*1;
                this.storesGroup[id] = this.options[option].getAttribute('group');
            }
            Event.observe(this.storeSwitcher, 'change', this.selectorOnChange);
        }
    },

    getContainerId : function()
    {
        return this.containerId;
    },

    isSameSore: function(checkId, currentId)
    {
        return (this.storesGroup[currentId] == this.storesGroup[checkId]);
    }
}

var varienStoreSwitcher = new varienStore('store_switcher_container', 'store_switcher', '<?= $this->getSwitchUrl() ?>', <?= $this->getUseAjax() ?>, <?= $this->getUseConfirm() ?>);
//]]>
</script>
<?php endif ?>
