var RecaptchaOptions = {
    theme : 'custom'
};
var MyNameisE = {
    Signup: {
        
    }
};
MyNameisE.Signup = {
    Picture: {
        init: function() {
            if($.browser.msie) {
                $(document).ready(function() {
                    MyNameisE.Signup.Picture.do_init();
                });
            }
            else {
                MyNameisE.Signup.Picture.do_init();
            }
        },
        do_init: function() {
            var buster = new Date().valueOf();

            new Ajax_upload('#upload_button', {
                action: '/settings/pictures.js?'+buster,
                name: 'picture',
                onSubmit: function(file, extension) {
                    if (! (extension && /^(jpg|jpeg)$/i.test(extension))){
                        alert('Only jpg\'s are allowed.');
                        return false;
                    }

                    $('#uploader').hide();
                    $('#uploading').show();

                    $('.btn-skip').addClass('inactive').attr('disabled', true);
                },
                onComplete: function(file, response) {
                    var response = eval('(' + response + ')');
                    if(response.result == 'error') {
                        alert(response.text);
                        $('#uploader').show();
                        $('#uploading').hide();
                    }
                    else if(response.result == 'ok'){
                        $('#avatars').append('<input type="hidden" name="picture_id" value="'+response.id+'">');
                        $('#avatars').append('<li><img src="'+response.small+'" /></li>');

                        //$('#uploader').show();
                        $('#uploading').hide();

                        $.get('/settings/pictures/' + response.id + '.js', function(response, test) {
                            if(response == 'ok') {
                                $('.btn-save_and_continue').show();
                                $('.btn-skip').hide();
                                $('#uploadfinished').show();
                            }
                        });
                    }
                    else {
                        alert('The upload failed. Please try again.');
                        $('#uploader').show();
                        $('#uploading').hide();
                    }
                }
            });
        }
    },
    Services: {
        remove: function(accountid, name) {
            if(confirm('Are you sure you want to remove '+name+'?')) {
                var url = '/signup/deleteservice/'+accountid+'.js';
                $.get(url, function(response) {
                    var response = eval('(' + response + ')');
                    if(response.result == 'error') {
                        alert(response.text);
                    }
                    else if(response.result == 'ok'){
                        // hide the right one
                        $('#service_'+response.id).hide();
                    }
                    else {
                        alert('The service could not be removed. Please try again.');
                    }
                });
            }
            return false;
        }
    }
};
$(document).ready(function() {
    $("#active_services li h3").tooltip({
        delay: 0,
        track: true,
        fade: 200,
        showURL: false
    });
    $("#active_services li a").tooltip({
        delay: 0,
        track: false,
        fade: 200,
        showURL: false
    });
    
    $(".info li a").tooltip({
        delay:0,
        showURL: false,
        fade: 200
    });
    
    $(".services li a[href!='#']").tooltip({
        delay:0,
        track: true,
        fade: 200
    });
    $(".services li a[href='#']").tooltip({
        delay:0,
        track: true,
        fade: 200,
        showURL: false
    });
    $("#info").tooltip({
        delay:500,
        showURL: false,
        fade: 200
    });
    if($("input.error").length) {
        $("input.error:first").focus();
    }
    else {
        if($("input[name='username']").length) {
            $("input[name='username']").focus();
        }
        else if($("input[name='firstname']").length) {
            $("input[name='firstname']").focus();
        }
        else if($("input[name='name']").length) {
            $("input[name='name']").focus();
        }
        else if($("input[name='credential_1']").length) {
            $("input[name='credential_1']").focus();
        }
        else if($("input[name='credential_2']").length) {
            $("input[name='credential_2']").focus();
        }
    }
    
    $("a[rel*='external'][href!='#']").attr('target', '_blank');
    $("a[rel*='me'][href!='#']").attr('target', '_blank');
});