CX.Controls.FileCabinet=Class.create({initialize:function(b,d,c,a){if(arguments.length==0){return;
}if(!a||!a.defaultFolder){throw"No default folder specified.";
}this.filesElement=$(d);
this.foldersElement=$(c);
this.uploadFrameId=this.filesElement.autoId()+"_UploadFrame";
this.defaultFolder=a.defaultFolder;
this.subgroup=a.subgroup;
this.baseUrl=CX.url_root()+(this.subgroup?"/group/"+this.subgroup:"")+"/file_cabinet";
this.applyStatistics(a);
this._onFolderList();
this._onFolderSelected(b||this.defaultFolder);
if(this.level){this._onFinishAjaxUpload=this.finishAjaxUpload.bind(this);
}},toggleAll:function(a){$$(".select_upload").each(function(b){b.checked=!!a;
});
},refresh:function(){var a=[0],b;
b=function(){if(a[0]++){this.unlock();
}}.bind(this);
this.lock();
this.refreshFiles(this.folder||this.defaultFolder,b);
this.refreshFolderList(b);
},refreshFolderList:function(b){var a=b?function(c){this._onFolderList(c);
b(c);
}:this._onFolderList;
this.folders=$A([]);
this.folderMap=$H({});
bigLoading(this.foldersElement);
new Ajax.Updater(this.foldersElement,this.baseUrl+"/show_file_folders",{method:"get",evalScripts:true,onComplete:a.bind(this),onFailure:this._onFailure.bind(this)});
},refreshFiles:function(c,d){var b=$F("per_page"),a=d?function(f){this._onFileList(f);
d(f);
}:this._onFileList;
if(c!=this.folder){this._onFolderSelected(c);
}bigLoading(this.filesElement);
new Ajax.Updater(this.filesElement,this.baseUrl+"/show_file_uploads/"+this.folder,{method:"get",evalScripts:true,onComplete:a.bind(this),onFailure:this._onFailure.bind(this),parameters:{per_page:b}});
},expandCollapse:function(b){var a=$(b).up(".folder-item-line");
if(a&&a.next()){a.toggleClassName("folder-open").toggleClassName("folder-closed");
a.next().toggle();
}},navToFolder:function(a){this.expandCollapse(a);
this.lock();
var b=function(c){this._onFileList(c);
this.unlock();
}.bind(this);
this.refreshFiles(a.id.substring(1),this.unlock.bind(this));
},enableAjaxUpload:function(){if(this.level){CX.mktempHiddenFrame(this.uploadFrameId,this.filesElement,this._onFinishAjaxUpload);
}},finishAjaxUpload:function(){CX.Overlays.closeFixedDialog();
this.refreshFolderList();
var a=this.uploadFrameId;
window.setTimeout(function(){CX.destroyTempHiddenFrame(a);
},10);
if(this.full&&!this.warnedUpload){this.warnedUpload=true;
return this.errorMessage("This file upload would meet or exceed the "+this.human_limit+" limit for this file cabinet.");
}return true;
},_sendFolderCommand:function(f,b,d){if(this.level<2){return;
}var a=(f=="delete_folder"&&b==this.folder)?this.defaultFolder:this.folder;
var c=Prototype.emptyFunction;
if(f=="rename_folder"){c=Element.update.curry("folder_name_caption",d["folder[name]"].escapeHTML());
}bigLoading(this.foldersElement);
new Ajax.Updater(this.foldersElement,this.baseUrl+"/"+f+"/"+b,{parameters:d,evalScripts:true,onSuccess:c,onComplete:function(g){this._onFolderList(g);
this.refreshFiles(a);
}.bind(this)});
},_sendFileCommand:function(i,g,c,b){if(!this.level){return;
}var f=[],a=$F("per_page"),h,d;
if(!b){$$(".select_upload").each(function(j){if(j.checked){f.push(j.value);
}});
if(f.length==0){return this.errorMessage("No files selected.");
}}else{f.push(b);
}bigLoading(this.filesElement);
h={"files[]":f,per_page:a};
if(c){h.name=c;
}g=g||this.folder;
this._onFolderSelected(g);
new Ajax.Updater(this.filesElement,this.baseUrl+"/"+i+"/"+g,{parameters:h,evalScripts:true,onComplete:function(j){this.refreshFolderList();
this._onFileList(j);
}.bind(this)});
},promptUploadToFolder:function(c,b){if(!this.level){return;
}if(this.full){return this.errorMessage("You have reached the "+this.human_limit+" limit for this file cabinet.");
}this.warnedUpload=false;
var d=$("form_upload_file");
this.enableAjaxUpload();
$("upload_target").update(this.folderMap[this.folder].name);
this._openDialog(c,d,b);
if(d.action){var a=d.action.lastIndexOf("/");
d.action=d.action.substring(0,a+1)+this.folder;
}},promptEditUpload:function(b,d,a){var c=$("edit_upload");
c.action=c.action.replace(/(\d)+$/,b.id);
$("edit_upload_file").update(b.name);
$("edit_upload_display_name").setValue(b.display_name);
$("edit_upload_file_date").setValue(b.file_date);
$("edit_upload_description").setValue(b.description);
this._openDialog(d,c,a);
},ajaxEditUpload:function(){var a=$("edit_upload");
new Ajax.Request(a.action,{parameters:a.serialize(),onSuccess:function(b){fileControl.refreshFiles(fileControl.folder);
$("edit_upload_error_text").hide();
CX.Overlays.closeFixedDialog();
},onFailure:function(b){$("edit_upload_error_text").update(b.responseText);
$("edit_upload_error_text").show();
}});
},promptNewFolder:function(b,a){if(this.level<2){return;
}this._populateFolderTarget("New","parent_target");
this._openDialog(b,$("form_new_folder"),a);
},promptUpdateFolder:function(c){if(this.level<2){return;
}var b=this.folderMap[c];
if(!b){return;
}$("update_folder_id").value=""+c;
$("update_folder_name").update(b.name.escapeHTML());
var a=$("update_folder_visibility");
a.selectedIndex=(b.visibility=="managers")?0:(a.options.length-(b.visibility=="anyone"?1:2));
this._openDialog(null,$("form_update_folder"));
},promptRenameFolder:function(b){if(this.level<2){return;
}var a=this.folderMap[b];
if(!a){return;
}$("rename_folder_id").value=""+b;
$("rename_folder_name").value=a.name;
$("rename_folder_oldname").update(a.name.escapeHTML());
this._openDialog(null,$("form_rename_folder"));
},promptDeleteFolder:function(b){if(this.level<2){return;
}var a=this.folderMap[b];
if(a&&confirm('Delete the "'+a.name+'" folder along with ALL file(s) or sub-folder(s) inside this folder?  You cannot undo this.')){this._sendFolderCommand("delete_folder",b);
}},promptMoveSelected:function(c,b){if(!this.level||!this._assertSomeFilesSelected()){return;
}if($("target_type_gallery")&&$("target_type_gallery").checked){this._populateFolderTarget("Move","gallery_target");
var a=($("gallery_target").selectedIndex==0)?"show":"hide";
$("target_new_gallery")[a]();
}else{this._populateFolderTarget("Move","folder_target");
}this._openDialog(c,$("form_move_files"),b);
},promptDeleteSelected:function(){if(this.level&&this._assertSomeFilesSelected()&&confirm("Delete the selected file(s) from this folder?")){this._sendFileCommand("delete_files");
}},promptDeleteFile:function(b,a){if(this.level&&confirm("Are you sure you want to delete the file:\n\n    "+a+"\n\nYou cannot undo this.")){this._sendFileCommand("delete_files",this.folder,null,b);
}},_assertSomeFilesSelected:function(){var a=$$(".select_upload:checked").length>0;
if(!a){this.errorMessage("No files selected.");
}return a;
},_populateFolderTarget:function(f,d){if(f=="New"){var a=$("new_folder_visiblity");
if(a&&this.subgroup&&a=="managers"){a.selectedIndex=1;
}}var b=false,c="";
if(f=="New"&&!this.subgroup){c+='<option value="">(Top Level)</option>\n';
}this.folders.each(function(g){if(g.id){if(f!="Move"||g.id!=this.folder){c+='<option value="'+g.id+'">'+g.indent+g.name+"</option>\n";
}}else{if(b){c+="</optgroup>\n";
}b=true;
c+='<optgroup label="'+g.indent+g.name+'">\n<option value="">(Top Level)</option>\n';
}});
if(b){c+="</optgroup>\n";
}$(d).replace('<select id="'+d+'" name="'+d+'" autocomplete="off">'+c+"</select>");
},errorMessage:function(a){alert(a);
return false;
},onFileUpload:function(a){CX.Overlays.hideFixedDialog();
bigLoading(this.filesElement);
return CX.targetTempHiddenFrame(a,this.uploadFrameId);
},onNewFolder:function(){var b=$F("new_folder_name"),c=$F("parent_target"),a=$F("new_folder_visibility");
if(!b||b.blank()){return this.errorMessage("Folder name is missing.");
}CX.Overlays.closeFixedDialog();
var d={"folder[name]":b};
if(a&&!(this.subgroup&&a=="managers")){d["folder[visibility]"]=a;
}this._sendFolderCommand("new_folder",c,d);
},onRenameFolder:function(){var c=$F("rename_folder_id"),a=$F("rename_folder_name"),b=$("rename_folder_oldname").innerHTML.unescapeHTML();
if(!a||a.blank()){return this.errorMessage("Folder name is missing.");
}CX.Overlays.closeModalDialog();
if(a==b){return;
}this._sendFolderCommand("rename_folder",c,{"folder[name]":a});
},onUpdateFolder:function(){var b=$F("update_folder_id"),a=$F("update_folder_visibility");
CX.Overlays.closeModalDialog();
this._sendFolderCommand("update_folder",b,{"folder[visibility]":a});
},onMoveSelected:function(){if(!this._assertSomeFilesSelected()){return;
}var b,a,c=null;
if($("target_type_gallery")&&$("target_type_gallery").checked){a="move_to_gallery";
b=$F("gallery_target");
if(!b||b=="0"||b.blank()){c=$F("new_gallery_name");
}}else{a="move_files";
b=$F("folder_target");
}CX.Overlays.closeFixedDialog();
this._sendFileCommand(a,b,c);
},lock:function(){if(this.locked){return;
}this.locked=true;
CX.Forms.lock("file_cabinet_content","file_cabinet_controls");
},unlock:function(){if(!this.locked){return;
}CX.Forms.unlock("file_cabinet_content","file_cabinet_controls");
this.locked=false;
},applyStatistics:function(a){if(!a){return;
}this.level=a.level||this.level||0;
this.full=a.full||this.full||false;
if(a.galleries){this.galleries=a.galleries;
}if(a.human_limit){$("cabinet_human_limit").update(this.human_limit=a.human_limit);
}if(a.percentage_used){$("cabinet_percentage_used").update(this.percentage_used=a.percentage_used);
}},_openDialog:function(c,b,a){var d=c?"fixedDialog":"modalDialog";
this.lock();
CX[d](c,b,Object.extend({close:this.unlock.bind(this)},a||{}));
},_onFailure:function(a){this.applyStatistics(a?a.responseJSON:null);
this.refreshFiles(this.defaultFolder,this.unlock.bind(this));
return false;
},_onFileList:function(c){this.applyStatistics(c?c.headerJSON:null);
var b=$("gallery_target");
if(b&&this.galleries){b.options.length=0;
for(var a=0;
a<this.galleries.length;
a++){b.options[a]=new Option(this.galleries[a][0],this.galleries[a][1]);
}}},_onFolderList:function(c){this.applyStatistics(c?c.headerJSON:null);
this.folders=$A([]);
this.folderMap=$H({});
var a=this.foldersElement.getElementsByTagName("li");
for(var b=0;
b<a.length;
b++){var d={};
if(a[b].className.indexOf("folder-item")<0||!(d.name=a[b].getAttribute("folderName"))){continue;
}d.indent=a[b].getAttribute("folderIndent")||"";
d.id=a[b].getAttribute("folderId");
d.id=(d.id&&!d.id.blank()&&d.id.charAt(0)=="f")?d.id.substring(1):null;
d.size=a[b].getAttribute("folderSize");
d.size=(d.size&&!d.size.blank()?parseInt(d.size):0);
d.parent_id=a[b].getAttribute("folderParentId");
d.parent_id=(d.parent_id&&!d.parent_id.blank()&&d.parent_id.charAt(0)=="f")?d.parent_id.substring(1):null;
d.visibility=a[b].getAttribute("folderVisibility")||"members";
this.folderMap[d.id]=d;
this.folders.push(d);
}},_onFolderSelected:function(d){var c,g=this.folderMap[d];
if(!g){return;
}$("folder_name_caption").update(g.name.escapeHTML());
if(this.level>=2){var b=$("folder_item_"+g.id).down("a.iact"),a=$("current_folder_actions");
if(a){a.update(b?b.readAttribute("menuItems"):"");
}}if(this.folder){for(g=this.folderMap[this.folder];
g&&(c=$("folder_item_line_"+g.id));
g=this.folderMap[g.parent_id]){c.removeClassName(g.id==this.folder?"folder-selected":"child-folder-selected");
}}this.folder=d;
for(g=this.folderMap[d];
g&&(c=$("folder_item_line_"+g.id));
g=this.folderMap[g.parent_id]){c.addClassName(g.id==d?"folder-selected":"child-folder-selected");
}}});
ASSET_MANIFEST=["/javascripts/cx/app/file_cabinet.js"];
try{GLOBAL_ASSET_MANIFEST=GLOBAL_ASSET_MANIFEST.concat(ASSET_MANIFEST);
}catch(e){GLOBAL_ASSET_MANIFEST=ASSET_MANIFEST;
}
