﻿function closeSearch() {
    $("#searchContainer").attr('class', 'displaynone');
    document.getElementById('loader').innerHTML = '<img src="' + PROJECT_URL + 'content/img/icon_search.gif" alt="" onclick="openSearch()" style="cursor:pointer;" />';
}

function openSearch() {
    $("#searchContainer").attr('class', 'displayblock');
    //$("#searchli").attr('class', 'searchactive');
    document.getElementById('loader').innerHTML = '<img src="' + PROJECT_URL + 'content/img/icon_close.gif" alt="" onclick="closeSearch()" style="cursor:pointer;" />'
}

function handleSearch() {
    $.get("" + PROJECT_URL + "xml/GetItemsBySearch/?s=" + $("#s").val(), {}, function(data) { $('#searchContainer').html(data); });
    document.getElementById('loader').innerHTML = '<img src="' + PROJECT_URL + 'content/img/icon_close.gif" alt="" onclick="closeSearch()" style="cursor:pointer;" />'
}

function cacheFix() {
    return "?t=" + new Date().getTime();
}

function clearText(thefield) {
    if (thefield.defaultValue == thefield.value)
        thefield.value = ""
}

$(document).ready(function() {
    var timeOutId;
    $('#s').keyup(function() {
        $("#searchContainer").attr('class', 'displayblock');
        if (timeOutId) {
            clearTimeout(timeOutId); // vorige zoekrequest van de agenda schrappen
        }
        if ($(this).val().length > 3) {
            document.getElementById('loader').innerHTML = '<img src="' + PROJECT_URL + 'content/img/icon_search_loader.gif" alt="""" />'
            $('#searchContainer').html('<li>Bezig met zoeken...</li>');
            timeOutId = setTimeout("handleSearch()", 500);
        } else {
            $('#searchContainer').html('<li>Vul een specifiekere zoek opdracht in.</li>');
            document.getElementById('loader').innerHTML = '<img src="' + PROJECT_URL + 'content/img/icon_close.gif" alt="" onclick="closeSearch()" style="cursor:pointer;" />';
        }
    });


    $('.listing .item').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }
	);
    $('.listing .item').click(function() {
        if ($(this).next().is(":visible")) {
            $('.listing .question').hide();
            $(this).removeClass('open');
        } else {
            $('.listing .question').hide();
            $('.listing .item').removeClass('open');
            $(this).addClass('open');
            $(this).next().toggle();
        }
    });

    $('.cat_title').click(function() {
        if ($(this).next().is(":visible")) {
            $('.listing').hide();
            $(this).removeClass('open');
        } else {
            $('.listing').hide();
            $('.listing .question').hide();
            $('.listing .item').removeClass('open');

            $('.cat_title.open').removeClass('open');
            $(this).addClass('open');
            $(this).next().toggle();
        }
    });


});
