var Cache=Class.create();
Cache.prototype={initialize:function(){this.cache=new Array();
},put:function(B,A){this.cache[B]=A;
},putList:function(A){A.each(function(C,B){this.cache[C.id]=C;
}.bind(this));
},get:function(A){return this.cache[A];
}};
var Movie=Class.create();
Movie.prototype={initialize:function(C,B,A){this.id=C;
this.title=B;
this.image=A;
}};
Movie.Cache=new Cache();
Rating.Cache=new Cache();
var MovieSearch={};
MovieSearch.Request=Class.create();
MovieSearch.Request.prototype={options:{onSuccess:Prototype.emptyFunction,onFailure:Prototype.emptyFunction,onTimeout:Prototype.emptyFunction,timeout:3000},initialize:function(B,A){Object.extend(this.options,A||{});
this.options.parameters="movieAction=searchMovies&format=json&search="+encodeURIComponent(B);
new Ajax.Request("/movie.sv",this.options);
}};
MovieSearch.Widget=Class.create();
MovieSearch.Widget.STATES={READY:0,SEARCHING:1,TIMEOUT:2,FAILURE:3};
MovieSearch.Widget.VIEWS={SEARCHING:GENERIC_VIEWS.LOADING,TIMEOUT:GENERIC_VIEWS.TIMEOUT,EMPTY:"No movies found.",MOVIE:new Template('<li>#{title} (<span class=jlink movieId="#{id}">add</span>)</li>'),FAILURE:GENERIC_VIEWS.FAILURE};
MovieSearch.Widget.prototype={options:{searchField:"searchField",searchButton:"searchButton",searchResults:"searchResults",timeout:30000},initialize:function(A){Object.extend(this.options,A||{});
this.observe();
},observe:function(){Event.observe($(this.options.searchField),"keypress",this.handleEnter.bindAsEventListener(this));
Event.observe($(this.options.searchButton),"click",this.handleSearch.bindAsEventListener(this));
new DefaultTextWidget($(this.options.searchField),"Type movie title, click SEARCH");
},handleEnter:function(A){if(A.keyCode==Event.KEY_RETURN){this.handleSearch();
Event.stop(A);
return false;
}else{return true;
}},handleSearch:function(A){new MovieSearch.Request($F(this.options.searchField),{onSuccess:this.processSearch.bind(this),onFailure:this.processFailure.bind(this),onTimeout:this.processTimeout.bind(this)});
this.state=MovieSearch.Widget.STATES.SEARCHING;
this.paint();
Tracker.tag("WidgetMovieBoardSearch");
},processSearch:function(response){this.movies=eval("("+response.responseText+")");
Movie.Cache.putList(this.movies);
this.state=MovieSearch.Widget.STATES.READY;
this.paint();
(this.options.onSuccess||Prototype.emptyFunction)();
},processFailure:function(){this.state=MovieSearch.Widget.STATES.FAILURE;
this.paint();
},processTimeout:function(){this.movies=new Array();
this.state=MovieSearch.Widget.STATES.TIMEOUT;
this.paint();
},paint:function(){switch(this.state){case MovieSearch.Widget.STATES.READY:this.html="";
if(this.movies.length==0){this.html=MovieSearch.Widget.VIEWS.EMPTY;
}else{this.html="<ul>";
this.movies.each(function(A,B){this.html+=MovieSearch.Widget.VIEWS.MOVIE.evaluate(A);
}.bind(this));
this.html+="</ul>";
}$(this.options.searchResults).update(this.html);
break;
case MovieSearch.Widget.STATES.SEARCHING:$(this.options.searchResults).update(MovieSearch.Widget.VIEWS.SEARCHING);
break;
case MovieSearch.Widget.STATES.TIMEOUT:$(this.options.searchResults).update(MovieSearch.Widget.VIEWS.TIMEOUT);
break;
case MovieSearch.Widget.STATES.FAILURE:$(this.options.searchResults).update(MovieSearch.Widget.VIEWS.FAILURE);
break;
}}};
var MovieBoard={};
MovieBoard.Widget=Class.create();
MovieBoard.Widget.prototype={templates:{image:new Template("<input type='checkbox' checked='true' name='imgurl[]' value='#{image}' /> "),caption:new Template('<textarea name="caption[]">#{review}</textarea>'),rating:new Template("<input type='checkbox' checked='true' name='rating[]' value='#{score}' /> ")},initialize:function(){this.index=0;
this.movies=new Array();
this.observers=new Array();
this.listen();
},listen:function(){this.observers.each(function(C,D){Event.stopObserving(this.observers[D][0],this.observers[D][1],this.observers[D][2],this.observers[D][3]);
}.bind(this));
var B=$A($("popular").getElementsByClassName("jlink"));
B.each(function(E,D){var C=this.onAdd.bindAsEventListener(this);
Event.observe($(E),"click",C);
this.observers.push([$(E),"click",C,false]);
}.bind(this));
if($("searchResults")!=null){B=$A($("searchResults").getElementsByClassName("jlink"));
B.each(function(E,D){var C=this.onAdd.bindAsEventListener(this);
Event.observe($(E),"click",C);
this.observers.push([$(E),"click",C,false]);
}.bind(this));
}var A=$A(document.getElementsByClassName("createBtn"));
A.each(function(D,C){Event.observe($(D),"click",this.onCreate.bindAsEventListener(this));
}.bind(this));
},onAdd:function(D){this.index+=1;
var C=Event.element(D);
var B=C.readAttribute("movieId");
var A=Movie.Cache.get(B);
this.movies.push(A);
new RatingWidget.Request($("ratingWidgets"),A.id,this,this.index);
Tracker.tag("WidgetMovieBoardAdd");
},onRemove:function(A){this.movieId=A;
this.movies=this.movies.findAll(function(B){return B.id!=this.movieId;
}.bind(this));
this.paint();
},onRate:function(B,C,A){Rating.Cache.put(B,{score:C,review:A});
this.paint();
},onCreate:function(A){$("movieIds").value=this.movies.pluck("id");
Event.stop(A);
$("movieBoardForm").submit();
},paint:function(){this.html="";
this.movies.each(function(A){this.html+=this.templates.image.evaluate(A);
var B=Rating.Cache.get(A.id);
this.html+=this.templates.caption.evaluate(B||{});
var C=B!=null?{score:this.score_map(B.score)}:{};
this.html+=this.templates.rating.evaluate(C||{});
}.bind(this));
$("widgetPreviewForm").update(this.html);
$("widgetPreviewForm").submit();
},score_map:function(A){switch(A){case 1:return 1;
break;
case 2:return 2;
break;
case 3:return 3;
break;
case 4:return 4;
break;
case 5:return 5;
break;
case 6:return 0.5;
break;
case 7:return 1.5;
break;
case 8:return 2.5;
break;
case 9:return 3.5;
break;
case 10:return 4.5;
break;
case 11:return 0;
break;
case 12:return 0;
break;
}}};
var RatingWidget={};
RatingWidget.Request=Class.create();
RatingWidget.Request.VIEWS={LOADING:GENERIC_VIEWS.LOADING,TIMEOUT:GENERIC_VIEWS.TIMEOUT,FAILURE:GENERIC_VIEWS.FAILURE};
RatingWidget.Request.prototype={html:new Template("<li id='ratingWidget#{index}'>#{html}(<span id='removeLink#{index}' class='jlink'>Remove</span>)</li>"),initialize:function(D,A,E,C){this.element=D;
this.movieId=A;
this.board=E;
this.index=C;
var B={parameters:"rateAction=getRatingWidget&movieId="+A+"&index="+C,onSuccess:this.onSuccess.bind(this),onFailure:this.onFailure.bind(this),onTimeout:this.onTimeout.bind(this)};
new Ajax.Request("/rate.sv",B);
},onSuccess:function(A,B){Rating.Cache.put(this.movieId,B);
A.responseText.evalScripts();
var C={index:this.index,html:A.responseText};
$(this.element).insert(this.html.evaluate(C));
Event.observe($("removeLink"+this.index),"click",this.onRemove.bind(this));
this.board.paint();
},onFailure:function(A){$(this.element).update(RatingWidget.Request.VIEWS.FAILURE);
},onTimeout:function(A){$(this.element).update(RatingWidget.Request.VIEWS.TIMEOUT);
},onRemove:function(){Element.remove($("ratingWidget"+this.index));
this.board.onRemove(this.movieId);
}};
var WidgetPartnerLink={initialize:function(){var A=$A($$("#widgetCatalog .partner"));
A.each(function(C,B){Event.observe($(C),"click",this.track.bindAsEventListener(this));
}.bind(this));
},track:function(C){var A=Event.element(C);
var B=A.getAttribute("partnerId");
while(A&&B==null){B=A.getAttribute("partnerId");
A=A.parentNode;
}Tracker.tag("WidgetCatalogPartner"+B);
}};
var WidgetCopy=Class.create();
WidgetCopy.prototype={VIEWS:{FORM:'<p>Copy this widget to the about me section of my profile</p><input type="submit" name="widgetSubmitBtn" value="Copy" />',SUBMITTING:"<p>Submitting...</p>",FAILURE:'<div class="error">There was an error sending your request.  Please try again.</div>',TIMEOUT:'<div class="error">Your request timed out.  Please try again.</div>',SUCCESS:new Template('<div>This widget has been copied to your profile.  <a href="/user/#{username}">Check it out!</a></div>')},initialize:function(A,B){this.widget=A;
this.username=B;
Event.observe("widgetCopyForm","submit",this.onSubmit.bind(this));
},onSubmit:function(A){Event.stop(A);
var B=Form.serialize("widgetCopyForm");
new Ajax.Request("/widget.sv",{parameters:B,onSuccess:this.onSuccess.bind(this),onFailure:this.onFailure.bind(this),onTimeout:this.onTimeout.bind(this)});
Tracker.tag("WidgetCopySubmit");
$("widgetCopyText").update(this.VIEWS.SUBMITTING);
return false;
},onSuccess:function(A){$("widgetCopyText").update(this.VIEWS.SUCCESS.evaluate({username:this.username}));
},onFailure:function(A){$("widgetCopyText").update(this.VIEWS.FAILURE+this.VIEWS.FORM);
},onTimeout:function(A){$("widgetCopyText").update(this.VIEWS.TIMEOUT+this.VIEWS.FORM);
}};
var WidgetDelete=Class.create();
WidgetDelete.prototype={VIEWS:{FORM:'<p>Delete this widget from your profile?</p><input type="submit" name="widgetSubmitBtn" value="Delete" />',SUBMITTING:"<p>Deleting...</p>",FAILURE:'<div class="error">There was an error sending your request.  Please try again.</div>',TIMEOUT:'<div class="error">Your request timed out.  Please try again.</div>',SUCCESS:new Template('<div>This widget has been deleted from your profile.  <a href="/user/#{username}">View your changes</a>.</div>')},initialize:function(A,B){this.widget=A;
this.username=B;
Event.observe("widgetDeleteForm","submit",this.onSubmit.bind(this));
},onSubmit:function(A){Event.stop(A);
var B=Form.serialize("widgetDeleteForm");
new Ajax.Request("/widget.sv",{parameters:B,onSuccess:this.onSuccess.bind(this),onFailure:this.onFailure.bind(this),onTimeout:this.onTimeout.bind(this)});
Tracker.tag("WidgetDeleteSubmit");
$("widgetDeleteText").update(this.VIEWS.SUBMITTING);
return false;
},onSuccess:function(A){$("widgetDeleteText").update(this.VIEWS.SUCCESS.evaluate({username:this.username}));
},onFailure:function(A){$("widgetDeleteText").update(this.VIEWS.FAILURE+this.VIEWS.FORM);
},onTimeout:function(A){$("widgetDeleteText").update(this.VIEWS.TIMEOUT+this.VIEWS.FORM);
}};
var WidgetBulletin=Class.create();
WidgetBulletin.prototype={VIEWS:{FORM:'<input type="submit" name="widgetSendBtn" value="Send" />',SUBMITTING:"<p>Submitting...</p>",FAILURE:'<div class="error">There was an error sending your request.  Please try again.</div>',TIMEOUT:'<div class="error">Your request timed out.  Please try again.</div>',SUCCESS:"<div>Your message has been posted to your friends' profiles.</div>"},initialize:function(A){this.widget=A;
this.selected=false;
this.defaults=new DefaultTextWidget($("bulletin"),"Type a note for your friends");
Event.observe("widgetSendForm","submit",this.onSubmit.bindAsEventListener(this));
Event.observe("selectLink","click",this.onSelect.bindAsEventListener(this));
},onSelect:function(A){this.selected=!this.selected;
$("widgetSendForm").friends.checked=this.selected;
for(i=0;
i<$("widgetSendForm").friends.length;
i++){$("widgetSendForm").friends[i].checked=this.selected;
}},onSubmit:function(A){Event.stop(A);
if($F("bulletin")==this.defaults.text){alert("Don't forget to type in a message for your friends!");
return false;
}var B=Form.serialize("widgetSendForm");
new Ajax.Request("/widget.sv",{parameters:B,onSuccess:this.onSuccess.bind(this),onFailure:this.onFailure.bind(this),onTimeout:this.onTimeout.bind(this)});
Tracker.tag("WidgetBulletinSubmit");
$("widgetSendText").update(this.VIEWS.SUBMITTING);
return false;
},onSuccess:function(A){$("widgetSendText").update(this.VIEWS.SUCCESS);
},onFailure:function(A){$("widgetSendText").update(this.VIEWS.FAILURE+this.VIEWS.FORM);
},onTimeout:function(A){$("widgetSendText").update(this.VIEWS.TIMEOUT+this.VIEWS.FORM);
}};




