
smashion.page.ForumSubscription=Class.create(smashion.common.global.AjaxHandler,{initialize:function(element,serviceUrl,formUrl,topicId,options){this.element=$(element);this.serviceUrl=serviceUrl;this.formUrl=formUrl;this.topicId=topicId;this.isSubscribed=false;this.clicked=false;this.options=Object.extend({linkClass:"subscribetopic"},options||{});this.initElement();},initElement:function(){var req=new Ajax.Request(this.serviceUrl,{method:"get",parameters:{"action":"query","id":this.topicId},onSuccess:this.createControl.bind(this),onFailure:this.handleAjaxFailure.bind(this),onException:this.handleAjaxException.bind(this)});},createControl:function(transport,json){this.control=new Element("a",{href:"javascript:void(0)","class":this.options.linkClass});this.element.appendChild(this.control);if(this.isSuccess(json)){this.setSubscriptionState(json.body);Event.observe(this.control,'click',this.btnClick.bindAsEventListener(this));}else if(this.isNotLoggedIn(json)){this.control.innerHTML="Subscribe";this.control.onclick=smashion.common.global.login;}else{return;}
this.createStatus(this.element);},setSubscriptionState:function(subscribed){this.isSubscribed=subscribed;if(this.isSubscribed){this.control.innerHTML="Unsubscribe"}else{this.control.innerHTML="Subscribe"}},btnClick:function(event){if(!this.clicked){this.clicked=true;if(this.isSubscribed){this.unsubscribe(event);}else{this.subscribe(event);}}
if(event){Event.stop(event);}
return false;},subscribe:function(event){Dialog.confirm({url:this.formUrl,options:{onSuccess:this.checkLogin.bind(this)}},{className:"smashionpwc01",width:400,okLabel:"Submit",cancelLabel:"Cancel",onOk:this.submitForm.bind(this)});},checkLogin:function(transport,json){if(this.isNotLoggedIn(json)){smashion.common.global.login();}
this.clicked=false;},submitForm:function(win){$("forumSubForm").request({onSuccess:this.onFormSuccess.bind(this)});return false;},onFormSuccess:function(transport,json){Windows.focusedWindow.close();this.setSubscriptionState(true);},unsubscribe:function(event){var req=new Ajax.Request(this.serviceUrl,{method:"post",parameters:{"action":"remove","id":this.topicId},onSuccess:this.unsubscribeCallback.bind(this),onFailure:this.handleAjaxFailure.bind(this),onException:this.handleAjaxException.bind(this)});},unsubscribeCallback:function(transport,json){if(!this.validateResponse(json)){return false;}
this.setSubscriptionState(false);this.clicked=false;},handleNotLoggedIn:function(error){smashion.common.global.login();},dispose:function(){}})
smashion.page.ForumSubscriptionUpdate=Class.create(smashion.common.global.AjaxHandler,{initialize:function(element,formUrl,frequencyDisplay){this.element=$(element);this.formUrl=formUrl;this.frequencyDisplay=$(frequencyDisplay);this.clicked=false;this.newFreqText=this.frequencyDisplay.innerHTML;Event.observe(this.element,'click',this.btnClick.bindAsEventListener(this));},btnClick:function(event){if(!this.clicked){this.clicked=true;Dialog.confirm({url:this.formUrl,options:{onSuccess:this.checkLogin.bind(this)}},{className:"smashionpwc01",width:400,okLabel:"Submit",cancelLabel:"Cancel",onOk:this.submitForm.bind(this)});}
if(event){Event.stop(event);}
return false;},checkLogin:function(transport,json){if(this.isNotLoggedIn(json)){smashion.common.global.login();}
this.clicked=false;},submitForm:function(win){var freqValue=Form.getInputs("forumSubForm","radio","frequency").find(function(radio){return radio.checked;}).value;$("forumSubForm").request({onSuccess:this.onFormSuccess.bind(this)});if(freqValue=="D"){this.newFreqText="Once a day";}else{this.newFreqText="Every Post";}
return false;},onFormSuccess:function(transport,json){Windows.focusedWindow.close();this.frequencyDisplay.innerHTML=this.newFreqText;},handleNotLoggedIn:function(error){smashion.common.global.login();},dispose:function(){}})
