(function(n){function u(){var n=config("search_history",""),r=[],t,u,i;if(!n)return r;for(n=n.split(/\|\|/ig),t=0,u=n.length;t<u;t++)i=n[t].split(/\$/ig),i.length==2&&r.push({key:i[0],type:i[1]});return r}function f(n,t){var e=config("search_history",""),i="",t=t?t:"key",u,o,f;for(e=e.split(/\|\|/ig),u=0,o=e.length;u<o;u++)if(f=e[u].split(/\$/ig),f.length==2&&f[0]!=n&&(i!=""&&(i+="||"),i+=f[0]+"$"+f[1]),u>=r-1)break;return i=n+"$"+t+"||"+i,config("search_history",i,!0),!0}function e(){config("search_history","",!0)}var r=20,i,t;n.suggestKeyClick=function(n){$("#search_keyword").val($(n).data("key")),$("#search_type").val($(n).data("type")),t(),__log("search","suggest","by_keyword_btn")},n.suggestKeyClean=e,location.href.search(/mz:(\/\/)?search/ig)>-1&&typeof $GET.key!="undefined"&&$GET.key&&f($GET.key,$GET.t),i=function(){var t=config("search_site",""),i=config("search_type","key"),n;$("#search_site").val(t),$("#search_type").val(i),$GET.key?$("#search_keyword").val($GET.key):$("#search_keyword").val(config("search_keyword","")),n=t?"搜索"+t.split(/\|/ig).length+"个网站":"搜索所有站点",n+=i=="key"?"关键词":"作者",$("#searchplan").attr("title",n).text(n)},t=function(){var n=$.trim($("#search_keyword").val());n.search(/^http:\/\//ig)>-1?$e.tabopen("mz://reader/?url="+escape(n),{checkExists:!0}):($e.tabopen("mz://search/?key="+escape(n)+"&t="+$("#search_type").val()+"&site="+$("#search_site").val()),config("search_keyword",$("#search_keyword").val(),!0))},$("#search_btn").live("click",function(){$.trim($("#search_keyword").val())!=""&&(t(),__log("search","submit","by_search_btn"))}),$("#search_keyword").live("keyup",function(){event.keyCode==13&&$.trim($("#search_keyword").val())!=""&&(t(),__log("search","submit","by_keyword_btn"))}).live("click",function(){var e=u(),i,r,f;if(e.length!=0){var s=$(this).parent(),t=fetchoffset(s.get(0)),n,o=!1;$("#search_sugg").length==0?(o=!0,n=$('<div class="search_sugg" id="search_sugg"><\/div>')):n=$("#search_sugg"),i=["paddingRight","paddingLeft","marginRight","marginLeft"],r="";for(r in i)f=parseInt($(".tab_option").css(i[r]),10),f>0&&(t.width-=f);n.css({top:t.popy+5,left:t.x,width:t.width}).html(mzTpl("tpl_searchsuggest")({keys:e})),o&&$(this).parent().append(n),n.show()}}).live("blur",function(){setTimeout(function(){$("#search_sugg").hide()},300)}),i(),$("#searchplan").live("click",function(){var n=$e.getSearchTask().split(/\|\|/g),t=config("search_site","").split(/\|/ig);n=_.chain(n).sortBy(function(n){return $e.pinyin($e.getextension(n,"title")).substr(0,1).charCodeAt()}).map(function(n){var i=$e.getextension(n,"title");return{name:n,title:i,pinyin:$e.pinyin(i).toLowerCase(),domain:$e.getextension(n,"domain"),selected:$.inArray(n,t)>-1?!0:!1}}).value(),art.dialog({id:"search",lock:!0,fixed:!0,opacity:.7,content:mzTpl("tpl_search",{site:n,type:config("search_type","key")}),okVal:"确认保存",padding:"5px 10px",ok:function(){var n="",t;$(".siteul .active").each(function(){n+=$(this).data("val")+"|"}),config("search_site",n.substr(0,n.length-1),!0),t=$(".typeul li.active").eq(0).data("val"),config("search_type",t,!0),i()},cancelVal:"取消",cancel:!0});$("#search_plan_div ul li").on("click",function(){var n=$(this).data("only")?!0:!1;n?($('*[data-key="'+$(this).data("key")+'"]').removeClass("active"),$(this).addClass("active")):$(this).toggleClass("active")});$("#cancel_btn").on("click",function(){$(".siteul li").removeClass("active")});$("#search_filter").on("focus",function(){$(this).val()==$(this).data("default")&&$(this).val("").css("color","#333333")}).on("blur",function(){$(this).val()==""&&$(this).val($(this).data("default")).css("color","#999999")}).on("keyup",function(){var n=$.trim($(this).val()).toLowerCase();$(".siteul li").each(function(){var t=($(this).attr("index")?$(this).attr("index"):"").toLowerCase();n==""||t.search(n)>-1?$(this).show():$(this).hide()})}).val($("#search_filter").data("default")).css("color","#999999")})})(window)