/**
 * sets up onchange events for search drop down select box
 * and autocomplete functions for each section
 */
function setUpQuickSearchAutoComplete() {
    var AREAS = {
        VESSELS: "/llint/ajax/vessels.htm",
        COMPANIES: "/llint/ajax/companies.htm",
        REPORTS: "/llint/ajax/reports.htm",
        PLACES: "/llint/ajax/places.htm",
        CASUALTIES: "/llint/ajax/casualties.htm",
        ARTICLES: "/llint/ajax/articles.htm"
    };

    $(".searchBox").keydown(
            function() {
                getAutoComplete($(this).attr("id"));
    });

    // for seasearcher panel on lli homepage
    $(".sSsearchBox").keydown(
                    function() {
                        getSsAutoComplete($(this).attr("id"));
    });

    $("#searchDropDown").change(
            function() {
                var currentText = $(".searchBox").val();
                var section = $("#searchDropDown").val();
                $(".searchBox").hide();
                $("#"+section).show();
                $("#"+section).val(currentText);
            });

    function getAutoComplete(section) {
       switch (section)
                {
            case "vessels":
                $("#vessels").autocomplete(AREAS.VESSELS, {minChars:3, width:496, scroll:false, setMax:15, searchName:"vessels", selectOnly:true, formatItem:formatVesselItem,onFindValue:findValue}).result(submitSpecifiedItem);
                break;
            case "companies":
                $("#companies").autocomplete(AREAS.COMPANIES, {minChars:3, width:496, scroll:false, setMax:15, searchName:"companies", selectOnly:true, formatItem:formatCompanyItem}).result(submitSpecifiedItem);
                break;
            case "reports":
                $("#reports").autocomplete(AREAS.REPORTS, {minChars:3, width:496, scroll:false, setMax:15, searchName:"reports", selectOnly:true, formatItem:formatReportItem}).result(submitSpecifiedItem);
                break;
            case "places":
                $("#places").autocomplete(AREAS.PLACES, {minChars:3, width:496, scroll:false, setMax:15, searchName:"places", selectOnly:true, formatItem:formatPlaceItem}).result(submitSpecifiedItem);
                break;
            case "casualties":
                $("#casualties").autocomplete(AREAS.CASUALTIES, {minChars:3, width:496, scroll:false, setMax:15, searchName:"casualties", selectOnly:true, formatItem:formatCasualtyItem}).result(submitSpecifiedItem);
                break;
        }
    }

    function getSsAutoComplete(section) {
           switch (section)
                {
                case "sSvessels":
                    $("#sSvessels").autocomplete(AREAS.VESSELS, {minChars:3,width:300,selectOnly:true,formatItem: formatVesselItem}).result(submitSeaSearcherItem);
                    break;
                case "sScompanies":
                    $("#sScompanies").autocomplete(AREAS.COMPANIES, {minChars:3,width:300,selectOnly:true,formatItem: formatCompanyItem}).result(submitSeaSearcherItem);
                    break;
                case "sSplaces":
                    $("#sSplaces").autocomplete(AREAS.PLACES, {minChars:3,width:300,selectOnly:true,formatItem: formatPlaceItem}).result(submitSeaSearcherItem);
                    break;
                }
    }

    function findValue() {
        alert("found");
    }
    
    function formatCompanyItem(row) {
        var companyName = row[0];
        //var companyId = row[1]; // we could display this for the user...
        var countryName = row[2];
        var countryCode = row[3];
        var sep = " - ";
        return "<span style='font-size: 12px'>" + companyName + sep + createCountrySpanTag(countryCode, countryName) + "</span>";
    }

    function formatReportItem(row) {
        var reportName = row[0];
        var reportId = row[1];
        var reportCountry = row[2];
        var reportDate = row[3];
        var sep = " - ";
        return "<span style='font-size: 12px'>" + reportName + sep + reportCountry + sep + "(" + reportDate + ")" + "</span>";
    }

    function formatPlaceItem(row) {
        var placeName = row[0];
        //var placeId = row[1]; // we could display this for the user...
        var countryCode = row[2];
        var countryName = row[3];
        //var flagCode = row[4]; // needs adding to the places autonomy index?
        var timeZone = row[5];
        var uncTad = row[6]; // todo -rdc - temporarily using this for the flagCode as well...
        var sep = " - ";
        return "<span style='font-size: 12px'>" + placeName + sep + countryCode + sep + createCountrySpanTag(uncTad.substring(0, 2).toLowerCase(), countryName) + sep + formatTimeZone(timeZone) + sep + uncTad + "</span>";
    }

    function formatVesselItem(row) {
        var vesselName = row[0];
        var vesselId = row[1];
        var flagCode = row[2];
        var vesselStatus = row[3];
        var vesselType = row[4];
        var countryName = row[5];
        var vesselImo = row[6];
        var sep = " - ";
        return  "<span style='font-size: 12px'>" + vesselName + sep + createCountrySpanTag(flagCode, countryName) + sep + vesselImo + sep + vesselType + sep + vesselStatus + "</span>";
    }

    function formatCasualtyItem(row) {
        var vesselName = row[0];
        var casualtyArea = row[1];
        var vesselId = row[2];
        var sep = " - ";
        return "<span style='font-size: 11px'>" + vesselName + sep + vesselId + sep + casualtyArea + "</span>";
    }

    function formatTimeZone(timeZone) {
        var GMT = "GMT";
        if (timeZone != null && (timeZone.indexOf("null") == -1)) {
            var timeZoneFormatted = timeZone.substring(2);
            if (timeZone.indexOf("W")) {
                return GMT + " -" + timeZoneFormatted + "H";
            } else {
                return GMT + " +" + timeZoneFormatted + "H";
            }
        }
        return GMT;
    }

    function createCountrySpanTag(flagCode, countryName) {
        return "<span class='ac_flag' style='background-image: url(/llint/images/flags/" + flagCode + ".png)'>" + countryName + "</span>"
    }

    function submitSpecifiedItem(e, row) {
        //alert($("div.ac_results").html());
        var itemId = row[1];
        $("#searchId").val(itemId);
        // submit the form
        $("#quickSearch").submit();
    }

   function submitSeaSearcherItem(e, row) {
        var itemId = row[1];
        $("#"+$(this).attr("id")+"Id").val(itemId);
        // submit the form
        $("#"+$(this).attr("id")+"Form").submit();
    }

        /* function submitSpecifiedItem(e, row) {
        var itemId = row[1];
        $("#searchId").val(itemId);
        // submit the form
        $("#quickSearch").submit();
    }

        function submitSpecifiedItem(e, row) {
        var itemId = row[1];
        $("#searchId").val(itemId);
        // submit the form
        $("#quickSearch").submit();
    }*/

}

addLoadEvent(setUpQuickSearchAutoComplete);