Component.StoryForm=Class.create(Component.Form,{initialize:function($super,A){$super(A);
this.counter=0;
},onSubmit:function(B){var C=this.element.down("input[name=title]");
var A=this.element.down("input[name=url]");
if(!TextUtils.hasText($F(C))||$F(C)==C.readAttribute("default")){B.stop();
new View.ErrorPopup(this.element,{content:"Whoops, don't forget a story headline!"});
new Effect.Highlight("titleLabel");
return false;
}if(!TextUtils.isValidUrl($F(A))){B.stop();
new View.ErrorPopup(this.element,{content:"This doesn't appear to be a valid url.  Please correct it."});
new Effect.Highlight("urlLabel");
return false;
}$("submit").disabled=true;
if(this.counter>0){alert("Please wait -- we're working on your request.");
B.stop();
return false;
}else{this.counter++;
return true;
}}});
Component.StoryCommentDeleteLink=Class.create(Component.Link,{onClick:function(A){A.stop();
new View.ConfirmationPopup(this.element,{content:"Are you sure you want to delete this comment?",onConfirm:this.onDelete.bindAsEventListener(this),onCancel:this.onCancel.bindAsEventListener(this)});
},onCancel:function(){},onDelete:function(){new Ajax.Request("/api/v1/news/"+this.element.readAttribute("story")+"/comments/"+this.element.readAttribute("comment")+".json",{parameters:{_method:"delete"},method:"post",onSuccess:this.onSuccess.bind(this),onFailure:this.onFailure.bind(this)});
},onSuccess:function(A){new Effect.Shrink(this.element.up("li"),{direction:"top-left",duration:0.5});
},onFailure:function(A){new View.ErrorPopup(this.element,{title:"Unexpected Error",content:"Failed to delete the comment."});
}});
Component.TextCounter=Class.create({initialize:function(A){this.element=$(A);
this.target=$(this.element.readAttribute("text"));
this.limit=parseInt(this.target.readAttribute("maxlength"));
this.target.observe("keyup",this.onKeyUp.bindAsEventListener(this));
},onKeyUp:function(A){this.element.update(this.limit-this.target.value.length);
}});
var StoryThumbsWidget=Class.create({VOTES:{THUMBS_UP:1,THUMBS_DOWN:2,THUMBS_NONE:3},vote:3,setOptions:function(A,B){this.options={widget:"thumbs"};
this.stats={score:0,votes:0};
Object.extend(this.options,A||{});
Object.extend(this.stats,B||{});
},initialize:function(C,A,B){this.id=C;
this.setOptions(A,B);
this.up=$A($(this.options.widget).getElementsByClassName("up")).first();
this.down=$A($(this.options.widget).getElementsByClassName("down")).first();
this.score=$A($(this.options.widget).getElementsByClassName("score")).first();
this.votes=$A($(this.options.widget).getElementsByClassName("votes")).first();
Event.observe(this.up,"click",this.handleUp.bind(this));
Event.observe(this.down,"click",this.handleDown.bind(this));
},handleUp:function(){if(!currentUser.isLoggedIn()){alert("Login or register to vote!");
return ;
}switch(this.vote){case this.VOTES.THUMBS_UP:this.vote=this.VOTES.THUMBS_NONE;
this.stats.score-=1;
this.stats.votes-=1;
break;
case this.VOTES.THUMBS_DOWN:this.vote=this.VOTES.THUMBS_UP;
this.stats.score+=1;
break;
case this.VOTES.THUMBS_NONE:this.vote=this.VOTES.THUMBS_UP;
this.stats.score+=1;
this.stats.votes+=1;
break;
}this.paint();
var A="newsAction=voteOnStory&storyId="+this.id+"&vote="+this.vote;
new Ajax.Request("/news.sv",{parameters:A});
},handleDown:function(){if(!currentUser.isLoggedIn()){alert("Login or register to vote!");
return ;
}switch(this.vote){case this.VOTES.THUMBS_UP:this.vote=this.VOTES.THUMBS_DOWN;
this.stats.score-=1;
break;
case this.VOTES.THUMBS_DOWN:this.vote=this.VOTES.THUMBS_NONE;
this.stats.votes-=1;
break;
case this.VOTES.THUMBS_NONE:this.vote=this.VOTES.THUMBS_DOWN;
this.stats.votes+=1;
break;
}this.paint();
var A="newsAction=voteOnStory&storyId="+this.id+"&vote="+this.vote;
new Ajax.Request("/news.sv",{parameters:A});
},paint:function(){Element.removeClassName(this.up,"on");
Element.removeClassName(this.down,"on");
Element.removeClassName(this.up,"off");
Element.removeClassName(this.down,"off");
switch(this.vote){case this.VOTES.THUMBS_UP:Element.removeClassName(this.up,"off");
Element.removeClassName(this.down,"on");
Element.addClassName(this.up,"on");
Element.addClassName(this.down,"off");
break;
case this.VOTES.THUMBS_DOWN:Element.removeClassName(this.up,"on");
Element.removeClassName(this.down,"off");
Element.addClassName(this.up,"off");
Element.addClassName(this.down,"on");
break;
case this.VOTES.THUMBS_NONE:Element.removeClassName(this.up,"on");
Element.removeClassName(this.down,"on");
Element.addClassName(this.up,"off");
Element.addClassName(this.down,"off");
break;
}this.score.update(this.stats.score);
}});
var StoryBookmarkWidget=Class.create({STATES:{READY:0,SAVING:1,SAVED:2,FAILED:3,TIMED_OUT:4},initialize:function(A,B){this.state=this.STATES.READY;
this.storyId=B;
this.link=A;
Event.observe(this.link,"click",this.onClick.bindAsEventListener(this));
},onClick:function(A){A.stop();
if(this.state!=this.STATES.READY){return ;
}this.state=this.STATES.SAVING;
$(this.link).update("Saving...");
var B="newsAction=bookmark&storyId="+this.storyId;
new Ajax.Request("/news.sv",{parameters:B,onSuccess:this.onSuccess.bind(this),onFailure:this.onFailure.bind(this),onTimeout:this.onTimeout.bind(this)});
},onSuccess:function(A){this.state=this.STATES.SAVED;
$(this.link).update('<a href="/newsAction.do?viewSaved=">Saved!</a>');
},onFailure:function(){this.state=this.STATES.FAILED;
$(this.link).update("Failed - Please Refresh/Try Again");
},onTimeout:function(){this.state=this.STATES.TIMED_OUT;
$(this.link).update("Timed Out - Please Refresh/Try Again");
}});
var StoryUnboookmarkWidget=Class.create({STATES:{READY:0,REMOVING:1,REMOVED:2},initialize:function(A,B){this.state=this.STATES.READY;
this.storyId=B;
this.link=A;
Event.observe(this.link,"click",this.handleUnbookmark.bindAsEventListener(this));
},handleUnbookmark:function(A){A.stop();
if(this.state==this.STATES.REMOVED){return ;
}this.state=this.STATES.REMOVING;
this.paint();
var B="newsAction=unbookmark&storyId="+this.storyId;
new Ajax.Request("/news.sv",{parameters:B,onComplete:this.processUnbookmark.bind(this)});
},processUnbookmark:function(A){this.state=this.STATES.REMOVED;
this.paint();
},paint:function(){switch(this.state){case this.STATES.READY:break;
case this.STATES.REMOVING:$(this.link).update("Removing...");
break;
case this.STATES.REMOVED:$(this.link).update('<a href="/newsAction.do?viewSaved=">Removed</a>');
break;
}}});
MovieSearch=Class.create({STATES:{READY:0,SEARCHING:1,TIMEOUT:2,FAILURE:3},initialize:function(A){this.setOptions(A);
this.setObservers();
},setOptions:function(A){this.options={SEARCHING:GENERIC_VIEWS.LOADING,TIMEOUT:GENERIC_VIEWS.TIMEOUT,EMPTY:"No movies found.",MOVIE:new Template("<label><input type=radio name=movieId value=#{id} /> #{title}</label><br/>"),FAILURE:GENERIC_VIEWS.FAILURE,searchField:"mSearchField",searchButton:"mSearchButton",searchResults:"mSearchResults",timeout:15000};
Object.extend(this.options,A||{});
},setObservers:function(){Event.observe($(this.options.searchField),"keypress",this.onEnter.bindAsEventListener(this));
Event.observe($(this.options.searchButton),"click",this.onSearch.bindAsEventListener(this));
new DefaultTextWidget($(this.options.searchField),"Type movie title, click SEARCH");
},onEnter:function(A){if(A.keyCode==Event.KEY_RETURN){this.onSearch(A);
return false;
}else{return true;
}},onSearch:function(B){Event.stop(B);
if(this.state==this.STATES.SEARCHING){return ;
}var A="movieAction=searchMovies&format=json&search="+encodeURIComponent($F(this.options.searchField));
new Ajax.Request("/movie.sv",{parameters:A,onSuccess:this.onSuccess.bind(this),onFailure:this.onFailure.bind(this),onTimeout:this.onTimeout.bind(this),timeout:this.options.timeout});
this.state=this.STATES.SEARCHING;
this.paint();
},onSuccess:function(response){this.movies=eval("("+response.responseText+")");
this.state=this.STATES.READY;
this.paint();
},onFailure:function(){this.state=this.STATES.FAILURE;
this.paint();
},onTimeout:function(){this.state=this.STATES.TIMEOUT;
this.paint();
},paint:function(){switch(this.state){case this.STATES.READY:this.html="";
if(this.movies.length==0){this.html=this.options.EMPTY;
}else{this.movies.each(function(A,B){this.html+=this.options.MOVIE.evaluate(A);
}.bind(this));
}$(this.options.searchResults).update(this.html);
break;
case this.STATES.SEARCHING:$(this.options.searchResults).update(this.options.SEARCHING);
break;
case this.STATES.TIMEOUT:$(this.options.searchResults).update(this.options.TIMEOUT);
break;
case this.STATES.FAILURE:$(this.options.searchResults).update(this.options.FAILURE);
break;
}}});
ActorSearch=Class.create({STATES:{READY:0,SEARCHING:1,TIMEOUT:2,FAILURE:3},initialize:function(A){this.setOptions(A);
this.setObservers();
},setOptions:function(A){this.options={SEARCHING:GENERIC_VIEWS.LOADING,TIMEOUT:GENERIC_VIEWS.TIMEOUT,EMPTY:"No actors found.",ACTOR:new Template("<label><input type=radio name=actorId value=#{id} />#{name}</label><br/>"),FAILURE:GENERIC_VIEWS.FAILURE,searchField:"aSearchField",searchButton:"aSearchButton",searchResults:"aSearchResults",timeout:15000};
Object.extend(this.options,A||{});
},setObservers:function(){Event.observe($(this.options.searchField),"keypress",this.onEnter.bindAsEventListener(this));
Event.observe($(this.options.searchButton),"click",this.onSearch.bindAsEventListener(this));
new DefaultTextWidget($(this.options.searchField),"Type actor name, click SEARCH");
},onEnter:function(A){if(A.keyCode==Event.KEY_RETURN){this.onSearch(A);
return false;
}else{return true;
}},onSearch:function(B){Event.stop(B);
if(this.state==this.STATES.SEARCHING){return ;
}var A="actorAction=searchActors&format=json&search="+encodeURIComponent($F(this.options.searchField));
new Ajax.Request("/actor.sv",{parameters:A,onSuccess:this.onSuccess.bind(this),onFailure:this.onFailure.bind(this),onTimeout:this.onTimeout.bind(this),timeout:this.options.timeout});
this.state=this.STATES.SEARCHING;
this.paint();
},onSuccess:function(response){this.actors=eval("("+response.responseText+")");
this.state=this.STATES.READY;
this.paint();
},onFailure:function(){this.state=this.STATES.FAILURE;
this.paint();
},onTimeout:function(){this.state=this.STATES.TIMEOUT;
this.paint();
},paint:function(){switch(this.state){case this.STATES.READY:this.html="";
if(this.actors.length==0){this.html=this.options.EMPTY;
}else{this.actors.each(function(B,A){this.html+=this.options.ACTOR.evaluate(B);
}.bind(this));
}$(this.options.searchResults).update(this.html);
break;
case this.STATES.SEARCHING:$(this.options.searchResults).update(this.options.SEARCHING);
break;
case this.STATES.TIMEOUT:$(this.options.searchResults).update(this.options.TIMEOUT);
break;
case this.STATES.FAILURE:$(this.options.searchResults).update(this.options.FAILURE);
break;
}}});




