(function(jQuery, $, window){ /*! Ajax Search pro 2.0 js */ (function ($) { var methods = { init: function (options, elem) { var $this = this; this.elem = elem; this.$elem = $(elem); $this.searching = false; $this.o = $.extend({}, options); $this.n = new Object(); $this.n.container = $(this.elem); $this.o.rid = $this.n.container.attr('id').match(/^ajaxsearchlite(.*)/)[1]; $this.o.id = $this.n.container.attr('id').match(/^ajaxsearchlite(.*)/)[1]; $this.n.probox = $('.probox', $this.n.container); $this.n.proinput = $('.proinput', $this.n.container); $this.n.text = $('.proinput input.orig', $this.n.container); $this.n.textAutocomplete = $('.proinput input.autocomplete', $this.n.container); $this.n.loading = $('.proinput .loading', $this.n.container); $this.n.proloading = $('.proloading', $this.n.container); $this.n.proclose = $('.proclose', $this.n.container); $this.n.promagnifier = $('.promagnifier', $this.n.container); $this.n.prosettings = $('.prosettings', $this.n.container); $this.n.searchsettings = $('#ajaxsearchlitesettings' + $this.o.rid); $this.n.resultsDiv = $('#ajaxsearchliteres' + $this.o.rid); $this.n.hiddenContainer = $('#asl_hidden_data'); $this.n.aslItemOverlay = $('.asl_item_overlay', $this.n.hiddenContainer); $this.resizeTimeout = null; $this.n.showmore = $('.showmore a', $this.n.resultsDiv); $this.n.items = $('.item', $this.n.resultsDiv); $this.n.results = $('.results', $this.n.resultsDiv); $this.n.resdrg = $('.resdrg', $this.n.resultsDiv); // Isotopic Layout variables $this.il = { columns: 3, itemsPerPage: 6 }; $this.firstClick = true; $this.post = null; $this.postAuto = null; $this.cleanUp(); $this.n.textAutocomplete.val(''); $this.o.resultitemheight = parseInt($this.o.resultitemheight); $this.scroll = new Object(); $this.settScroll = null; $this.n.resultsAppend = $('#wpdreams_asl_results_' + $this.o.id); $this.currentPage = 1; $this.isotopic = null; $this.animation = "bounceIn"; switch ($this.o.resultstype) { case "vertical": $this.animation = $this.o.vresultanimation; break; case "isotopic": $this.animation = $this.o.iianimation; break; default: $this.animation = $this.o.hresultanimation; } $this.filterFns = { number: function () { var $parent = $(this).parent(); while (!$parent.hasClass('isotopic')) { $parent = $parent.parent(); } var number = $(this).attr('data-itemnum'); //var currentPage = parseInt($('nav>ul li.asl_active span', $parent).html(), 10); var currentPage = $this.currentPage; //var itemsPerPage = parseInt($parent.data("itemsperpage")); var itemsPerPage = $this.il.itemsPerPage; return ( (parseInt(number, 10) < itemsPerPage * currentPage) && (parseInt(number, 10) >= itemsPerPage * (currentPage - 1)) ); } }; $this.disableMobileScroll = false; $this.n.searchsettings.detach().appendTo("body"); if ($this.o.resultsposition == 'hover') { $this.n.resultsDiv.detach().appendTo("body"); } else if ($this.n.resultsAppend.length > 0) { $this.n.resultsDiv.detach().appendTo($this.n.resultsAppend); } if ($this.o.resultstype == 'horizontal') { $this.createHorizontalScroll(); } else if ($this.o.resultstype == 'vertical') { $this.createVerticalScroll(); } if ($this.o.resultstype == 'polaroid') $this.n.results.addClass('photostack'); if (detectIE()) $this.n.container.addClass('asl_msie'); $this.initEvents(); return this; }, duplicateCheck: function() { var $this = this; var duplicateChk = {}; $('div[id*=ajaxsearchlite]').each (function () { if (duplicateChk.hasOwnProperty(this.id)) { $(this).remove(); } else { duplicateChk[this.id] = 'true'; } }); }, analytics: function(term) { var $this = this; if ($this.o.analytics && $this.o.analyticsString != '' && typeof ga == "function") { ga('send', 'pageview', { 'page': '/' + $this.o.analyticsString.replace("{asl_term}", term), 'title': 'Ajax Search' }); } }, createVerticalScroll: function () { var $this = this; $this.scroll = $this.n.results.mCustomScrollbar({ contentTouchScroll: true, scrollButtons: { enable: true, scrollType: "pixels", scrollSpeed: $this.o.resultitemheight, scrollAmount: $this.o.resultitemheight }, callbacks: { onScroll: function () { if (is_touch_device()) return; var top = parseInt($('.mCSB_container', $this.n.results).position().top); var children = $('.mCSB_container .resdrg').children(); var overall = 0; var prev = 3000; var diff = 4000; var s_diff = 10000; var s_overall = 10000; var $last = null; children.each(function () { diff = Math.abs((Math.abs(top) - overall)); if (diff < prev) { s_diff = diff; s_overall = overall; $last = $(this); } overall += $(this).outerHeight(true); prev = diff; }); if ($last.hasClass('group')) s_overall = s_overall + ($last.outerHeight(true) - $last.outerHeight(false)); $(".mCSB_container", $this.n.resultsDiv).animate({ top: -s_overall }); } } }); }, createHorizontalScroll: function () { var $this = this; $this.scroll = $this.n.results.mCustomScrollbar({ horizontalScroll: true, contentTouchScroll: true, scrollButtons: { enable: true, scrollType: 'pixels', scrollSpeed: 'auto', scrollAmount: 100 } }); }, initEvents: function () { var $this = this; $($this.n.text.parent()).submit(function (e) { e.preventDefault(); $this.n.text.keyup(); }); $this.n.text.click(function () { if ($this.firstClick) { $(this).val(''); $this.firstClick = false; } }); $this.n.resultsDiv.css({ opacity: 0 }); $(document).bind("click touchend", function () { $this.hideSettings(); if ($this.opened == false || $this.o.closeOnDocClick != 1) return; $this.hideResults(); }); $this.n.proclose.bind("click touchend", function () { if ($this.opened == false) return; $this.n.text.val(""); $this.n.textAutocomplete.val(""); $this.hideResults(); $this.n.text.focus(); }); $($this.elem).bind("click touchend", function (e) { e.stopImmediatePropagation(); }); $this.n.resultsDiv.bind("click touchend", function (e) { e.stopImmediatePropagation(); }); $this.n.searchsettings.bind("click touchend", function (e) { e.stopImmediatePropagation(); }); $this.n.prosettings.on("click", function () { if ($this.n.prosettings.attr('opened') == 0) { $this.showSettings(); } else { $this.hideSettings(); } }); // jQuery bind not working! $(document).bind('touchmove', function (e) { if ($this.disableMobileScroll == true) e.preventDefault(); }); var resizeTimer; $(window).on("resize", function () { clearTimeout(resizeTimer); resizeTimer = setTimeout(function() { $this.resize(); }, 250); }); var scrollTimer; $(window).on("scroll", function () { clearTimeout(scrollTimer); scrollTimer = setTimeout(function() { $this.scrolling(false); }, 250); }); $this.initNavigationEvent(); $(window).trigger('resize'); $(window).trigger('scroll'); $this.initMagnifierEvent(); $this.initAutocompleteEvent(); $this.initPagerEvent(); $this.initOverlayEvent(); $this.initFacetEvents(); }, initNavigationEvent: function () { var $this = this; $($this.n.resultsDiv).on('mouseenter', '.item', function () { //alert("hover"); $('.item', $this.n.resultsDiv).removeClass('hovered'); $(this).addClass('hovered'); } ); $($this.n.resultsDiv).on('mouseleave', '.item', function () { //alert("hover"); $('.item', $this.n.resultsDiv).removeClass('hovered'); } ); $(document).keydown(function (e) { if (window.event) { var keycode = window.event.keyCode; var ktype = window.event.type; } else if (e) { var keycode = e.which; var ktype = e.type; } //$('.item', $this.n.resultsDiv).hover(); if ($('.item', $this.n.resultsDiv).length > 0 && $this.n.resultsDiv.css('display') != 'none') { if (keycode == 40) { e.stopPropagation(); e.preventDefault(); $this.n.text.blur(); if ($this.post != null) $this.post.abort(); if ($('.item.hovered', $this.n.resultsDiv).length == 0) { $('.item', $this.n.resultsDiv).first().addClass('hovered'); } else { $('.item.hovered', $this.n.resultsDiv).removeClass('hovered').next().next('.item').addClass('hovered'); } $this.scroll.mCustomScrollbar("scrollTo", ".resdrg .item.hovered",{ scrollInertia: 200, callbacks: false }); } if (keycode == 38) { e.stopPropagation(); e.preventDefault(); $this.n.text.blur(); if ($this.post != null) $this.post.abort(); if ($('.item.hovered', $this.n.resultsDiv).length == 0) { $('.item', $this.n.resultsDiv).last().addClass('hovered'); } else { $('.item.hovered', $this.n.resultsDiv).removeClass('hovered').prev().prev('.item').addClass('hovered'); } $this.scroll.mCustomScrollbar("scrollTo", ".resdrg .item.hovered",{ scrollInertia: 200, callbacks: false }); } // Trigger click on return key if ( keycode == 13 && $('.item.hovered', $this.n.resultsDiv).length > 0 ) { e.stopPropagation(); e.preventDefault(); $('.item.hovered a.asl_res_url', $this.n.resultsDiv).get(0).click(); } } }); }, initMagnifierEvent: function () { var $this = this; var t; $this.n.promagnifier.add($this.n.text).bind('click keyup', function (e) { if (window.event) { $this.keycode = window.event.keyCode; $this.ktype = window.event.type; } else if (e) { $this.keycode = e.which; $this.ktype = e.type; } if ($this.n.text.val().length < $this.o.charcount) { $this.n.proloading.css('display', 'none'); $this.hideResults(); if ($this.post != null) $this.post.abort(); clearTimeout(t); return; } // Ignore arrows if ($this.keycode >= 37 && $this.keycode <= 40) return; if ($(this).hasClass('orig') && $this.ktype == 'click') return; if ($this.o.redirectonclick == 1 && $this.ktype == 'click') { location.href = $this.o.homeurl + '?s=' + $this.n.text.val(); return; } if ($this.o.triggeronclick == 0 && $this.ktype == 'click') return; if ($this.o.triggerontype == 0 && $this.ktype == 'keyup') return; if ($this.post != null) $this.post.abort(); clearTimeout(t); t = setTimeout(function () { $this.search(); }, 300); }); }, initPagerEvent: function () { var $this = this; $this.n.resultsDiv.on('click', 'nav>a', function (e) { e.preventDefault(); if ($(this).hasClass('asl_prev')) { $this.currentPage = $this.currentPage == 1 ? Math.ceil($this.n.items.length / $this.il.itemsPerPage) : --$this.currentPage; } else { $this.currentPage = $this.currentPage == Math.ceil($this.n.items.length / $this.il.itemsPerPage) ? 1 : ++$this.currentPage; } $('nav>ul li', $this.n.resultsDiv).removeClass('asl_active'); $($('nav>ul li', $this.n.resultsDiv).get($this.currentPage - 1)).addClass('asl_active'); $this.isotopic.arrange({filter: $this.filterFns['number']}); $this.removeAnimation(); }); $this.n.resultsDiv.on('click', 'nav>ul li', function (e) { e.preventDefault(); $this.currentPage = parseInt($('span', this).html(), 10); $('nav>ul li', $this.n.resultsDiv).removeClass('asl_active'); $($('nav>ul li', $this.n.resultsDiv).get($this.currentPage - 1)).addClass('asl_active'); $this.isotopic.arrange({filter: $this.filterFns['number']}); $this.removeAnimation(); }); }, initOverlayEvent: function () { var $this = this; if ($this.o.resultstype == "isotopic") { if ($this.o.iishowOverlay) { $this.n.resultsDiv.on('mouseenter', 'div.item', function (e) { $('.asl_item_overlay', this).fadeIn(); if ($(".asl_item_img", this).length>0) { if ($this.o.iiblurOverlay) $('.asl_item_overlay_img', this).fadeIn(); if ($this.o.iihideContent) $('.content', this).slideUp(100); } }); $this.n.resultsDiv.on('mouseleave', 'div.item', function (e) { $('.asl_item_overlay', this).fadeOut(); if ($(".asl_item_img", this).length>0) { if ($this.o.iiblurOverlay) $('.asl_item_overlay_img', this).fadeOut(); if ($this.o.iihideContent && $(".asl_item_img", this).length>0) $('.content', this).slideDown(100); } }); $this.n.resultsDiv.on('mouseenter', 'div.asl_item_inner', function (e) { $(this).addClass('animated pulse'); }); $this.n.resultsDiv.on('mouseleave', 'div.asl_item_inner', function (e) { $(this).removeClass('animated pulse'); }); $this.n.resultsDiv.on('click', '.asl_item_overlay', function(){ var url = $('.content h3 a', $(this).parent()).attr('href'); window.location = url; }); } $(window).on('resize', function () { if ($this.resizeTimeout != null) clearTimeout($this.resizeTimeout); $this.resizeTimeout = setTimeout(function () { $this.calculateIsotopeRows(); $this.showPagination(); $this.removeAnimation(); if ($this.isotopic != null) $this.isotopic.arrange({filter: $this.filterFns['number']}); }, 200); }); } }, initFacetEvents: function() { var $this = this; if ($this.o.trigger_on_facet_change == 1) $('input', $this.n.searchsettings).change(function(){ if ($this.n.text.val().length < $this.o.charcount) return; if ($this.post != null) $this.post.abort(); $this.search(); }); }, destroy: function () { return this.each(function () { var $this = $.extend({}, this, methods); $(window).unbind($this); }) }, searchfor: function (phrase) { $(".proinput input", this).val(phrase).trigger("keyup"); }, initAutocompleteEvent: function () { var $this = this; var tt; if ($this.o.autocomplete.enabled == 1 && !is_touch_device()) { $this.n.text.keyup(function (e) { if (window.event) { $this.keycode = window.event.keyCode; $this.ktype = window.event.type; } else if (e) { $this.keycode = e.which; $this.ktype = e.type; } var thekey = 39; // Lets change the keykode if the direction is rtl if ($('body').hasClass('rtl')) thekey = 37; if ($this.keycode == thekey && $this.n.textAutocomplete.val() != "") { e.preventDefault(); $this.n.text.val($this.n.textAutocomplete.val()); if ($this.post != null) $this.post.abort(); $this.search(); } else { if ($this.postAuto != null) $this.postAuto.abort(); $this.autocompleteGoogleOnly(); } }); } }, // If only google source is used, this is much faster.. autocompleteGoogleOnly: function () { var $this = this; var val = $this.n.text.val(); if ($this.n.text.val() == '') { $this.n.textAutocomplete.val(''); return; } var autocompleteVal = $this.n.textAutocomplete.val(); if (autocompleteVal != '' && autocompleteVal.indexOf(val) == 0) { return; } else { $this.n.textAutocomplete.val(''); } $.ajax({ url: 'https://clients1.google.com/complete/search', dataType: 'jsonp', data: { q: val, hl: $this.o.autocomplete.lang, nolabels: 't', client: 'hp', ds: '' }, success: function(data) { if (data[1].length > 0) { response = data[1][0][0].replace(/(<([^>]+)>)/ig,""); response = $('