/* Author: Pete Eveleigh */

/* nav dropdown effect */

/* Screen Width */

screenWidth = screen.width;

/* product page tab section */
$('.nav-tabs a:first').tab('show');

/* general tooltip triggers */
$('.icon').tooltip()


/* remove product from basket using AJAX */
// use event bubbling by checking for clicks within the basket and testing to see if they are remove clicks
	$('.my-cart').click(function(event){ 
		if($(event.target).is('.removefrombasket')){
			var rel = $(event.target).attr("rel");
			var rStr = Date.now();
	    	$.get('https://www.pccs-books.co.uk/cart/delete_from_cart/'+ rel + '/' + rStr, function(data) {$('.my-cart').load('https://www.pccs-books.co.uk/includes/_sidebasket');
				$('.cart-total').load('https://www.pccs-books.co.uk/includes/_tablecart');
		    });
		    event.preventDefault();
		}
		
		if($(event.target).is('.emptybasket')){$.get('https://www.pccs-books.co.uk/cart/clear_cart', function(data) {$('.my-cart').load('https://www.pccs-books.co.uk/includes/_sidebasket'); 
		    });
		    event.preventDefault();
		}
		
	});

	$('.cart-total').click(function(event){ 
		if($(event.target).is('.removefrombasket')){
			var rel = $(event.target).attr("rel");
			var rStr = Date.now();
	    	$.get('https://www.pccs-books.co.uk/cart/delete_from_cart/'+ rel +'/'+ rStr, function(data) {$('.cart-total').load('https://www.pccs-books.co.uk/includes/_tablecart');
				$('.my-cart').load('https://www.pccs-books.co.uk/includes/_sidebasket');
		    });
		    event.preventDefault();
		}
	});
	
	
	// same again for basket review - should roll these into 1 set of functions really
	$('#baskettable').click(function(event){ 
		if($(event.target).is('.removefrombasket')){
			var rel = $(event.target).attr("rel");
			var rStr = Date.now();
	    	$.get('https://www.pccs-books.co.uk/cart/delete_from_cart/'+ rel + '/' + rStr, function(data) {$('#baskettable').load('https://www.pccs-books.co.uk/includes/_tablebasket'); 
		    });
		    event.preventDefault();
		}
		
	});
	
	
/* script to pop up newsletter sign up automatically */
// this should pop up the newsletter sign-up modal (or a modified one) after a period of time (10 seconds?)
// also needs to write a cookie so that it doesn't do it more than once ...

	$(document).ready( function() {
		window.setTimeout(popupNewsletter, 10000);
	});
	
	function popupNewsletter() {
		
		// if this is our first page on this site exit
		//if (document.referrer.includes("pccs-books.co.uk") == false) {
		//	console.log("first page");
		//	return false;
		//}
		
		if (getCookie("newsletter") != null) {
			//console.log("pop up already activated");
			return false;
		} else {
			setCookie("newsletter", "true");
			//console.log("cookie set");
		}

		$('#newslettermodal').modal('show');

	}
	
	function setCookie(key, value) {
		var expires = new Date();
		expires.setTime(expires.getTime() + (365 * 24 * 60 * 60 * 1000));
		document.cookie = key + '=' + value +';path=/'+ ';expires=' + expires.toUTCString();
	}

	function getCookie(key) {
		var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
		return keyValue ? keyValue[2] : null;
	}

(function($) {
    "use strict";
    
    /*----------------------------
     Top Menu Stick
    ------------------------------ */
    var header = $('#header-sticky');
    var win = $(window);
    
    win.on('scroll', function() {
        if ($(this).scrollTop() > 120) {header.addClass("sticky");} else {header.removeClass("sticky");}
    });
    
    /*----------------------------
     Jquery MeanMenu
    ------------------------------ */
    jQuery('#mobile-menu-active').meanmenu({
	});
    
    /*----------------------------
     Wow js active
    ------------------------------ */
    new WOW().init();
    
    /*----------------------------
     Slider active
    ------------------------------ */
    $('.slider-active').owlCarousel({
        smartSpeed: 1000,
        margin: 0,
        autoplay: false,
        nav: true,
        dots: true,
        loop: true,
        navText: ['<i class="fa fa-angle-left"></i>', '<i class="fa fa-angle-right"></i>'],
        responsive: {
            0: {
                items: 1
            },
            768: {
                items: 1
            },
            1000: {
                items: 1
            }
        }
    })
    
    
    /* Quickview-active active */
    $('.quickview-active').owlCarousel({
        loop: true,
        autoplay: false,
        autoplayTimeout: 5000,
        navText: ['<i class="fa fa-angle-left"></i>', '<i class="fa fa-angle-right"></i>'],
        nav: true,
        item: 3,
        margin: 12,
    })
    
    
    /*--------------------------
    Tab active
    ---------------------------- */
    var ProductDetailsSmall = $('.product-details-small a');
    ProductDetailsSmall.on('click', function(e) {
        e.preventDefault();
        var $href = $(this).attr('href');
        ProductDetailsSmall.removeClass('active');
        $(this).addClass('active');
        $('.product-details-large .tab-pane').removeClass('active');
        $('.product-details-large ' + $href).addClass('active');
    })
    
    /*----------------------------
     Featured Books active
    ------------------------------ */
    $('.featured-books-active').owlCarousel({
        smartSpeed: 1000,
        nav: true,
        autoplay: true,
		autoplaySpeed: 3000,
		autoplayHoverPause: true,
		checkVisible: false,
		lazyLoad: true,
        dots: false,
        loop: true,
        margin: 20,
        navText: ['<i class="fa fa-angle-left"></i>', '<i class="fa fa-angle-right"></i>'],
        responsive: {
            0: {
                items: 1,
				touchDrag: true,
				nav: false
			},
            480: {
                items: 2
            },
            768: {
                items: 3
            },
            992: {
                items: 4
            },
            1170: {
                items: 4
            },
            1300: {
                items: 5
            }
        }, 
		onInitialized: showOwl
    })
	
	function showOwl(event) {
    	$("#featured").css("display", "block");
	}
	

    /*----------------------------
     Related Items active
    ------------------------------ */
    $('.related-items-active').owlCarousel({
        smartSpeed: 1000,
        nav: false,
        autoplay: true,
		autoplayHoverPause: true,
		items: 5,
        loop: true,
        navText: ['<i class="fa fa-angle-left"></i>', '<i class="fa fa-angle-right"></i>'],
        responsive: {
            0: {
                items: 1
            },
            768: {
                items: 3
            },
            1000: {
                items: 4
            },
            1170: {
                items: 4
            },
            1300: {
                items: 5
            }
        }
    })
    

    /*----------------------------
     Best Seller Sidebar
    ------------------------------ */
    $('.best-sellers-active').owlCarousel({
        smartSpeed: 1000,
        margin: 0,
        nav: false,
        autoplay: false,
        dots: false,
        loop: false,	
		touchDrag: false,
		mouseDrag: false,
        responsive: {
            0: {
                items: 1,
            },
            768: {
                items: 1
            },
            1000: {
                items: 1
            }
        }
    })
    
    
    /*----------------------------
     Parallax active
    ------------------------------ */
    // $('.bg').parallax("50%", 0.1);
    
    /*----------------------------
     Flexslider slider active
    ------------------------------ */
    $('.flexslider').flexslider({
        animationloop: false,
		loop: false,
        controlNav: "thumbnails"
    });
    
    /*-------------------------
      Showlogin toggle function
    --------------------------*/
    $('#showlogin').on('click', function() {$('#checkout-login').slideToggle(900);});
    
    /*-------------------------
      Showcoupon toggle function
    --------------------------*/
    $('#showcoupon').on('click', function() {$('#checkout_coupon').slideToggle(900);});
    
    /*-------------------------
      Create an account toggle function
    --------------------------*/
    $('#cbox').on('click', function() {$('#cbox_info').slideToggle(900);});
    
    /*-------------------------
      Create an account toggle function
    --------------------------*/
    $('#ship-box').on('click', function() {$('#ship-box-info').slideToggle(1000);});
    
    /*-------------------------
      Showlogin toggle function
    --------------------------*/
    $('#showcat').on('click', function() {$('#hidecat').slideToggle(900);});
    
    /*Category accordion*/
    $('.rx-parent').on('click', function() {
        $('.rx-child').slideToggle();
        $(this).toggleClass('rx-change');
    });
    
    /*--------------------------
       Countdown
    ---------------------------- 
    $('[data-countdown]').each(function() {
        var $this = $(this),
            finalDate = $(this).data('countdown');
        $this.countdown(finalDate, function(event) {
            $this.html(event.strftime('<div class="cdown days"><span class="counting counting-2">%-D</span>days</div><div class="cdown hours"><span class="counting counting-2">%-H</span>hrs</div><div class="cdown minutes"><span class="counting counting-2">%M</span>mins</div><div class="cdown seconds"><span class="counting">%S</span>secs</div>'));
        });
    });
    
    
    /*---------------------
    	Counter
    --------------------- 
    $('.counter').counterUp({
        delay: 10,
        time: 1000
    });
    
    
    /*---------------------
    	Category menu
    --------------------- */
    $('#cate-toggle li.has-sub>a').on('click', function() {
        $(this).removeAttr('href');
        var element = $(this).parent('li');
        if (element.hasClass('open')) {
            element.removeClass('open');
            element.find('li').removeClass('open');
            element.find('ul').slideUp();
        } else {
            element.addClass('open');
            element.children('ul').slideDown();
            element.siblings('li').children('ul').slideUp();
            element.siblings('li').removeClass('open');
            element.siblings('li').find('li').removeClass('open');
            element.siblings('li').find('ul').slideUp();
        }
    });
    $('#cate-toggle>ul>li.has-sub>a').append('<span class="holder"></span>');
    
    
    /*--------------------------
     ScrollUp
    ---------------------------- */
    $.scrollUp({
        scrollText: '<i class="fa fa-angle-up"></i>',
        easingType: 'linear',
        scrollSpeed: 900,
        animation: 'fade'
    });
    
    
    /* Category Dropdown Menu  */
    if ($(window).width() < 768) {
        function sidemenuDropdown() {
            var $this = $('.category-menu');
            $this.find('nav.menu .cr-dropdown').find('.left-menu').css('display', 'none');
            $this.find('nav.menu .cr-dropdown ul').slideUp();
            $this.find('nav.menu .cr-dropdown a').on('click', function(e) {
                e.preventDefault();
                $(this).parent('.cr-dropdown').children('ul').slideToggle();
            });
            $this.find('nav.menu .cr-dropdown ul .cr-sub-dropdown ul').css('display', 'none');
            $this.find('nav.menu .cr-dropdown ul .cr-sub-dropdown a').on('click', function(e) {
                e.preventDefault();
                $(this).parent('.cr-sub-dropdown').children('ul').slideToggle();
            });
        }
        sidemenuDropdown();
    }
	


})(jQuery);



