﻿uhri.search = {
    bodyTypes: [],
    bodyOptions: [],
    keyword:null,
    init: function () {
        uhri.search.loadBodyTypes();
        uhri.search.setKeyword();
        if ($('.mechanism-basic-search').length > 0) {
            $('.mechanism-basic-search').show();
            $('.body-basic-search').hide();
            $('.mechanism-basic-search select').change(function () {
                uhri.search.setBodyType();
            });
        } else {
            $('#BodyTypes input[type=checkbox]').click(uhri.search.bodyTypeOnClick);
            uhri.search.loadRegions();
        }
        
    },
    loadBodyTypes: function () {
        $.ajax({
            method: 'GET',
            url: '/search/bodytypes',
            success: function (result) {
                uhri.search.bodyTypes = result;
            },
            async: true
        });
    },

    setKeyword: function () {
        $('#Keyword').val($('#Keyword').attr('title'));
        $('#Keyword').focus(function () {
            if ($('#Keyword').val() == $('#Keyword').attr('title')) {
                $('#Keyword').val('');
            }
        });

        $('#Keyword').blur(function () {
            if ($('#Keyword').val() == '') {
                $('#Keyword').val($('#Keyword').attr('title'));
            }
        });
    },
    
    setBodyType: function () {
        if (uhri.search.bodyTypes.length > 0) {
            var selected = $('.mechanism-basic-search select').val();
            if (selected == uhri.emptyGuid) {
                $('.body-basic-search').hide('blind');
            } else {
                var bodyList = $('#Bodies');
                for (var j = 0; j < uhri.search.bodyTypes.length; j++) {
                    var bt = uhri.search.bodyTypes[j];
                    if (bt.Id == selected) {
                        for (var k = 1; k < $('#Bodies').children().length; k++) {
                            if ($.inArray($($('#Bodies').children()[k]).attr('value'), bt.Bodies) > -1) {
                                $($('#Bodies').children()[k]).show().attr('disabled', false);
                            } else {
                                $($('#Bodies').children()[k]).hide().attr('disabled', 'disabled');
                            }
                        }
                        break;
                    }
                }
                $('.body-basic-search').show('blind');
            }
        }
    },
    bodyTypeOnClick: function () {
        $('#Bodies li').addClass('disabled');
        $('#Bodies input[type=checkbox]').attr('disabled', 'disabled');
        var bodyTypeSelected = false;
        $('#BodyTypes input[type=checkbox]').each(function (i, e) {
            if (e.checked) {
                for (var i in uhri.search.bodyTypes) {
                    if (uhri.search.bodyTypes[i].Id == $(e).val()) {
                        for (var j in uhri.search.bodyTypes[i].Bodies) {
                            var chk = $('#Bodies input[value=' + uhri.search.bodyTypes[i].Bodies[j] + ']');
                            chk.attr('disabled', false);
                            chk.parent().removeClass('disabled');
                        }
                    }
                }
                bodyTypeSelected = true;
            }
        });
        if (!bodyTypeSelected) {
            $('#Bodies li').removeClass('disabled');
            $('#Bodies input[type=checkbox]').attr('disabled', false);
        }
        $('#Bodies input:disabled').each(function (i, e) {
            $(e).attr('checked', false);
            uhri.criteria.addRemove($(e).parents('.treeview').attr('data-type'), e);
        });
    },
    loadRegions: function () {
        $.ajax({
            method: 'GET',
            url: '/search/regions',
            success: function (result) {
                uhri.search.regions = result;
                $('#Regions input[type=checkbox]').click(uhri.search.regionOnClick);
            },
            async: true
        });
    },
    regionOnClick: function () {
        $('#Countries li').addClass('disabled');
        $('#Countries input[type=checkbox]').attr('disabled', 'disabled');
        var regionSelected = false;
        $('#Regions input[type=checkbox]').each(function (i, e) {
            if (e.checked) {
                for (var i in uhri.search.regions) {
                    if (uhri.search.regions[i].Id == $(e).val()) {
                        for (var j in uhri.search.regions[i].Countries) {
                            var chk = $('#Countries input[value=' + uhri.search.regions[i].Countries[j] + ']');
                            chk.attr('disabled', false);
                            chk.parent().removeClass('disabled');
                        }
                    }
                }
                regionSelected = true;
            }
        });
        if (!regionSelected) {
            $('#Countries li').removeClass('disabled');
            $('#Countries input[type=checkbox]').attr('disabled', false);
        }
        $('#Countries input:disabled').each(function (i, e) {
            $(e).attr('checked', false);
            uhri.criteria.addRemove($(e).parents('.treeview').attr('data-type'), e);
        });
    }
}

$(document).ready(function () {
	uhri.search.init();
});
