!function(){var t=navigator.platform.toUpperCase(),e="//graph.baidu.com/upload",a=-1!==t.indexOf("MAC"),n=$("#kw"),r=$("#form").parent(),o="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/soutu/css/soutu.css",i=10485760,s=102400,u={1:"抱歉，您上传的文件不是图片格式",2:"图片上传失败"},l={getEnv:function(){if(bds&&bds.comm){if(bds.comm.ishome&&bds.comm.newindex)return"newindex";if(bds.comm.ishome)return"index";if(/^\/imgsearch/.test(location.pathname))return"imgresult"}return"result"},supportVoice:function(){return window.URL=window.URL||window.webkitURL,navigator.getUserMedia=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia,window.AudioContext=window.AudioContext||window.webkitAudioContext,navigator.getUserMedia&&window.URL&&window.AudioContext&&window.Worker&&swfobject.hasFlashPlayerVersion("11.1.0")?!0:!1
},parseQuery:function(){for(var t=window.location.search.substr(1),e={},a=t.substring(t.indexOf("?")+1,t.length).split("&"),n=0;n<a.length;n++){var r=a[n],o=r.split("=");o[0]&&(e[o[0]]=decodeURIComponent(o[1]))}return e},getQuery:function(t){var e=new RegExp("(^|&)"+t+"=([^&]*)(&|$)","i"),a=window.location.search.substr(1).match(e);return null!=a?a[2]:null},isURL:function(t){return/^(((http[s]?:\/\/)|(ftp:\/\/))?([\w-]+\.)+(com|edu|gov|int|mil|net|org|biz|info|pro|name|museum|coop|aero|xxx|idv|al|dz|af|ar|ae|aw|om|az|eg|et|ie|ee|ad|ao|ai|ag|at|au|mo|bb|pg|bs|pk|py|ps|bh|pa|br|by|bm|bg|mp|bj|be|is|pr|ba|pl|bo|bz|bw|bt|bf|bi|bv|kp|gq|dk|de|tl|tp|tg|dm|do|ru|ec|er|fr|fo|pf|gf|tf|va|ph|fj|fi|cv|fk|gm|cg|cd|co|cr|gg|gd|gl|ge|cu|gp|gu|gy|kz|ht|kr|nl|an|hm|hn|ki|dj|kg|gn|gw|ca|gh|ga|kh|cz|zw|cm|qa|ky|km|ci|kw|cc|hr|ke|ck|lv|ls|la|lb|lt|lr|ly|li|re|lu|rw|ro|mg|im|mv|mt|mw|my|ml|mk|mh|mq|yt|mu|mr|us|um|as|vi|mn|ms|bd|pe|fm|mm|md|ma|mc|mz|mx|nr|np|ni|ne|ng|nu|no|nf|na|za|zq|aq|gs|eu|pw|pn|pt|jp|se|ch|sv|ws|yu|sl|sn|cy|sc|sa|cx|st|sh|kn|lc|sm|pm|vc|lk|sk|si|sj|sz|sd|sr|sb|so|tj|tw|th|tz|to|tc|tt|tn|tv|tr|tm|tk|wf|vu|gt|ve|bn|ug|ua|uy|uz|es|eh|gr|hk|sg|nc|nz|hu|sy|jm|am|ac|ye|iq|ir|il|it|in|id|uk|vg|io|jo|vn|zm|je|td|gi|cl|cf|cn)(:\d+)?(\/[^\s]*)?)$/.test(t)
},blobtoBase64:function(t,e){var a=new FileReader;a.onload=function(){e(this.result)},a.onerror=function(){e()},a.readAsDataURL(t)},compressImg:function(t){function e(t,e){var a=new FileReader;a.onload=function(){var t=this.result.split("base64,")[1];e(t,this.result)},a.onerror=function(){e()},a.readAsDataURL(t)}function a(t,e,a){e=e||"",a=a||512;for(var n=atob(t),r=[],o=n.length,i=0;o>i;i+=a){for(var s=n.slice(i,i+a),u=new Array(s.length),l=0;l<s.length;l++)u[l]=s.charCodeAt(l);var c=new Uint8Array(u);
r.push(c)}return new Blob(r,{type:e})}function n(t){var e=t.width,n=t.height;if(e>800||n>800){var r=Math.max(e/800,n/800);e/=r,n/=r}var o=document.createElement("canvas");o.width=e,o.height=n;var i=o.getContext("2d");i.fillRect(0,0,o.width,o.height),i.drawImage(t,0,0,e,n);var s=o.toDataURL("image/jpeg",1);return a(s.replace(/^.*?,/,""),"image/jpeg")}var r=new $.Deferred,o=t.size;return e(t,function(e,a){var i=new Image;i.onload=function(){return s>o&&i.width<800&&i.height<800?void r.resolve(t):(t=n(i),void r.resolve(t))
},i.src=a}),r.promise()},sendLog:function(t){var e=location.href.match(/sign=(\w{32})\b/),a=e&&e[1]||"";t.sign=a,t.fm="inlo",t.rsv_imageclick&&(t.rsv_imageclick=l.getEnv()+"_"+t.rsv_imageclick),window.soutu_mixsearch&&(t.rsv_imagemix=1),window.ns_c(t)}},c={graphIconHtml:function(){return'<span class="soutu-btn"></span>'},wrapHtml:function(){return'<div class="soutu-layer"><div class="soutu-url"><span class="soutu-url-wrap"><input id="soutu-url-kw" class="soutu-url-kw" maxlength="255" autocomplete="off" placeholder="在此处粘贴图片网址"/></span><span class="soutu-url-btn"><i class="soutu-icon soutu-url-btn-icon"></i></span><span class="soutu-url-error">请输入正确的图片网址</span></div><div class="soutu-state-normal"><div class="soutu-drop"><span class="soutu-drop-tip">拖拽图片到这里</span><i class="soutu-icon soutu-drop-icon"></i></div><div class="upload-wrap"><input type="file" class="upload-pic" value="上传图片"/><i class="soutu-icon upload-icon"></i><span class="upload-text">本地上传图片</span></div></div><a class="soutu-icon soutu-close" href="javascript:void(0);"></a></div>'
},waitingHtml:function(){return'<div class="soutu-state-waiting soutu-waiting"><div class="ball"><div class="b"></div><div class="b"></div><div class="b"></div></div><span>正在加载图片</span></div>'},errorHtml:function(t){return'<div class="soutu-state-error soutu-error"><i class="soutu-icon soutu-error-icon"></i><p class="soutu-error-main">'+t+'，请<a href="#" class="soutu-error-upload">重新上传</a></p><p class="soutu-error-tip">仅支持10M以下JPG,JPEG,PNG,BMP,GIF格式图片</p></div>'},newTabTipHtml:function(t){return['<div class="soutu-state-newtabtip soutu-newtab">','<div class="soutu-newtab-cont">','<div class="soutu-newtab-img" style="background-image:url('+t.imgUrl+')">',"</div>",'<div class="soutu-newtab-text">',"<p>"+t.text+"</p>","<span></span>","</div>","</div>",'<a class="soutu-newtab-link" href="'+t.url+'" target="_blank">查看搜索结果</a>',"</div>"].join("")
}},d={init:function(){if("imgresult"===l.getEnv()){var t=$("#form"),e=t.find(".soutu-input-image");if(e.length){var a=!0,r=l.parseQuery(),o=t.find("input[type=hidden]"),i=o.clone();t.attr("action","/imgsearch"),o.remove(),delete r.wd,t.append('<input type="hidden" name="sign" value="'+r.sign+'">');var s=function(){e.remove(),e.off("click.soutu"),e.removeClass("soutu-input-image-active"),n.off(".soutu"),n.attr("placeholder","").removeClass("soutu-input"),a=!1,t.find("input[type=hidden]").remove(),t.append(i),t.attr("action","/s")
};e.on("click.soutu",function(){s(),l.sendLog({rsv_imageclick:"del_thumb_by_click"})}),n.on("keydown",function(t){var a=n.val();return 8!==t.which||a.length?void(a.length<=1&&e.hasClass("soutu-input-image-active")&&e.removeClass("soutu-input-image-active")):void(e.hasClass("soutu-input-image-active")?(s(),l.sendLog({rsv_imageclick:"del_thumb_by_return"})):e.addClass("soutu-input-image-active"))}),n.on("focus.soutu",function(){e.show()}),n.on("click",function(){l.sendLog(a?{rsv_imageclick:"click_input_thumb"}:{rsv_imageclick:"click_input"})
}),n.on("blur.soutu",function(){e.removeClass("soutu-input-image-active")}),$("#su").on("click.soutu",function(){var t=n.val();return a&&!t.length?(l.sendLog({rsv_imageclick:"search_only_thumb"}),location.href="/imgsearch?"+$.param({sign:r.sign,wd:r.sign.substr(5,16)}),!1):void 0}),t.on("submit",function(){l.sendLog(a?{rsv_imageclick:"search_image_and_text"}:{rsv_imageclick:"search_only_text"})})}}}},p={init:function(){var t=this;t.canDrop="ondrop"in document.body;var e=$('<link rel="stylesheet" href="'+o+'" type="text/css" data-for="result"/>');
"newindex"===l.getEnv()?e.insertBefore(r.parent()):e.appendTo("head"),t.$graphIcon=$(c.graphIconHtml()).prependTo(n.parent()),r.addClass(a?"soutu-env-mac":"soutu-env-nomac").addClass("soutu-env-"+l.getEnv()),t.state="init",d.init(),t.listenIcon()},listenIcon:function(){var t=this;t.$graphIcon.on("click",function(e){e.stopPropagation(),e.preventDefault(),t.show(),t.log({rsv_imageclick:"camerabtn"})}),$(document).on("dragstart.soutu",function(t){var e,a=t.originalEvent.dataTransfer,n=t.target||t.srcElement,r=n.nodeName.toLowerCase();
if("img"===r){e=n.src;try{a.setData("text/plain",e)}catch(o){a.setData("Text",e)}}else if("a"===r){var i=$(n).children("img");if(i.length){e=i[0].src;try{a.setData("text/plain",e)}catch(o){a.setData("Text",e)}}}}),n.on("drop.soutu",function(e){e.originalEvent.dataTransfer&&e.originalEvent.dataTransfer.files&&e.originalEvent.dataTransfer.files.length&&(e.stopPropagation(),e.preventDefault(),t.$graphIcon.trigger("click"),t.handleDrop(e),t.log({rsv_imageclick:"iptdrop"}))}).on("dragover.soutu",function(){var t=window.bds&&bds.se&&bds.se.upn&&bds.se.upn.cookieset||[],e=t[0]&&1===t[0].v;
return!e}),$(window).one("index_off",function(){r.removeClass("soutu-env-newindex"),r.removeClass("soutu-no-skin")})},show:function(){var t=this;t.$layer||t.addLayer();var e=t.$layer;t.setState("normal"),e.show(),t.$graphIcon.hide(),1!==window.pageState||r.hasClass("soutu-env-result")||r.addClass("soutu-env-result"),"newindex"===l.getEnv()&&(r.addClass("soutu-env-newindex"),window.s_session&&!!s_session.userSkinName!=!1?e.removeClass("soutu-no-skin"):e.addClass("soutu-no-skin"))},close:function(){var t=this;
t.$layer.hide(),t.$graphIcon.show(),t.setState("normal"),t.$urlErrorTip.hide(),t.$urlInput.val("")},addLayer:function(){var t=this,e=t.$layer=$(c.wrapHtml()).prependTo($("#form"));t.$urlInput=e.find("#soutu-url-kw").on("focus",function(){e.find(".soutu-url-layer").addClass("focus")}).on("blur",function(){e.find(".soutu-url-layer").removeClass("focus")}),"newindex"===l.getEnv()&&$(".s-lite").not(".hide-icon").length&&e.find(".soutu-url-btn").css("width","103px"),t.$dropArea=e.find(".soutu-drop"),t.$urlErrorTip=e.find(".soutu-url-error"),t.$urlSearchBtn=e.find(".soutu-url-btn"),t.panelHidden=!1,t.listenLayer()
},handleDrop:function(t){var e=this;e.$dropArea.removeClass("drag-over");var a;if(t.originalEvent.dataTransfer&&(a=t.originalEvent.dataTransfer.files),a&&0!==a.length){var n=a[0];e.uptype="drag",e.upload(n,"PC_UPLOAD_SEARCH_MOVE")}else{var r;try{r=t.originalEvent.dataTransfer.getData("text/plain")||t.originalEvent.dataTransfer.getData("text/uri-list")}catch(o){r=t.originalEvent.dataTransfer.getData("Text")||t.originalEvent.dataTransfer.getData("URL")}r?e.handleURL(r):e.setState("error")}},listenLayer:function(){var t=this,e=t.$layer;
t.$urlInput.on("focus",function(){e.find(".soutu-url-panel").addClass("focus")}).on("blur",function(){e.find(".soutu-url-panel").removeClass("focus")}).on("keydown",function(e){var a=e.keyCode;return"none"!==t.$urlErrorTip.css("display")&&t.$urlErrorTip.hide(),13===a?(t.handleURL(this.value),e.stopPropagation(),e.preventDefault(),!1):void 0}),t.$urlSearchBtn.on("click",function(){var e=t.$urlInput.val();t.handleURL(e)}),t.$layer.find(".upload-pic").on("change",function(){var e=this.files[0];e&&(t.uptype="upload",t.upload(e,"PC_UPLOAD_SEARCH_FILE"))
}),t.$layer.find(".upload-pic").on("click",function(){t.log({rsv_imageclick:"uploadbtn"})}),t.$dropArea.on("dragover",function(e){t.$dropArea.addClass("drag-over"),e.stopPropagation(),e.preventDefault()}).on("dragleave",function(e){t.$dropArea.removeClass("drag-over"),e.stopPropagation(),e.preventDefault()}).on("drop",function(e){e.stopPropagation(),e.preventDefault(),t.handleDrop(e)}),t.$layer.find(".soutu-close").on("click",function(){t.close(!0),t.log({rsv_imageclick:"close"})}),t.$layer.on("click",".soutu-error-upload",function(e){e.stopPropagation(),e.preventDefault(),t.setState("normal")
}),$(document).on("click",function(e){var a=e.target,n=!0;do if(a=a.parentNode,a===t.$layer[0]||a===t.$graphIcon[0]){n=!1;break}while(a.parentNode);n&&t.close()})},setNormal:function(){this.$layer.find(".soutu-state-normal").show();var t=this.$layer.find(".upload-pic");t.val("")},setWaiting:function(){var t=this;t.$layer.append(c.waitingHtml());var e=[[0,40],[20,20],[40,0]],a=["rgb(55,137,250)","rgb(99,99,99)","rgb(235,67,70)"];t.$layer.find(".b").each(function(t,n){var r=0;$(n).css({"background-color":a[t]}),function o(){$(n).animate({left:e[t][r%2]},{duration:800,easing:"swing",progress:function(e,o){o>=.5&&$(n).css({"background-color":a[(r+t)%3]})
},complete:function(){o()}}),r++}()})},setError:function(t){var e=t.msg||u[2];this.$layer.append(c.errorHtml(e))},setNewTabTip:function(t){var e=this.$layer.find(".soutu-state-newtabtip");e.length&&e.remove(),e=$(c.newTabTipHtml(t)),this.$layer.append(e),l.sendLog({rsv_imageclick:"tipForNewTab"})},setState:function(t,e){var a=this;a.state=t,a.$layer.find(".soutu-state-normal").hide(),a.$layer.find(".soutu-state-error").remove(),a.$layer.find(".soutu-state-waiting").remove(),a.$layer.find(".soutu-state-newtabtip").remove(),a.$urlErrorTip.hide(),t=t.charAt(0).toUpperCase()+t.substring(1),a["set"+t].apply(a,[e||{}])
},handleURL:function(t){l.isURL(t)?(this.$urlInput.val(t),this.setState("normal"),this.$urlErrorTip.hide(),this.uploadUrl(t),this.log({rsv_imageclick:"searchurl-success"})):(this.setState("normal"),this.$urlErrorTip.show(),this.log({rsv_imageclick:"searchurl-error"}))},openResutlPage:function(t){var e=this,a=$("#form").attr("target");if("_blank"===a){var n="图片上传完毕，根据您的搜索习惯，<br/>将打开新窗口展示搜索结果",r=window.player;r&&r.config&&r.config.play.on&&(n="您正在听音乐，为了不打扰您继续听音乐，<br/>将打开新窗口查看搜索结果"),$.isString(e.uploadObj)?e.setState("newTabTip",{text:n,imgUrl:e.uploadObj,url:t}):l.blobtoBase64(e.uploadObj,function(a){e.setState("newTabTip",{text:n,imgUrl:a,url:t})
})}else location.href=t,e.close()},validate:function(t){var e=["jpg","jpeg","png","bmp","gif","webp"],a=t.name.split("."),n=a.pop().toLowerCase();return t.type&&n&&-1!==$.inArray(n,e)?t.size>i?2:0:1},doAjax:function(t){var a=this;"waiting"!==this.state&&this.setState("waiting"),$.ajax({url:e,type:"POST",data:t,processData:!1,contentType:!1,success:function(t){a.uploadComplete(t)},error:function(){a.setState("error")},always:function(){}})},uploadUrl:function(t){var e=this;e.uploadObj=t,this.log({rsv_imageclick:"uploadurl"}),e.upload(t,"PC_UPLOAD_SEARCH_URL")
},upload:function(t,e){var a=this,n=0===window.pageState?'{"page_from": "searchIndex"}':' {"page_from": "searchResult"}';if("PC_UPLOAD_SEARCH_URL"!==e){a.uploadObj=t,a.$urlInput.val("");var r=a.validate(t);if(r)return void a.setState("error",{msg:u[r]});a.log({rsv_imageclick:"uploadfile"})}a.setState("waiting");var o=new FormData;o.append("image",t),o.append("tn","pc"),o.append("from","pc"),o.append("image_source",e),o.append("range",n),a.doAjax(o)},uploadComplete:function(t){var e=this;0===t.status?setTimeout(function(){window.location.href=t.data.url
},30):e.setState("error")},log:l.sendLog};p.init(),"function"==typeof define&&define("soutu/js/tu",["require"],function(){return p})}();