"use strict";function launchListRequest(c,b){var a="/list/display-list-form/?content_type_id="+c+"&object_id="+b;new LaunchPad.Base({titleTextStr:"Add this Item to a List ",url:a}).open()}function launchEditListDetails(b){var a="/list/display-create-new-list-form/?list_id="+b;new LaunchPad.Base({titleTextStr:"List",url:a}).open()}function launchDeleteList(b){var a="/list/process-list-delete/?list_id="+b;new LaunchPad.Base({titleTextStr:"List",url:a}).open()}function launchAddToList(b){var a="/list/add-to-list/?list_id="+b;new LaunchPad.Base({titleTextStr:"List",url:a}).open()}function moveOn(a,b){new VinesAjax(a,{method:"GET",update:b,onComplete:function(){$(b).effects().start({opacity:[0,1]})}}).request()}function attachToExistingList(a,c,b){var d=$(b)[$(b).selectedIndex].value;a+="&list_id="+d;moveOn(a,c)}var ListManager=new Class({options:{list_items_count:0,list_id:0},initialize:function(a){this.options.list_id=a.list_id;this.options.form=$("js-list-form");clog(this.options.list_id);if(this.options.list_id>0){this.findAndWireExistingListItems()}this.initAutoCompleter();this.initSaveButton()},initAutoCompleter:function(){var b=$("list-search-field");var a="fireAcSelected"+b.id;var c=new VinesSearchAC(b,"/autosearch/",{allow_create:false,callBackEvent:a});$(b).addEvent(a,function(d,g){var f=true;$("js-newitems").getChildren("li").each(function(e){var j=e.getProperty("content_type_id");var h=e.getProperty("object_id");if(d.content_type_id==j&&d.object_id==h){alert("That is already in the list.");f=false}}.bind(this));if(f){new Request.JSON({url:"/list/get-list-item/",method:"post",onSuccess:this.addListItemHTML.bind(this)}).send({content_type_id:d.content_type_id,object_id:d.object_id})}}.bind(this))},makeStringPostSafe:function(a){return a.replace(/;/g,"%3B").replace(/&/g,"%26")},initSaveButton:function(){$$(".js-save-list").addEvent("click",function(b){if($("id_name").value===""){alert("You must enter a name for your list.")}else{vinesLoadingMask(true);var a=[];$("js-newitems").getChildren("li").each(function(e){var d=this.makeStringPostSafe(e.getElement("textarea").value);var c=new Hash({object_id:e.getProperty("object_id"),content_type_id:e.getProperty("content_type_id"),association_description:d});a.push(c)}.bind(this));this.options.form.fireEvent("submit");data={list_items:a,list_id:this.options.list_id,list_name:this.makeStringPostSafe($("id_name").value),list_description:this.makeStringPostSafe($("parchment").getElement("textarea").value)};new Request.JSON({url:"/list/save-list/",method:"post",onSuccess:function(c){if(c.success){this.options.list_id=c.list_id;vinesLoadingMask(false);location.replace(c.list_absolute_url)}else{vinesLoadingMask(false);alert("Doh, something broke and your changes were NOT saved. Please try again.")}}.bind(this)}).send(data)}}.bind(this))},addListItemHTML:function(a){this.updateCountOfListItems("increment");var b=new Element("li").setHTML(a.html);b.setProperty("order",this.options.list_items_count);b.setProperty("content_type_id",a.content_type_id);b.setProperty("object_id",a.object_id);b.getElement("input.js-order").value=this.options.list_items_count;b.inject($("js-newitems"),"bottom");this.wireClickEvents(b)},updateCountOfListItems:function(a){if(a=="increment"){this.options.list_items_count++}else{this.options.list_items_count--}return},findAndWireExistingListItems:function(){clog("find and wiring up existing list items....");$("js-newitems").getChildren("li").each(function(a){this.options.list_items_count++;this.wireClickEvents(a)}.bind(this));this.reorderList()},wireClickEvents:function(a){a.getElements(".js-remove-button").addEvent("click",function(b){this.removeListItem(a)}.bind(this));a.getElements(".js-up-button").addEvent("click",function(b){this.moveListItemUp(a)}.bind(this));a.getElement(".js-down-button").addEvent("click",function(b){this.moveListItemDown(a)}.bind(this));a.getElement("input.js-order").addEvent("keypress",function(d){var c=new Event(d);if(c.key=="enter"){c.stop();var b=a.getElement("input.js-order").value;if(isNaN(b)){alert("You must enter a valid number.")}else{this.moveListItemOrder(a,b)}}}.bind(this));a.getElement("input.js-order").addEvent("blur",function(c){var b=a.getElement("input.js-order").value;if(isNaN(b)){alert("You must enter a valid number.")}else{this.moveListItemOrder(a,b)}}.bind(this))},removeListItem:function(a){this.updateCountOfListItems("decrement");a.destroy();this.reorderList()},moveListItemUp:function(a){var b=a.getProperty("order");var c=a.getPrevious();if(c){a.inject(c,"before")}this.reorderList()},moveListItemDown:function(a){var c=a.getProperty("order");var b=a.getNext();if(b){a.inject(b,"after")}this.reorderList()},moveListItemOrder:function(a,e){if(e==a.getProperty("order")){clog("same order - no change")}else{if(e>this.options.list_items_count){var c=a.getParent().getLast();if(c!=a){a.inject(c,"after")}}else{if(e==0||e==1){var b=a.getParent().getFirst();if(b!=a){a.inject(b,"before")}}else{var d=null;a.getParent().getChildren("li").each(function(f){if(f.getProperty("order")==e){d=f}});if(d&&d!=a){a.inject(d,"after")}}}}this.reorderList()},reorderList:function(){i=1;$$("#js-newitems li").each(function(a){a.setProperty("order",i);a.getElement("input.js-order").setProperty("value",i);i++})}});ListManager.implement(new Events);ListManager.implement(new Options);
