(function ($) {
if (!$) return;
var ua = navigator.userAgent,
isIE = ua.match(/msie/i),
isIE6 = isIE && ua.match(/msie 6\./i);
$.extend({
amazonjs:{
isCustomerReviewEnabled: false,
isTrackEventEnabled: false,
resource: {},
initTemplate:function () {
var r = this.resource;
var linkOpenTag = '';
var smallImageTemplate =
[
'{{if SmallImage}}',
'{{if $item.setInfoMargin(SmallImage.width+10)}}{{/if}}',
'',
linkOpenTag,
'

',
'',
'
',
'{{/if}}'
].join('');
var mediumImageTemplate =
[
'{{if MediumImage}}',
'{{if $item.setInfoMargin(MediumImage.width+10)}}{{/if}}',
'',
linkOpenTag,
'

',
'',
'
',
'{{/if}}'
].join('');
var largeImageTemplate =
[
'{{if LargeImage}}',
'{{if $item.setInfoMargin(LargeImage.width+10)}}{{/if}}',
'',
linkOpenTag,
'

',
'',
'
',
'{{/if}}'
].join('');
var imageTemplate =
[
'{{if _ShowSmallImage}}',smallImageTemplate,'{{/if}}',
'{{if _ShowMediumImage}}',mediumImageTemplate,'{{/if}}',
'{{if _ShowLargeImage}}',largeImageTemplate,'{{/if}}'
].join('');
var linkTemplate = linkOpenTag + '${Title}';
var priceContentTemplate =
[
'{{if $item.isSale()}}',
'' + r.ListPrice + '${ListPrice.FormattedPrice}
',
'{{if OfferSummary.LowestNewPrice}}' + r.Price + '${OfferSummary.LowestNewPrice.FormattedPrice}{{/if}}',
'' + r.PriceUpdatedat + '',
'{{else}}',
'' + r.Price + '${ListPrice.FormattedPrice}',
'' + r.PriceUpdatedat + '',
'{{/if}}'
].join('');
var priceTemplate = '{{if ListPrice}}
' + priceContentTemplate + '
{{/if}}';
var priceLiTemplate = '{{if ListPrice}}' + priceContentTemplate + '{{/if}}';
var reviewLinkTemplate = '';
if (this.isCustomerReviewEnabled) {
reviewLinkTemplate = '' + r.SeeCustomerReviews + '';
}
this.partial = {
smallImage:smallImageTemplate,
mediumImage:mediumImageTemplate,
largeImage:largeImageTemplate,
link:linkTemplate,
price:priceTemplate
};
var defaultTemplates = {
Small:[
'',
imageTemplate,
'{{if _ShowDefaultImage}}',smallImageTemplate,'{{/if}}',
'
',
'
',linkTemplate,'
',
'
',
'{{if Creator}}- ${Creator}
{{/if}}',
'{{if Manufacturer}}- ${Manufacturer}
{{/if}}',
priceLiTemplate,
'{{if PublicationDate}}- ' + r.PublicationDate + '${PublicationDate}
{{/if}}',
'{{if SalesRank}}- ' + r.SalesRank + '' + r.SalesRankValue + '
{{/if}}',
'
',
'
',
reviewLinkTemplate,
'',
'
'
],
Music:[
'',
imageTemplate,
'{{if _ShowDefaultImage}}',mediumImageTemplate,'{{/if}}',
'
',
'
',linkTemplate,'
',
'
',
'{{if Artist}}- ${Artist}
{{/if}}',
'{{if Creator}}- ${Creator}
{{/if}}',
'{{if Label}}- ${Label}
{{/if}}',
priceLiTemplate,
'{{if ReleaseDate}}- ' + r.ReleaseDate + '${ReleaseDate}
{{/if}}',
'{{if SalesRank}}- ' + r.SalesRank + '' + r.SalesRankValue + '
{{/if}}',
'{{if RunningTime}}- ' + r.RunningTime + '' + r.RunningTimeValue + '
{{/if}}',
'
',
'
',
reviewLinkTemplate,
'',
'
'
],
DVD:[
'',
imageTemplate,
'{{if _ShowDefaultImage}}',mediumImageTemplate,'{{/if}}',
'
',
'
',linkTemplate,'
',
'
',
'{{if Director}}- ${Director}
{{/if}}',
'{{if Actor}}- ${Actor}
{{/if}}',
'{{if Label}}- ${Label}
{{/if}}',
priceLiTemplate,
'{{if ReleaseDate}}- ' + r.ReleaseDate + '${ReleaseDate}
{{/if}}',
'{{if SalesRank}}- ' + r.SalesRank + '' + r.SalesRankValue + '
{{/if}}',
'{{if RunningTime}}- ' + r.RunningTime + '' + r.RunningTimeValue + '
{{/if}}',
'
',
'
',
reviewLinkTemplate,
'',
'
'
],
Book:[
'',
imageTemplate,
'{{if _ShowDefaultImage}}',mediumImageTemplate,'{{/if}}',
'
',
'
',linkTemplate,'
',
'
',
'- ' + r.BookAuthor + '${Author}
',
priceLiTemplate,
'- ' + r.BookPublicationDate + '${PublicationDate}
',
'{{if SalesRank}}- ' + r.SalesRank + '' + r.SalesRankValue + '
{{/if}}',
'- ${Binding}' + r.NumberOfPagesValue + '
',
'- ISBN-10${ISBN}
',
'- ISBN-13${EAN}
',
'- ' + r.BookPublisher + '${Publisher}
',
'
',
'
',
reviewLinkTemplate,
'',
'
'
],
eBooks:[
'',
imageTemplate,
'{{if _ShowDefaultImage}}',mediumImageTemplate,'{{/if}}',
'
',
'
',linkTemplate,'
',
'
',
'- ' + r.BookAuthor + '${Author}
',
priceLiTemplate,
'- ' + r.BookPublicationDate + '${PublicationDate}
',
'{{if SalesRank}}- ' + r.SalesRank + '' + r.SalesRankValue + '
{{/if}}',
'- ${Binding}' + r.NumberOfPagesValue + '
',
'- ' + r.BookPublisher + '${Publisher}
',
'
',
'
',
reviewLinkTemplate,
'',
'
'
]
};
this.template(defaultTemplates);
var me = this;
$.each(this.addTemplateCallbacks, function (i, callback) {
var t = callback.call(me, me.partial);
if (t) me.template(t);
});
},
addTemplateCallbacks:[],
addTemplate:function (fn) {
if (typeof(fn) == 'function') {
this.addTemplateCallbacks.push(fn);
}
},
template:function (templates) { // set up jQuery template
$.each(templates, function (name, tmpl) {
if (tmpl) {
$.template('amazonjs' + name + 'Tpl', (typeof tmpl === 'string') ? tmpl : tmpl.join(''));
}
});
},
render:function (items) {
var $items = [];
this.initTemplate();
$(".amazonjs_item").each(function () {
var classNames = $(this).attr('class').split(' '),
c = classNames[0].split('_'),
asin = c[1],
countryCode = c[2],
tmpl = (c.length > 3) ? c[3] : null,
item = find(asin, countryCode),
imgSize = $(this).attr('data-img-size')
;
if (item) {
item._ImageSize = imgSize || '';
var $item = $.amazonjs.tmpl(item, $.amazonjs.formatTmplName(tmpl));
$(this).replaceWith($item.hide());
var $review = $item.find('.amazonjs_review');
if (isIE6) {
$item.css('position', 'static');
$review.css({
'float':'right',
'position':'static',
'marginRight':'32px'
});
}
$review.click(function () {
tb_show(this.title, this.href);
this.blur();
return false;
});
if ($.amazonjs.isTrackEventEnabled) {
$item.find('.amazonjs_link').click(function() {
try {
var data = $(this).data();
var title = $(this).attr('title');
if (data.role == 'amazonjs_product') {
if ($.isFunction(ga)) {
ga('send', 'event', 'AmazonJS', 'Click', data.asin + ' ' + title);
} else if (_gaq) {
_gaq.push(['_trackEvent', 'AmazonJS', 'Click', data.asin + ' ' + title]);
}
}
} catch (e) {
}
});
}
$items.push($item);
} else {
// add official link when amazon product is not fetched by using AWS API
tmpl = 'Link' + countryCode.toUpperCase();
item = {
asins:asin,
fc1:'000000',
lc1:'0000FF',
bc1:'000000',
bg1:'FFFFFF',
IS2:1,
lt1:'_blank',
f:'ifr',
m:'amazon'
};
var $item = $.amazonjs.tmpl(item, $.amazonjs.formatTmplName(tmpl));
$(this).replaceWith($item);
}
});
function find(asin, countryCode) {
for (var i = 0, length = items.length; i < length; i++) {
if (items[i].ASIN == asin && items[i].CountryCode == countryCode) {
return items[i];
}
}
}
if (amazonjsVars.isFadeInEnabled) {
function fadeIn() {
if ($items.length > 0) {
var $item = $items.shift();
$item.fadeIn();
setTimeout(fadeIn, 100);
}
}
fadeIn();
} else {
$.each($items, function() {
this.show();
});
}
},
formatTmplName:function (key) {
return (key) ? 'amazonjs' + key + 'Tpl' : null;
},
formatNumber:function (val) {
val += '';
var x = val.split('.'),
x1 = x[0],
x2 = x.length > 1 ? '.' + x[1] : '',
rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
},
formatDateTime:function (timestamp) {
var dt = new Date(timestamp * 1000);
var Y = dt.getFullYear(),
m = dt.getMonth() + 1,
d = dt.getDate(),
H = dt.getHours(),
i = dt.getMinutes();
if (m < 10) m = '0' + m;
if (d < 10) d = '0' + d;
if (H < 10) H = '0' + H;
if (i < 10) i = '0' + i;
return Y + '/' + m + '/' + d + ' ' + H + ':' + i;
},
getTemplate:function (item) {
var defaultTmpl = this.formatTmplName('Small');
if (item && item.ProductGroup) {
var tmpl = this.formatTmplName(item.ProductGroup);
return $.template[tmpl] || defaultTmpl;
}
return defaultTmpl;
},
prepareData:function (item) {
item._ShowDefaultImage = _ShowSmallImage = item._ShowMediumImage = item._ShowLargeImage = false;
if (item._ImageSize == 'small') item._ShowSmallImage = true;
else if (item._ImageSize == 'medium') item._ShowMediumImage = true;
else if (item._ImageSize == 'large') item._ShowLargeImage = true;
else item._ShowDefaultImage = true;
item.PublicationDate = item.PublicationDate || item.ReleaseDate;
item.Manufacturer = item.Manufacturer || item.Label;
if (item.SalesRank) item.SalesRank = this.formatNumber(item.SalesRank);
if (item.PublicationDate) item.PublicationDate = item.PublicationDate.replace(/-/g, '/');
if (item.ReleaseDate) item.ReleaseDate = item.ReleaseDate.replace(/-/g, '/');
if (item.Artist && item.Creator) {
var Creator = [];
var a = $.isArray(item.Artist) ? item.Artist : [item.Artist];
var c = $.isArray(item.Creator) ? item.Creator : [item.Creator];
$.each(c, function (i, value) {
if ($.inArray(value, a) == -1) Creator.push(value);
});
item.Creator = (Creator.length) ? Creator : null;
}
item.UpdatedDate = this.formatDateTime(item.UpdatedAt);
return item;
},
tmpl:function (item, tmpl) {
item = this.prepareData(item);
// overwrite ListPrice
if (!item.ListPrice && item.OfferSummary) {
item.ListPrice = item.OfferSummary.LowestNewPrice;
}
return $.tmpl($.template[tmpl] || this.getTemplate(item), item, {
isSale:function () {
if (this.data) {
var lp = this.data.ListPrice, la = Number(lp.Amount),
os = this.data.OfferSummary || {}, sp = os.LowestNewPrice || {}, sa = Number(sp.Amount);
return (!isNaN(sa) && sa < la);
}
return false;
},
setInfoMargin: function(margin) {
return this.data._InfoMarginLeft = margin;
}
});
}
}
});
if (amazonjsVars) {
function render() {
if (amazonjsVars.isCustomerReviewEnabled) {
if (typeof tb_pathToImage === 'undefined') {
tb_pathToImage = amazonjsVars.thickboxUrl + '/loadingAnimation.gif';
}
if (typeof tb_closeImage === 'undefined') {
tb_closeImage = amazonjsVars.thickboxUrl + '/tb-close.png';
}
}
$.amazonjs.isCustomerReviewEnabled = amazonjsVars.isCustomerReviewEnabled;
$.amazonjs.isTrackEventEnabled = amazonjsVars.isTrackEventEnabled;
$.amazonjs.resource = amazonjsVars.resource;
$.amazonjs.template(amazonjsVars.regionTempalte);
$.amazonjs.render(amazonjsVars.items);
}
$(document).ready(function(){
if (amazonjsVars.isFadeInEnabled) {
setTimeout(function () { render(); }, 1000);
} else {
render();
}
});
}
})(jQuery);