Created
May 20, 2015 01:05
-
-
Save jackmcpickle/ab5e656e2c035880a70b to your computer and use it in GitHub Desktop.
Sugar stem site main js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
var getSelectedID, initOptionDesc, initOptionImages, initOptionPrices, initProductSliders, resizeContainer, selectedParent; | |
$("input").placeholder(); | |
selectedParent = $('.selected').parents(".product-container"); | |
initOptionImages = function() { | |
var selectImages; | |
selectImages = $('option[data-image]').parent('select'); | |
return selectImages.on('change', function() { | |
var entryID, imageNum; | |
entryID = getSelectedID(); | |
imageNum = $(this).find('option:selected').data('image') || '0'; | |
return $(".js-product-gallery-" + entryID).flexslider(imageNum - 1); | |
}); | |
}; | |
initOptionPrices = function() { | |
var selectPrices; | |
selectPrices = $('option[data-price]').parent('select'); | |
return selectPrices.on('change', function() { | |
var currentPrice, entryID, optionPrice, priceField, updatedPrice; | |
entryID = getSelectedID(); | |
priceField = $('.js-product-item.selected').parent().find('.price'); | |
currentPrice = priceField.data('price'); | |
optionPrice = $(this).find('option:selected').data('price') || '0'; // if no price return 0 | |
updatedPrice = parseInt(currentPrice) + parseInt(optionPrice); | |
return priceField.html("$" + (updatedPrice.toFixed(2))); | |
}); | |
}; | |
initOptionDesc = function() { | |
var selectDesc; | |
selectDesc = $('option[data-desc]').parent('select'); | |
return selectDesc.on('change', function() { | |
var descArea, description, entryID; | |
entryID = getSelectedID(); | |
description = $(this).find('option:selected').data('desc') || ''; | |
descArea = $('.js-product-item.selected').parent().find('.option-description'); | |
return descArea.html("<p>" + description + "</p>"); | |
}); | |
}; | |
getSelectedID = function() { | |
return $('.js-product-item.selected').data('id'); | |
}; | |
initProductSliders = function(entryID) { | |
return $(".js-product-gallery-" + entryID).flexslider({ | |
animation: "slide", | |
controlNav: "thumbnails", | |
animationLoop: true, | |
slideshow: true, | |
smoothHeight: true, | |
animationSpeed: 250, | |
start: function(slider) { | |
return resizeContainer($('.selected').parents(".product-container")); | |
}, | |
before: function(slider) { | |
return resizeContainer($('.selected').parents(".product-container")); | |
} | |
}); | |
}; | |
resizeContainer = function(container) { | |
// set height of project details | |
var containerHeight; | |
containerHeight = container.find(".product-details").outerHeight() + 16; | |
return container.animate({ | |
paddingBottom: containerHeight + "px" | |
}, { | |
duration: 200 | |
}); | |
}; | |
$(".js-product-item, .js-close-details").on("click", function(element) { | |
var currentOpen, entryID, openContainer, slideCurrent, sliderOpen, timerDelayForClose; | |
element.preventDefault(); | |
// set up variables | |
sliderOpen = $(".product-item.selected").length > 0; | |
currentOpen = $(this).hasClass("selected"); | |
slideCurrent = $(this); | |
entryID = $(this).data('id'); | |
timerDelayForClose = 500; | |
// Close any currently open product | |
if (sliderOpen) { | |
openContainer = $(".product-item.selected").parent(".product-container"); | |
$(".product-item").removeClass("selected"); | |
$(".product-details").slideUp(timerDelayForClose); | |
openContainer.animate({ | |
paddingBottom: "10px" | |
}, { | |
duration: timerDelayForClose | |
}); | |
} | |
// Check if we are not just closing the current product | |
if (!currentOpen) { | |
return setTimeout((function() { | |
var container; | |
// Add selected class to this div | |
slideCurrent.addClass("selected"); | |
// add padding to row | |
container = slideCurrent.parent(".product-container"); | |
resizeContainer(container); | |
slideCurrent.next(".product-details").slideDown(timerDelayForClose); | |
return setTimeout((function() { | |
return initProductSliders(entryID); | |
}), timerDelayForClose); | |
}), timerDelayForClose); | |
} | |
}); | |
$(window).on('resize', function() { | |
return resizeContainer($('.selected').parents(".product-container")); | |
}); | |
// Banner slider | |
$('.js-banner-slides').flexslider({ | |
animation: "slide", | |
controlNav: false, | |
animationLoop: true, | |
slideshow: false | |
}); | |
initOptionImages(); | |
initOptionPrices(); | |
return initOptionDesc(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment