$(document).ready(function(){
    initQuestionnaire();
    initQuestionDetailsToggle();

    // hide submit buttons for university search (form submitted by pressing "enter" or select)
    $('form.ranking span.button').hide();
    $('div.jumpto span.button').hide();
    $('div.jumpto select').change(function(){ // submit jumpto-form when select item changed
        $(this).parent().parent().get(0).submit();
    });

    if ($.fn.autocomplete) { // check if Autocomplete script available
        $("div.jumpto input[@type=text]").autocomplete("search.txt", {
            minChars:3,
            matchSubset:1,
            matchContains:1,
            cacheLength:10,
            formatItem:formatItem,
            selectOnly:1
        });
    }
});

/*
 * Sets up star-rating and also mouseover highlighting.
 */
function initQuestionnaire() {
    if ($.fn.rating) { // check if Rating script available
        $('form .shoplink .yourrating .rating').rating();
    }
}

/*
 *  Adds listener to to Comment links, which will
 *  toggle the textarea for the comments.
 */
function initQuestionDetailsToggle() {
    $('#layer form .shoplink ol ul').hide();
    // only in some countries we need this procedere. so check if a special-hacky-div exists which will rendered if we are in one of these countries
    if (document.getElementById("showShopInteraction")) {
        $('#layer form .shoplink ol h4 a').attr("title", "Details anzeigen");
        $('#layer form .shoplink ol h4 img').click( function() {
            var $detailsElement = $('ul', $(this).parent().parent());
            if($detailsElement.css("display") == "block") {
                $detailsElement.hide();
                $detailsElement.parent().removeClass("opened");
                $('#shoplinkHead').css("display", "inline");
                document.getElementById("close").style.display = "none";
                document.getElementById("shoplink").style.backgroundColor = "#fff";
                document.getElementById("shoplink").style.position = "relative";
                $('#center .pltMultilink').attr("style", "overflow: hidden;");
                $('#center .columnWrap  .simple').attr("style", "overflow: hidden;");
           }
            return false;
         });
        $('#layer form .shoplink ol h4 a').click( function() {
            var $detailsElement = $('ul', $(this).parent().parent());
            if($detailsElement.css("display") != "block") {
                $detailsElement.show();
                $detailsElement.parent().addClass("opened");
                document.getElementById("close").style.display = "block";
                $('#shoplinkHead').css("display", "none");
                document.getElementById("shoplink").style.backgroundColor = "#eaeaea";
                document.getElementById("shoplink").style.position = "absolute";
                $('#center .pltMultilink').attr("style", "overflow: visible;");
                $('#center .columnWrap  .simple').attr("style", "overflow: visible;");
            }
            return false;
        });
    }
}