﻿$(document).ready(function () {

    autocompletetagurl = 'resources/'
    autocompleteitemurl = 'resource/'

    autocompleteobj = $('#MainContent_UC_Autocomplete1_dvautocomplete_wrapper');
    if (autocompleteobj[0] != undefined) {

        autocompleteobj.find('.autocomplete_imagebutton').click(function () {
            window.location.replace('resources/' + autocompleteobj.find(".autocomplete_textbox").val()); 
        });

        autocompleteobj.find(".autocomplete_results").hide();
        autocompleteobj.find(".autocomplete_textbox").focus();


        // keypressing within the textbox
        autocompleteobj.find(".autocomplete_textbox").keyup(function (e) {
            if (e.keyCode == 13)
                autocompleteredirectpage(autocompletetagurl + $(this).val());
            // Get the data
            autocompletegetdata(autocompleteobj.find(".autocomplete_textbox"), autocompleteobj.find(".autocomplete_results"));
        });

        autocompleteobj.find(".autocomplete_results").find("li").find('a').hide();
        autocompleteobj.find(".autocomplete_results").find("li").mouseenter(function () {
            $(this).find('a').show();
        }).mouseleave(function () {
            $(this).find('a').hide();
        });
        autocompleteobj.find(".autocomplete_results").find("li").find('span').click(function () {
            window.location = $(this).text();
        });

        var mouse_is_inside = false;
        autocompleteobj.find(".autocomplete_results").hover(function () {
            mouse_is_inside = true;
        }, function () {
            mouse_is_inside = false;
        });

        $("body").mouseup(function () {
            if (!mouse_is_inside) {
                autocompleteobj.find(".autocomplete_results").hide();
            }
        });


    }

});

// TODO: This is hard coded needs sorting.
function autocompleteredirectpage(url) {
    var siteroot = '/';
    path = siteroot + url;
    if (siteroot.substring(siteroot.length - 1) != '/')
        path = siteroot + '/' + url;
    window.location = path;
}


// this method uses a JSON call to GetTag_ResourcesAutoComplete and passes a search string and a count
// this will return a List class of autocompleteData.
function autocompletegetdata(searchtextbox, searchresults) {
    searchresults.empty();
    if (searchtextbox.val() != "") {
        searchresults.show();

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/models/CareersTagged_local.asmx/GetTag_ResourcesAutoComplete",
            data: '{ search: "' + searchtextbox.val() + '", count:10 }',
            dataType: "json",
            timeout: 4000,
            success: function (msg) {

                searchresults.empty();
                searchresults.append('<ul id="autocompletelist">')

                obj = $.parseJSON(msg.d);
                for (var i = 0; i < obj.length; i++) {
                    if (obj[i].itemurl != "")
                        $("#autocompletelist").append("<li>" + "<span>" + obj[i].tag + " (" + obj[i].itemcount + ")</span><a href='" + obj[i].itemurl + "' target='" + obj[i].target + "'>try this</a>" + "<div class='clear'></div></li>");
                    else
                        $("#autocompletelist").append("<li>" + "<span>" + obj[i].tag + " (" + obj[i].itemcount + ")</span>" + "<div class='clear'></div></li>");
                }

                searchresults.find("li").find('a').hide();
                searchresults.find("li").mouseenter(function () {
                    $(this).find('a').show();
                }).mouseleave(function () {
                    $(this).find('a').hide();
                });
                searchresults.find("li").find('span').click(function () {

                    var strinval = $(this).text().substring(0, $(this).text().lastIndexOf('(') - 1);

                    autocompleteredirectpage(autocompletetagurl + strinval);
                });

                searchresults.append('</ul>')

            },
            error: function (msg) {
                alert(msg.statusText);
            }
        });
    }
}


