/*
 * jQuery UI Autocomplete HTML Extension
 *
 * Copyright 2010, Scott González (http://scottgonzalez.com)
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * http://github.com/scottgonzalez/jquery-ui-extensions
 */
(function( $ ) {

var proto = $.ui.autocomplete.prototype,
	initSource = proto._initSource;

function filter( array, term ) {
	var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
	return $.grep( array, function(value) {
		return matcher.test( $( "<div>" ).html( value.label || value.value || value ).text() );
	});
}

$.extend( proto, {
	_initSource: function() {
		if ( this.options.html && $.isArray(this.options.source) ) {
			this.source = function( request, response ) {
				response( filter( this.options.source, request.term ) );
			};
		} else {
			initSource.call( this );
		}
	},

	_renderItem: function( ul, item) {
		return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( $( "<a></a>" )[ this.options.html ? "html" : "text" ]( item.label ) )
			.appendTo( ul );
	}
});

})( jQuery );


var processing = false;
var rt;
var cache = {};
var _response;
var _request;

$(function() {
    if (_doSearch) {
        $(".suggest").catcomplete({
            delay: 100,
            source: function(request, response) {
                rt = request.term;
                _response = response;
                _request = request;
                if (request.term.toLowerCase() in cache) {
                    response($.map(cache[request.term.toLowerCase()].SearchTerm, function(item) {
                        return {
                            label: formatDisplayRow(item, request.term),
                            value: item.Url == '' ? item.SearchTerm : '',
                            cat: item.Category,
                            url: item.Url
                        }
                    }));
                    return;
                }
                $.ajax({
                    url: reqUrl,
                    dataType: "jsonp",
                    data: {
                        p: _p,
                        m: _m,
                        n: _n,
                        q: request.term,
                        fn: 'doSomething'
                    }
                });
            },
            minLength: _minLength,
            html: true,
            select: function(event, ui) {
                ui.item ? HandleClick(ui.item) : null;
            },
            open: function() {
                $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
            },
            close: function() {
                $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
            }
            ,
            create: function(event, ui) {
                $('.ui-autocomplete.ui-menu').appendTo($('.suggestions'));
            }
        });
    }


    $(".suggest").keypress(function(e) {
        if (e.which == 13) {
            HandleClick($('.suggest').val());
            return false;
        }
    });
});              // end $(fn())


function doSomething(data) {
    cache[_request.term.toLowerCase()] = data;
    _response($.map(data.SearchTerm, function(item) {
        return {
            label: formatDisplayRow(item, _request.term),
            value: item.Url == '' ? item.SearchTerm : '',
            cat: item.Category,
            url: item.Url
        }
    }));
}

String.prototype.replaceAll = function(stringToFind, stringToReplace) {
    var temp = this;
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}

function CleanSearchString(searchString) {
    searchString = $.trim(searchString);
    searchString = searchString.replaceAll("&", "and");
    searchString = searchString.replaceAll("+", "and");
    searchString = searchString.replaceAll("$", " ");
    searchString = searchString.replaceAll(",", " ");
    searchString = searchString.replaceAll("/", " ");
    searchString = searchString.replaceAll(":", " ");
    searchString = searchString.replaceAll(";", " ");
    searchString = searchString.replaceAll("=", " ");
    searchString = searchString.replaceAll("?", " ");
    searchString = searchString.replaceAll("@", " ");
    searchString = searchString.replaceAll(" ", "+");
    
    return escape(searchString);
}

function HandleClick(item) {
    if (processing) return false;
    processing = true;
    if (item == null) {
        var _searchTerm = $.trim($('#' + _searchBoxId).val()).toLowerCase();
        if (_searchTerm == "search" || _searchTerm == "") {
            processing = false;
            return false;
        }
        window.location = searchUrl + CleanSearchString(_searchTerm);

    }
    else {
        if (item.url != null && item.url != '') {
            window.location = item.url;
        }
        else {
            window.location = searchUrl + (item.value == null ? CleanSearchString(item) : CleanSearchString(item.value));
        }
    }
    return false;
}


$.widget("custom.catcomplete", $.ui.autocomplete, {
    _renderMenu: function(ul, items) {
        var self = this,
				currentCategory = "",
				stCat = "st",
				aiCat = "ai",
				bStFirst = true,
				bAiFirst = true;

        $(ul).addClass('menux');
        $.each(items, function(index, item) {

            if (item.cat != currentCategory) {
                if (item.cat == stCat && bStFirst) {
                    ul.append("<li class='ui-autocomplete-category'>Search Suggestions</li>");
                    bStFirst = false;
                }
                else if (item.cat == aiCat && bAiFirst) {
                    ul.append("<li class='ui-autocomplete-category'>Latest Content</li>");
                    bAiFirst = false;
                }
                currentCategory = item.cat;
            }
            self._renderItem(ul, item);
        });
        $('.menux li span').highlight(rt);
    }
});

function formatDisplayRow(item, st) {
    var d = '<span>' + item.SearchTerm + '</span>';
    var i = item.ThumbnailUrl ? ' style="background-image:url(' + imagePath + item.ThumbnailUrl + ')"' : '';
    return '<div' + i + '>' + d + '</div>';
}


jQuery.fn.highlight = function(pat) {
    function innerHighlight(node, pat) {
        var skip = 0;
        if (node.nodeType == 3) {
            var pos = node.data.toUpperCase().indexOf(pat);
            if (pos >= 0) {
                var spannode = document.createElement('b');
                var middlebit = node.splitText(pos);
                var endbit = middlebit.splitText(pat.length);
                var middleclone = middlebit.cloneNode(true);
                spannode.appendChild(middleclone);
                middlebit.parentNode.replaceChild(spannode, middlebit);
                skip = 1;
            }
        }
        else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
            for (var i = 0; i < node.childNodes.length; ++i) {
                i += innerHighlight(node.childNodes[i], pat);
            }
        }
        return skip;
    }
    return this.each(function() {
        innerHighlight(this, pat.toUpperCase());
    });
};

jQuery.fn.removeHighlight = function() {
    function newNormalize(node) {
        for (var i = 0, children = node.childNodes, nodeCount = children.length; i < nodeCount; i++) {
            var child = children[i];
            if (child.nodeType == 1) {
                newNormalize(child);
                continue;
            }
            if (child.nodeType != 3) { continue; }
            var next = child.nextSibling;
            if (next == null || next.nodeType != 3) { continue; }
            var combined_text = child.nodeValue + next.nodeValue;
            new_node = node.ownerDocument.createTextNode(combined_text);
            node.insertBefore(new_node, child);
            node.removeChild(child);
            node.removeChild(next);
            i--;
            nodeCount--;
        }
    }

    return this.find("span.highlight").each(function() {
        var thisParent = this.parentNode;
        thisParent.replaceChild(this.firstChild, this);
        newNormalize(thisParent);
    }).end();
};
