Toggle radio buttons test

by makfruit

HTML

<div id="my-store-2146206"></div><div>
<script type="text/javascript" src="http://app.ecwid.com/script.js?2146206" charset="utf-8"></script><script type="text/javascript"> xProductBrowser("categoriesPerRow=3","views=grid(3,3) list(10) table(20)","categoryView=grid","searchView=list","id=my-store-2146206");</script>
</div>

JavaScript

if (typeof(Ecwid) == 'object') {
  Ecwid.OnAPILoaded.add(function() {
    Ecwid.OnPageLoaded.add(function(page) {
      if ('PRODUCT' == page.type) {
        //
        var radioButtons = [];
        var currentIndex = 0;

        // Toggle function
        var toggleRadioButton = function() {
          if (++currentIndex >= radioButtons.length) {
            currentIndex = 0
          }
          radioButtons[currentIndex].prop('checked', true).click();
        }

        // Find all radiobuttons
        jQuery('.ecwid-productBrowser-details-optionRadioButton').each(function(){
          radioButtons.push(jQuery('input:radio', this));
        });

        // Return if there is no radio button
        if (radioButtons.length == 0) {
          return;
        }

        // Create a toggle button
        var toggleButton = jQuery("<button>Activate next</button>").insertBefore(".ecwid-productBrowser-details-optionPanel-radio");

        // Bind a click event handler
        toggleButton.bind('click', function () {
          toggleRadioButton();
        });
      }
    });
  });
}