
// think i'll try to stick as much site-wide js code here

$(document).ready( function() {
    // filter by tags (display more and hide)
    $('#show-more-tags').click( function(event) {
        event.preventDefault(); // prevents annoying scroll up to the top on click event
        $('.more-tags').toggle('400');
        $('#show-more-tags span').toggle();
    });

    // search bar
    $('.search input.text').click( function() {
        $(this).val('');
        $(this).css({'color': '#444', 'font-weight': 'bold'});
    });

    $("input.numeric").numeric();
});

function isUrl(s) {
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(s);
}

function bindAutoComplete(elements, options){
    all_options = $.extend({
       width: 150,
       max: 10,
       highlight: false,
       multiple: true,
       multipleSeparator: ", ",
       scroll: true,
       scrollHeight: 300,
       autoFill: true
    }, options);
    $(elements).autocomplete('/tags/autocomplete/', all_options);
}

function generateOptions(objs, selectedValue) {
    if (!objs || objs.length == 0)
        return '<option value="" selected="selected">---------</option>';

    options = ''
    $.each(objs, function(index, obj) {
        options += '<option value="' + obj.pk + '"';
        if (selectedValue && selectedValue == String(obj.pk)) {
            options += ' selected';
        }
        options += '>' + obj.fields.name + '</option>';
    });
    return options;
}

jQuery.fn.hint = function(blurClass, form){
    if (!blurClass) {
        blurClass = 'hint';
    }

    $(this).each(function(){
        var input = $(this);
        var title = $(this).attr('title');

        if (title) {
            // on blur, set value to title attr if text is blank
            input.blur(function(){
                if (input.val() === '') {
                    input.val(title).addClass(blurClass);
                }
            }).focus(remove).blur(); // now change all inputs to title
            // clear the pre-defined text when form is submitted
        }
    });

    form.submit(function(){
        $(this).find('input[title!=""]').each(remove);
    });

    function remove(){
        if ($(this).val() === $(this).attr('title') && $(this).hasClass(blurClass)) {
            $(this).val('').removeClass(blurClass);
        }
    }
};
