var testMode=false;var verificationState={};function notificationUpdatePending(){Toggle.display('loading');}
function notificationUpdateReturned(request){testMode=false;Toggle.display('loading');hideAllIMTypes();$('test_link').style.display=(anyNotifications())?'':'none';var response=request.responseText.evalJSON();if(response.outcome=='errors'){var errorListHTML='';notificationTypes.each(function(type){var msg=response.errors[type];if(msg){$('notify_'+type+'_label').style.color='red';errorListHTML+='<li>'+msg+'</li>';}});$('notify_errors_list').innerHTML=errorListHTML;Effect.BlindDown('notify_errors');}
else{notificationTypes.each(function(type){$('notify_'+type+'_label').style.color=null;})
Effect.BlindUp('notify_errors',{afterEffect:function(){$('notify_errors').innerHTML='';}});var updatedTypes=response.updated_types;if(updatedTypes.length>0){if($F('notify_aim')&&updatedTypes.indexOf('aim')>=0){verificationState['aim']='test';}
if($F('notify_yahoo')&&updatedTypes.indexOf('yahoo')>=0){verificationState['yahoo']='test';}
if($F('notify_gtalk')&&updatedTypes.indexOf('gtalk')>=0){verificationState['gtalk']='auth';}
if($F('notify_msn')&&updatedTypes.indexOf('msn')>=0){verificationState['msn']='auth';}
syncDisplayToVerificationState();if(anyPendingVerifications()){showVerifyPanel();}
else{hideVerifyPanel();}}
else{hideVerifyPanel();}}}
function isIMBlocked(){return boxChecked('block_im');}
function isBroadcast(){return boxChecked('broadcast');}
function isNotify(){return boxChecked('notify');}
function setBlockIMPref(){new Ajax.Request('/set_enable_im_pref?enable_im='+!isIMBlocked(),{method:'get',onSuccess:testComplete});syncTestLinkDisplay();if(isIMBlocked()){Effect.BlindUp($('im_notification_fields'));}
else{Effect.BlindDown($('im_notification_fields'));}}
function syncTestLinkDisplay(){$('test_link').style.display=(anyNotifications()&&!isIMBlocked())?'':'none';}
function anyNotifications(){return $F('notify_aim')||$F('notify_yahoo')||$F('notify_gtalk')||$F('notify_msn');}
function anyPendingVerifications(){return notificationTypes.find(function(type){return verificationState[type]?true:false;})?true:false;}
function hideAllIMTypes(){document.getElementsByClassName('im_type','verify_im').each(function(e){e.hide();});}
function hideVerifyPanel(){if($('verify_im')){$('verify_im').hide();scroll(0,0);}}
function showVerifyPanel(){if($('verify_im')){$('verify_im').show();scroll(0,0);}}
function syncFieldsDisplay(){var el=$('IM_entry_fields');if(el){if(isNotify()||isBroadcast()){el.show();}
else{el.hide();}}}
function syncDisplayToVerificationState(){notificationTypes.each(function(type){syncDisplay(type,'auth');syncDisplay(type,'test');});}
function syncDisplay(type,step,suffix){setDisplay(type,step,verificationState[type]==step,suffix||null);}
function setDisplay(type,step,show,suffix){var id=step+'_'+type;var element=(suffix&&$(id+'_'+suffix))?$(id+'_'+suffix):$(id);if(element){element.style.display=show?'':'none';}}
function closeVerifyIM(){$('verify_im').hide();}
function testIM(imNetwork){new Ajax.Request('/test_im?im_network='+imNetwork,{method:'get',onSuccess:testComplete});}
function testComplete(request){var response=request.responseText.evalJSON();verificationState[response.im_network]=null;if(!testMode){syncDisplayToVerificationState();if(!anyPendingVerifications()){closeVerifyIM();}}}
function authIM(imNetwork){new Ajax.Request('/auth_im?im_network='+imNetwork,{method:'get',onSuccess:authComplete});}
function authComplete(request){var response=request.responseText.evalJSON();verificationState[response.im_network]='test';if(!testMode){syncDisplayToVerificationState();}}
function testNotifications(){hideAllIMTypes();testMode=true;notificationTypes.each(function(type){setDisplay(type,'auth',$F('notify_'+type),'tp');setDisplay(type,'test',$F('notify_'+type),'tp');});showVerifyPanel();}
function testAIM(){testIM('aim');}
function testYahoo(){testIM('yahoo');}
function authMSN(){authIM('msn');}
function testMSN(){testIM('msn');}
function authGTalk(){authIM('gtalk');}
function testGTalk(){testIM('gtalk');}
function setBroadcastPref(){var broadcast=isBroadcast();syncFieldsDisplay();oortle.sendMessage("set_broadcast_pref",{broadcast:broadcast});}
function handleBroadcastPrefSet(event){syncFieldsDisplay();if($('broadcast')){$('broadcast').checked=event.broadcast;}}
function setNotificationPref(){var notify=isNotify();syncFieldsDisplay();oortle.sendMessage("set_notification_pref",{notify:notify});}
function handleNotificationPrefSet(event){syncFieldsDisplay();if($('notify')){$('notify').checked=event.notify;}}
function hideFloatingPanel(){$('float_prompt_loading').hide();}
function showFloatingPanel(){$('float_prompt_loading').show();}
function floatingPanelEnableNotifications(){hideFloatingPanel();$('notify').checked=true;setNotificationPref();showSubscribe();}
function disableFloatingPanel(){hideFloatingPanel();$('notify').checked=false;setNotificationPref();}