if(document.readyState !== "complete") {
    InstantClick.on("change", function () {
        if(topNavAnchors = document.querySelectorAll("#primary .nav > li > a")) {
            var subMenus = document.querySelectorAll("#primary .nav > li > ul");

            document.body.onclick = function() {
                for (var s = 0; s < subMenus.length; s++) {
                    if (subMenus[s].classList.contains("visible")) {
                      subMenus[s].classList.remove("visible");
                    }
                }
            };
      
            var topNavLi = document.querySelectorAll("#primary .nav > li ");

            var j = 0;  
            
            for (var i = 0; i < topNavAnchors.length; i++) {        
                if(topNavLi[i].getElementsByTagName("ul").length != 0) {
                    j++;
                    topNavAnchors[i].onclick = function(event) {
                        event.preventDefault();
                        event.stopPropagation();
                        for (var s = 0; s < subMenus.length; s++) {
                            if (subMenus[s] !== this.nextElementSibling && subMenus[s].classList.contains("visible")) {
                                subMenus[s].classList.remove("visible");
                            }
                        }
                        if (!this.nextElementSibling.classList.contains("visible")) {
                            this.nextElementSibling.classList.add("visible");
                        }
                    };      
                }
            }
        }

        if(languageForm = document.forms["language"]) {
            var languageSelect = languageForm.getElementsByTagName("select");   

            languageSelect[0].onchange= function() {
                languageForm.submit();
            };
        }

        if(currencyForm = document.forms["currency"]) {
            var currencySelect = currencyForm.getElementsByTagName("select");       

            currencySelect[0].onchange= function() {
                currencyForm.submit();
            };    
        }

        if(!supportsSVG()) {      
            document.getElementsByTagName("body")[0].className += " no-svg";      
        }

    if (document.documentElement.getAttribute("dir") === "rtl") {
      document.getElementsByTagName("body")[0].className += " rtl";
    }

        if ($.colorbox && window.matchMedia) {
            // Establishing media check
            widthCheck = window.matchMedia("(max-width: 768px)");
            if (widthCheck.matches){
                $.colorbox.remove();
            }
        }

        if((tabsAnchors = document.querySelectorAll("#tabs > a")) && tabsAnchors.length) {
            for (var i = 1; i <= tabsAnchors.length; i++) {
                var id = tabsAnchors[i-1].getAttribute('href').substring(1);
                var p = tabsAnchors[0].parentNode;
                p.insertBefore(document.getElementById(id), tabsAnchors[i]);
                tabsAnchors[i-1].onclick = function(event) {
                    event.preventDefault();
                    if (this.nextElementSibling.classList.contains('selected')) {
                        this.nextElementSibling.classList.remove('selected');
                        this.classList.remove('selected');
                    } else {
                        if (s = document.querySelector('.tab-content.selected')) {
                            s.classList.remove('selected');
                        }
                        this.nextElementSibling.classList.add('selected');
                    }
                };
            }

            document.getElementById(tabsAnchors[0].getAttribute('href').substring(1)).classList.add('selected');
        }

        if (addToCartButtons = document.querySelectorAll(".cart .button, #button-add-to-cart")) {
            for (var i = 0; i < addToCartButtons.length; i++) {
                if (addToCartButtons[i].id !== 'button-cart') {
                    var re = /addToCart\('(.*)'\);/;
                    var quantity;

                    if(re.test(addToCartButtons[i].getAttribute('onclick'))) {
                        addToCartButtons[i].onclick = function() {        
                            quantity = typeof(quantity) != 'undefined' ? quantity : 1;

                            $.ajax({
                                url: 'index.php?route=checkout/cart/add',
                                type: 'post',
                                data: 'product_id=' + this.getAttribute('onclick').match(re)[1] + '&quantity=' + quantity,
                                dataType: 'json',
                                success: function(json) {
                                    $('.success, .warning, .attention, .information, .error').remove();

                                    if (json['redirect']) {
                                        location = json['redirect'];
                                    }

                                    if (json['success']) {                  
                                        window.location.href = 'index.php?route=checkout/cart';  
                                    } 
                                }
                            });
                        }
                    }
                }
            }
        }

        var mySwiper = new Swiper('.swiper-container', {
            loop: true,
            calculateHeight: true,
            slideElement: "li",
            pagination: '.swiper-pagination',
            centeredSlides: true
        });

        if((acatLi = document.querySelectorAll("#all-categories .nav > li")) && acatLi.length) {
            for (var i = 1; i <= acatLi.length; i++) {
                acatLi[i-1].firstElementChild.onclick = function(event) {
                    c = this.parentNode;
                    if (!c.classList.contains('selected') && (c.children.length !== 1)) {
                        event.preventDefault();
                        if (s = document.querySelector('#all-categories li.selected')) {
                            s.classList.remove('selected');
                        }
                        c.classList.add('selected');
                    }
                };
            }
        }

    }, false);

    if ($.colorbox ) {
        var mql = window.matchMedia("(max-width: 768px)");

        // Add a media query change listener
        mql.addListener(function(m) {
            if(m.matches) {
                // Changed to portrait
                $.colorbox.remove();
            } else {
                // Changed to landscape
                $('.colorbox').colorbox({
                    overlayClose: true,
                    opacity: 0.5,
                    rel: "colorbox"
                });
            }
        });
    }

}

function supportsSVG() {
    return !!document.createElementNS && !!document.createElementNS('http://www.w3.org/2000/svg', "svg").createSVGRect;
}

/*
  classList polyfill
  Copyright © 2014 Remy Sharp, http://remysharp.com
  License: http://rem.mit-license.org
*/

(function () {

if (typeof window.Element === "undefined" || "classList" in document.documentElement) return;

var prototype = Array.prototype,
    push = prototype.push,
    splice = prototype.splice,
    join = prototype.join;

function DOMTokenList(el) {
  this.el = el;
  // The className needs to be trimmed and split on whitespace
  // to retrieve a list of classes.
  var classes = el.className.replace(/^\s+|\s+$/g,'').split(/\s+/);
  for (var i = 0; i < classes.length; i++) {
    push.call(this, classes[i]);
  }
};

DOMTokenList.prototype = {
  add: function(token) {
    if(this.contains(token)) return;
    push.call(this, token);
    this.el.className = this.toString();
  },
  contains: function(token) {
    return this.el.className.indexOf(token) != -1;
  },
  item: function(index) {
    return this[index] || null;
  },
  remove: function(token) {
    if (!this.contains(token)) return;
    for (var i = 0; i < this.length; i++) {
      if (this[i] == token) break;
    }
    splice.call(this, i, 1);
    this.el.className = this.toString();
  },
  toString: function() {
    return join.call(this, ' ');
  },
  toggle: function(token) {
    if (!this.contains(token)) {
      this.add(token);
    } else {
      this.remove(token);
    }

    return this.contains(token);
  }
};

window.DOMTokenList = DOMTokenList;

function defineElementGetter (obj, prop, getter) {
    if (Object.defineProperty) {
        Object.defineProperty(obj, prop,{
            get : getter
        });
    } else {
        obj.__defineGetter__(prop, getter);
    }
}

defineElementGetter(Element.prototype, 'classList', function () {
  return new DOMTokenList(this);
});

})();