CNB.Toolbar=new Class({Implements:Options,options:{cid:null,serviceCid:null,title:'',url:'',shortUrl:'',serviceUrl:null,contentType:null,summary:'',hasVoted:false,twitterRelated:'zdnet,techrepublic',classes:{shareBtn:'t-share',shareContainer:'t-share-overlay',voteOptions:'t-vote-options',voteContainer:'t-vote',commentContainer:'t-comments'}},initialize:function(container,options){this.setOptions(options);this.toolbar=$(container);if(this.toolbar===null){return false}this.voteHistory=Cookie.read('service');this.hasVoted=this.options.hasVoted;this.cls=this.options.classes;this.title=encodeURIComponent(this.options.title);this.url=encodeURIComponent(this.options.url);this.shortUrl=encodeURIComponent(this.options.shortUrl);this.summary=encodeURIComponent(this.options.summary)},load:function(){this.commentContainer=this.toolbar.getElement('.'+this.cls.commentContainer);this.voteContainer=this.toolbar.getElement('.'+this.cls.voteContainer);window.addEvent('domready',this.requestData.bind(this));this.commentContainer.addEvent('click',this.handleCommentScroll);if(!this.options.hasVoted){this.initVoting()}this.initShare()},initShare:function(){var btn=this.toolbar.getElement('.'+this.cls.shareBtn);var cont=this.toolbar.getElement('.'+this.cls.shareContainer);var pop=new CNB.Pop(btn,cont).load();this.toolbar.getElements('a[shareaction]').each(function(el){el.addEvent('click',this.handleShareAction.bindWithEvent(this,el))},this);this.toolbar.getElements('a[toolaction]').each(function(el){el.addEvent('click',this.handleToolAction.bindWithEvent(this,el))},this)},initVoting:function(){this.voteOptions=this.toolbar.getElement('.'+this.cls.voteOptions);if(!this.voteOptions){return false}this.voteLoader=new CNB.Loading(this.voteOptions,{'float':'left'});this.voteOptions.getElements('a[voteaction]').each(function(el){var action=el.getProperty('voteaction');var voteValue,regSource;switch(action){case'vote-plus':voteValue=1;regSource='t-vote-up';break;case'vote-minus':voteValue=-1;regSource='t-vote-down';break}el.addEvent('click',CNB.Reg.gatedEvent.bindWithEvent(CNB.Reg,[this.vote.pass(voteValue,this),'vote','Please Log In To Cast Your Vote.']))},this)},requestData:function(){var request=new Request.JSON({url:this.options.serviceUrl,data:{'cid':this.options.cid,'ct':this.options.contentType,'serviceCid':this.options.serviceCid},onSuccess:this.handleDataRequest.bind(this)}).send()},handleDataRequest:function(data){if(data){var item=$splat(data)[0];this.setCommentCount(item.commentCount);this.setVoteCount(item.voteCount);if(item.shortUrl!=''){this.shortUrl=encodeURIComponent(item.shortUrl)}}},setCommentCount:function(count){this.commentContainer.getElement('.count').set('text',count);var suffix=(count==1)?'Comment':'Comments';this.commentContainer.getElement('.suffix').set('text',suffix)},setVoteCount:function(count){this.voteContainer.getElement('.count').set('text',count);var suffix=(count==1)?'Vote':'Votes';this.voteContainer.getElement('.suffix').set('text',suffix)},vote:function(credit){this.voteLoader.add();var request=new Request.JSON({url:this.options.serviceUrl,data:{'cid':this.options.cid,'serviceCid':this.options.serviceCid,'ct':this.options.contentType,'title':this.options.title,'url':this.options.url,'regId':CNB.User.get('regId'),'cr':credit},onSuccess:this.handleVoteResponse.bind(this),onComplete:this.voteLoader.remove.bind(this.voteLoader)}).send()},handleVoteResponse:function(data){if(data){try{this.voteOptions.empty();if(!$chk(data.voteCount)){this.voteOptions.set('text','Sorry, there is an error');return false}this.setVoteCount(data.voteCount);this.voteOptions.set('html','thanks').addClass('t-vote-thanks');Cookie.write('service',this.voteHistory+'~'+this.options.cid,{duration:300,domain:CNB.getCookieHost(),path:'/'});DW.redir({usrAction:421})}catch(e){CNB.log(e)}}},handleShareAction:function(e,el){e.stop();var action=el.getProperty('shareaction');var c;switch(action){case'buzz':window.open('http://buzz.yahoo.com/buzz?targetUrl='+this.url+'&headline='+this.title+'&summary='+this.summary);break;case'reddit':window.open('http://reddit.com/submit?url='+this.url+'&title='+this.title);break;case'email':window.open('mailto:?subject='+this.title+'&body='+this.url+' %0D%0D'+this.summary);DW.redir({usrAction:19});break;case'digg':window.open('http://digg.com/submit?url='+this.url+'&title='+this.title);DW.redir({usrAction:152});break;case'facebook':c=this.getPopUpCoords(626,436);window.open('http://www.facebook.com/sharer.php?u='+this.url+'&t='+this.title,'facebook_share','width='+c.w+',height='+c.h+',left='+c.x+',top='+c.y+',toolbar=0,personalbar=0,status=0,resizable=1');DW.redir({usrAction:501});break;case'stumbleupon':window.open('http://www.stumbleupon.com/submit?url='+this.url);break;case'twitter':c=this.getPopUpCoords(550,450);var twitterUrl='http://twitter.com/share?text='+this.title;twitterUrl+='&related='+encodeURIComponent(this.options.twitterRelated);twitterUrl+=(!this.shortUrl)?'&url='+this.url:'&url='+this.shortUrl+'&counturl='+this.url;window.open(twitterUrl,'twitter_tweet','width='+c.w+',height='+c.h+',left='+c.x+',top='+c.y+',personalbar=0,toolbar=0,scrollbars=1,resizable=1');DW.redir({usrAction:460,tag:'twitter'});break}},getPopUpCoords:function(w,h){var sw=screen.width,sh=screen.height;var x=Math.round((sw/2)-(w/2));var y=(sh>h)?Math.round((sh/2)-(h/2)):0;return{x:x,y:y,w:w,h:h}},handleToolAction:function(e,el){var action=el.getProperty('toolaction');switch(action){case'print':window.print();DW.redir({usrAction:20});break;case'pdf':DW.redir({usrAction:177});break}},handleCommentScroll:function(e){e.stop();var href=this.getElement('a').getProperty('href');var scroll=new Fx.Scroll(window).toElement($$(href)[0])}});window.addEvent('domready',function(){var s2=document.createElement('script');s2.type='text/javascript';s2.src='http://platform.twitter.com/widgets.js';(document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(s2)});
