(function ($) {
$.fn.extend({
ajaxyLiveSearch: function (options, arg) {
if (options && typeof (options) == 'object') {
options = $.extend({
}, $.ajaxyLiveSearch.defaults, options);
}
else {
options = $.ajaxyLiveSearch.defaults;
} // this creates a plugin for each element in
// the selector or runs the function once per
// selector. To have it do so for just the
// first element (once), return false after
// creating the plugin to stop the each iteration
if (this.is('input')) {
this.each(function () {
new $.ajaxyLiveSearch.load(this, options, arg);
});
return;
}
}
});
$.ajaxyLiveSearch = {
element: null,
timeout: null,
options: null,
load: function (elem, options, arg) {
this.element = elem;
this.timeout = null;
this.options = options;
if ($(elem).val() == '') {
$(elem).val(options.text);
}
$(elem).attr('autocomplete', 'off');
if ($('#live-search_sb').length == 0) {
$('body').append('
' +
'
' +
'' +
'
' +
'' +
'' +
'
' +
'' +
'
' +
'
');
}
$.ajaxyLiveSearch.loadEvents(this);
},
loadResults: function (object) {
options = object.options;
elem = object.element;
window.sf_lastElement = elem;
if (jQuery(elem).val() != '')
{
jQuery('body').data('live-search_results', null);
var loading = '
');
var pos = this.bounds(elem, options);
var containerPos = this.bounds('.searchx', options);
if (!pos) {
jQuery('#live-search_sb').hide();
return false;
}
if (Math.ceil(containerPos.left) + parseInt(options.width, 10) > jQuery(window).width()) {
jQuery('#live-search_sb').css('width', jQuery(window).width() - containerPos.left - 20);
}
if (jQuery('body').hasClass('rtl')) {
jQuery('#live-search_sb').css({
top: pos.bottom,
right: containerPos.right
});
} else {
jQuery('#live-search_sb').css({
top: pos.bottom,
left: containerPos.left
});
}
jQuery('#live-search_sb').show();
var data = {
action: 'ts_ac_do_search',
ts_ac_query: jQuery(elem).val()
};
if (options.ajaxData) {
data = window[options.ajaxData](data);
}
if (options.search) {
var mresults = options.search.split(',');
var results = [
];
var m = '';
var s = 0;
var c = [
];
for (var kindex in mresults) {
var dm = mresults[kindex].split(':');
if (dm.length == 2) {
if (dm[1].indexOf(jQuery(elem).val()) == 0) {
results[results.length] = mresults[kindex];
}
} else if (dm.length == 1) {
if (mresults[kindex].indexOf(jQuery(elem).val()) == 0) {
results[results.length] = mresults[kindex];
}
}
}
c = $.ajaxyLiveSearch.htmlArrayResults(results);
m += c[0];
s += c[1];
var sf_selected = '';
if (s == 0)
{
sf_selected = ' live-search_selected';
}
m += '
{total} ' + '
';
m = m.replace(/{search_value_escaped}/g, jQuery(elem).val());
m = m.replace(/{search_url_escaped}/g, options.searchUrl.replace('%s', encodeURI(jQuery(elem).val())));
m = m.replace(/{search_value}/g, jQuery(elem).val());
m = m.replace(/{total}/g, s);
jQuery('body').data('live-search_results', results);
if (s > 0)
{
jQuery('#live-search_val').html('
' + m + '
');
}
else
{
jQuery('#live-search_val').html('
' + m + '
');
}
$.ajaxyLiveSearch.loadLiveEvents(object);
jQuery('#live-search_sb').show();
} else {
jQuery.post(options.ajaxUrl, data, function (resp) {
var results = JSON.parse(resp);
var m = '';
var s = 0;
for (var mindex in results)
{
var c = [];
for (var kindex in results[mindex]) {
c = $.ajaxyLiveSearch.htmlResults(results[mindex][kindex], mindex, kindex);
m += c[0];
s += c[1];
}
}
var sf_selected = '';
if (s == 0)
{
sf_selected = ' live-search_selected';
m += '
' + '
';
} else {
if (!options.callback) {
m += '
' + sf_templates + '
';
}
m = m.replace(/{search_value_escaped}/g, jQuery(elem).val());
m = m.replace(/{search_url_escaped}/g, options.searchUrl.replace('%s', encodeURI(jQuery(elem).val())));
//m = m.replace(/{search_value}/g, jQuery(elem).val());
//m = m.replace(/{total}/g, s);
}
jQuery('body').data('live-search_results', results);
if (s > 0)
{
jQuery('#live-search_val').html('
' + m + '
');
}
else
{
jQuery('#live-search_val').html('
' + m + '
');
}
$.ajaxyLiveSearch.loadLiveEvents(object);
jQuery('#live-search_sb').show();
});
}
}
else
{
jQuery('#live-search_sb').hide();
}
},
bounds: function (elem, options) {
var offset = jQuery(elem).offset();
if (offset) {
return {
top: offset.top,
left: offset.left + options.leftOffset,
bottom: offset.top + jQuery(elem).innerHeight() + options.topOffset,
right: offset.left - jQuery('#live-search_sb').innerWidth() + jQuery(elem).innerWidth()
};
}
},
htmlResults: function (results, type, array_index) {
var m = '';
var s = 0;
if (typeof (results) != 'undefined')
{
if (results.all.length > 0)
{
m += '
' + results.title + '
';
for (var i = 0; i < results.all.length; i++)
{
s++;
m += '
';
}
}
return new Array(m, s);
},
htmlArrayResults: function (results) {
var m = '';
var s = 0;
if (typeof (results) != 'undefined')
{
if (results.length > 0)
{
m += '
';
for (var i = 0; i < results.length; i++)
{
var md = results[i].split(':');
var title = '';
if (md.length == 2) {
title = md[1];
} else {
title = results[i];
}
s++;
m += '