// function - gets the dimensions of the main section & applying full height to the left sidebar
//----------------------------------------------------------------------------------------------------

/* forces equal height on product blocks */

equalheight = function(container){

	var currentTallest = 0,
	     currentRowStart = 0,
	     rowDivs = new Array(),
	     $el,
	     topPosition = 0;
	 $(container).each(function() {

	   $el = $(this);
	   $($el).height('auto')
	   topPostion = $el.position().top;

	   if (currentRowStart != topPostion) {
	     for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
	       rowDivs[currentDiv].height(currentTallest);
	     }
	     rowDivs.length = 0; // empty the array
	     currentRowStart = topPostion;
	     currentTallest = $el.height();
	     rowDivs.push($el);
	   } else {
	     rowDivs.push($el);
	     currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
	  }
	   for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
	     rowDivs[currentDiv].height(currentTallest);
	   }
	 });
}

function jqUpdateSize(){
	
	var mainHeight = $('div.main').height();
	var sidebarRightHeight = $('div.sidebar.right').height();
	var sidebarLeftHeight = $('header').height();
	var sidebarLeftWidth = $('div.sidebar.left').width();
	var heroHeight = $(window).height() - 249; 					// canvas height minus header (79px) & the actionbar (166px)
	var heroHeightPad = $(window).height() - 205; 				// canvas height minus header (79px) & the actionbar (160px)
	var heroHeightMobile = $(window).height() - 50; 			// canvas height minus header (50px)
		
	/* determines which column is the tallest (main or right sidebar) */	
	if(sidebarRightHeight > mainHeight) {
		var sidebarExtension = sidebarRightHeight
	}
	
	if(sidebarRightHeight < mainHeight) { 
		var sidebarExtension = mainHeight
	}
	
	/* everything except mobile */
	if ($(window).width() > 640) {
		$('.sidebar.left').height(mainHeight);
		$('.sidebar-continuous').height(sidebarRightHeight);
		$('ul.subnav').width(sidebarLeftWidth);
		$('#hero-home').height(heroHeight);
		/* if ($('#hero-slider').length != 0) {$('#hero-slider').iosSlider('update');} */

		equalheight('.v-cards .columns'); // equal height on v-cards

	}
	
	if ($(window).width() < 1101) { 
		$('#hero-home').height(heroHeightPad);
		/* if ($('#hero-slider').length != 0) {$('#hero-slider').iosSlider('update');} */
	} 
	
	/* mobile only */
	if ($(window).width() < 640) { 
		$('.sidebar.left').height(sidebarLeftHeight);
		$('ul.subnav').css({'width' : '100%'});
		$('#hero-home').height(heroHeightMobile);
		/* if ($('#hero-slider').length != 0) {$('#hero-slider').iosSlider('update');} */
	}
	
	if ($(window).width() > 1023) { 
		$('.sidebar.left').height(sidebarExtension);
	} 
	
	
};

$(document).ready(jqUpdateSize);			// When the document is ready
$(window).load(jqUpdateSize);				// When the page is fully loaded 
$(window).resize(jqUpdateSize);				// When the browser changes size


$(document).ready(function(){

	
	
// front page - slider swipe
//----------------------------------------------------------------------------------------------------
	
	function slideChange(args) {
		$('#pagination li').removeClass('active');
		$('#pagination li:eq(' + (args.currentSlideNumber - 1) + ')').addClass('active');
	}
	
	
	/* front page - hero slider */
	if ($('#hero-slider').length != 0) {
		
		$('#hero-slider').iosSlider({
			desktopClickDrag: true,
			snapToChildren: true,
			infiniteSlider: true,
			navSlideSelector: '#pagination li',
			onSlideChange: slideChange,
			autoSlide: true,
			autoSlideTimer: 8000,
			navNextSelector: $('.pagination-nav.next'),
			navPrevSelector: $('.pagination-nav.prev')
		});	
		
	}
	
	/* products - sticky subnav */
	if ($('ul.subnav').length != 0) {
		setTimeout(function(){
			$('ul.subnav').addClass('stuck');
		}, 600);
	}
	
	
	
	
});


$(window).load(function() {
	$(document).foundation('equalizer','reflow');
});