if(!Date.prototype.toLocalDate)Date.prototype.toLocalDate=function(){function e(a){return a<10?"0"+a:a}return this.getFullYear()+"-"+e(this.getMonth()+1)+"-"+e(this.getDate())};
var taobaoquery={version_:"2015-05-17_1",sessions_:{},putSession:function(e,a){var b=this;b.sessions_[e]={timestamp:(new Date).getTime(),obj:a};setTimeout(function(){e in b.sessions_&&b.sessions_[e].timestamp+1E4<(new Date).getTime()&&delete b.sessions_[e]},1E4)},getSession:function(e){return e in this.sessions_?this.sessions_[e].obj:null},removeSession:function(e){delete this.sessions_[e]},query:function(e,a,b){var f=e.split(" "),h=f[0],k=f[1];f.length==2?this.selectShop(h,k,function(f){kernel.log(e+
" returns "+JSON.stringify(f));a(f)}):k.match(/\?$/)?(k=k.substr(0,k.length-1),f=JSON.parse(f[2]),this.queryShop(h,k,f,function(f){kernel.log(e+" returns "+JSON.stringify(f));a(f)},b)):this.selectShopField(h,k,f[2],f[3],function(f){kernel.log(e+" returns "+JSON.stringify(f));a(f)},b)},parseRecords:function(e,a){var b=a.split("\n"),f=b[0].split("\t");if(f.length!=3)return{prefix:null,records:[]};var h=f[0],k=f[1],g;try{g=JSON.parse(f[2])}catch(q){g={text:f[2]}}var x={},l;k.indexOf(e)<0?(x.prefix={timestamp:h,
datetime:k,data:g},l=1):(x.prefix=null,l=0);for(x.records=[];l<b.length;l++)if(f=b[l].split("\t"),f.length==3){h=f[0];k=f[1];try{g=JSON.parse(f[2])}catch(u){g={text:f[2]}}x.records.push({timestamp:h,datetime:k,data:g})}return x},diffDataObj:function(e,a,b,f){var h={};if(null==b)return e?h[f]=e[f]:a&&(h[f]=a[f]),h;var k={};if(e)for(n in e){if(!(f&&f!=n))if(a)if(typeof e[n]=="object"){var g=e[n];"length"in g||(g=[]);if(n in a){var q=a[n];"length"in q||(q=[]);g=kernel.diffArray(g,q,n in k?k[n]:null);
h[n]={num:g.num};if(b){if(g.added.length>0)h[n].added=g.added.length;if(g.removed.length>0)h[n].removed=g.removed.length}else{if(g.added.length>0)h[n].added=g.added;if(g.removed.length>0)h[n].removed=g.removed}}else h[n]=b?{num:g.length,added:g.length}:{num:g.length,added:g}}else h[n]=e[n]!=a[n]?[e[n],a[n]]:[e[n]];else h[n]=typeof e[n]=="object"?{num:e[n].length}:[e[n]]}else for(n in a)f&&f!=n||(h[n]=typeof a[n]=="object"?{num:a[n].length}:[a[n]]);return h},diffhome:function(e,a,b,f,h){var k=taobaoquery,
g={};k.readDateRecords(e,"shop"+a,function(a){try{var e;if(a.records.length>0){var l=[],u=[],w=[];for(r in a.records)l=l.concat(a.records[r].data.categories),u=u.concat(a.records[r].data.channels),w=w.concat(a.records[r].data.items);l=kernel.uniqueSort(l);u=kernel.uniqueSort(u);w=kernel.uniqueSort(w);e={categories:l,channels:u,items:w}}else e=null;var y;if(a.prefix.length>0){l=[];u=[];w=[];for(r in a.prefix)l=l.concat(a.prefix[r].data.categories),u=u.concat(a.prefix[r].data.channels),w=w.concat(a.prefix[r].data.items);
l=kernel.uniqueSort(l);u=kernel.uniqueSort(u);w=kernel.uniqueSort(w);y={categories:l,channels:u,items:w}}else y=null;g=k.diffDataObj(e,y,f,h)}catch(z){kernel.log("diffhome exception: "+z.toString())}b(g)})},diffcategory:function(e,a,b,f,h){var k=taobaoquery,g={};k.readDateRecords(e,"shop"+a+"/category",function(a){try{var e;if(a.records.length>0){var l=[];for(r in a.records){var u=a.records[r].data;for(c in u)u[c].title.length>0&&l.push(u[c].title)}e={categories:kernel.uniqueSort(l)}}else e=null;
var w;if(a.prefix.length>0){l=[];for(r in a.prefix)for(c in u=a.prefix[r].data,u)u[c].title.length>0&&l.push(u[c].title);w={categories:kernel.uniqueSort(l)}}else w=null;g=k.diffDataObj(e,w,f,h)}catch(y){kernel.log("diffcategory exception: "+y.toString())}b(g)})},diffseller:function(e,a,b,f,h){var k=taobaoquery,g={};k.readDateRecords(e,"shop"+a+"/seller",function(a){try{var e;if(a.records.length>0){var l=a.records[0].data;e={creditLevel:l.creditLevel,goodRatePercentage:l.goodRatePercentage,shopTitle:l.shopTitle,
fansCountText:l.fansCountText,evaluateInfo_shopping:kernel.selectArray(l.evaluateInfo,"title","\u53d1\u8d27\u901f\u5ea6","score"),evaluateInfo_service:kernel.selectArray(l.evaluateInfo,"title","\u670d\u52a1\u6001\u5ea6","score"),evaluateInfo_desc:kernel.selectArray(l.evaluateInfo,"title","\u63cf\u8ff0\u76f8\u7b26","score")}}else e=null;var u;a.prefix.length>0?(l=a.prefix[0].data,u={creditLevel:l.creditLevel,goodRatePercentage:l.goodRatePercentage,shopTitle:l.shopTitle,fansCountText:l.fansCountText,
evaluateInfo_shopping:kernel.selectArray(l.evaluateInfo,"title","\u53d1\u8d27\u901f\u5ea6","score"),evaluateInfo_service:kernel.selectArray(l.evaluateInfo,"title","\u670d\u52a1\u6001\u5ea6","score"),evaluateInfo_desc:kernel.selectArray(l.evaluateInfo,"title","\u63cf\u8ff0\u76f8\u7b26","score")}):u=null;g=k.diffDataObj(e,u,f,h)}catch(w){kernel.log("diffseller exception: "+w.toString())}b(g)})},parseShopModules:function(e){var a={};try{for(m in e){var b=e[m];if(b.channels)for(c in b.channels){var f=
b.channels[c],h=[];for(mm in f.modules){var k=f.modules[mm];"itemids"in k&&(h=h.concat(k.itemids));"channels"in k&&(h=h.concat(k.channels))}a[f.href]=f.href in a?a[f.href].concat(h):h}}var g=[];for(c in a)a[c]=kernel.uniqueSort(a[c]),g.push(c)}catch(q){kernel.log("parseShopModules exception: "+q.toString())}a.urls=kernel.uniqueSort(g);return a},diffshopmodule:function(e,a,b,f,h){var k=taobaoquery,g={};k.readDateRecords(e,"shop"+a+"/shopmodule",function(a){try{var e;e=a.records.length>0?k.parseShopModules(a.records[0].data):
null;var l;l=a.prefix.length>0?k.parseShopModules(a.prefix[0].data):null;if(null==f){var u;if(e)u=e;else if(l)u=l;else{b(g);return}var w=[];for(d in u)d!="urls"&&w.push(d);g.urls=w;b(g);return}if(e==null&&l==null)g={urls:{num:0}};else if(e==null)g={urls:{num:l.urls.length}};else if(l==null)g={urls:{num:e.urls.length}};else{w=k.diffDataObj(e,l,f,h);u=0;for(d in w)d!="urls"&&f&&("added"in w[d]||"removed"in w[d]?u++:delete w[d]);w.urls.changes=u;g=f?{urls:w.urls}:w}}catch(y){kernel.log("diffshopmodule exception: "+
y.toString())}b(g)})},parseItemModules:function(e){var a={};for(m in e){var b=e[m];if(b.length!=0){var f;f=b.title in a?a[b.title]:[];"channels"in b&&(f=f.concat(b.channels));"itemids"in b&&(f=f.concat(b.itemids));a[b.title]=f}}for(m in a)a[m]=kernel.uniqueSort(a[m]);return a},diffitemmodule:function(e,a,b,f,h){var k=taobaoquery,g={};k.readDateRecords(e,"shop"+a+"/itemmodule",function(a){try{var e;e=a.records.length>0?k.parseItemModules(a.records[0].data):null;var l;l=a.prefix.length?k.parseItemModules(a.prefix[0].data):
null;g=k.diffDataObj(e,l,f,h)}catch(u){kernel.log("diffitemmodule exception: "+u.toString())}b(g)})},diffnewon:function(e,a,b,f,h){var k=taobaoquery,g={};k.readDateRecords(e,"shop"+a+"/newon",function(a){try{var e;e=a.records.length>0?{items:a.records[a.records.length-1].data}:null;var l;l=a.prefix.length>0?{items:a.prefix[a.prefix.length-1].data}:null;if(e&&"items"in e&&!("length"in e.items))e.items=[];if(l&&"items"in l&&!("length"in l.items))l.items=[];if(null==f)g=k.diffDataObj(e,l,!1,h),g.items=
g.items.added;else if(g=k.diffDataObj(e,l,f,h),"items"in g&&"removed"in g.items&&delete g.items.removed,"items"in g&&"num"in g.items&&delete g.items.num,!("items"in g&&"added"in g.items))g.items.num=0}catch(u){kernel.log("diffnewon exception: "+u.toString())}b(g)})},difflist:function(e,a,b,f,h){var k=taobaoquery,g={};k.readDateRecords(e,"shop"+a+"/list",function(a){try{var e;if(a.records.length>0){var l=[];for(r in a.records)l=l.concat(a.records[r].data);e={items:kernel.uniqueSort(l)}}else e=null;
var u;if(a.prefix.length>0){l=[];for(r in a.prefix)l=l.concat(a.prefix[r].data);u={items:kernel.uniqueSort(l)}}else u=null;g=k.diffDataObj(e,u,f,h)}catch(w){kernel.log("difflist exception: "+w.toString())}b(g)})},readDateRecords:function(e,a,b){var f=this,h={prefix:[],records:[]};kernel.getLocalStore(e,a,function(k){try{if(k){var g=f.parseRecords(e,k);h.records=g.records;if(g.prefix){var q=g.prefix.datetime.substr(0,10);kernel.getLocalStore(q,a,function(a){try{if(a){var e=f.parseRecords(q,a);h.prefix=
e.records}b(h)}catch(g){kernel.log(g.toString()),b(h)}})}else b(h)}else b(h)}catch(x){kernel.log("readDateRecords exception: "+x.toString()),b(h)}})},parseItemInfo:function(e,a){var b={title:[],priceUnits:[],skuModel:[],picsPath:[],props:[]};try{for(o in e){var f=e[o].data;b.title.push(f.title);for(p in f.priceUnits){var h=f.priceUnits[p],k=h.name;k?k+=":":k="";"rangePrice"in h?b.priceUnits.push(k+h.rangePrice):b.priceUnits.push(k+h.price)}if("skuModel"in f&&"skuProps"in f.skuModel)for(s in f.skuModel.skuProps){var g=
f.skuModel.skuProps[s];for(v in g.values)b.skuModel.push(g.propName+":"+g.values[v].name)}for(p in f.picsPath)b.picsPath.push(f.picsPath[p]);for(p in f.props){var q=f.props[p];b.props.push(q.name+":"+q.value)}}b.title=kernel.uniqueSort(b.title);b.priceUnits=kernel.uniqueSort(b.priceUnits);b.skuModel=kernel.uniqueSort(b.skuModel);b.picsPath=kernel.uniqueSort(b.picsPath);b.props=kernel.uniqueSort(b.props);if(typeof a!="undefined"&&"skuModel"in f){if("ppathIdmap"in f.skuModel){if(!("ppathIdmap"in a))a.ppathIdmap=
{};for(q in f.skuModel.ppathIdmap)a.ppathIdmap[f.skuModel.ppathIdmap[q]]=q}if("skuProps"in f.skuModel){if(!("skuProps"in a))a.skuProps=[];a.skuProps=a.skuProps.concat(f.skuModel.skuProps)}}return b}catch(x){return kernel.log("parseItemInfo exception:"+x),null}},diffitem:function(e,a,b,f,h,k,g){var q=taobaoquery,x={},a="shop"+a+"/items/"+taobao.numericHash(parseInt(b))+"/"+b;q.readDateRecords(e,a,function(a){try{var e;e=a.records.length>0?q.parseItemInfo(a.records,g):null;var b;b=a.prefix.length>0?
q.parseItemInfo(a.prefix,g):null;x=q.diffDataObj(e,b,h,k)}catch(y){}f(x)})},diffdescription:function(e,a,b,f,h,k){var g=taobaoquery,q={},a="shop"+a+"/items/"+taobao.numericHash(parseInt(b))+"/"+b+"/description";g.readDateRecords(e,a,function(a){try{q=g.diffDataObj(a.records.length>0?a.records[a.records.length-1].data:null,a.prefix.length>0?a.prefix[0].data:null,h,k)}catch(e){}f(q)})},diffcombo:function(e,a,b,f,h,k){var g=taobaoquery,q={},a="shop"+a+"/items/"+taobao.numericHash(parseInt(b))+"/"+b+
"/combo";g.readDateRecords(e,a,function(a){try{var e;if(a.records.length>0){var b=[];for(r in a.records)for(d in a.records[r].data)b.push(a.records[r].data[d].name+":"+a.records[r].data[d].itemids.join(","));e={combos:kernel.uniqueSort(b)}}else e=null;var w;if(a.prefix.length>0){b=[];for(r in a.prefix)for(d in a.prefix[r].data)b.push(a.prefix[r].data[d].name+":"+a.prefix[r].data[d].itemids.join(","));e={combos:kernel.uniqueSort(b)}}else w=null;q=g.diffDataObj(e,w,h,k)}catch(y){}f(q)})},parseSkuName:function(e,
a,b){for(pp in e){var f=e[pp];if(f.propId==a)for(v in f.values)if(b==f.values[v].valueId)return f.propName+"#"+f.values[v].name}return null},parseSaleInfo:function(e,a,b){var f={};f.totalSoldQuantity=e.itemInfoModel.totalSoldQuantity;f.quantity=e.itemInfoModel.quantity;f.skus=[];if("skuModel"in e&&"skus"in e.skuModel&&"skuProps"in b&&"ppathIdmap"in b)for(s in e.skuModel.skus){var h=s;if(h in b.ppathIdmap){var k=b.ppathIdmap[h].split(";"),g=[];for(sm in k){var q=k[sm].split(":");(q=this.parseSkuName(b.skuProps,
q[0],q[1]))&&g.push(q)}h=e.skuModel.skus[h];a?f.skus.push(g.join(",")+":"+h.price):f.skus.push(g.join(",")+":"+h.quantity+","+h.price)}}return f},diffsale:function(e,a,b,f,h,k,g){var q=taobaoquery,x={};if(q.isempty(g))g.empty=!1,q.diffitem(e,a,b,function(){q.diffsale(e,a,b,f,h,k,g)},!0,null,g);else{var l="shop"+a+"/items/"+taobao.numericHash(parseInt(b))+"/"+b+"/sale";q.readDateRecords(e,l,function(a){try{var e;e=a.records.length>0?q.parseSaleInfo(a.records[a.records.length-1].data,h,g):null;var b;
b=a.prefix.length>0?q.parseSaleInfo(a.prefix[0].data,h,g):null;x=q.diffDataObj(e,b,h,k)}catch(l){}f(x)})}},parseRateInfo:function(e,a){var b={};b.rateCounts=e.rateCounts;if("tagList"in e){var f=[];for(t in e.tagList)a?f.push(e.tagList[t].title):f.push(e.tagList[t].title+"("+e.tagList[t].count+")");b.tagList=f}return b},diffrate:function(e,a,b,f,h,k){var g=taobaoquery,q={},a="shop"+a+"/items/"+taobao.numericHash(parseInt(b))+"/"+b+"/rate";g.readDateRecords(e,a,function(a){try{var e;e=a.records.length>
0?g.parseRateInfo(a.records[a.records.length-1].data,h):null;var b;b=a.prefix.length>0?g.parseRateInfo(a.prefix[0].data,h):null;q=g.diffDataObj(e,b,h,k)}catch(w){}f(q)})},diffrelated:function(e,a,b,f,h,k){var g=taobaoquery,q={},a="shop"+a+"/items/"+taobao.numericHash(parseInt(b))+"/"+b+"/related";g.readDateRecords(e,a,function(a){try{q=g.diffDataObj(a.records.length>0?a.records[a.records.length-1].data:null,a.prefix.length>0?a.prefix[0].data:null,h,k),h&&q.related.num==0&&delete q.related}catch(e){}f(q)})},
parseDealInfo:function(e,a,b){var f=[];for(i=0;i<a.length;i++)a[i][4].indexOf(e)==0&&f.push(a[i].join("\t"));return b?{deals:{added:f.length}}:{deals:{num:f.length,added:f}}},diffdeal:function(e,a,b,f,h){var k=taobaoquery,a="shop"+a+"/items/"+taobao.numericHash(parseInt(b))+"/"+b+"/deal",b=new Date;b.setTime(Date.parse(e)+864E5);b=b.toLocalDate();k.readDateRecords(b,a,function(a){try{var b,x=[];if(a.prefix.length>0)for(i=0;i<a.prefix.length;i++)for(j=0;j<a.prefix[i].data.length;j++)x.push(a.prefix[i].data[j]);
if(a.records.length>0)for(i=0;i<a.records.length;i++)for(j=0;j<a.records[i].data.length;j++)x.push(a.records[i].data[j]);b=k.parseDealInfo(e,x,h);f(b)}catch(l){f({})}})},selectShopItemField:function(e,a,b,f,h){var k=this,g={},a="shop"+a+"/items/"+taobao.numericHash(parseInt(b))+"/"+b;kernel.getLocalStore(e,a,function(a){try{if(a){var b=k.parseRecords(e,a),l;l=b.records.length>0?b.records[b.records.length-1].data:b.prefix.data;switch(f){case "info":g.price=l.priceUnits[0].price,g.title=l.title,g.pic=
l.picsPath[0]}}}catch(u){}h(g)})},isempty:function(e){for(s in e)return!1;return!0},selectShop:function(e,a,b){var f=this,h={},k={},g,q=null;if(a.indexOf("_")>0){var x=a.split("_"),a=x[0],q=x[1];if(x.length==2)g="item,combo,sale,rate,related,deal".split(",");else{f.selectShopItemField(e,a,q,x[2],b);return}}else g="home,category,seller,shopmodule,itemmodule,newon,list".split(",");var l=0,u=function(w){h[g[l]]=w;l++;if(l>=g.length)b(h);else{w=f["diff"+g[l]];try{q?w(e,a,q,u,!0,null,k):w(e,a,u,!0,null,
k)}catch(x){kernel.log("selectShop exception:"+x),b(h)}}},x=f["diff"+g[0]];try{q?x(e,a,q,u,!0,null,k):x(e,a,u,!0,null,k)}catch(w){kernel.log("selectShop exception: "+w),b(h)}},selectShopField:function(e,a,b,f,h){var k=null;a.indexOf("_")>0&&(k=a.split("_"),a=k[0],k=k[1]);var g=!1,q=f.indexOf("_");q>0&&(f=f.substr(0,q),g=null);q=this["diff"+b];if(typeof q=="undefined")h({});else try{var x={};k?q(e,a,k,h,g,f,x):q(e,a,h,g,f)}catch(l){kernel.log("selectShopField "+b+" exception: "+l),h({})}},queryShop:function(e,
a,b,f,h){switch(b.action){case "dqd":this.dqd(e,a,b.itemids,b.v,f);break;case "dst":this.dst(e,a,b.itemids,f);break;case "dmr":this.dmr(e,a,b.itemids,b.recommended,b.end,f,h);break;case "diu":this.diu(e,a,b.itemids,b.field,f);break;case "ldq":this.ldq(a,f);break;default:f({ret:-1})}},readItemQuantity:function(e,a,b,f){var h=this,a="shop"+a+"/items/"+taobao.numericHash(parseInt(b))+"/"+b+"/sale";kernel.getLocalStore(e,a,function(a){try{if(a){var b=h.parseRecords(e,a);if(!b.prefix&&b.records.length<
0)f(-1);else{var q=a=null;if(b.prefix)q=a=b.prefix.data.itemInfoModel.quantity;for(r in b.records){var x=b.records[r].data.itemInfoModel.quantity;if(!a||x<a)a=x;if(!q||x>q)q=x}f(q-a,{minq:a,maxq:q})}}else f(-1)}catch(l){f(null)}})},dqd:function(e,a,b,f,h){var k=this;if(b.length>50)h({ret:-1});else{var g=[];taobao.iterateList(g,b,function(b,g,h,u){var w=g[h];k.readItemQuantity(e,a,w,function(a,e){null==a?u(h,!1):(a<0||a>=f&&b.push([w,e.minq,e.maxq]),u(h,!0))})},function(a){a?h({ret:0,results:g}):h({ret:-1})})}},
readItemShelve:function(e,a,b,f){var h=this,a="shop"+a+"/items/"+taobao.numericHash(parseInt(b))+"/"+b+"/related";kernel.getLocalStore(e,a,function(a){try{if(a){var b=h.parseRecords(e,a);if(!b.prefix&&b.records.length<0)f({});else{var q;q=b.records.length>0?b.records[b.records.length-1].data:b.prefix.data;"shelve"in q&&q.shelve?f(q.shelve):f({})}}else f({})}catch(x){f(null)}})},dst:function(e,a,b,f){var h=this;if(b.length>50)f({ret:-1});else{var k=[];taobao.iterateList(k,b,function(b,f,k,l){var u=
f[k];h.readItemShelve(e,a,u,function(a){null==a?l(k,!1):("starts"in a&&"ends"in a&&b.push([u,a.starts,a.ends]),l(k,!0))})},function(a){a?f({ret:0,shelves:k}):f({ret:-1})})}},readItemRelated:function(e,a,b,f,h){var k=this,a="shop"+a+"/items/"+taobao.numericHash(parseInt(b))+"/"+b+"/related";kernel.getLocalStore(e,a,function(a){try{if(a){var b=k.parseRecords(e,a);if(!b.prefix&&b.records.length<0)h([]);else{var x;x=b.records.length>0?b.records[b.records.length-1].data:b.prefix.data;f?h(kernel.uniqueSort(x.recommends)):
h(kernel.uniqueSort(x.related))}}else h([])}catch(l){h(null)}})},dmr:function(e,a,b,f,h,k,g){var q=this;if(b.length>50)k({ret:-1});else{var x=g+" "+e+" "+a+" dmr";taobao.iterateList([],b,function(b,g,h,k){q.readItemRelated(e,a,g[h],f,function(a){if(null==a)k(h,!1);else{if(a.length!=0){var b=q.getSession(x),b=b?b.concat(a):a;q.putSession(x,b)}k(h,!0)}})},function(a){if(a)if(h){var b=q.getSession(x),a={},e,f;for(f in b)e=b[f],e in a?a[e]+=1:a[e]=1;q.removeSession(x);f=[];for(e in a)f.push({id:e,n:a[e]});
f=f.sort(function(a,b){return b.n-a.n});k({ret:0,relatedItems:f.splice(0,20)})}else k({ret:0});else k({ret:-1})})}},diu:function(e,a,b,f,h){var k=this;if(b.length>50)h({ret:-1});else{var g=[];taobao.iterateList(g,b,function(b,g,h,u){var w=g[h];k.diffitem(e,a,w,function(a){f in a&&("added"in a[f]||"removed"in a[f])&&b.push(w);u(h,!0)},!0,f)},function(){h({ret:0,itemids:g})})}},ldq:function(e,a){var b={};if(e in taobao.itemDealAccessTime_){var f=taobao.itemDealAccessTime_[e];for(ia in f)b[ia]=f[ia]}else b=
{};a({ret:0,accesstimes:b})},reload:function(){try{var e=document.getElementById("taobaoquery");document.body.removeChild(e);e=document.createElement("script");e.id="taobaoquery";e.src="taobaoquery.js?_="+(new Date).getTime();document.body.appendChild(e)}catch(a){}}};window.taobaoquery=taobaoquery;
