//ee8888862affa2b7b2aa537dcb1eab62
var OWWiki=new Class({Implements:[Chain,Options,Events],options:{defaults:{},input_size:30,textarea_cols:60,textarea_height:null,post_save_elements:{},ante_save_elements:{},onCancel:$empty},initialize:function(B,A){this.setOptions(A);this.definition=B;this.data={};this.updating=null;if(this.options.active){if($(this.options.editid)){$(this.options.editid).removeEvents("click");$(this.options.editid).addEvent("click",function(C){new Event(C).stop();if(this.options.active){this.edit()}}.bind(this))}if($(this.options.saveid)){$(this.options.saveid).removeEvents("click");$(this.options.saveid).addEvent("click",function(C){this.save();new Event(C).stop()}.bind(this))}if($(this.options.cancelid)){$(this.options.cancelid).removeEvents("click");$(this.options.cancelid).addEvent("click",function(C){this.cancel();new Event(C).stop()}.bind(this))}}},edit:function(){if(this.lock){return }this.lock=true;this.fireEvent("beforeEdit");this.fetchQueue=[];this.definition.each(function(A){if($(A.contid+"_separator")){$(A.contid+"_separator").style.display="none"}this.editField(A)}.bind(this));if(this.fetchQueue.length){this.fetch(this.fetchQueue)}if($(this.options.editid)){$(this.options.editid).style.display="none"}if($(this.options.saveid)){$(this.options.saveid).style.display=""}if($(this.options.cancelid)){$(this.options.cancelid).style.display=""}this.lock=false;this.fireEvent("edit")},editField:function(C){if($(C.contid)==null){return }var D=null;var I=this.options.defaults[C.name];if(C.type=="text"){D=new Element("input",{type:"text","class":"text",size:this.options.input_size})}else{if(C.type=="select"){D=new Element("select");if(I!=""){D.adopt(new Element("option",{value:null}).set("text",I))}if($type(C.values)=="array"){for(i=0;i<C.values.length;i++){var H=C.values[i];if(H==""){continue}var B=new Element("option",{value:i});B.set("text",H);if(B.value==I){B.selected=1}D.adopt(B)}}else{for(k in C.values){var H=C.values[k];if(H==""){continue}var B=new Element("option",{value:k}).set("text",H);if(B.value==I){B.selected=1}D.adopt(B)}}}else{if(C.type=="checkbox"){D=new Element("input",{type:"checkbox"})}else{D=new Element("textarea",{cols:this.options.textarea_cols,styles:{height:this.options.textarea_height,"text-align":"left"}})}}}var G="";if(C.hide){G="none"}D.setStyle("display",G);$extend(D,{id:C.uplinkid,name:C.name,title:C.name});var E="";var A=true;if(A&&$(C.indexid)&&$(C.indexid).value===""){A=false}E=E.replace("/&nbsp;/gi","");E=E.replace(/<br\s*\/?>/gi,"\n");E=E.replace(/\n\n\n+/g,"\n\n");var F=false;if(!A&&E.match(/\S/)){D.value=E;if(!snooth.options.user.moderator&&!C.indexid){D.disabled=1;D.setStyle("background-color","#ccc")}}else{if(!A&&I){F=true}}D.injectBefore($(C.contid));$(C.contid).setStyle("display","none");if(F){this.addShrinkwrap(D,I)}if(A){this.fetchQueue.push(C.name)}},addShrinkwrap:function(B,A){var B=$(B);if(B==null){return }if(A==null){return }if(B.get("tag")=="select"){return }B.value=B.title=A;B.style.color="#aaa";B.removeEvents("focus");B.addEvent("focus",function(){if(!A){return null}if(B.value==A){B.value="";B.style.color="#000"}}.bind(this));B.removeEvents("blur");B.addEvent("blur",function(){if(!A){return null}if(!B.value.match(/\S/)){B.value=A;B.style.color="#aaa"}}.bind(this))},saveField:function(F,G){var E=$(F.uplinkid);if(E==null){return 1}if(G==null){G=E.value}var B=$(F.contid);var D=$(F.contid+"_data");if(E==null||B==null){return 0}if((G.match(/\S/)||snooth.options.user.moderator)&&G!=this.options.defaults[F.name]){var C=G.trim();if(B.get("tag")=="a"&&C.match(/\S/)){if(C.match(/@/)){B.href="mailto:"+C}else{B.href=C}}else{C=C.replace(/\n\n\n+/g,"\n\n");C=C.replace(/\n/g,"<br />")}if(!this.options.hideSave){B.set("html",C);if(D){D.set("html",G.trim());if(C==""||C==null||C==this.options.defaults[F.name]){B.set("html","&nbsp;")}}}var A=B;this.data[F.name]=G}E.dispose();if(!this.options.hideSave){B.setStyle("display","")}return 1},cancel:function(){this.definition.each(function(A){if($(A.uplinkid)==null){return }$(A.uplinkid).dispose();$(A.contid).setStyle("display","");if($(A.contid+"_separator")&&$(A.contid).get("text").match(/\S/)){$(A.contid+"_separator").style.display=""}});if($(this.options.saveid)){$(this.options.saveid).style.display="none"}if($(this.options.cancelid)){$(this.options.cancelid).style.display="none"}if($(this.options.loaderid)){$(this.options.loaderid).style.display="none"}if($(this.options.editid)){$(this.options.editid).style.display=""}this.fireEvent("cancel");this.fireEvent("finish")},fetchQueue:[],fetch:function(A){if(this.fetch_lock){return }this.fetch_lock=true;var B=snooth.options.site_url+"/wiki-fetch/"+this.options.type+"/"+this.options.key+"/";var C={fields:A};this.definition.each(function(D){if($(D.uplinkid)){$(D.uplinkid).value="loading..."}if($(D.indexid)){C[D.name+"_index"]=$(D.indexid).value}});new Request.JSON({url:B,onComplete:function(D){if(D.error!=""&&D.error!=null){show_generic_bubble("Sorry!",D.error)}else{var G=D.data||{};var E={};var F=[];for(key in G){this.data[key]=G[key]}this.definition.each(function(I){E[I.name]=I;el=$(I.uplinkid);if(el){var J=G[I.name];if(I["extends"]){F.push(I)}var H=this.options.defaults[I.name];if(el.type&&el.type=="checkbox"){if(J){el.checked=1;el.value=1}else{el.checked=0;el.value=0}}else{if($defined(J)&&J!==""){el.value=J;if(!snooth.options.user.moderator&&!I.indexid){el.disabled=1;el.setStyles({background:"none","background-color":"#eee",border:"default"})}}else{this.addShrinkwrap(el,H)}}}}.bind(this));F.each(function(I){var H=E[I["extends"]];if(!H){return }parel=$(H.uplinkid);el=$(I.uplinkid);if(!parel||!el){return }if(parel.disabled&&!el.disabled){el.disabled=1;el.setStyles({background:"none","background-color":"#eee",border:"default"})}}.bind(this))}this.fetch_lock=false;this.fireEvent("fetch")}.bind(this),onError:function(){show_generic_bubble("Sorry!","An internal error occurred");this.fetch_lock=false}}).post(C)},save:function(){if(this.lock){return }this.lock=true;var A=snooth.options.site_url+"/wiki-update/"+this.options.type+"/"+this.options.key+"/";var B={};this.definition.each(function(D){var C=$(D.uplinkid);if(C==null){return }if(C.disabled){return }if(C.value.match(/\S/)||snooth.options.user.moderator||D.indexid||C.type=="checkbox"){if(C.type=="checkbox"){if(C.checked){B[D.name]=1}else{B[D.name]=0}}else{if(C.value==this.options.defaults[C.name]){B[D.name]=""}else{B[D.name]=C.value.trim()}}}if(!snooth.options.user.moderator){C.disabled=true}if(D.indexid&&$(D.indexid)){B[D.name+"_index"]=$(D.indexid).value}}.bind(this));new Request({url:A,onComplete:function(C){try{var C=JSON.decode(C)}catch(E){return this.saveError()}if(C==null){return this.saveError()}if(C.error!=""&&C.error!=null){return this.saveError(C.error)}else{var D=1;this.definition.each(function(F){var G=null;if(C.data&&C.data[F.name]!=""){G=C.data[F.name]}D*=this.saveField(F,G)}.bind(this));if(D){if($(this.options.saveid)){$(this.options.saveid).style.display="none"}if($(this.options.cancelid)){$(this.options.cancelid).style.display="none"}if($(this.options.loaderid)){$(this.options.loaderid).style.display="none"}if($(this.options.editid)){$(this.options.editid).style.display=""}this.definition.each(function(F){if($(F.contid+"_separator")&&$(F.contid).get("text").match(/\S/)){$(F.contid+"_separator").style.display=""}}.bind(this));this.fireEvent("save");this.fireEvent("finish")}}this.lock=false}.bind(this)}).post(B);if(this.options.loaderid&&$(this.options.loaderid)){["saveid","cancelid","deleteid"].each(function(C){if(this.options[C]&&$(this.options[C])){$(this.options[C]).setStyle("display","none")}}.bind(this));$(this.options.loaderid).setStyle("display","")}},saveError:function(A){if(A==null||A==""){A="A problem occurred.  Please try again later."}this.lock=false;if($(this.options.loaderid)){$(this.options.loaderid).style.display="none"}if($(this.options.saveid)){$(this.options.saveid).style.display=""}if($(this.options.cancelid)){$(this.options.cancelid).style.display=""}show_generic_bubble("Sorry!",A)}});if(window.snooth&&snooth.options&&snooth.options.page=="details"){snooth.post_load("Observer");snooth.post_load("Autocompleter");__pl_owwiki=function(B){if(!B){B={}}var C=$("wiki-edit");if(C==null){return }details.type_array=[];details.color_array=[];details.options.wiki_def.each(function(D){if(D.name=="type"){details.type_array=D.values}if(D.name=="color"){details.color_array=D.values}}.bind(details));var A=(snooth.options.user.id>0)?1:0;details.wiki=new OWWiki(details.options.wiki_def,{active:A,key:details.options.wine_data.id,type:"wine",defaults:{vintage:'Please enter a four-digit year, or "nv"',region:"Region: please be specific!",winery:"Please provide the winery",price:"The expected price of the wine",type:"Select the wine type...",color:"Select the wine color...",varietal:"Varietal of the wine",upc:"Please provide the barcode...",currency:"Select the currency..."},editid:"wiki-edit",cancelid:"wiki-cancel",saveid:"wiki-save",loaderid:"wiki-loader",onBeforeEdit:function(){["type","color","region","varietal","winery","tags","drink","upc"].each(function(F){var E=$(F+"_label");if(E){E.setStyle("display","none")}});var D=$("tags_content");if(D){D.setStyle("display","none")}if($("drink_content")){$("drink_content").setStyle("display","none")}},onEdit:function(){$("g_priceup").setStyle("display","none");$("g_currencyup").setStyle("display","none");if($("g_currencyup")){$("g_currencyup").value="USD"}details.wiki.data.currency="USD"}.bind(details),onFetch:function(){init_wiki_autocomplete();if(!details.wiki.data.available){$("g_priceup").setStyle("display","");$("g_currencyup").setStyle("display","")}else{var D=new Element("dd",{id:"g_availablenotice"}).setStyles({"font-style":"italic",color:"#999","margin-top":"8px"}).set("text","This wine is in-stock, so its price cannot be altered");D.injectAfter($("g_currencyup"))}}.bind(details),onFinish:function(){var E=details.wiki.data.name;var D=details.wiki.data.vintage;if(D){E+=' <a href="'+snooth.options.site_url+"/wines/"+D+'/">'+D+"</a>"}$("wine-name").set("html",E);$("product-details").empty();new Request({url:"?of=product_details&ts="+new Date().getTime(),onSuccess:function(F){$("product-details").set("html",F);__pl_owwiki({no_edit:1})},onComplete:function(){}}).get()}.bind(details),hideSave:true});if(!B.no_edit){details.wiki.edit()}};init_wiki_autocomplete=function(){if($("g_regionup")){details.regionpop=new Autocompleter.Ajax.Json($("g_regionup"),snooth.options.site_url+"/autocompleter/region",{postVar:"key",minLength:2,maxChoices:20,null_equivalent:"No matches!",onInvalidChoice:function(){if($("g_regionup").value==details.wiki.options.defaults.region){return }alert('Sorry, the region "'+$("g_regionup").value+'" does not exist, and it cannot be created.  If you wish to add this region to our database, please contact us!')}})}if($("g_wineryup")){details.winerypop=new Autocompleter.Ajax.Json($("g_wineryup"),snooth.options.site_url+"/autocompleter/winery",{postVar:"key",minLength:2,maxChoices:20,null_equivalent:"No matches!",onInvalidChoice:function(){if($("g_wineryup").value==details.wiki.options.defaults.winery){return }if(!confirm('The winery "'+$("g_wineryup").value+'" does not exist yet, do you want us to create it?')){$("g_wineryup").value=""}}.bind(details)})}};snooth.addEvent("__plev_owwiki",__pl_owwiki);snooth.fireEvent("__plev_owwiki")};