window.Parsley.setLocale(unithemeparams.locale);

//message
var uni_popup_message = function(text, type) {
  var message_div = jQuery('#uni_popup');
      message_text = message_div.text(text);

  if (type == 'success') {
    message_div.addClass('success-message');
  }
  if (type == 'warning') {
    message_div.addClass('warning-message');
  }
  if (type == 'error') {
    message_div.addClass('error-message');
  }

  if (text != '') {
    message_div.fadeIn(400).dequeue().animate({ left: 25 }, 250, function(){
      setTimeout(function(){
        message_div.animate({ left: -125 }, 250).dequeue().fadeOut(400, function(){

            setTimeout(function(){
              message_div.removeClass('success-message warning-message error-message');
            }, 1);
        });
      }, 3000);
    });
  }
};

jQuery(function ($) {

    $.fn.iLightInputNumber = function (options) {

        var inBox = '.input-number-box',
            newInput = '.input-number',
            moreVal = '.input-number-more',
            lessVal = '.input-number-less';

        this.each(function () {

            var el = $(this);
            $('<div class="' + inBox.substr(1) + '"></div>').insertAfter(el);
            var parent = el.find('+ ' + inBox);
            parent.append(el);
            var classes = el.attr('class');
            parent.append('<input class="' + newInput.substr(1) + '" type="text">');
            el.hide();
            var newEl = el.next();
            newEl.addClass(classes);
            var attrValue;

            function setInputAttr(attrName) {
                if (el.attr(attrName)) {
                    attrValue = el.attr(attrName);
                    newEl.attr(attrName, attrValue);
                }
            }

            setInputAttr('value');
            setInputAttr('placeholder');
            setInputAttr('min');
            setInputAttr('max');
            setInputAttr('step');

            parent.append('<div class=' + moreVal.substr(1) + '>+</div>');
            parent.append('<div class=' + lessVal.substr(1) + '>-</div>');

        }); //end each

        var value,
            step;

        var interval = null,
            timeout = null;

            function ToggleValue(input) {
                input.val(parseInt(input.val(), 10) + d);
                console.log(input);
            }

            $('body').on('mousedown', moreVal, function () {
                var el = $(this);
                var input = el.siblings(newInput);
                moreValFn(input);
                timeout = setTimeout(function(){
                    interval = setInterval(function(){ moreValFn(input); }, 50);
                }, 200);

            });

            $('body').on('mousedown', lessVal, function () {
                var el = $(this);
                var input = el.siblings(newInput);
                lessValFn(input);
                timeout = setTimeout(function(){
                    interval = setInterval(function(){ lessValFn(input); }, 50);
                }, 200);
            });

            $(moreVal +', '+ lessVal).on("mouseup mouseout", function() {
                clearTimeout(timeout);
                clearInterval(interval);
            });

            function moreValFn(input){
                var max = input.attr('max');
                checkInputAttr(input);
                var newValue = value + step;
                if (newValue > max) {
                    newValue = max;
                }
                changeInputsVal(input, newValue);
            }

            function lessValFn(input){
                var min = input.attr('min');
                checkInputAttr(input);
                var newValue = value - step;
                if (newValue < min) {
                    newValue = min;
                }
                changeInputsVal(input, newValue);
            }

            function changeInputsVal(input, newValue){
                input.val(newValue);
                var inputNumber = input.siblings(this);
                inputNumber.val(newValue);
            }

            function checkInputAttr(input) {
                if (input.attr('value')) {
                    value = parseFloat(input.attr('value'));
                } else if (input.attr('placeholder')) {
                    value = parseFloat(input.attr('placeholder'));
                }
                if (!( $.isNumeric(value) )) {
                    value = 0;
                }
                if (input.attr('step')) {
                    step = parseFloat(input.attr('step'));
                } else {
                    step = 1;
                }
            }

            $(newInput).change(function () {
                var input = $(this);
                var value = parseFloat(input.val());
                var min = input.attr('min');
                var max = input.attr('max');
                if (value < min) {
                    value = min;
                } else if (value > max) {
                    value = max;
                }
                if (!( $.isNumeric(value) )) {
                    value = '';
                }
                input.val(value);
                input.siblings(this).val(value);
            });

            $(newInput).keydown(function(e){
                var input = $(this);
                var k = e.keyCode;
                if( k == 38 ){
                    moreValFn(input);
                }else if( k == 40){
                    lessValFn(input);
                }
            });
    };
});

jQuery(function ($) {
    $('.quantity input[type=number]').iLightInputNumber();
});

jQuery( document ).ready( function( $ ) {
    'use strict';

	//// ---> Check issue element
	jQuery.fn.exists = function() {
	  return jQuery(this).length;
	}

	// global vars
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	var slideDescMtop = (($(".slideDesc").height() / 2 ) + 8) * -1;

	if($('.homeBxSlider').exists()){
		$('.homeBxSliderWrap').css({
		   	'height': winHeight
		});
		$('.homeBxSlider').css({
		   	'height': winHeight
		});
		$('.homeBxSlider .slide').css({
		   	'height': winHeight
		});
		$('.homeBxSlider .slide .slideDesc').css({
		   	'margin-top': slideDescMtop
		});

		$(window).resize( function(e)
		{
		    var winWidth = $(window).width();
			var winHeight = $(window).height();
			$('.homeBxSliderWrap').css({
			   	'height': winHeight
			});
			$('.homeBxSlider').css({
			   	'height': winHeight
			});
			$('.homeBxSlider .slide').css({
			   	'height': winHeight
			});
		});
	}

	
	if($('.homeBxSlider').exists()){
		var homeSlide = $('.homeBxSlider').bxSlider({
			mode:"fade",
			auto:true,
			speed:700,
			pause:4000,
			onSlideAfter: function(){
				var gcs = homeSlide.getCurrentSlide();
		        $('.homeBxSlider').find('.slide:not(li[data-slide="'+gcs+'"])').removeClass("active");
				$('.homeBxSlider').find('.slide[data-slide="'+gcs+'"]').addClass("active");
				
		    }
		});
        // Triggers an event - on bx slider init
		$( document.body ).trigger( 'unitheme_home_slider_init_event', [ homeSlide ] );
	}

	if($('.contactGallery').exists()){
		var contactGallery = $('.contactGallery').find("ul").bxSlider({
			auto:true,
			controls:false
		});
	}

	$("#joinEventBtn").fancybox({
		wrapCSS    : 'eventRegistrationFancyboxPopup',
		helpers : {
	        overlay : {
	            css : {
	                'background' : 'rgba(255, 255, 255, 0.94)'
	            }
	        }
	    }
	});

    $(".membershipCardOrder").on("click", function (e) {
            e.preventDefault();

            var price_id = $(this).data("priceid"),
                price_title = $(this).data("pricetitle");

            $.fancybox.open('#membershipCardOrderPopup', {
		        wrapCSS    : 'eventRegistrationFancyboxPopup',
		        helpers : {
			        overlay : {
				        css : {
					        'background' : 'rgba(255, 255, 255, 0.94)'
					        }
				    }
		        },
                beforeShow: function() {
                    $("#uni_price_title").empty().text(price_title);
                    $("input[name=uni_price_id]").val(price_id);
                }
            });
    });

	if(!$('.single-uni_event .nextEventBox').exists()){
		$("body.single-uni_event").addClass("single-uni_event_last");
	}

	$('.singlePostWrap a img').closest("a").addClass("wp-image-wrap");

    $(".teamItem").on("click", function(){
    	var userDescId = $(this).data("userid");
    	$("#"+userDescId).addClass("show");
    });


	$(".closeTeamDesc").on("click", function(){
    	$(this).closest(".teamItemDesc").removeClass("show");
    });
    
	$(".miniCart").on("click", function(){
		$(this).closest(".contentWrap").addClass("showMiniCart");
	});
	$(".closeCartPopup").on("click", function(){
		$(this).closest(".contentWrap").removeClass("showMiniCart");
	});

	$('.options select').selectric();

	$(".galleryThumbItem").on("click", function(e){
		e.preventDefault();
		if (!$(this).hasClass("active")) {
			$("a.galleryThumbItem.active").removeClass("active");
			$(this).addClass("active");
			var imgID = $(this).attr("href");
			$(".productGalleryWrap .current").removeClass("current");
			$(imgID).addClass("current");	
		}
	});

	
	$(".categoryList span").on("click", function(){
		if ($(this).hasClass("clicked")) {
			$(this).removeClass("clicked").closest(".categoryList").find("ul").slideUp(300);
		} else {
			$(this).addClass("clicked").closest(".categoryList").find("ul").slideDown(300);
		}
	});

	$(".sortingList span").on("click", function(){
		if ($(this).hasClass("clicked")) {
			$(this).removeClass("clicked").closest(".sortingList").find("ul").slideUp(300);
		} else {
			$(this).addClass("clicked").closest(".sortingList").find("ul").slideDown(300);
		}
	});

	$(document).on('click', function(e) {
	  if (!$(e.target).parents().hasClass('categoryList') /*&& !$(e.target).hasClass('miniCartWrap')*/ )  {
	    $(".categoryList").find("ul").slideUp(300);
		$(".categoryList span").removeClass("clicked");
	  }
	});

	$(document).on('click', function(e) {
	  if (!$(e.target).parents().hasClass('sortingList') /*&& !$(e.target).hasClass('miniCartWrap')*/ )  {
	    $(".sortingList").find("ul").slideUp(300);
		$(".sortingList span").removeClass("clicked");
	  }
	});

	$(".classesFilter a").on("click", function(e){
		e.preventDefault();
		var filterData = $(this).data("filter");
		if (filterData == "all") {
			$(".classesFilter a.active").removeClass("active");
			$(this).addClass("active");
			$(".fc-content-skeleton a.fc-event.hide").removeClass("hide");
		} else {
			$(".classesFilter a.active").removeClass("active");
			$(this).addClass("active");
			$(".fc-content-skeleton a.fc-event.hide").removeClass("hide");
			$(".fc-content-skeleton a.fc-event").not("."+filterData).addClass("hide");
		}
	});


	$('body').on('click', 'button.fc-button', function() {
		$(".classesFilter a.active").removeClass("active");
		$(".classesFilter a[data-filter='all']").addClass("active");
	});

	$('.showMobileMenu').on("click", function(e){
		e.preventDefault();
		$(this).toggleClass('open').closest("body").toggleClass('animated');
	});

	$(".cartPage .woocommerce td.product-name dl.variation").each(function(){
		var dlContainer = $(this).closest(".cartProduct").find("h4");
		$(this).appendTo(dlContainer);
	});	

	$(".miniCartItem dd, .page-template-templ-wishlist .variation dd").each(function(){
		$("<br>").insertAfter($(this));
	});	

	$(".page-template-templ-wishlist .uni-wishlist-variation-details dl.variation").each(function(){
		var dlContainer = $(this).closest(".uni-wishlist-item-details").find("h4.uni-wishlist-item-title");
		$(this).appendTo(dlContainer);
	});	

	$(".page-template-templ-wishlist .uni-wishlist-item-availability span").each(function(){
		var dlContainer = $(this).closest(".uni-wishlist-item-details").find("h4.uni-wishlist-item-title");
		$(this).appendTo(dlContainer);
	});	

	$(".single-product .woocommerce-breadcrumb").prependTo(".single-product .singleProductWrap");
	$(".singlePostWithSidebar").closest("body").addClass("single-post-with-sidebar");

	if ( $(".postItem p").length > 0 ) {
		$(".postItem p").dotdotdot({
			wrap: 'letter'	
		});
    }
    if ( $(".postItem h4").length > 0 ) {
		$(".postItem h4").dotdotdot({
			wrap: 'letter'	
		});
    }
    if ( $(".gridItemDesc p ").length > 0 ) {
		$(".gridItemDesc p ").dotdotdot({
			wrap: 'letter'	
		});
    }
    if ( $(".mainItemDesc p ").length > 0 ) {
		$(".mainItemDesc p ").dotdotdot({
			wrap: 'word'	
		});
    }
    if ( $(".gridItemDesc h3").length > 0 ) {
		$(".gridItemDesc h3").dotdotdot({
			wrap: 'letter'	
		});
    }
    if ( $(".mainItemDesc h3").length > 0 ) {
		$(".mainItemDesc h3").dotdotdot({
			wrap: 'letter'	
		});
    }

    if($('.teamItemDescWrap').exists()){
		$('.teamItemDescWrap').jScrollPane({
			autoReinitialise: true
		});
	}

	$(".membershipCardItem p").each(function(){
		var winHeight = $(this).height();

	});

	var h=0; // the height of the highest element (after the function runs)
	var h_elem;  // the highest element (after the function runs)
	$(".membershipCardItem p").each(function () {
	    if ($(this).outerHeight() > h) {
	        h_elem = $(this);
	        h = $(this).outerHeight();
	    }
	    
	});
	$(".membershipCardItem p").css({
		"min-height" : h,
		"max-height" : h
	});
	//console.log(h);
	

	/* Sticky */
    if ( !unithemeparams.white_nav ) {
		var sticky_navigation_offset_top = 0;
		var sticky_navigation = function(){
			var scroll_top = $(window).scrollTop();
			if (scroll_top > sticky_navigation_offset_top) { 
				$('#header .headerWrap').addClass("is-sticky");
			} else {
				$('#header .headerWrap').removeClass("is-sticky"); 
			}
		};
		sticky_navigation();

		$(window).on('scroll', function() {
			 sticky_navigation();
		});
    }
	/* END Sticky */
    
    if (winWidth > 767) {
		$('div[data-type="parallax"]').each(function(){
	        var $bgobj = $(this); // assigning the object
	        var bgobjTop = $(this).offset().top;
	    
	        $(window).scroll(function() {
	        	//console.log($(window).scrollTop() + winHeight)
	        	//console.log(bgobjTop)

				if ( ($(window).scrollTop() + winHeight) > bgobjTop )        	
				{

					var yPos = -(($(window).scrollTop() - $bgobj.offset().top) / $bgobj.data('speed')); 
	            
		            // Put together our final background position
		            var coords = '50% '+ yPos + 'px';

		            // Move the background
		            $bgobj.css({ backgroundPosition: coords });	
				}
	        }); 
	    });  
    }

    $(window).resize( function(e)
		{
		    var winWidth = $(window).width();
			var winHeight = $(window).height();

			if (winWidth > 767) {
				$('div[data-type="parallax"]').each(function(){
			        var $bgobj = $(this); // assigning the object
			        var bgobjTop = $(this).offset().top;
			    
			        $(window).scroll(function() {
			        	//console.log($(window).scrollTop() + winHeight)
			        	//console.log(bgobjTop)

						if ( ($(window).scrollTop() + winHeight) > bgobjTop )        	
						{

							var yPos = -(($(window).scrollTop() - $bgobj.offset().top) / $bgobj.data('speed')); 
			            
				            // Put together our final background position
				            var coords = '50% '+ yPos + 'px';

				            // Move the background
				            $bgobj.css({ backgroundPosition: coords });	
						}
			        }); 
			    });
		    }
	});

	// hide the loader after page is loaded
    $(window).load(function() {

    	var headerHeight = $(".headerWrap").height();
    	$("#header").css("height", headerHeight + 1);

		$(window).resize( function(e)
		{
		    var headerHeight = $(".headerWrap").height();
    		$("#header").css("height", headerHeight + 1);
		});

    	/* Ecwid */
		$("#ecwid_body.ecwid-shopping-cart .uni-container").prepend("<style> .pageHeader h1 {color:"+unithemeparams.shop_text_color+";} </style> <div class='pageHeader' style='background-image: url("+unithemeparams.shop_image+");'><h1>"+unithemeparams.shop_text+"</h1></div>");

        $(".loaderWrap").addClass("hide");

    });

    $("body").on("click", ".uni_input_submit", function (e) {
        var submit_button = $(this),
            this_form = submit_button.closest("form");
        this_form.submit();
    });

    $("body").on("submit", ".uni_form", function (e) {

        var submit_button = $(this),
            this_form = submit_button.closest("form"),
            action = this_form.attr('action');
            //console.log(submit_button);
        var form_valid = this_form.parsley({excluded: '[disabled]'}).validate();

            if ( form_valid ) {
                var dataToSend = this_form.serialize();

			    $.ajax({
				    type: 'post',
	        	    url: action,
	        	    data: dataToSend + '&cheaters_always_disable_js=' + 'true_bro',
	        	    dataType: 'json',
	        	    beforeSend: function(){
	        	        this_form.block({
	        	            message: null,
                            overlayCSS: { background: '#fff', opacity: 0.6 }
                        });
	        	    },
	        	    success: function(response) {
	        		    if ( response.status == "success" ) {
                            this_form.unblock();
                            uni_popup_message(response.message, "success");
                            $.fancybox.close();
	        		    } else if ( response.status == "error" ) {
                            this_form.unblock();
                            uni_popup_message(response.message, "error");
	        		    }
	        	    },
	        	    error:function(response){
	        	        this_form.unblock();
	        	        uni_popup_message("Error!", "warning");
	        	    }
	            });
            }
            return false;
    });

    if ( unithemeparams.lazy_load_on_products ) {
        $('.products').infinitescroll({
            navSelector  : ".woocommerce-pagination",
            nextSelector : ".woocommerce-pagination ul li a.next",
            itemSelector : "li.product",
            loading: {
                finished: undefined,
                finishedMsg: "<em>"+unithemeparams.lazy_load_end+"</em>",
                img: unithemeparams.lazy_loader,
                msg: null,
                msgText: "",
                selector: null,
                speed: 'fast',
                start: undefined
            },
            pixelsFromNavToBottom: 240
        });
    }
    if ( unithemeparams.lazy_load_on_events ) {
        $('.eventsWrap').infinitescroll({
            navSelector  : ".pagination",
            nextSelector : ".pagination ul li a.next",
            itemSelector : ".eventItem",
            loading: {
                finished: undefined,
                finishedMsg: "<em>"+unithemeparams.lazy_load_end+"</em>",
                img: unithemeparams.lazy_loader,
                msg: null,
                msgText: "",
                selector: null,
                speed: 'fast',
                start: undefined
            },
            pixelsFromNavToBottom: 700
        });
    }
    if ( unithemeparams.lazy_load_on_posts ) {
        $('.blogPostWrap').infinitescroll({
            navSelector  : ".pagination",
            nextSelector : ".pagination ul li a.next",
            itemSelector : ".postItem",
            loading: {
                finished: undefined,
                finishedMsg: "<em>"+unithemeparams.lazy_load_end+"</em>",
                img: unithemeparams.lazy_loader,
                msg: null,
                msgText: "",
                selector: null,
                speed: 'fast',
                start: undefined
            },
            pixelsFromNavToBottom: 240
        });
    }

    //
    //Cookies.remove('uni_is_retina');
    var uni_is_retina = Cookies.get('uni_is_retina');
    if ( typeof uni_is_retina === 'undefined' ) {
        Cookies.set('uni_is_retina', uniIsRetina(), { expires: 7 });
        window.location.reload();
    }

    function uniIsRetina(){
        return ((window.matchMedia && (window.matchMedia('only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx), only screen and (min-resolution: 75.6dpcm)').matches || window.matchMedia('only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (min--moz-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)').matches)) || (window.devicePixelRatio && window.devicePixelRatio >= 2)) && /(iPad|iPhone|iPod)/g.test(navigator.userAgent);
    }

});

	


