Colloquio.Models.Quote=Class.create(Colloquio.Models.CoreModel,{initialize:function($super,initData)
{if(typeof initData=='object')
{$super(initData.id);Object.extend(this,initData);}
else if(typeof initData=='number')
{$super(initData);}}});Colloquio.Models.Quote.CATEGORY_QUOTE='quote';Colloquio.Models.Quote.CATEGORY_CHAT_LOG='chat_log';Colloquio.Models.Quote.CATEGORY_DIALOG='dialog';Colloquio.Views.ValidationException=Class.create(Colloquio.Exception,{});Colloquio.Views.Quote={};Colloquio.Views.Quote.QuoteView=Class.create(Colloquio.Views.CoreView,{BODY_CLASS:'quote-body',RATING_CLASS:'quote-rating',FLAG_QUOTE_LINK_ID:'flag_quote',REMOVE_QUOTE_LINK_ID:'remove_quote',QUOTE_ACTIONS_ID:'quote-actions',FAVORITERS_CLASS:'favoriters',FAVORITES_COUNT_CLASS:'quote-favorites-count',FAVORITE_QUOTE_CLASS:'quote-favorite',FAVORITE_CLASS:'favorite',UNFAVORITE_CLASS:'unfavorite',FAVORITE_LABEL:'Favorite quote',UNFAVORITE_LABEL:'Unfavorite quote',SEE_ALL_FAVORITERS_ID:'favoriters-see-all',EFFECT_DURATION:0.3,initialize:function($super,element,options)
{$super(element);this._setOptions({minimal:false,userLoggedIn:false,solo:true},options);this.model=new Colloquio.Models.Quote(this.element.id.split('_')[1].toInteger());var ratingElem=this.element.selectFirstByClassName(this.RATING_CLASS);if(ratingElem)
this.rating=new Colloquio.Views.Rating.RatingView(ratingElem,this.model,options);this.body=new Colloquio.Views.Quote.QuoteBodyView(this.element.selectFirstByClassName(this.BODY_CLASS),this.model,options);if(this.options.solo)
{document.observe('dom:loaded',function(){this.seeAllFavoriters=$(this.SEE_ALL_FAVORITERS_ID);if(this.seeAllFavoriters)
{this.favoriters=$$('.'+this.FAVORITERS_CLASS)[0];this.seeAllFavoriters.observe('click',this._showAllFavoriters.bind(this));}}.bind(this));if(this.options.userLoggedIn)
{if($(this.FLAG_QUOTE_LINK_ID))
{var activators={};activators[this.FLAG_QUOTE_LINK_ID]=this.QUOTE_ACTIONS_ID;new Colloquio.Views.Quote.FlagReminder(activators,{quote:this.model});}
if($(this.REMOVE_QUOTE_LINK_ID))
{var activators={};activators[this.REMOVE_QUOTE_LINK_ID]=this.QUOTE_ACTIONS_ID;this.confirmer=new Colloquio.Controls.InlineConfirmer(activators,{confirm:this._removeQuote.bind(this)});}}}
else if(!(this.options.minimal||this.options.solo))
{this.favoritesCount=this.element.selectFirstByClassName(this.FAVORITES_COUNT_CLASS);this.favoriteQuote=this.element.selectFirstByClassName(this.FAVORITE_QUOTE_CLASS);this._initializeFavActionEvent();}},_initializeFavActionEvent:function()
{var url=this.favoriteQuote.hasClassName(this.FAVORITE_CLASS)?this._favoriteQuoteUrl():this._unfavoriteQuoteUrl();this.favoriteQuote.observe('click',this._favActionQuote.bind(this,url));},_removeQuote:function()
{new Ajax.Request(this._quoteUrl(),{asynchronous:true,evalScripts:true,method:'delete',onSuccess:this._removeQuoteSuccess.bind(this),onFailure:this._removeQuoteFail.bind(this),parameters:'authenticity_token='+encodeURIComponent(window._authToken)});},_favActionQuote:function(url)
{if(!this.options.userLoggedIn)
{window.location='/users/login';return;}
new Ajax.Request(url,{asynchronous:true,evalScripts:true,method:'post',onComplete:this._favoriteQuoteComplete.bind(this),parameters:{'inlist':'true','authenticity_token':encodeURIComponent(window._authToken)}});},_favoriteQuoteComplete:function(transport)
{this.favoriteQuote.focus();this.favoriteQuote.stopObserving('click');var favCount=transport.responseJSON.favorites_count;var beginEffect=new Effect.Opacity(this.favoritesCount,{duration:this.EFFECT_DURATION,from:1.0,to:0.0,afterFinish:function(){this.favoritesCount.update(favCount);new Effect.Opacity(this.favoritesCount,{duration:this.EFFECT_DURATION,from:0.0,to:1.0});}.bind(this)});if(this.favoriteQuote.hasClassName(this.FAVORITE_CLASS))
this.favoriteQuote.update(this.UNFAVORITE_LABEL);else
this.favoriteQuote.update(this.FAVORITE_LABEL);this.favoriteQuote.toggleClassName(this.FAVORITE_CLASS);this.favoriteQuote.toggleClassName(this.UNFAVORITE_CLASS);this._initializeFavActionEvent();},_removeQuoteSuccess:function()
{this.confirmer.reminder.update('This quote has been removed.');},_removeQuoteFail:function()
{this.confirmer.reminder.update('There was an error removing this quote.');},_showAllFavoriters:function()
{this.seeAllFavoriters.appendSibling(Colloquio.Dom.newImage('/images/loader.gif','Loading'));new Ajax.Updater(this.favoriters,this._quoteFavoritersUrl(),{method:'get'});},_quoteFavoritersUrl:function()
{return this._quoteUrl()+'/favoriters';},_quoteUrl:function()
{return'/quotes/'+this.model.id;},_favoriteQuoteUrl:function()
{return'/quotes/favorite/'+this.model.id;},_unfavoriteQuoteUrl:function()
{return'/quotes/unfavorite/'+this.model.id;}});Colloquio.Views.Quote.USERS_AUTOCOMPLETE_URL='/users/auto_complete';Colloquio.Views.Quote.PEOPLE_AUTOCOMPLETE_URL='/people/auto_complete';Colloquio.Views.Quote.QuoteBodyView=Class.create(Colloquio.Views.CoreView,{EXPANDER_LABEL:'read more...',EXPANDER_CLASS:'quote-expander',EXPANDER_PANEL_CLASS:'quote-expander-panel',SCREENNAME_HOVER_CLASS:'hover',CUTOFF_HEIGHT:11,CUTOFF_CLASS:'cutoff',_QUOTES_BASE_URL:'/quotes',initialize:function(element,quote,options)
{this.element=$(element);this.quote=quote;this._setOptions({hideOverflow:false,linkBody:false},options);if(this.options.linkBody)
this.element.observe('click',this.show.bindAsEventListener(this));if(this.options.hideOverflow&&this.element.hasClassName(this.CUTOFF_CLASS))
{this.expanderPanel=Colloquio.Dom.newNode(Colloquio.Html.Tag.DIV,{cssClass:this.EXPANDER_PANEL_CLASS});this.expander=Colloquio.Dom.newAnchor(this.EXPANDER_LABEL,this._getShowUri(),{cssClass:this.EXPANDER_CLASS,href:'javascript:void(0);'});this.element.appendLineBreak();this.expanderPanel.appendChild(this.expander);this.element.appendSibling(this.expanderPanel);this.expander.observe('click',this.expand.bindAsEventListener(this));}
this.initializeScrennames();},initializeScrennames:function()
{var snElements=this.element.select('a.'+Colloquio.Views.Quote.ParticipantView.prototype.SCREENNAME_CLASS);this.screennames=new Hash();snElements.each(function(snElem){var inner=snElem.down();var sn;if(inner)
sn=inner.innerHTML;else
sn=snElem.innerHTML;if(!this.screennames.get(sn))this.screennames.set(sn,new Array());this.screennames.get(sn).push(snElem);snElem.observe('mouseover',this._onScreennameMouseover.bindAsEventListener(this));snElem.observe('mouseout',this._onScreennameMouseout.bindAsEventListener(this));}.bind(this));},expand:function(event)
{var loader=Colloquio.Dom.newImage('/images/loader.gif','Loading');this.expander.appendSibling(loader);new Ajax.Updater(this.element,this._getBodyUri(),{method:'get',onComplete:this._onExpandComplete.bind(this)});},_onExpandComplete:function()
{this.expanderPanel.remove();this.initializeScrennames();},_onScreennameMouseover:function(event)
{var sn=event.element().innerHTML;this.screennames.get(sn).each(function(elem){elem.addClassName(this.SCREENNAME_HOVER_CLASS);}.bind(this));},_onScreennameMouseout:function(event)
{var sn=event.element().innerHTML;this.screennames.get(sn).each(function(elem){elem.removeClassName(this.SCREENNAME_HOVER_CLASS);}.bind(this));},show:function(event)
{window.location=this._getShowUri();},_getShowUri:function()
{return this._QUOTES_BASE_URL+'/'+this.quote.id;},_getBodyUri:function()
{return this._QUOTES_BASE_URL+'/'+this.quote.id+'/body';}});Colloquio.Views.Quote.NewQuoteView=Class.create(Colloquio.Views.CoreView,{initialize:function(model)
{this.element=$('quote_new');this.body=$('quote_body');this.quote=model;if(model.category!=Colloquio.Models.Quote.CATEGORY_QUOTE)
{this.participantsView=new Colloquio.Views.Quote.NewParticipantsView(this);new Form.Element.Observer(this.body,0.3,this._bodyChanged.bindAsEventListener(this));if(this.body.getValue()!='')
this._bodyChanged();}},_bodyChanged:function(event)
{this.participantsView.update(this.body.getValue());}});Colloquio.Views.Quote.QuotesListView=Class.create(Colloquio.Views.ListView,{CONCISE_QUOTE_CLASS:'concise',initialize:function($super,element,url,options)
{$super(element,url,options);this.quotes=new Array();this._setOptions({minimal:false,hideOverflow:true,linkBody:true},options);this._initializeView();},_initializeView:function()
{var quoteElements=this.element.selectByClassName(this.CONCISE_QUOTE_CLASS);var quoteOptions=this.options;quoteOptions.solo=false;quoteElements.each(function(element){this.quotes.push(new Colloquio.Views.Quote.QuoteView(element.firstDescendant(),quoteOptions));}.bind(this));}});Colloquio.Views.Quote.FlagReminder=Class.create(Colloquio.Controls.InlineReminder,{reminderElement:function()
{this.reason=new Colloquio.Controls.LabeledTextField('Enter a reason (optional)',{name:'reason'});this.submit=Colloquio.Dom.newNode(Colloquio.Html.Tag.INPUT,{type:Colloquio.Html.InputType.BUTTON,value:'Flag'});this.submit.observe('click',this._submitFlagging.bind(this));var form=Colloquio.Dom.newNode(Colloquio.Html.Tag.FORM,[Colloquio.Dom.newAuthTokenField(),this.reason.getElement(),this.submit,]);form.onsubmit=function(){return false;};form.observe('submit',this._submitFlagging.bind(this));return form;},_submitFlagging:function()
{new Ajax.Request(this._actionUrl(),{asynchronous:true,evalScripts:true,method:'post',onSuccess:this._flaggingSuccess.bind(this),onFailure:this._flaggingFailure.bind(this),parameters:Form.serialize(this.submit.up())});},_flaggingSuccess:function()
{this.submit.up().update('Quote flagged! We\'ll take a look as soon as we can.')},_flaggingFailure:function()
{this.submit.up().update('There was an error flagging this quote. Please try again later.')},_actionUrl:function()
{return'/quotes/'+this.options.quote.id+'/flaggings';}});