// Check if current string ends with the given sequence
String.prototype.endsWith = function(suffix) {
        return this.indexOf(suffix, this.length - suffix.length) !== -1;
};

$(document).ready(function () {
    // disable auto completion of id_tax field
    $('#id_taxon').attr('autocomplete', 'off');
    $('#id_goterm').attr('autocomplete', 'off').css('width', '320px');
    // react on click but also make sure that when page is loaded the level2 is hidden or shown according to selection
    $('.level2').prev().children('input').click(checkbox_status).map(checkbox_status);
    // hide 'This paper is present' in submit paper template
    $("#id_present_text").hide();
    
    // since levels are defined at the input level (in 
    // the form), up-cascade the class definitions
    // to the nearest 'li', to allow css magic
    $(":input.level1").parent('li').addClass('level1')
    $(":input.level2").parent('li').addClass('level2')
    $(":input.level3").parent('li').addClass('level3')
    $(":input.level3_short").parent('li').addClass('level3_short')
    $(":input.level3_medium").parent('li').addClass('level3_medium')
    if ($("#id_wrong_password").val() == 'True') {
        alert("Wrong username/password.\nPlease try again.");
    }
    
    if ($("#id_registered").val() == 'True') {
        alert("Thank you for registering.");
    }
    // This open extra options in the side menu if the user is
    // on their userpage
    var current_path = window.location.pathname;
    if (current_path.endsWith('annotatorpages/userpage/')){
        $("#id_my_annotations_sidebar").show('slow');
    } else {
        $("#id_my_annotations_sidebar").hide();
    }

    // This flag is on the 'noscript.html' template, and
    // if javacscript is enable, redirct the user to
    // the value of the hidden field (the last page they were on)
    var url = $("#id_javascript_flag_redirect").val();
    $(location).attr('href',url);
});

$("a.new_window").click(function(){
        window.open(this.href);
        return false;
    });

function checkbox_status () {
    if ($(this).attr('checked')) {
        $(this).parent().next('.level2').show()
    } else {
        $(this).parent().next('.level2').hide()
    }
};



// custom ajax written to make marcs life just a little less complicated,
// might want to delete, may monica needs it, i don't know...

$("#id_old_picture_annotation_id").change( function() {
        var input = $("#id_old_picture_annotation_id").val();
        $.get(
            url = 'ajax_annotation_values/'+input,
            data = {},
            callback = function(output) {
                    html = '<option value ="">ready</option>';
                    $.each(output, function(i,o){
                        html += '<option value ="'+o+'">'+o+'</option>';
                        });
                        $("select#test").html(html);
                    },
                type = 'json' // declare reply as json
            );
    });



$('#id_goterm').keyup(
    $.debounce(500, function () {
        var menu = $('#go_selection').hide();
        var input = $(this);
        var inputcontent = input.val().split(',').pop();
        if (inputcontent.length >= 2) {
            menu.empty();
            $.get(
                url = 'ajax_get_go/' + inputcontent,
                data = {}, // submit no data
                callback = function (answer) {
                    if (answer.length >= 1) {
                        var html = '';
                        for (var i=0; i<answer.length; i++) {
                            html += "<div class='go_sub_selection' name='" + answer[i][0] + "'><nobr><span>";
                            html += answer[i][0] + " - " + answer[i][1] + "</span></nobr></div>";
                            }
                        menu.append(html);
                        menu.css('left', input.position().left);
                        menu.css('top', input.position().top + input.height() + 10);

                        // enable on click reaction
                        $('.go_sub_selection').click(function () {
                            var inputfinalcontent = input.val().split(',');
                            // get rid of last input
                            inputfinalcontent.pop();
                            // add GO term
                            inputfinalcontent.push($(this).attr('name') + ',');
                            // replace in form field with the new data
                            input.val(inputfinalcontent);
                            menu.hide();
                        });

                        menu.show();
                    } else {
                        menu.hide();
                    };

                }, type = 'json' // declare reply as json
            );
        };
    })
);

