
/* ---------- @ GLOBAL VARIABLES -----------*/
var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};
/* ---------- @ DOCUMENT READY FUNCTIONS -----------*/
	$(document).ready(function(e){

		// Site wide
		 $(document).scrollsnap({
			snaps: 'article:not(.hero, .footer-module, .nosnap)',
			proximity: 200
		});
		subnav();
		shortlist();
		 
		if($(".product-grid").size() > 0){
			product_index();
		}
	});
/* ---------- @ SUB NAV -----------*/
function subnav(){
	$("nav .uk-parent").click(function(){
		$(".subnav:not("+$(this).find("a").attr("href")+")").slideUp();
		$($(this).find("a").attr("href")).slideToggle();
		return false;
	});
	if(isMobile.any()){
		$('a.shortlist_action').on('vclick', function(e) {
			parent = $(this).parent('.shortlist_action_container');

			$.ajax({
			  url: $(this).attr('href'),
			  success: function(data) {	    
			  // Do something with the result
				$(".uk-navbar-nav .shortlist .uk-badge").html(data.total_items);
				$.ajax({
				  url: '/global/shortlist-list',
				  success: function(data_nested) {
						$(".shortlist-container").html(data_nested);
						shortlist();
						jQuery.UIkit.offcanvas.offcanvas.show('#shortlist');
					}
				});
			  }
			});
		  // stop event propagation here
		  return false;
		});
	}else{
		$('a.shortlist_action').on('click',function(){	
			parent = $(this).parent('.shortlist_action_container');

			$.ajax({
			  url: $(this).attr('href'),
			  success: function(data) {	    
			  // Do something with the result
				$(".uk-navbar-nav .shortlist .uk-badge").html(data.total_items);
				$.ajax({
				  url: '/global/shortlist-list',
				  success: function(data_nested) {
						$(".shortlist-container").html(data_nested);
						shortlist();
						jQuery.UIkit.offcanvas.offcanvas.show('#shortlist');
					}
				});
			  }
			});
		  // stop event propagation here
		  return false;
		});
	}
}
/* ---------- @ SHORTLIST -----------*/
function shortlist(){
	if(isMobile.any()){
		$(".shortlist-delete").on('vclick', function(e) {
			$(this).parent().remove();

			$.ajax({
			  url: $(this).attr('href'),
			  success: function(data) {	    
			  // Do something with the result
				$(".uk-navbar-nav .shortlist .uk-badge").html(data.total_items);
				$.ajax({
				  url: '/global/shortlist-list',
				  success: function(data_nested) {
						$(".shortlist-container").html(data_nested);
						shortlist();
					}
				});
			  }
			});
		  // stop event propagation here
		  return false;
		});
	}else{
		$(".shortlist-delete").click(function(){
			$(this).parent().remove();

			$.ajax({
			  url: $(this).attr('href'),
			  success: function(data) {	    
			  // Do something with the result
				$(".uk-navbar-nav .shortlist .uk-badge").html(data.total_items);
				$.ajax({
				  url: '/global/shortlist-list',
				  success: function(data_nested) {
						$(".shortlist-container").html(data_nested);
						shortlist();
					}
				});
			  }
			});
		  // stop event propagation here
		  return false;
		});
	};
	
}
/* ---------- @ RESIZE FUNCTIONS -----------*/
	$(window).resize(function() {
		
	});
	
// Product Index
	function product_index_add_touch(){
		
	}
	function product_index(){
		// Touchscreen functions
		if(isMobile.any()){
			
		    $('.product-grid .product-width-medium-1-2, .search-index .product-width-medium-1-2').on('vclick', function(e) {
					$(this).siblings().find(".slide").animate({
							marginTop:"-24"
					}).find("section").fadeOut();
					if($(this).find(".slide").css('margin-top') == '-24px'){
						$(this).find(".slide").animate({
								marginTop:"-="+($(this).height()/4)
						}).find("section").fadeIn();
					}
			        
			   }); 
		} else {
		// Desktop functions
		    $(".product-grid .product-width-medium-1-2, .search-index .product-width-medium-1-2 ").mouseenter(function() {
			   $(this).find(".slide").animate({
					marginTop:"-=50"
				}).find("section").fadeIn();
			  })
			  .mouseleave(function() {
			    $(this).find("section").fadeOut();
				$(this).find(".slide").animate({
						marginTop:"+=50"
					});
			  });
		}
		// If search index...
		function resize() {
			$(".up_large").css('height',($(window).height()-70));
			$('.up_large .headline').css("padding-top",((($(".up_large").height()-70)/2) - ($('.up_large .headline').height()/2)));
	    }

	    // Bind resize to wintow resize event
	    $(window).on('resize', function() {
	        resize();
	    });

	    // Trigger the initial resize
	    resize();
	
		
		
		// Show product title
		$(".up_large .headline").find("h1").animate(
		    { opacity: 1 },
		    { queue: true, duration: 'slow' }
		  );
		$.doTimeout( 1000, function(){
			$(".up_large .headline").find("span").animate(
			    { opacity: 1 },
			    { queue: true, duration: 'slow' }
			  );
		});
		

	}
		
