function onYouTubeIframeAPIReady() {
    player = new YT.Player('filmDiv', {
        height: "588",
        width: "1000",
        videoId: filmRef,
        events: {
        "onReady": onPlayerReady,
        "onStateChange": onPlayerStateChange
        }
    });
}
function onPlayerReady(event) {
    //event.target.playVideo();
}

function onPlayerStateChange(event) {
    if (player.getPlayerState() == 0) {
        loadVoteWindow()
    }
}
function stopVideo() {
    player.stopVideo()
}
function rewatch() {
    player.playVideo();
    dhxWindow.window('w1').close();
}
function showRelatedFilms(id) {
    document.getElementById("filterResults").innerHTML = "<br><br><i class='fa fa-sync fa-spin fa-3x fa-fw'></i><span class='sr-only'>Loading...</span>";
    ajaxFunction("ajaxRelatedFilms.html?id="+id, "filterResults");
}
function showPopularFilms(id) {
    document.getElementById("filterResults").innerHTML = "<br><br><i class='fa fa-sync fa-spin fa-3x fa-fw'></i><span class='sr-only'>Loading...</span>";
    ajaxFunction("ajaxPopularFilms.html", "filterResults");
}
function filterFilms(pageNo) {
    document.getElementById("filmsList").innerHTML = "<br><br><i class='fa fa-sync fa-spin fa-3x fa-fw'></i><span class='sr-only'>Loading...</span>";
    var compYear = document.getElementById("compYear").value;
    var categoryID = document.getElementById("categoryID").value;
    var countryID = document.getElementById("countryID").value;
	console.log(countryID);
    ajaxFunction("ajaxFilterFilms.html?page="+pageNo+"&compYear="+compYear+"&categoryID="+categoryID+"&countryID="+countryID,"filmsList");
}
function ajaxFunction(theURL,theSpan) {
    var theTime = new Date().getMilliseconds();
    var xmlhttp;
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        alert("Your browser does not support XMLHTTP!");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            document.getElementById(theSpan).innerHTML = xmlhttp.responseText;
        }
    };
    xmlhttp.open("GET", theURL, true);
    xmlhttp.send(null);
} 
function showDisabilityFilms(theValue,page) {
    document.getElementById("filterResults").innerHTML = "<br><br><i class='fa fa-sync fa-spin fa-3x fa-fw'></i><span class='sr-only'>Loading...</span>";
    ajaxFunction("ajaxSearchFilms.html?q="+theValue+"&page="+page, "filterResults");
}
function showTaggedFilms(theValue,page) {
    document.getElementById("filterResults").innerHTML = "<br><br><i class='fa fa-sync fa-spin fa-3x fa-fw'></i><span class='sr-only'>Loading...</span>";
    ajaxFunction("ajaxSearchFilms.html?q="+theValue+"&page="+page, "filterResults");
}
var searchWait = 0;        
function searchFilms(theValue,page) {
    document.getElementById("filterResults").innerHTML = "<br><br><i class='fa fa-sync fa-spin fa-3x fa-fw'></i><span class='sr-only'>Loading...</span>";
    clearTimeout(searchWait);
    searchWait = setTimeout(function() {            
        if(theValue.length > 2) {
            ajaxFunction("ajaxSearchFilms.html?q="+theValue+"&page="+page, "filterResults");
        }
    }, 200);                        
}
$('#closeSearch').click(function () {
    // Hide the search input, close button and autocomplete list
    $('.search-container').hide();
    $('#autocompleteList').hide();

    // Show the search nav item
    $('#searchLink').show();
});

$('#searchLink').click(function () {
    // Hide the search nav item
    $('#searchLink').hide();

    // Show the search input, close button and autocomplete list
    $('.search-container').show();
    $('#autocompleteList').show();

    // Focus on the search input
    $('#searchInput').focus();
});

$(document).ready(function () {
    var debounceTimeout;
    var selectedIndex = -1;
    var $autocompleteList = $('#autocompleteList');
    var $searchInput = $('#searchInput');

    function navigateList(direction) {
        var $items = $autocompleteList.find('.list-group-item');
        if (!$items.length) return;

        $items.removeClass('active');
        selectedIndex += (direction === 'up') ? -1 : 1;
        selectedIndex = Math.max(0, Math.min(selectedIndex, $items.length - 1));

        var $activeItem = $items.eq(selectedIndex).addClass('active');
        ensureVisible($activeItem);
    }

    function ensureVisible($element) {
        var listTop = $autocompleteList.offset().top;
        var elementTop = $element.offset().top;
        var offset = elementTop - listTop;

        if (offset < 0 || offset > $autocompleteList.height()) {
            $element[0].scrollIntoView({ behavior: 'smooth', block: 'nearest' });
        }
    }

    function redirectToSelectedItem() {
        var $selectedItem = $autocompleteList.find('.list-group-item').eq(selectedIndex);
        if ($selectedItem.length) {
            window.location.href = $selectedItem.attr('href');
        }
    }

    function debouncedSearch(query) {
        clearTimeout(debounceTimeout);
        debounceTimeout = setTimeout(function () {
            if (query.length > 3) {
                $.get('ajaxSearcher.asp', { q: query }, processSearchResults);
            } else {
                $autocompleteList.empty();
            }
            selectedIndex = -1;
        }, 300);
    }

    // Global click listener to hide the autocomplete list
    $(document).click(function () {
        $('#autocompleteList').hide();
    });

    // Prevent click events inside the search input and autocomplete list from propagating
    $('#searchInput, #autocompleteList, #searchLink').click(function (event) {
        event.stopPropagation();
    });

    // Show the autocomplete list when focusing on the search input
    $('#searchInput').focus(function () {
        if ($('#searchInput').val().length > 0) {
            $('#autocompleteList').show();
        }
    });

    $searchInput.on('keyup', function (e) {
        var query = $(this).val();
        switch (e.keyCode) {
            case 38: // Up arrow key
            case 40: // Down arrow key
                e.preventDefault();
                navigateList(e.keyCode === 38 ? 'up' : 'down');
                break;
            case 13: // Enter key
                e.preventDefault();
                redirectToSelectedItem();
                break;
            default:
                debouncedSearch(query);
        }
    });

    function processSearchResults(data) {
        try {
            var searchResults = JSON.parse(data);
            $autocompleteList.empty();

            $.each(searchResults.hits, function (i, hit) {
                var $itemLink = $('<a>', {
                    href: "https://www.focusonability.com.au/FOA/films/" + hit.filmId + ".html",
                    class: 'list-group-item list-group-item-action d-flex align-items-center'
                });

                $('<img>', {
                    src: "https://img.youtube.com/vi/" + hit.videoFile + "/1.jpg",
                    class: 'mr-3',
                    width: '60',
                    height: '60'
                }).appendTo($itemLink);

                var $textContent = $('<div>').append(
                    $('<div>').text("'" + hit.title + "' by " + hit.author),
                    $('<div>', { class: 'text-muted small' }).text(hit.category + " (" + hit.compYear + ")")
                );

                $itemLink.append($textContent).appendTo($autocompleteList);
            });
        } catch (error) {
            console.error('Error processing search results:', error);
        }
    }
});
$(document).ready(function () {
    $('.dropdown').on('show.bs.dropdown', function (e) {
        $(this).find('.dropdown-menu').first().stop(true, true).slideDown(300);
    });

    $('.dropdown').on('hide.bs.dropdown', function (e) {
        $(this).find('.dropdown-menu').first().stop(true, true).slideUp(200);
    });
});
if ($('.carousel').length) {
    $('.carousel').carousel({
        interval: 2000
    });
}

$(function () {
    $('[data-toggle="tooltip"]').tooltip()
})