$(document).ready(function () {
    //Search Auto Complete
    $('#txtSearch').keyup(function () {
        var tmpString;
        if ($(this).val().length > 2) {
            showResults($(this).val());
        } else {
            $('#rList').fadeOut();
        }
    });
    $('#txtSearch').blur(function () {
        $('#rList').fadeOut();
    });
});

function showResults(searchTerm) {
    $.ajax({ //load the xml
        type: "GET",
        url: "/xml/searchResults.xml",
        dataType: "xml",
        success: findString //on completion load the function
    });

    function findString(xml) {
        var tmpString = '';
        var i = 0;
        searchTerm = searchTerm.toLowerCase();
        $(xml).find('searchResults').each(function () { //look through every xml node called 'searchResults'
            var searchResults = $(this);
            tmpString = tmpString + '<div id="rList">';
            tmpString = tmpString + '<div class="rTop"></div>';
            $(searchResults).find('result').each(function () { //look through every node called result
                //Check the description or the title for the search term
                if ($(this).attr('description').toLowerCase().indexOf(searchTerm) >= 0 || $(this).attr('title').toLowerCase().indexOf(searchTerm) >= 0 || $(this).attr('keywords').toLowerCase().indexOf(searchTerm) >= 0) {
                    if (i < 8) {
                        //organize the results
                        tmpString = tmpString + '<div class="rGroup">';
                        tmpString = tmpString + '<a href="' + $(this).attr('url') + '" title="' + $(this).attr('title') + '">' + $(this).attr('title') + '</a><br/>';

                        //console.log($(this).attr('description'));
                        if ($(this).attr('description').length > 0) {
                            //tmpString = tmpString + $(this).attr('description');
                            var trimString = $(this).attr('description');
                            tmpString = tmpString + summaryWriter(70, trimString,true);
                            //if ($(this).attr('description').length > 49) {
                                //tmpString = tmpString + Left($(this).attr('description'), 49) + '...';
                            //} else {
                                //tmpString = tmpString + $(this).attr('description');
                            //}
                        }

                        if (i < 8 - 1) {
                            tmpString = tmpString + '<div class="rDivider"></div>';
                        }
                        tmpString = tmpString + '</div><!--rGroup-->';
                        i = i + 1;
                    }
                } else {
                    $('#rList').css('display', 'none');
                }
            });
            tmpString = tmpString + '<div class="rBot"></div>';
            tmpString = tmpString + '</div><!--rList-->';
        });
        $('#leftContent').prepend(tmpString);

        //position the listing
        var pos = $('#txtSearch').position();
        $('#rList').css('left', parseInt(pos.left) - parseInt($('#txtSearch').width()) - 15);
        $('#rList').css('top', parseInt(pos.top) + parseInt($('#txtSearch').height()) + 6);
        if (i > 0) {$('#rList').fadeIn();}
    }
}

function summaryWriter(trimLength, _string, onSpace){
        if(_string.length > 0){
            //trim the content
            if(_string.length >= trimLength){
                //temp hack
                if(!onSpace){
                    _string = Left(_string, trimLength) + "...";
                }else{
                    var i = trimLength;
                    //Add blank space to end
                    _string = String(" ") + _string;

                    while(_string.charAt(i) != ' '){
                        i = i - 1;
                    }

                    i = i - 1
                    if(_string.charAt(i) == ","){ i = i - 1; }
                    _string = $.trim(_string);
                    _string = String(_string).substring(0, i) + "...";
                }
            }else{
                _string = _string;
            }
        }else{
            _string = _string;
        }

        return _string
}
