$(function(){$(window).on("resize",function(){CHV.fn.uploader.boxSizer();if(typeof user_background_full_fix=="function"){user_background_full_fix();}
CHV.fn.bindSelectableItems();});if($("body").hasClass("landing")&&$("#home-cover, #maintenance-wrapper").css("background-image")){var $landing_el=$(!$("#maintenance-wrapper").exists()?"#home-cover":"#maintenance-wrapper");var landing_src=$landing_el.css("background-image").slice(4,-1).replace(/^\"|\"$/g,"");function showHomeCover(){$("body").addClass("load");setTimeout(function(){$("body").addClass("loaded");},400*1.5);}
if(landing_src){$("").attr("src",landing_src).on("load error",function(){$(this).remove();showHomeCover();});}else{showHomeCover();}}
var anywhere_upload=CHV.fn.uploader.selectors.root,anywhere_upload_queue=CHV.fn.uploader.selectors.queue,$anywhere_upload=$(anywhere_upload),$anywhere_upload_queue=$(anywhere_upload_queue);$(document).on("click","[data-action=top-bar-upload]",function(e){CHV.fn.uploader.toggle();});$("[data-action=close-upload], [data-action=cancel-upload]",$anywhere_upload).click(function(){if($anywhere_upload.is(":animated"))return;$("[data-action=top-bar-upload]","#top-bar").click();});$("[data-action=cancel-upload-remaining]",$anywhere_upload).click(function(){$("[data-action=cancel]",$anywhere_upload_queue).click();CHV.fn.uploader.is_uploading=false;if(CHV.fn.uploader.results.success.length>0){CHV.fn.uploader.displayResults();return;}else{CHV.fn.uploader.reset();}});$(document).on("click","[data-action=upload-privacy]:not(disabled)",function(e){if(e.isDefaultPrevented())return;current_privacy=$(this).data("privacy");target_privacy=current_privacy=="public"?"private":"public";this_lock=$(".icon",this).data("lock");this_unlock=$(".icon",this).data("unlock");$(".icon",this).removeClass(this_lock+" "+this_unlock).addClass(current_privacy=="public"?this_lock:this_unlock);$(this).data("privacy",target_privacy);$("[data-action=upload-privacy-copy]").html($("[data-action=upload-privacy]").html());$upload_button=$("[data-action=upload]",$anywhere_upload);$upload_button.text($upload_button.data(target_privacy));$(this).tipTip("hide");});$(CHV.fn.uploader.selectors.file+", "+CHV.fn.uploader.selectors.camera).on("change",function(e){if(!$(CHV.fn.uploader.selectors.root).data("shown")){CHV.fn.uploader.toggle({callback:function(e){CHV.fn.uploader.add(e);}},e);}else{CHV.fn.uploader.add(e);}}).on("click",function(e){if($(this).data('login-needed')&&!PF.fn.is_user_logged()){return;}});function isFileTransfer(e){var e=e.originalEvent,isFileTransfer=false;if(e.dataTransfer.types){for(var i=0;i').css({width:"100%",height:"100%",position:"fixed",zIndex:1000,left:0,top:0}));}}});$(document).on({dragover:function(e){e.preventDefault();if(!isFileTransfer(e)){return false;}
if(!$(CHV.fn.uploader.selectors.root).data("shown")){CHV.fn.uploader.toggle({reset:false});}},dragleave:function(e){$(CHV.fn.uploader.selectors.dropzone).remove();if($.isEmptyObject(CHV.fn.uploader.files)){CHV.fn.uploader.toggle();}},drop:function(e){e.preventDefault();CHV.fn.uploader.add(e);$(CHV.fn.uploader.selectors.dropzone).remove();},},CHV.fn.uploader.selectors.dropzone);}
$(document).on("keyup change","[data-action=resize-combo-input]",function(e){var $parent=$(this).closest("[data-action=resize-combo-input]");var $input_width=$("[name=form-width]",$parent);var $input_height=$("[name=form-height]",$parent);var ratio=$input_width.data("initial")/ $input_height.data("initial");if($(e.target).is($input_width)){$input_height.prop("value",Math.round(Math.round($input_width.prop("value")/ratio)));}else{$input_width.prop("value",Math.round(Math.round($input_height.prop("value")*ratio)));}})
$(document).on("click",anywhere_upload_queue+" [data-action=edit]",function(){var $item=$(this).closest("li"),$queue=$item.closest("ul"),id=$item.data("id"),file=CHV.fn.uploader.files[id];var modal=PF.obj.modal.selectors.root;var queueObject=$.extend({},file.formValues||file.parsedMeta);if(typeof queueObject.category_id==typeof undefined){var upload_category=$("[name=upload-category-id]",CHV.fn.uploader.selectors.root).prop("value")||null;queueObject.category_id=upload_category;}
if(typeof queueObject.nsfw==typeof undefined){var upload_nsfw=$("[name=upload-nsfw]:checked",CHV.fn.uploader.selectors.root).prop("value")||null;queueObject.nsfw=upload_nsfw;}
PF.fn.modal.call({type:"html",template:$("#anywhere-upload-edit-item").html(),callback:function(){$.each(queueObject,function(i,v){var name="[name=form-"+i.replace(/_/g,"-")+"]";var $input=$(name,modal);if(!$input.exists())return true;if($input.is(":checkbox")){$input.prop("checked",$input.attr("value")==v);}else if($input.is("select")){var $option=$input.find("[value="+v+"]");if(!$option.exists()){$option=$input.find("option:first");}
$option.prop("selected",true);}else{$input.prop("value",v);}
if(i=="width"||i=="height"){$input.prop("max",file.parsedMeta[i]).data("initial",file.parsedMeta[i]);}});if(file.parsedMeta.mimetype!=="image/gif"){$("[ data-content=animated-gif-warning]",modal).remove();}
$(".image-preview",modal).append($('',{'class':'canvas'}));var source_canvas=$(".queue-item[data-id="+id+"] .preview .canvas")[0];var target_canvas=$(".image-preview .canvas",modal)[0];target_canvas.width=source_canvas.width;target_canvas.height=source_canvas.height;var target_canvas_ctx=target_canvas.getContext('2d');target_canvas_ctx.drawImage(source_canvas,0,0);},confirm:function(){if(!PF.fn.form_modal_has_changed()){PF.fn.modal.close();return;}
var errors=false;$.each(["width","height"],function(i,v){var $input=$("[name=form-"+v+"]",modal);var input_val=parseInt($input.val());var min_val=parseInt($input.attr("min"));var max_val=parseInt($input.attr("max"));if(input_val>max_val||input_val-1){CHV.fn.uploader.clipboardImages.splice(c_index,1);}}
delete CHV.fn.uploader.files[id];CHV.fn.uploader.queueSize();if(Object.size(CHV.fn.uploader.files)==0){if(CHV.fn.uploader.results.success.length==0&&CHV.fn.uploader.results.error.length==0){CHV.fn.uploader.reset();}}else{if(item_xhr_cancel){if($("li.waiting",$queue).first().length!==0){CHV.fn.uploader.upload($("li.waiting",$queue).first());}else if(CHV.fn.uploader.results.success.length!==0||CHV.fn.uploader.results.error.length!==0){CHV.fn.uploader.displayResults();}}}});$(document).on("click","[data-action=upload]",function(){$("[data-group=upload], [data-group=upload-queue-ready]",$anywhere_upload).hide();$("[data-group=uploading]",$anywhere_upload).show();CHV.fn.uploader.queueSize();CHV.fn.uploader.can_add=false;$queue_items=$("li",$anywhere_upload_queue);$queue_items.addClass("uploading waiting");CHV.fn.uploader.timestamp=new Date().getTime();CHV.fn.uploader.upload($queue_items.first("li"));});if($("body#user").exists()){if(PF.obj.listing.query_string.page>1){var State=History.getState();console.log(State.data)
if(State.data&&typeof State.data.scrollTop!=="undefined"){if($(window).scrollTop()!==State.data.scrollTop){$(window).scrollTop(State.data.scrollTop);}}else{var scrollTop=$("#background-cover").height()-160;$("html, body").animate({scrollTop:scrollTop},0);}}}
if(!PF.fn.isDevice('phone')){if($("#top-bar-shade").exists()){if($("#top-bar-shade").css("opacity")){$("#top-bar-shade").data("initial-opacity",Number($("#top-bar-shade").css("opacity")));}}
$(window).scroll(function(){var Y=$(window).scrollTop();var is_slim_shady=$("#top-bar-shade").exists()&&!$("html").hasClass("top-bar-box-shadow-none");if(Y<0)return;var $top_bar=$("#top-bar");var rate=Number(Y /($("#background-cover, [data-content=follow-scroll-opacity]").height()-$top_bar.height()));if(rate>1)rate=1;if(is_slim_shady){if($("#top-bar-shade").data("initial-opacity")){rate+=$("#top-bar-shade").data("initial-opacity");}
$("#top-bar-shade").css({opacity:rate});}
if(rate==1)return;$("#background-cover-src").css({transform:"translate(0, "+Y*0.8+"px"+")"});});}
CHV.fn.bindSelectableItems();if($("body#image").exists()){var ImageZoomClass=function(){if(CHV.obj.image_viewer.$container.hasClass("jscursor-zoom-in")){CHV.obj.image_viewer.$container.addClass("cursor-zoom-in").removeClass("jscursor-zoom-in");}}
if($(CHV.obj.image_viewer.selector+" [data-load=full]").length>0){if(CHV.obj.image_viewer.$loading.exists()){CHV.obj.image_viewer.$loading.removeClass("soft-hidden").css({zIndex:2});PF.fn.loading.inline(CHV.obj.image_viewer.$loading,{color:"white",size:"small",center:true,valign:true});CHV.obj.image_viewer.$loading.hide().fadeIn("slow");}
CHV.obj.image_viewer.image.html=CHV.obj.image_viewer.$container.html();CHV.obj.image_viewer.$container.prepend($(CHV.obj.image_viewer.image.html).css({top:0,zIndex:0}));CHV.obj.image_viewer.$container.find("img").eq(0).css("zIndex",1);CHV.obj.image_viewer.$container.find("img").eq(1).attr("src",CHV.obj.image_viewer.image.url).css({width:CHV.obj.image_viewer.$container.find("img")[0].getBoundingClientRect().width,height:CHV.obj.image_viewer.$container.find("img")[0].getBoundingClientRect().height});CHV.obj.image_viewer.$container.find("img").eq(1).imagesLoaded(function(){CHV.obj.image_viewer.$container.find("img").eq(1).css({width:"",height:""});CHV.obj.image_viewer.$container.find("img").eq(0).remove();PF.fn.loading.destroy(CHV.obj.image_viewer.$loading);ImageZoomClass();});$(document).bind("DOMSubtreeModified",function(){if($("html").height()>$(window).innerHeight()&&!$("html").hasClass("scrollbar-y")){$("html").addClass("scrollbar-y");$(document).data({width:$(this).width(),height:$(this).height()});CHV.fn.image_viewer_full_fix();}});$(window).on("resize",function(){CHV.fn.image_viewer_full_fix();});$(document).on("keyup",function(e){var $this=$(e.target),key=e.charCode||e.keyCode;if($this.is(":input")){return;}else{if(CHV.obj.image_viewer.$navigation.exists()&&(key==37||key==39)){var navigation_jump_url=$("[data-action="+(key==37?"prev":"next")+"]",CHV.obj.image_viewer.$navigation).attr("href");if(typeof navigation_jump_url!=="undefined"&&navigation_jump_url!==""){window.location=$("[data-action="+(key==37?"prev":"next")+"]",CHV.obj.image_viewer.$navigation).attr("href");}}}});}else{ImageZoomClass();}}
$(document).on("click",CHV.obj.image_viewer.container,function(e){var zoom=$(this).hasClass("cursor-zoom-in")||$(this).hasClass("cursor-zoom-out");if(!zoom)return;if(zoom&&PF.fn.isDevice(["phone","phablet"])){}
var zoom_in=$(this).hasClass("cursor-zoom-in");$(this).removeClass("cursor-zoom-in cursor-zoom-out");if(zoom_in){var width=$(this)[0].getBoundingClientRect().width,height=$(this)[0].getBoundingClientRect().height,ratio=$("img",this).attr("width")/$("img",this).attr("height"),new_width;$(this).data({dimentions:{width:width,height:height},ratio:ratio});if($("img",this).attr("width")>$(window).width()){$(this).css({width:"100%"});new_width=$(this).width();$(this).css({width:width});}else{new_width=$("img",this).attr("width");}
$(this).addClass("cursor-zoom-out").css({width:new_width,height:(new_width/ratio)+"px"});}else{$(this).addClass("cursor-zoom-in").css($(this).data("dimentions"));}
e.preventDefault();}).on("contextmenu",CHV.obj.image_viewer.container,function(e){if(!CHV.obj.config.image.right_click){e.preventDefault();return false;}});$(document).on("click","[data-action=test-email]",function(e){e.preventDefault();var $email=$("input[name=test-email]");if($email.prop("disabled")){return;}
var $parent=$(this).closest(".input-label");var email=$email.val();if(!email.isEmail()){PF.fn.growl.expirable(PF.fn._s("Please provide a valid email address"));return;}
$email.prop("disabled",true);PF.fn.loading.inline($('.loading',$parent),{size:"small",valign:"middle"});$parent.find(".btn .text").hide();$.ajax({data:{action:"test",test:{object:"email"},email:email},cache:false}).complete(function(XHR){var response=XHR.responseJSON;$email.prop("disabled",false);$('.loading',$parent).empty();$parent.find(".btn .text").show();PF.fn.growl.call(response[response.status_code==200?"success":"error"].message);});});$(document).on("click",".list-item, [data-action=list-tools] [data-action]",function(e){var $this=$(e.target),$list_item=$this.closest(".list-item");if($list_item&&$list_item.find("[data-action=select]").exists()&&(e.ctrlKey||e.metaKey)&&e.altKey){CHV.fn.list_editor.toggleSelectItem($list_item,!$list_item.hasClass("selected"));e.preventDefault();e.stopPropagation();}});PF.fn.listing.ajax.callback=function(XHR){if(XHR.status!==200)return;CHV.fn.list_editor.listMassActionSet("select");};$(document).on("click","[data-action=list-select-all]",function(){CHV.fn.list_editor.selectItem($(".list-item:visible:not(.selected)"));CHV.fn.list_editor.listMassActionSet("clear");});$(document).on("click","[data-action=list-clear-all]",function(){PF.fn.close_pops();CHV.fn.list_editor.clearSelection();});$(document).on("click","[data-action=list-tools] [data-action]",function(e){if(e.isPropagationStopped())return false;var $this_list_item=$(this).closest(PF.obj.listing.selectors.list_item),$this_list_item_tools=$(this).closest("[data-action=list-tools]");var $this_icon,this_add_class,this_remove_class,this_label_text,dealing_with;if(typeof $this_list_item.data("type")!=="undefined"){dealing_with=$this_list_item.data("type");}else{console.log("Error: data-type not defined");return;}
switch($(this).data("action")){case"select":CHV.fn.list_editor.toggleSelectItem($this_list_item,!$this_list_item.hasClass("selected"));break;case"edit":var modal_source="[data-modal=form-edit-single]";switch(dealing_with){case"image":$("[name=form-image-title]",modal_source).attr("value",$this_list_item.data("title"));$("[name=form-image-description]",modal_source).html(PF.fn.htmlEncode($this_list_item.data("description")));$("[name=form-album-id]",modal_source).find("option").removeAttr("selected");$("[name=form-album-id]",modal_source).find("[value="+$this_list_item.data(dealing_with=="image"?"album-id":"id")+"]").attr("selected",true);$("[name=form-category-id]",modal_source).find("option").removeAttr("selected");$("[name=form-category-id]",modal_source).find("[value="+$this_list_item.data("category-id")+"]").attr("selected",true);$("[name=form-nsfw]",modal_source).attr("checked",$this_list_item.data("flag")=="unsafe");$("[name=form-album-name]",modal_source).attr("value","");$("[name=form-album-description]",modal_source).html("");$("[name=form-privacy]",modal_source).find("option").removeAttr("selected");break;case"album":$("[data-action=album-switch]",modal_source).remove();$("[name=form-album-name]",modal_source).attr("value",$this_list_item.data("name"));$("[name=form-album-description]",modal_source).html(PF.fn.htmlEncode($this_list_item.data("description")));$("[name=form-privacy]",modal_source).find("option").removeAttr("selected");$("[name=form-privacy]",modal_source).find("[value="+$this_list_item.data('privacy')+"]").attr("selected",true);break;}
PF.fn.modal.call({type:"html",template:$(modal_source).html(),ajax:{url:PF.obj.config.json_api,deferred:{success:function(XHR){CHV.fn.list_editor.updateItem("[data-id="+$this_list_item.data("id")+"]",XHR.responseJSON[dealing_with],"edit");}}},confirm:function(){var $modal=$(PF.obj.modal.selectors.root);if((dealing_with=="image"||dealing_with=="album")&&$("[data-content=form-new-album]",$modal).is(":visible")&&$("[name=form-album-name]",$modal).val()==""){PF.fn.growl.call(PF.fn._s("You must enter the album name."));$("[name=form-album-name]",$modal).highlight();return false;}
if(!PF.fn.form_modal_has_changed()){PF.fn.modal.close();return;}
PF.obj.modal.form_data={action:"edit",edit:$this_list_item.data("type"),single:true,owner:CHV.obj.resource.user.id,editing:{id:$this_list_item.data("id"),description:$("[name=form-"+dealing_with+"-description]",$modal).val()}};switch(dealing_with){case"image":PF.obj.modal.form_data.editing.title=$("[name=form-image-title]",$modal).val();PF.obj.modal.form_data.editing.category_id=$("[name=form-category-id]",$modal).val()||null;PF.obj.modal.form_data.editing.nsfw=$("[name=form-nsfw]",$modal).prop("checked")?1:0;break;case"album":PF.obj.modal.form_data.editing.name=$("[name=form-album-name]",$modal).val();PF.obj.modal.form_data.editing.privacy=$("[name=form-privacy]",$modal).val();break;}
PF.obj.modal.form_data.editing.new_album=$("[data-content=form-new-album]",$modal).is(":visible");if(PF.obj.modal.form_data.editing.new_album){PF.obj.modal.form_data.editing.album_name=$("[name=form-album-name]",$modal).val();PF.obj.modal.form_data.editing.album_privacy=$("[name=form-privacy]",$modal).val();PF.obj.modal.form_data.editing.album_description=$("[name=form-album-description]",$modal).val();}else{PF.obj.modal.form_data.editing.album_id=$("[name=form-album-id]",$modal).val();}
return true;}});break;case"move":var modal_source="[data-modal=form-move-single]";$("[name=form-album-id]",modal_source).find("option").removeAttr("selected");$("[name=form-album-id]",modal_source).find("[value="+$this_list_item.data(dealing_with=="image"?"album-id":"id")+"]").attr("selected",true);$("[name=form-album-name]",modal_source).attr("value","");$("[name=form-album-description]",modal_source).html("");$("[name=form-privacy]",modal_source).find("option").removeAttr("selected");PF.fn.modal.call({type:"html",template:$(modal_source).html(),ajax:{url:PF.obj.config.json_api,deferred:{success:function(XHR){CHV.fn.list_editor.updateMoveItemLists(XHR.responseJSON,dealing_with,$this_list_item);}}},confirm:function(){var $modal=$(PF.obj.modal.selectors.root);if($("[data-content=form-new-album]",$modal).is(":visible")&&$("[name=form-album-name]",$modal).val()==""){PF.fn.growl.call(PF.fn._s("You must enter the album name."));$("[name=form-album-name]",$modal).highlight();return false;}
if(!PF.fn.form_modal_has_changed()){PF.fn.modal.close();return;}
PF.obj.modal.form_data={action:"edit",edit:$this_list_item.data("type"),single:true,owner:CHV.obj.resource.user.id,editing:{id:$this_list_item.data("id")}};PF.obj.modal.form_data.editing.new_album=$("[data-content=form-new-album]",$modal).is(":visible");if(PF.obj.modal.form_data.editing.new_album){PF.obj.modal.form_data.editing.album_name=$("[name=form-album-name]",$modal).val();PF.obj.modal.form_data.editing.album_privacy=$("[name=form-privacy]",$modal).val();PF.obj.modal.form_data.editing.album_description=$("[name=form-album-description]",$modal).val();}else{PF.obj.modal.form_data.editing.album_id=$("[name=form-album-id]",$modal).val();}
return true;}});break;case"delete":PF.fn.modal.call({type:"html",template:$("[data-modal=form-delete-single]").html(),button_submit:PF.fn._s("Confirm"),ajax:{url:PF.obj.config.json_api,deferred:{success:function(XHR){if(dealing_with=="album"){$("[name=form-album-id]","[data-modal]").find("[value="+$this_list_item.data("id")+"]").remove();CHV.fn.list_editor.updateUserCounters("image",XHR.responseJSON.success.affected,"-");}
CHV.fn.list_editor.deleteFromList($this_list_item);CHV.fn.queuePixel();}}},confirm:function(){PF.obj.modal.form_data={action:"delete",single:true,delete:$this_list_item.data("type"),deleting:{id:$this_list_item.data("id")}};return true;}});break;case"flag":$.ajax({type:"POST",data:{action:'edit',edit:'image',single:true,editing:{id:$this_list_item.data("id"),nsfw:$this_list_item.data("flag")=="unsafe"?0:1}}}).complete(function(XHR){var response=XHR.responseJSON,flag=response.image.nsfw==1?"unsafe":"safe";$this_list_item.removeClass("safe unsafe").addClass(flag).attr("data-flag",flag).data("flag",flag);CHV.fn.list_editor.selectionCount();});break;}});$(".pop-box-menu a","[data-content=list-selection]").click(function(e){var $content_listing=$(PF.obj.listing.selectors.content_listing_visible);if(typeof $content_listing.data("list")!=="undefined"){dealing_with=$content_listing.data("list");}else{console.log("Error: data-list not defined");return;}
var $targets=$(PF.obj.listing.selectors.list_item+".selected",$content_listing),ids=$.map($targets,function(e,i){return $(e).data("id");});$(this).closest(".pop-btn").click();switch($(this).data("action")){case"get-embed-codes":var template="[data-modal=form-embed-codes]";$("textarea",template).html(""),objects=[];$targets.each(function(){objects.push({image:$.parseJSON(decodeURIComponent($(this).data("object")))});});CHV.fn.fillEmbedCodes(objects,template,"html");PF.fn.modal.call({type:"html",template:$(template).html(),buttons:false});break;case"clear":CHV.fn.list_editor.clearSelection();e.stopPropagation();break;case"move":case"create-album":var template=$(this).data("action")=="move"?"form-move-multiple":"form-create-album",modal_source="[data-modal="+template+"]",dealing_id_data=(/image/.test(dealing_with)?"album-id":"id");$("[name=form-album-id]",modal_source).find("[value=null]").remove();$("[name=form-album-id]",modal_source).find("option").removeAttr("selected");$("[name=form-album-name]",modal_source).attr("value","");$("[name=form-album-description]",modal_source).html("");$("[name=form-privacy]",modal_source).find("option").removeAttr("selected");var album_id=$targets.first().data(dealing_id_data),same_album=true;$targets.each(function(){if($(this).data(dealing_id_data)!==album_id){same_album=false;return false;}});if(!same_album){$("[name=form-album-id]",modal_source).prepend('');}
$("[name=form-album-id]",modal_source).find("[value="+(same_album?$targets.first().data(dealing_id_data):"null")+"]").attr("selected",true);PF.fn.modal.call({type:"html",template:$(modal_source).html(),ajax:{url:PF.obj.config.json_api,deferred:{success:function(XHR){CHV.fn.list_editor.updateMoveItemLists(XHR.responseJSON,dealing_with,$targets);}}},confirm:function(){var $modal=$(PF.obj.modal.selectors.root),new_album=false;if($("[data-content=form-new-album]",$modal).is(":visible")&&$("[name=form-album-name]",$modal).val()==""){PF.fn.growl.call(PF.fn._s("You must enter the album name."));$("[name=form-album-name]",$modal).highlight();return false;}
if($("[data-content=form-new-album]",$modal).is(":visible")){new_album=true;}
if(!PF.fn.form_modal_has_changed()){PF.fn.modal.close();return;}
var album_object=new_album?"creating":"moving";PF.obj.modal.form_data={action:new_album?"create-album":"move",type:dealing_with,owner:CHV.obj.resource.user.id,multiple:true,album:{ids:ids,"new":new_album}};if(new_album){PF.obj.modal.form_data.album.name=$("[name=form-album-name]",$modal).val();PF.obj.modal.form_data.album.privacy=$("[name=form-privacy]",$modal).val();PF.obj.modal.form_data.album.description=$("[name=form-album-description]",$modal).val();}else{PF.obj.modal.form_data.album.id=$("[name=form-album-id]",$modal).val();}
return true;}});break;case"delete":PF.fn.modal.call({template:$("[data-modal=form-delete-multiple]").html(),button_submit:PF.fn._s("Confirm"),ajax:{url:PF.obj.config.json_api,deferred:{success:function(XHR){if(dealing_with=="albums"){$targets.each(function(){$("[name=form-album-id]","[data-modal]").find("[value="+$(this).data("id")+"]").remove();});CHV.fn.list_editor.updateUserCounters("image",XHR.responseJSON.success.affected,"-");}
CHV.fn.list_editor.deleteFromList($targets);CHV.fn.queuePixel();}}},confirm:function(){PF.obj.modal.form_data={action:"delete",from:"list","delete":dealing_with,multiple:true,deleting:{ids:ids}};return true;}});break;case"assign-category":var category_id=$targets.first().data("category-id"),same_category=true;$targets.each(function(){if($(this).data("category-id")!==category_id){same_category=false;return false;}});PF.fn.modal.call({type:"html",template:$("[data-modal=form-assign-category]").html(),forced:true,ajax:{url:PF.obj.config.json_api,deferred:{success:function(XHR){$targets.each(function(){var response=XHR.responseJSON;$(this).data("category-id",response.category_id)});CHV.fn.list_editor.clearSelection();}}},confirm:function(){var $modal=$(PF.obj.modal.selectors.root),form_category=$("[name=form-category-id]",$modal).val()||null;if(same_category&&category_id==form_category){PF.fn.modal.close(function(){CHV.fn.list_editor.clearSelection();});return false;}
PF.obj.modal.form_data={action:"edit-category",from:"list",multiple:true,editing:{ids:ids,category_id:form_category}};return true;}});break;case"flag-safe":case"flag-unsafe":var action=$(this).data("action"),flag=action=="flag-safe"?"safe":"unsafe";PF.fn.modal.call({template:$("[data-modal=form-"+action+"]").html(),button_submit:PF.fn._s("Confirm"),ajax:{url:PF.obj.config.json_api,deferred:{success:function(XHR){$targets.each(function(){$(this).removeClass("safe unsafe").addClass(flag).removeAttr("data-flag").attr("data-flag",flag).data("flag",flag);});CHV.fn.list_editor.clearSelection();}}},confirm:function(){PF.obj.modal.form_data={action:action,from:"list",multiple:true,editing:{ids:ids,nsfw:action=="flag-safe"?0:1}};return true;}});break;}
if(PF.fn.isDevice(["phone","phablet"])){return false;}});if($("body#image").exists()){$(window).scroll(function(){CHV.obj.topBar.transparencyScrollToggle();});}
$(document).on("click","[data-action=disconnect]",function(){var $this=$(this),connection=$this.data("connection");PF.fn.modal.confirm({message:$this.data("confirm-message"),ajax:{data:{action:'disconnect',disconnect:connection,user_id:CHV.obj.resource.user.id},deferred:{success:function(XHR){var response=XHR.responseJSON;$("[data-connection="+connection+"]").fadeOut(function(){$($("[data-connect="+connection+"]")).fadeIn();$(this).remove();if($("[data-connection]").length==0){$("[data-content=empty-message]").show();}
PF.fn.growl.expirable(response.success.message);});},error:function(XHR){var response=XHR.responseJSON;PF.fn.growl.call(response.error.message);}}}});});$(document).on("click","[data-action=delete-avatar]",function(){var $parent=$(".user-settings-avatar"),$loading=$(".loading-placeholder",$parent),$top=$("#top-bar");$loading.removeClass("hidden");PF.fn.loading.inline($loading,{center:true});$.ajax({type:"POST",data:{action:"delete",delete:"avatar",owner:CHV.obj.resource.user.id}}).complete(function(XHR){$loading.addClass("hidden").empty();if(XHR.status==200){if(CHV.obj.logged_user.id==CHV.obj.resource.user.id){$("img.user-image",$top).hide();$(".default-user-image",$top).removeClass("hidden");}
$(".default-user-image",$parent).removeClass("hidden").css({opacity:0});$(".btn-alt",$parent).closest("div").hide();$("img.user-image",$parent).fadeOut(function(){$(".default-user-image",$parent).animate({opacity:1});});}else{PF.fn.growl.expirable(PF.fn._s("An error occurred. Please try again later."));}});});$(document).on("change","[data-content=user-avatar-upload-input]",function(e){e.preventDefault();e.stopPropagation();var $this=$(this),$parent=$(".user-settings-avatar"),$loading=$(".loading-placeholder",".user-settings-avatar"),$top=$("#top-bar"),user_avatar_file=$(this)[0].files[0];if($this.data("uploading")){return;}
if(/^image\/.*$/.test(user_avatar_file.type)==false){PF.fn.growl.call(PF.fn._s("Please select a valid image file type."));return;}
if(user_avatar_file.size>CHV.obj.config.user.avatar_max_filesize.getBytes()){PF.fn.growl.call(PF.fn._s("Please select a picture of at most %s size.",CHV.obj.config.user.avatar_max_filesize));return;}
$loading.removeClass("hidden");PF.fn.loading.inline($loading,{center:true});$this.data("uploading",true);var user_avatar_fd=new FormData();user_avatar_fd.append("source",user_avatar_file);user_avatar_fd.append("action","upload");user_avatar_fd.append("type","file");user_avatar_fd.append("what","avatar");user_avatar_fd.append("owner",CHV.obj.resource.user.id);user_avatar_fd.append("auth_token",PF.obj.config.auth_token);avatarXHR=new XMLHttpRequest();avatarXHR.open("POST",PF.obj.config.json_api,true);avatarXHR.send(user_avatar_fd);avatarXHR.onreadystatechange=function(){if(this.readyState==4){var response=this.responseType!=="json"?JSON.parse(this.response):this.response,image=response.success.image;$loading.addClass("hidden").empty();if(this.status==200){change_avatar=function(parent){$("img.user-image",parent).attr("src",image.url).removeClass("hidden").show();};hide_default=function(parent){$(".default-user-image",parent).addClass("hidden");};hide_default($parent);$(".btn-alt",$parent).closest("div").show();change_avatar($parent);if(CHV.obj.logged_user.id==CHV.obj.resource.user.id){change_avatar($top);hide_default($top);}
PF.fn.growl.expirable(PF.fn._s("Profile image updated."));}else{PF.fn.growl.expirable(PF.fn._s("An error occurred. Please try again later."));}
$this.data("uploading",false);}}});$(document).on("change","[data-content=user-background-upload-input]",function(e){e.preventDefault();e.stopPropagation();var $this=$(this),$parent=$("[data-content=user-background-cover]"),$src=$("[data-content=user-background-cover-src]"),$loading=$(".loading-placeholder",$parent),$top=$("#top-bar"),user_file=$(this)[0].files[0];if($this.data("uploading")){return;}
if(/^image\/.*$/.test(user_file.type)==false){PF.fn.growl.call(PF.fn._s("Please select a valid image file type."));return;}
if(user_file.size>CHV.obj.config.user.background_max_filesize.getBytes()){PF.fn.growl.call(PF.fn._s("Please select a picture of at most %s size.",CHV.obj.config.user.background_max_filesize));return;}
$loading.removeClass("hidden");PF.fn.loading.inline($loading,{center:true,size:'big',color:'#FFF'});$this.data("uploading",true);var user_picture_fd=new FormData();user_picture_fd.append("source",user_file);user_picture_fd.append("action","upload");user_picture_fd.append("type","file");user_picture_fd.append("what","background");user_picture_fd.append("owner",CHV.obj.resource.user.id);user_picture_fd.append("auth_token",PF.obj.config.auth_token);avatarXHR=new XMLHttpRequest();avatarXHR.open("POST",PF.obj.config.json_api,true);avatarXHR.send(user_picture_fd);avatarXHR.onreadystatechange=function(){if(this.readyState==4){var response=this.responseType!=="json"?JSON.parse(this.response):this.response,image=response.success.image;if(this.status==200){var $img=$("");$img.attr('src',image.url).imagesLoaded(function(){$loading.addClass("hidden").empty();$src.css("background-image","url("+image.url+")").hide().fadeIn();$("[data-content=user-change-background]",$parent).removeClass("hidden");$parent.removeClass("no-background");$("[data-content=user-upload-background]").hide();$("[data-content=user-change-background]").show();PF.fn.growl.expirable(PF.fn._s("Profile background image updated."));$img.remove();if(typeof user_background_full_fix=="function"){user_background_full_fix();}});}else{$loading.addClass("hidden").empty();PF.fn.growl.expirable(PF.fn._s("An error occurred. Please try again later."));}
$this.data("uploading",false);}}});CHV.fn.user_background={delete:{submit:function(){PF.obj.modal.form_data={action:"delete",delete:"background",owner:CHV.obj.resource.user.id};return true;},deferred:{success:{before:function(XHR){$("[data-content=user-background-cover-src]").css("background-image","none");$("[data-content=user-background-cover]").addClass("no-background").height("");$("[data-content=user-upload-background]").removeClass("hidden").show();$("[data-content=user-change-background]").hide();$("#top-bar").removeClass("transparent background-transparent");$("#top-bar-shade").remove();},done:function(XHR){PF.fn.modal.close(function(){PF.fn.growl.expirable(PF.fn._s("Profile background image deleted."));});}},error:function(XHR){PF.fn.growl.expirable(PF.fn._s("Error deleting profile background image."));}}}};CHV.str.mainform="[data-content=main-form]";CHV.obj.timezone={'selector':"[data-content=timezone]",'input':"#timezone-region"};$(document).on("keyup change",CHV.str.mainform+" :input",function(){if($(this).is("[name=username]")){$("[data-text=username]").text($(this).val());}});$(document).on("change",CHV.obj.timezone.input,function(){var value=$(this).val(),$timezone_combo=$("#timezone-combo-"+value);$timezone_combo.find("option:first").prop("selected",true);$(CHV.obj.timezone.selector).val($timezone_combo.val()).change();});$(document).on("change","[id^=timezone-combo-]",function(){var value=$(this).val();$(CHV.obj.timezone.selector).val(value).change();});$(document).on("keyup change blur","[name^=new-password]",function(){var $new_password=$("[name=new-password]"),$new_password_confirm=$("[name=new-password-confirm]"),hide=$new_password.val()==$new_password_confirm.val(),$warning=$new_password_confirm.closest(".input-password").find(".input-warning");if($(this).is($new_password_confirm)){$new_password_confirm.data("touched",true);}
if($new_password_confirm.data("touched")){$warning.text(!hide?$warning.data("text"):"")[!hide?'removeClass':'addClass']('hidden-visibility');}});$(document).on("submit",CHV.obj.mainform,function(){switch($(this).data("type")){case"password":var $p1=$("[name=new-password]",this),$p2=$("[name=new-password-confirm]",this);if($p1.val()!==""||$p2.val()!==""){if($p1.val()!==$p2.val()){$p1.highlight();$p2.highlight();PF.fn.growl.expirable(PF.fn._s("Passwords don't match"));return false;}}
break;}});$(document).on("change","[name=theme_tone]",function(){$("html")[0].className=$("html")[0].className.replace(/\btone-[\w-]+\b/g,'');$("html").addClass("tone-"+$(this).val());});$(document).on("change","[name=theme_top_bar_color]",function(){$("#top-bar, .top-bar").removeClass("black white").addClass($(this).val());});$(document).on("click","[data-action=check-for-updates]",function(){PF.fn.loading.fullscreen();CHV.fn.system.checkUpdates(function(XHR){PF.fn.loading.destroy("fullscreen");if(XHR.status!==200){PF.fn.growl.call(PF.fn._s("An error occurred. Please try again later."));return;}
var data=XHR.responseJSON.software;if(PF.fn.versionCompare(CHV.obj.system_info.version,data.current_version)==-1){PF.fn.modal.simple({title:PF.fn._s("Update available v%s",data.current_version),message:'
'+PF.fn._s('There is an update available for your system. You can automatic download and install this update or go to %s to proceed to download the file.','chevereto.com')+'
'+PF.fn._s('The release notes for this update are:')+'