/*
 * Discussion Application scripts 0.1
 * Copyright(c) 2008, Conduit IT.
 * 
 */


var DiscussionApplication=Ext.extend(Ext.Panel,{layout:"fit",page_limit:10,thread_id:0,user_email:"",user_name:"",disFormWin:null,autoScroll:true,images_dir:null,initComponent:function(){this.store=new Ext.data.JsonStore({autoLoad:true,url:"json.php?target=discussion&method=getDiscussionPosts",root:"results",totalProperty:"total",pruneModifiedRecords:true,fields:["date_added","name","rating_value","message"],baseParams:{thread_id:this.thread_id,sort_dir:"DESC",limit:this.page_limit}});var _1=new Ext.XTemplate("<div class=\"citDiscussionResults\">","<div class=\"items\">","<tpl for=\".\">","<div class=\"itemBox\">","<h3>"+this.lang.label.reviewByOn.replace("[1]","{name}").replace("[2]","{date_added}")+"</h3>","<p>"+this.lang.label.rating+": {rating_value}</p>","<p>{message}</p>","</div>","</tpl>","</div>","</div>");var _2=new Ext.DataView({store:this.store,tpl:_1,autoWidth:true,autoHeight:true,loadingText:this.lang.mask.loading,itemSelector:"div.itemBox",emptyText:"<div class=\"citDiscussionResults\"><p>"+this.lang.message.beTheFirst+"</p></div>"});this.items=[_2];this.bbar=new Ext.PagingToolbar({store:this.store,pageSize:this.page_limit,displayInfo:true,emptyMsg:this.lang.message.noReviews});var _3=new Ext.data.JsonStore({data:[{value:"DESC",label:this.lang.label.newestToOldest},{value:"ASC",label:this.lang.label.oldestToNewest}],fields:["value","label"]});this.sortCombo=new Ext.form.ComboBox({fieldLabel:"",store:_3,autoWidth:true,mode:"local",displayField:"label",valueField:"value",value:"DESC",selectOnFocus:true,editable:false,triggerAction:"all"});this.newButton=new Ext.Button({text:this.lang.button.newReview,handler:this.showFormWin,scope:this,cls:"b2c-btn"});this.tbar=["<span id=\"diss_avg_rate\"></span>",this.lang.label.sortBy+": ",this.sortCombo,new Ext.Toolbar.Fill(),this.newButton];DiscussionApplication.superclass.initComponent.call(this);},initEvents:function(){this.sortCombo.on("select",this.changeResultOrder,this);this.store.on("load",this.getAverageRating,this);DiscussionApplication.superclass.initEvents.call(this);},destroy:function(){try{this.store=null;this.sortCombo=null;}
catch(e){}
DiscussionApplication.superclass.destroy.call(this);},showFormWin:function(){if(!this.disFormWin){this.disFormWin=new DiscussionFormWin({user_email:this.user_email,user_name:this.user_name,thread_id:this.thread_id,animateTarget:this.newButton.getEl(),title:this.lang.title.writeAReview,lang:this.lang.DiscussionFormWin});}
this.disFormWin.show();this.disFormWin.on("refreshNeeded",this.store.load,this.store);},changeResultOrder:function(_4){this.store.baseParams["sort_dir"]=_4.getValue();this.store.load();},getAverageRating:function(){try{var _5=this.store.reader.jsonData;var _6=Ext.get("diss_avg_rate");if(_6){_6.update(this.genStarHTML(_5["rating_parts"]));}}
catch(e){}},genStarHTML:function(_7){if(_7&&this.images_dir){var s=this.lang.label.averageRating+": ";var i;for(i=0;i<_7["full"];i++){s+="<img src=\""+this.images_dir+"/star_full.gif\" alt=\"*\" />";}
if(_7["part"]){s+="<img src=\""+this.images_dir+"/star_"+_7["part"]+".gif\" alt=\"X\" />";}
for(i=0;i<_7["empty"];i++){s+="<img src=\""+this.images_dir+"/star_empty.gif\" alt=\"0\" />";}
return s+"&nbsp;&nbsp;";}
return"";}});
var DiscussionFormWin=Ext.extend(Ext.Window,{layout:"fit",closable:true,shim:false,bodyStyle:"padding:1em; margin:0; text-align:left;background-color:#FFF",buttonAlign:"center",width:400,user_email:"",user_name:"",thread_id:0,animateTarget:null,animCollapse:true,closeAction:"hide",border:false,bodyBorder:false,shadow:false,listeners:{beforedestroy:function(w){Ext.destroy(w.mask);}},initComponent:function(){this.closeButton=new Ext.Button({text:this.lang.button.cancel});this.sendButton=new Ext.Button({text:this.lang.button.submit});var _2=new Ext.data.JsonStore({data:[{value:"5",label:this.lang.label.excellent},{value:"4",label:this.lang.label.good},{value:"3",label:this.lang.label.metExpectations},{value:"2",label:this.lang.label.smallProblems},{value:"1",label:this.lang.label.disappointed}],fields:["value","label"]});this.formPanel=new Ext.FormPanel({autoHeight:true,border:false,bodyBorder:false,labelWidth:150,labelAlign:"right",items:[{xtype:"hidden",name:"post_data[thread_id]",value:this.thread_id},{xtype:"textfield",name:"post_data[name]",value:this.user_name,allowBlank:false,fieldLabel:this.lang.label.yourName,anchor:"90%"},{xtype:"textfield",name:"email_from",vtype:"email",value:this.user_email,allowBlank:false,fieldLabel:this.lang.label.yourEmail,anchor:"90%"},{xtype:"combo",name:"combo_rating",mode:"local",store:_2,value:"5",allowBlank:false,fieldLabel:this.lang.label.productRating,anchor:"90%",valueField:"value",displayField:"label",selectOnFocus:true,editable:false,triggerAction:"all",hiddenName:"post_data[rating_value]"},{xtype:"textarea",name:"post_data[message]",value:"",allowBlank:false,fieldLabel:this.lang.label.review,anchor:"90%"}]});this.items=[this.formPanel];this.buttons=[this.closeButton,this.sendButton];DiscussionFormWin.superclass.initComponent.call(this);},initEvents:function(){this.closeButton.on("click",this.prepareToHide,this);this.sendButton.on("click",this.submitForm,this);DiscussionFormWin.superclass.initEvents.call(this);},destroy:function(){try{this.formPanel=null;this.closeButton=null;this.sendButton=null;}
catch(e){}
DiscussionFormWin.superclass.destroy.call(this);},prepareToHide:function(){var _3=new Ext.util.DelayedTask({});_3.delay(100,this.hide,this);},submitForm:function(){var _4=this.formPanel.getForm();if(_4.isValid()){_4.waitMsgTarget=true;_4.submit({url:"json.php?target=discussion&method=submitDiscussionPost",params:{},clientValidation:false,scope:this,success:this.showThankYou,failure:this.showErrorMsg,discardUrl:false,nocache:false,waitMsg:this.lang.mask.submitting,timeout:45,scripts:false});}},showThankYou:function(_5,_6){var _7;if(_6.result&&_6.result.pending_msg){_7=_6.result.pending_msg;}else{_7=this.lang.message.reviewSubmitted;}
Ext.MessageBox.show({title:this.lang.title.thankYou,msg:_7,buttons:Ext.MessageBox.OK,animEl:this.animateTarget,modal:false,width:200});if(_6.result&&_6.result.refresh_needed){this.fireEvent("refreshNeeded",_6.result.refresh_needed);}
this.formPanel.getForm().reset();this.prepareToHide();},showErrorMsg:function(_8,_9){var _a;if(_9.result&&_9.result.error_msg){_a=_9.result.error_msg;}else{_a=this.lang.label.message.errorOccured;}
Ext.MessageBox.show({title:this.lang.title.error,msg:_a,buttons:Ext.MessageBox.OK,animEl:this.animateTarget,modal:false,width:200});this.formPanel.getForm().reset();this.prepareToHide();}});
