$(document).ready(function() {
var searchTimeout = null;
var searchInput = '';
var loading = '
';
var results_element = null;
$('#search-site-input, #scrolling-search-site-input').on('keyup', function() {
results_element = $(this).attr('data-results-element');
results_element = '#' + results_element;
if($(this).val() != searchInput) {
$('.search-results-list').html('');
$(results_element + ' .search-results-list').html(loading);
searchInput = $(this).val();
clearTimeout(searchTimeout);
searchTimeout = setTimeout(searchProducts, 400);
}
$('.search-results-list').show();
});
var searchProducts = function() {
//console.log(searchInput);
if(searchInput.length > 0) {
$(results_element).show();
$('#current-search-term').html(searchInput);
var resultList = $(results_element + ' .search-results-list');
resultList.html(loading);
var resultContent = '';
resultContent += ' Top Results for "' + searchInput + '"';
resultList.html(resultContent + loading);
var send = {searchterm: searchInput};
$.ajax({
type: 'POST',
url: 'request/quicksearch',
data: send,
dataType: 'json',
encode: true
}).done(function(results) {
//console.log(results);
var displayLimit = 8;
var countProducts = results.products.length;
var prodLimit = 0;
var countItems = results.items.length;
var itemLimit = 0;
var countCategories = results.categories.length;
var catLimit = 0;
if(countProducts > 0) {
if(countProducts > 3) {
prodLimit += 3;
} else {
prodLimit += countProducts;
}
}
if(countItems > 0) {
if(countItems > 3) {
itemLimit += 3;
} else {
itemLimit += countItems;
}
}
if(countCategories > 0) {
if(countCategories > 2) {
catLimit += 2;
} else {
catLimit += countCategories;
}
}
var countOutput = prodLimit + itemLimit + catLimit;
if(countOutput < displayLimit) {
var leftOver = displayLimit - countOutput;
if(countProducts > prodLimit) {
var remainingProduct = countProducts - prodLimit;
if(remainingProduct > 0) {
prodLimit += (remainingProduct + countOutput <= displayLimit ? remainingProduct : displayLimit - countOutput);
}
}
countOutput = prodLimit + itemLimit + catLimit
if(countOutput < displayLimit) {
leftOver = displayLimit - countOutput;
if(countItems > itemLimit) {
var remainingItems = countItems - itemLimit;
if(remainingItems > 0) {
itemLimit += (remainingItems + countOutput <= displayLimit ? remainingItems : displayLimit - countOutput);
}
}
}
countOutput = prodLimit + itemLimit + catLimit
if(countOutput < displayLimit) {
leftOver = displayLimit - countOutput;
if(countCategories > catLimit) {
var remainingCats = countCategories - catLimit;
if(remainingCats > 0) {
catLimit += (remainingCats + countOutput <= displayLimit ? remainingCats : displayLimit - countOutput);
}
}
}
}
if(prodLimit) {
for(i=0; i < prodLimit; i++) {
data = results.products[i];
if(data && data.product) {
//Determine Image
if(data.productImg) {
if(data.productImg.small && data.productImg.small != '') {
var pImg = productImgUrl.small + data.productImg.small;
} else if(data.productImg.large && data.productImg.large != '') {
var pImg = productImgUrl.large + data.productImg.large;
} else {
var pImg = productImgUrl.large + 'noa.jpg';
}
} else {
var pImg = productImgUrl.large + 'noa.jpg';
}
if(data.product.customLinkTo && data.product.customLinkTo.length > 0) {
var linkTo = data.product.customLinkTo;
} else {
var linkTo = 'p/' + formatProductLink(data.product.name) + '/' + data.product.productid;
}
resultContent += ' ' + data.product.name + '';
}
} //End for loop
} //End product matches
//BUILD ITEMS OUTPUT
if(itemLimit) {
for(i=0; i < itemLimit; i++) {
data = results.items[i];
if(data && data.item && data.product) {
//Determine Image
if(data.itemImg) {
//Check for item or product images
if(data.itemImg.small && data.itemImg.small != '') {
var pImg = productImgUrl.small + data.itemImg.small;
} else if(data.itemImg.large && data.itemImg.large != '') {
var pImg = productImgUrl.large + data.itemImg.large;
}
} else if(data.productImg) {
if(data.productImg.small && data.productImg.small != '') {
var pImg = productImgUrl.small + data.productImg.small;
} else if(data.productImg.large && data.productImg.large != '') {
var pImg = productImgUrl.large + data.productImg.large;
}
} else {
var pImg = productImgUrl.large + 'noa.jpg';
}
if(data.product.customLinkTo && data.product.customLinkTo.length > 0) {
var linkTo = data.product.customLinkTo;
} else {
var linkTo = 'p/' + formatProductLink(data.product.name) + '/' + data.product.productid + '/' + data.item.itemid;
}
resultContent += ' ' + data.item.name + '';
}
} //End for loop
} //End item matches
if(catLimit) {
for(i=0; i < catLimit; i++) {
var cat = results.categories[i];
if(cat) {
if(!cat.isBrand) {
var linkTo = (cat.customLinkTo && cat.customLinkTo.length > 0 ? cat.customLinkTo : 'c/' + cat.url);
} else {
var linkTo = (cat.customLinkTo && cat.customLinkTo.length > 0 ? cat.customLinkTo : 'b/' + cat.url);
}
resultContent += 'Shop ' + cat.name + '';
}
}
}
if(displayLimit > 0) {
}
resultContent += 'See All Results ';
$(resultList).html(resultContent);
});
} else {
$(results_element).hide();
}
};
});