var Settings = { lastfm_login_url: null, getAccountInfo: function(callback) { Session.query('/user/update', {}, callback); }, saveAccountInfo: function(params, callback) { Session.query('/user/update', params, callback); }, loadFormData: function(form) { if (form == 'account') { this.getAccountInfo(function(data){ $('.settings-container .form input[name$="username"]').val(data.username); $('.settings-container .form input[name$="email"]').val(data.email); }) } } } $('.settings').live('click', function() { Ajax.loadSettingsPage(); }); $('.settings-container .tabs .tab').live('click', function(){ if (!$(this).hasClass('active')) { $('.settings-container .tabs .tab').removeClass('active'); $(this).addClass('active'); $('.form-container').html($('.forms .'+ $(this).attr('data-fieldset')).html()); Settings.loadFormData($(this).attr('data-fieldset')); } if ($(this).attr('data-fieldset') == 'lastfm') { Session.query('/lastfm/getinfo', {}, function(data){ if (data.connected) { $('.form-container input[name$="username"]').first().val(data.username); } else { Settings.lastfm_login_url = data.lastfm_login_url; } }) } }); $('.lastfm-connect').live('click', function(){ window.open(Settings.lastfm_login_url); }); $('.settings-container .form input').live('blur', function(){ var active_tab = $('.settings-container .tabs .tab.active').attr('data-fieldset'); if (active_tab == 'account') { params = { 'username': $('.settings-container .form input[name$="username"]').first().val(), 'email': $('.settings-container .form input[name$="email"]').first().val(), }; Settings.saveAccountInfo(params, function(){ $('#header-container .hello .greating').text('Hi there, '+ (params.username.length > 0 ? params.username : '%username%') +'!'); }); } })