$.fn.clikSearch=function(q){var d=$.extend({},{template:{inner:'<a href="{{link}}"><img src="{{thumb_src}}" class="contentItemImage popup_{{id}}" width="{{imgWidth}}" title="{{caption}}"/></a><h3><a class="articleLink popup_{{id}}" href="{{link}}">{{{caption}}}</a></h3>{{#gallery}}<p class="contentitemDescription">{{{title}}}</p>{{/gallery}}',outer:'<div class="contentitemArticle"></div>',loading:'<div class="loading"></div>',pagination:'<div class="paginationControl"><span class="previous{{^prev}} previous-empty{{/prev}}">{{#prev}}<a href="#page={{prev}}">{{{previousLabel}}}</a>{{/prev}}{{^prev}}{{{previousLabel}}}{{/prev}}</span><span class="info">{{>paginate}}</span><span class="next{{^next}} next-empty{{/next}}">{{#next}}<a href="#page={{next}}">{{{nextLabel}}}</a>{{/next}}{{^next}}{{{nextLabel}}}{{/next}}</span></div>',
paginationInner:{list:'{{#pages}}<span class="pageLink"> <a{{^current}} href="#page={{index}}"{{/current}}>{{text}}</a></span>{{/pages}}',pages:" Page {{page}} of {{total}} ",items:" Items {{start}}-{{end}} of {{totalItems}}"}},searchTarget:"div.searchTarget",paginatorFormat:"items",paginatorPosition:"bottom",hiddenFields:{sites_id:"",mode:"",types:"",payment_groups:"",perPage:""},noResults:"No results found",previousLabel:"Previous",nextLabel:"Next",usePopups:false,imgWidth:100},q),t=function(i,
e,b){var g=1,a=[],c,f=0,j=0,n=0,h={};i.empty();if(e.perPage)g=Math.ceil(e.total/e.perPage);else e.perPage=e.results.length;e.results.length||i.append(d.noResults);$.each(e.results,function(o,k){var l;if(0<=o&&o<Math.max(e.perPage,1)){l=$(Mustache.to_html(d.template.outer)).appendTo(i).append(Mustache.to_html(d.template.loading,null));f++;clik.getPhoto(k.photos_id,function(){photos[k.photos_id].thumb_src=thumbnails[k.photos_id].src;$(Mustache.to_html(d.template.inner,$.extend({imgWidth:d.imgWidth},
photos[k.photos_id],d.showGalleries&&photos[k.photos_id].galleries_id?{gallery:galleries[photos[k.photos_id].galleries_id]}:{}))).appendTo(l.empty());n++;d.usePopups&&l.find("img").addClass("popup").end().find("a.articleLink").addClass("imgPopup");l.find("img").bind("load.search error.search",function(r){var s=$(this);r.type!="load"&&s.remove();if(!--n&&!f&&!j){j=1;clik.doColumnResize();j=0}$(this).unbind("load.search error.search")});if(!--f&&!j){j=1;clik.doColumnResize();j=0}})}});if(g>1){if(d.paginatorFormat==
"list"||d.paginatorFormat=="listnotext"){h.pageLinkTotal=7;h.ellipsis="...";h.pageLinkLeft=Math.floor((h.pageLinkTotal-3)/2);h.pageLinkRight=Math.ceil((h.pageLinkTotal-3)/2);if(b-h.pageLinkLeft<=1)h.pageLinkRight=h.pageLinkRight+2-(b-h.pageLinkLeft);if(b+h.pageLinkLeft>=g)h.pageLinkLeft=h.pageLinkLeft+1-(g-b-h.pageLinkLeft);a[0]={index:1,current:b==1,text:1};if(d.paginatorFormat=="listnotext")a[0].text="&nbsp;";for(c=2;c<g;c+=1)if(c==b||c>b-h.pageLinkLeft&&c<b+h.pageLinkRight){a[a.length]={index:c,
current:b==c,text:c};if(d.paginatorFormat=="listnotext")a[a.length-1].text="&nbsp;"}else if(c==b-h.pageLinkLeft||c==b+h.pageLinkRight)if(c==2||c==g-1){a[a.length]={index:c,current:false,text:c};if(d.paginatorFormat=="listnotext")a[a.length-1].text="&nbsp;"}else a[a.length]={index:h.ellipsis,current:true,text:h.ellipsis};if(g>1){a[a.length]={index:g,current:b==g,text:g};if(d.paginatorFormat=="listnotext")a[a.length-1].text="&nbsp;"}}b=Mustache.to_html(d.template.pagination,{prev:b-1,next:b<g&&b+1,
previousLabel:d.previousLabel,nextLabel:d.nextLabel,paginate:{pages:a,page:b,total:g,start:(b-1)*e.perPage+1,end:Math.min(b*e.perPage,e.total),totalItems:e.total}},{paginate:d.template.paginationInner[d.paginatorFormat=="listnotext"?"list":d.paginatorFormat]});switch(d.paginatorPosition){case "topleft":case "topright":i.prepend(b);break;case "bottomleft":case "bottomright":i.append(b)}}},p=function(i,e){var b=$(i);$.each(e,function(g,a){var c=b.find("[name="+g+"]"),f;if(c.length>1){c.attr("checked",
false);$.each(a.split(","),function(){c.filter("[value="+this+"]").attr("checked",true)})}else if(f=c.data("selectList")){f.remove();$.each(a.split(","),function(){f.add(""+this)})}else c.is("[type=checkbox]")?c.attr("checked",parseInt(a,10)):c.val(a)});b.submit()},m=false;return this.ajaxForm({beforeSubmit:function(i,e){var b={},g=0,a=[];$.each(i,function(c,f){if(!(f.name in d.hiddenFields))if(f.name in b){if(a[b[f.name]].search(/\=$/)==-1)a[b[f.name]]+=",";a[b[f.name]]+=f.value}else{a.push(encodeURIComponent(f.name)+
"="+encodeURIComponent(f.value));b[f.name]=g++}});m=true;if($("body").hasClass("type_search"))document.location.hash=a.join("&");else document.location=e.attr("action")+"#"+a.join("&");$(d.searchTarget).html(Mustache.to_html(d.template.loading)).removeClass("empty")},success:function(i,e,b,g){e=$(d.searchTarget);t(e,i,i.page);e.undelegate(".paginationControl a[href]","click").delegate(".paginationControl a[href]","click",function(){var a;a=this.href;var c;a=(a||"").split("#");a=a[Math.min(a.length,
1)];a=decodeURIComponent(a);try{c=(a.match(RegExp("(?:^#?|&)page=([^&]*)",void 0))||[null,null])[1]}catch(f){c=a}a=parseInt(c,10);p(g,{page:a});return false})},error:function(){$(d.searchTarget).html("Search error")},dataType:"json",url:"_search.cfm"}).each(function(){var i=this,e="",b=function(){if(!m&&document.location.hash!=e){e=document.location.hash;var g=RegExp("(?:^#?|&)\\w+=([^&]*)","g"),a,c;$(i);var f={};if(document.location.hash){for(;a=g.exec(document.location.hash);){c=decodeURIComponent(a[0].match(/(\w+)=/)[1]);
a=decodeURIComponent(a[1]);f[c]=a}f.formid!=$(i).attr("id")&&$("#"+f.formid).length||p(i,f)}}return m=false};if($("body").hasClass("type_search")){$(window).bind("hashchange.search",b);b()}$(this).find("input[name=q],input[name=photos_payment],select").bind("change",function(){this.form.page.value=1}).end().find(".searchAdvancedButton").bind("click",function(){$(i).find(".searchAdvanced").toggle()})})};
