( function( $ ) {
"use strict";
	$(document).ready(function(){

    $('[data-toggle="genesis-tooltip"]').tooltip({
      container : 'body'
    });
		
		$('select').select2({
			minimumResultsForSearch: Infinity,
			dropdownParent: '.site-content'
		});

		var offset = 480;
    $(window).scroll(function() {
      if ($(this).scrollTop() > offset) {
          jQuery('#back-top').addClass('active');
      } else {
          jQuery('#back-top').removeClass('active');
      }
    });

		$('#back-top').click(function(e){
			e.preventDefault();
			$('body,html').animate({
	      scrollTop: 0
	    }, 500);
	    return false;
		});

		$('#search-header-btn > a').click(function(e){
			e.preventDefault();
			$('.search-header').toggleClass('active');
			setTimeout(function() { $('#the-search').focus() }, 100);
		});

    $('#setting-header-btn > a').click(function(e){
      e.preventDefault();
      $('.setting-header').toggleClass('active');
    });

		$('.cart-header .cart-content').perfectScrollbar({
			suppressScrollX: true
		});

		var $pswp = $('.pswp')[0];
    var image = [];

    $('.product-images-wrap').each( function() {
      var $pic     = $(this),
      getItems = function() {
        var items = [];
        $pic.find('a').each(function() {
          var $href   = $(this).attr('href'),
              $size   = $(this).data('size').split('x'),
              $width  = $size[0],
              $height = $size[1];

          var item = {
              src : $href,
              w   : $width,
              h   : $height
          }

          items.push(item);
        });
        return items;
      }

      var items = getItems();

      $.each(items, function(index, value) {
        image[index]     = new Image();
        image[index].src = value['src'];
      });



      $pic.on('click', 'a', function(event) {
        event.preventDefault();
        
        var $index = $(this).attr('data-index');
        var options = {
            index: $index,
            bgOpacity: 0.7,
            showHideOpacity: true, 
            shareButtons: [
					    {id:'facebook', label:'Share on Facebook', url:'https://www.facebook.com/sharer/sharer.php?u={{url}}'},
					    {id:'twitter', label:'Tweet', url:'https://twitter.com/intent/tweet?text={{text}}&url={{url}}'},
					    {id:'pinterest', label:'Pin it', url:'http://www.pinterest.com/pin/create/button/?url={{url}}&media={{image_url}}&description={{text}}'},
						],
        }

        var lightBox = new PhotoSwipe($pswp, PhotoSwipeUI_Default, items, options);
        lightBox.init();
      });
    });

		$('.tabs-nav li a').click(function(ev){
      ev.preventDefault();
      var tab_id = $(this).attr('href');

      $('.tabs-nav li a').removeClass('active');
      $('.tab-content').removeClass('active');

      $(this).addClass('active');
      $(tab_id).addClass('active');
    });

		$('.menu-toggle').click(function(e){
			e.preventDefault();
			$(this).toggleClass('active');
			$('.main-navigation ul.menu').toggleClass('inactive');
		});

		$('.menu-expand').click(function(ev){
      ev.preventDefault();
			$(this).toggleClass('active');
			$(this).siblings('.sub-menu').toggleClass('menu-expanded');
    });

    jQuery('.cart-qty .qty-control').click(function(ev){
    ev.preventDefault();
      var button = jQuery(this);
      var oldValue = button.parent().parent().find('input').val();

      if(button.hasClass('cart-qty-inc')) {
        var newVal = parseFloat(oldValue) + 1;
      }else{
        if(oldValue > 0){
            var newVal = parseFloat(oldValue) - 1;
        }else{
          newVal = 0;
        }
      }

      button.parent().parent().find('input').val(newVal);

    });

	});
} )( jQuery );