/**
* JavaScript file jquery.amazon-product-search.js v0.1.1 last modified 11/09/20
*/
var yahoosuggestcallback;
(function(jQuery) {
var settings, self, content, width, requestUrl, keywords, amazonTags, control, slider, position, limit, amazonUl, amazonData, currentID, cookies, searchControl, deleteButton, autoCompleteResponse;
jQuery.fn.amazonProductSearch = function() {
self = this;
var args = jQuery.makeArray(arguments),
options = jQuery.isPlainObject(args[args.length - 1]) ? args.pop() : {};
requestUrl = args.pop();
keywords = jQuery.trim(args.join(' '));
settings = jQuery.extend({}, jQuery.fn.amazonProductSearch.defaults, options);
position = 0;
currentID = 1000;
amazonTags = [];
cookies = getCookie('amazon-tags');
cookies = cookies != '' ? jQuery.trim(cookies).split(',') : [];
cookies = unique( cookies.concat(settings.defaultKeywords || []) );
return this.each(function() {
jQuery(this)
.append(
content = jQuery('
', { id: 'amazon_content' }).css({ fontSize: settings.baseFontSize })
.append(
jQuery('', { id: 'amazon_top' })
.append( jQuery('', { id: 'amazon_loading', html: '読み込み中……' }) )
.append(
control = jQuery('', { id: 'amazon_search_form' })
.append(jQuery('', { id: 'amazon_label', htmlFor: 'amazon_keyword', html: '追加キーワード ' }))
.append(jQuery('', { id: 'amazon_keyword', name: 'amazon_keyword' }))
)
)
.append(
jQuery('', { id: 'amazon_tags_form' })
.append( amazonTags[amazonTags.length] = jQuery('', { 'class': 'amazon_tags' }) )
)
.append( jQuery('', { 'class': 'clear_float' }) )
.append(
jQuery('', { id: 'amazon_showcase' })
.append(
slider = jQuery('', { id: 'amazon_slider' })
.append( amazonUl = jQuery('') )
)
.append(
jQuery('', { id: 'amazon_slider_left', 'class': 'slider_button', html: '<' })
.button({ disabled: true })
.click(function() {
if (position > 0) {
position--;
amazonUl.animate({ left: position * -190 });
jQuery('#amazon_slider_right').button('enable');
}
if (position === 0) {
jQuery(this).button('disable');
}
})
)
.append(
jQuery('', { id: 'amazon_slider_right', 'class': 'slider_button', html: '>' })
.button()
.click(function() {
if (position < limit) {
position++;
amazonUl.animate({ left: position * -190 });
jQuery('#amazon_slider_left').button('enable');
}
if (position == limit) {
jQuery(this).button('disable');
}
})
)
)
);
if (settings.width) {
content.width(settings.width);
}
jQuery('#amazon_slider_left').css({ left: 0, textIndent: '-0.6em' });
jQuery('#amazon_slider_right').css({ left: (content.width() - 18), textIndent: '-0.5em' });
deleteButton = jQuery('', { id: 'delete_amazon_tag', html: 'タグ削除' });
if (cookies.length) {
jQuery.each(cookies, function(index, value) {
addAmazonTag(value);
});
}
requestAmazonInfo(keywords);
jQuery('#amazon_search_form').submit(function(event) {
event.preventDefault();
var value = jQuery('#amazon_keyword').val();
jQuery('#amazon_loading').html('読み込み中……').show();
addAmazonTag(value);
requestAmazonInfo(value);
jQuery('#amazon_keyword').autocomplete('close');
});
deleteButton.button({
icons: { primary: 'ui-icon-closethick' },
text: false
})
.click(function(event) {
event.preventDefault();
var element = jQuery(this).closest('label').prev('input'),
parent = element.parent('div'),
pIndex = parent.index();
if (element.length) {
var v = element.val();
cookies = jQuery.map(cookies, function(value, index) {
if (v == value) return null;
else return value;
});
setCookie('amazon-tags', cookies.join(','), getExpDate(30, 0, 0));
deleteButton.detach();
element.button('destroy')
.next('label').andSelf().remove();
if (!parent.children().length) {
parent.remove();
amazonTags = jQuery.map(amazonTags, function(element, index) {
if (index == pIndex) return null;
else return element;
});
}
jQuery('.amazon_tags').buttonset();
}
});
jQuery('#amazon_keyword').autocomplete({
delay: 100,
source: function(request, response) {
jQuery.getJSON(
'http://completion.amazon.co.jp/search/complete?callback=?',
{
'method': 'completion',
'search-alias': 'aps',
'mkt': 6,
'q': request.term
},
function(data, status) {
if (status == 'success' && data) {
response(data[1]);
}
}
);
},
minLength: 1,
select: function(event, ui) {
var label = ui.item.label;
jQuery('#amazon_loading').html('読み込み中……').show();
addAmazonTag(label);
requestAmazonInfo(label);
},
open: function() {
jQuery( this ).removeClass( 'ui-corner-all' ).addClass( 'ui-corner-top' );
},
close: function() {
jQuery( this ).removeClass( 'ui-corner-top' ).addClass( 'ui-corner-all' );
}
}).focus();
});
};
jQuery.fn.amazonProductSearch.defaults = {
defaultKeywords: ['ファッション', '腕時計', 'DVD', 'ゲーム'],
baseFontSize: '12px',
searchIndex: 'All',
itemPage: 1,
sort: ''
};
function requestAmazonInfo(query) {
var params = {
'search_index': settings.searchIndex,
'item_page': settings.itemPage,
'sort': settings.sort,
'keywords': query
};
jQuery.getJSON(
requestUrl,
params,
function(data, status) {
var result, index, length, item, items = [];
if (status == 'success' && data && data.result != 'failed') {
if (data.result.Item) {
result = data.result.Item;
for (index = 0, length = result.length; index < length; index++) {
item = result[index];
items[index] = {
itemName: item.ItemAttributes.Title,
itemCaption: item.EditorialReviews && item.EditorialReviews.EditorialReview && item.EditorialReviews.EditorialReview.Content ? item.EditorialReviews.EditorialReview.Content : '',
usedPrice: item.OfferSummary.LowestUsedPrice ? item.OfferSummary.LowestUsedPrice.Amount : '',
newPrice: item.OfferSummary.LowestNewPrice ? item.OfferSummary.LowestNewPrice.Amount : '',
collectiblePrice: item.OfferSummary.LowestCollectiblePrice ? item.OfferSummary.LowestCollectiblePrice.Amount : '',
itemCode: item.ASIN,
itemUrl: item.DetailPageURL,
imageURL: item.MediumImage ? item.MediumImage.URL : ''
};
}
amazonProductSearchComplete(items);
}
else {
jQuery('#amazon_loading').html('商品が見つかりませんでした').show();
}
}
}
);
}
function amazonProductSearchComplete(items) {
if (items && items.length > 0) {
jQuery('#amazon_loading').html(items.length + ' 件の商品がヒットしました');
jQuery('div#amazon_slider ul').children().remove();
amazonUl.css({ left: 0 });
jQuery('#amazon_slider_left').button('disable');
position = 0;
limit = items.length - Math.floor(slider.width() / 190);
if (limit <= 0) {
jQuery('#amazon_slider_right').button('disable');
}
else {
jQuery('#amazon_slider_right').button('enable');
}
var dialog = [], array = [], offsetLeft = 0, date = new Date(), stamp;
stamp = date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes();
for (var i = 0, l = items.length; i < l; i++) {
var amazon = items[i],
name = amazon.itemName,
caption = amazon.itemCaption,
usedPrice = amazon.usedPrice,
newPrice = amazon.newPrice,
collectiblePrice = amazon.collectiblePrice,
code = amazon.itemCode,
url = amazon.itemUrl,
image = amazon.imageURL,
array = [];
if (name.length > 26) name = name.slice(0, 26) + '...';
if (usedPrice) {
while (usedPrice.length > 0) {
array[array.length] = usedPrice.slice(-3);
usedPrice = usedPrice.slice(0, -3);
}
usedPrice = array.reverse().join(',');
}
if (newPrice) {
array.length = 0;
while (newPrice.length > 0) {
array[array.length] = newPrice.slice(-3);
newPrice = newPrice.slice(0, -3);
}
newPrice = array.reverse().join(',');
}
if (collectiblePrice) {
array.length = 0;
while (collectiblePrice.length > 0) {
array[array.length] = collectiblePrice.slice(-3);
collectiblePrice = collectiblePrice.slice(0, -3);
}
collectiblePrice = array.reverse().join(',');
}
dialog[i] = jQuery('', { 'class': 'amazon_caption' });
content.append(
dialog[i]
.append(
jQuery('')
.append( jQuery('', { href: url, html: amazon.itemName, target: '_blank' }) )
)
.append( jQuery('', { html: caption }) )
.append( jQuery('', { id: 'notes', html: '価格および発送可能時期は表示された日付/時刻の時点のものであり、変更される場合があります。商品の販売においては、購入の時点で [Amazon.co.jp または Javari.jp の適用ある方] に表示されている価格および発送可能時期の情報が適用されます。', css: { color: '#00F' } }) )
);
dialog[i]
.dialog({
autoOpen: false,
title: '商品詳細',
width: 500,
height: 300,
open: function(event, ui) {
jQuery(event.target).find('a').blur();
}
});
amazonUl.append( amazonList = jQuery('', { 'class': 'amazon_list' }) );
amazonList.css({ left: offsetLeft });
offsetLeft += 190;
if (image) {
amazonList
.append(
jQuery('', { href: url, title: name, target: '_blank' })
.append( jQuery('', { src: image, alt: name }) )
)
.append( jQuery('
') );
}
amazonList
.append(
jQuery('', { href: url, html: name, data: { index: i }, target: '_blank' })
.mouseover(function() {
//console.log(jQuery(this).data('index'));
})
)
.append(
amazonData = jQuery('', { 'class': 'amazon_data' })
.append(
jQuery('', { 'class': 'amazon_info', html: '商品詳細', data: { index: i } })
.click(function() {
dialog[jQuery(this).data('index')].dialog('open');
})
)
);
if (usedPrice) {
amazonData.append( jQuery('
') ).append( jQuery('', { html: '中古価格 ' }) ) .append( jQuery('', { 'class': 'amazon_price', html: usedPrice + '円' }) )
}
if (newPrice) {
amazonData.append( jQuery('
') ).append( jQuery('', { html: '新品価格 ' }) ) .append( jQuery('', { 'class': 'amazon_price', html: newPrice + '円' }) )
}
if (collectiblePrice) {
amazonData.append( jQuery('
') ).append( jQuery('', { html: 'コレクター価格 ' }) ) .append( jQuery('', { 'class': 'amazon_price', html: collectiblePrice + '円' }) )
}
amazonData.append( jQuery('', { id: 'timestamp', html: '(' + stamp + ' JST 時点)' }) );
amazonData.append( jQuery('', { 'class': 'clear_float' }) );
}
}
}
function addAmazonTag(value) {
if (value == '') return;
var radioID = 'amazon_' + currentID,
width = content.width(),
tIndex = -1,
input, label;
input = jQuery('', { id: radioID, name: 'amazon_tags', type: 'radio', value: value });
label = jQuery('', { htmlFor: radioID, html: value })
.click(function() {
jQuery('#amazon_loading').html('読み込み中……').show();
deleteButton.detach();
jQuery(this).children('span').append(deleteButton);
requestAmazonInfo(value);
});
deleteButton.detach();
jQuery.each(amazonTags, function(index, element) {
element.append(input).append(label);
jQuery('.amazon_tags').buttonset();
if (element.width() + 35 <= width) {
tIndex = index;
return false;
}
input.next().andSelf().detach();
});
if (tIndex == -1) {
jQuery('#amazon_tags_form').append(
amazonTags[amazonTags.length] = jQuery('', { 'class': 'amazon_tags' })
.append(input).append(label)
);
jQuery('.amazon_tags').buttonset();
}
cookies.push(value);
cookies = unique(cookies);
setCookie('amazon-tags', cookies.join(','), getExpDate(30, 0, 0));
currentID++;
return radioID;
}
function unique(array) {
var storage = {}, uniqueArray = [], i, value;
for (i = 0; i < array.length; i++) {
value = array[i];
if (!(value in storage)) {
storage[value] = true;
uniqueArray.push(value);
}
}
return uniqueArray;
}
/* クッキー管理用ユーティリティ */
function getExpDate(days, hours, minutes) {
var expDate = new Date();
if (typeof days == 'number' && typeof hours == 'number' && typeof minutes == 'number') {
expDate.setDate(expDate.getDate() + parseInt(days));
expDate.setHours(expDate.getHours() + parseInt(hours));
expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
return expDate.toGMTString();
}
}
function getCookieVal(offset) {
var endstr = document.cookie.indexOf(';', offset);
if (endstr == -1) {
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
}
function getCookie(name) {
var arg = name + '=';
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
return getCookieVal(j);
}
i = document.cookie.indexOf(' ', i) + 1;
if (i === 0) break;
}
return '';
}
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + '=' + escape(value) +
((expires) ? '; expires=' + expires : '') +
((path) ? '; path=' + path : '') +
((domain) ? '; domain=' + domain : '') +
((secure) ? '; secure=' + secure : '');
}
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + '=' +
((path) ? '; path=' + path : '') +
((domain) ? '; domain=' + domain : '') +
'; expires=Thu, 01-Jan-70 00:00:01 GMT';
}
}
})(jQuery);