/* Copyright (C) YOOtheme GmbH, YOOtheme Proprietary Use License (http://www.yootheme.com/license) */

jQuery(function($){var path,url=widgetkitajax,token=$("#widgetkit").data("token");$('select[name="settings[style]"]').bind("change",function(){$("#form").trigger("submit")});var finder=$("#finder").finder({url:url+"&task=dirs_gallery"}).delegate("a","click",function(e){$("#finder li").removeClass("selected");path=$(this).parent().addClass("selected").data("path")});$("#gallery").delegate(".delete-photo","click",function(e){e.preventDefault();var btn=$(this);if(confirm("Do you really want to delete "+btn.data("path")+" ?")){$.post(url+"&task=delete_file_gallery",{file:btn.data("path"),token:token},function(response){var status=$.parseJSON(response)["status"]||0;if(status){btn.parent().fadeOut(300,function(){$().remove()})}else{alert("Couldn't delete "+btn.data("path")+" !")}})}});$("a.create-folder").bind("click",function(e){e.preventDefault();var root=(path?path:"")+"/",name=prompt("Create new folder in: "+root,"");if(name!==null&&name!==""){$.post(url+"&task=create_folder_gallery",{path:root+name,token:token},function(response){var status=$.parseJSON(response)["status"]||0;if(!status){alert("Couldn't create "+name+" !");return}if(root=="/"){finder.trigger("retrieve:finder").trigger("retrieve:finder")}else{finder.find(".selected a").trigger("click").trigger("click")}})}});$("a.delete-folder").bind("click",function(e){e.preventDefault();if(!path){alert("Please select a folder!");return}var folders_in_path=[];$('#form input[name="paths[]"]').each(function(){var field=$(this);if(field.val().indexOf(path)!=-1){folders_in_path.push(field.closest(".box"))}});if(confirm("Do you really want to delete "+path+" ?")){$.post(url+"&task=delete_folder_gallery",{path:path,token:token},function(response){var status=$.parseJSON(response)["status"]||0;if(!status){alert("Couldn't delete "+path+" !");return}finder.find(".selected").fadeOut(300,function(){$(this).remove();path=null});if(folders_in_path.length){$.each(folders_in_path,function(){$(this).fadeOut(300,function(){$(this).remove()})})}})}});$("button.add-folder").bind("click",function(e){e.preventDefault();var duplicate;$.each($('#form input[name="paths[]"]').serializeArray(),function(i,field){if(field.value==path){duplicate=true}});if(!path||duplicate)return;$("#gallery").trigger("add",[path])});$("#gallery").bind("add",function(e,path){var $this=$(this),container=$('<div class="box"><div class="deletable"></div><h3>'+path+'</h3><div class="content"></div></div>').appendTo($this),content=container.find(".content"),dropinfo=null,meter=null,loading=false,loadimages=function(){if(loading){return}content.children().remove();loading=true;$.post(url+"&task=files_gallery",{path:path},function(data){$.each(data,function(i,val){content.append('<div class="file"><div class="image"><img src="'+$this.data("url")+val.path+'" class="size-auto"/></div><div class="filename">'+val.name+'</div><input type="text" name="captions['+val.path+']" placeholder="Enter caption here..." /><input type="text" name="links['+val.path+']" placeholder="Enter custom link here..." /> <button data-path="'+val.path+'" class="delete-photo"></button></div>')});content.append('<input type="hidden" name="paths[]" value="'+path+'" />');if($.support.ajaxupload){content.append('<div class="dropinfo">Drop new images here to upload.</div>')}$("input:text",$this.trigger("update")).placeholder();loading=false},"json")};content.uploadOnDrag({action:url+"&task=upload_files_gallery",single:true,params:{path:path,token:token},loadstart:function(){dropinfo=content.find(".dropinfo");meter=dropinfo.html('<div class="meter">&nbsp;</div>').find(".meter")},progress:function(percent){meter.css("width",String(percent)+"%").html(String(parseInt(percent))+"%")},allcomplete:function(){loadimages();content.find(".dropinfo").html("Drop new images here to upload.")}});loadimages()});$("#gallery").delegate(".box","delete",function(e,button){$(this).fadeOut(300,function(){$(this).remove()})})});