function randomString(string_length) {
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    return randomstring;
}

$(function()
{
    /*
     * Clear a input on focus
     */
    $('.clearOnFocus').focus(function() {
        if($(this).val() == $(this).attr('title')) {
            $(this).val('');
        }
    });

    $('.clearOnFocus').blur(function() {
        if($(this).val() == '') {
            $(this).val($(this).attr('title'));
        }
    });

    /*
     * Change a bottom logos opacity on hover
     */
    $('#logos-bottom img').css({
        opacity: '0.35'
    });

    $('#logos-bottom img').hover(
        function() {
            $(this).animate({
                opacity: '1.0'
            }, 'fast');
        },
        function() {
            $(this).animate({
                opacity: '0.35'
            }, 'fast');
        }
    );

    /*
     * Clone a last input
     */
    $('.clone-last-input').click(function() {
        var input = $(this).prev('input');
        var cloned = input.clone();
        cloned.insertAfter(input).attr('value', '');
    });

    /*
     * Announcement gallery
     */
    /*
    $('.gallery-box .thumbs img').click(function() {
        
        // Get image id
        var image_id = $(this).attr('class').split('-', 2)[1];

        // Replace the medium photo
        $('.gallery-box a.preview img').attr('src', 'media/img/temp/announcement_photos/'+image_id+'/medium.jpg');

        // Replace link to the large photo
        $('.gallery-box a.preview').attr('href', 'media/img/temp/announcement_photos/'+image_id+'/large.jpg')

        return false;

    });
    */
    /*
     * prettyPhoto
     */
    if ($("a[rel^='prettyPhoto']").length) {
        $("a[rel^='prettyPhoto']").prettyPhoto({
            showTitle: false,
            theme: 'facebook'
        });
    }
    
    /*
     * Generate random password
     */
    $('.generate-password').click(function() {
        var password = randomString(10);
        $(this).parentsUntil('form').find('input[type=password]').attr('value', password)
        $('.generated-password').show();
        $('.generated-password span').html(password);
    })
});
