//Creado con Ardora - www.webardora.net
//bajo licencia Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)
//para otros usos contacte con el autor
var collapsed = false;var allUser;var userN;var userName;var userGru;var userCur;var userType;var extraObj;var indexCom=0;var actualFile="";var maxShared;var maxUser;var useShared;var useUser;
$(document).ready(function() {$("#atab1").css("background","#FFFFFF");$("#atab1").css("fontWeight","bold");$("#divFull").hide();$("#infoFile").hide();getIdUser();
$("#sidebar-toggle").click(function() {if(!collapsed){$("#sidebar").animate({width: "0px"});$(".main-content").animate({width: "100%"},function(){visorResize();});
} else { $("#sidebar").animate({width:"30%"}); $(".main-content").animate({width:"70%"},function(){visorResize();});} collapsed = !collapsed;});
$(".ctab").hide();$("#tab1").show();$(".atab").click(function(e) {$(".atab").css("backgroundImage","linear-gradient(to bottom,#FFFFFF,#C0C0C0)");$(".atab").css("fontWeight","normal");e.preventDefault();$(".ctab").hide();$("#" + $(this).attr("id").substring(1,9)).show();$(this).css("background","#FFFFFF");$(this).css("fontWeight","bold");});
showShared();showPersonal();$("#bRefreshS").click(function(e) {showShared();});$("#bRefreshPersonal").click(function(e) {showPersonal();});$("#bRefreshAlumnado").click(function(e) {showAlumnado();});
$("#bNewFolderS").click(function(e) {addFolder(0);});$("#bNewFolderPersonal").click(function(e) {addFolder(1);});$("#bNewFolderAlumnado").click(function(e) {addFolder(2);});$("#bDelFolderS").click(function(e) {delFolder(0);});$("#bDelFolderPersonal").click(function(e) {delFolder(1);});$("#bDelFolderAlumnado").click(function(e) {delFolder(2);});
$("#bUpLoadS").click(function(e) {upLoad(0);});$("#bUpLoadPersonal").click(function(e) {upLoad(1);});$("#bUpLoadAlumnado").click(function(e) {upLoad(2);});});
function showShared(){$(".butBarVisor").hide();$("#infoFile").hide();actualFile="";$("#visor").empty();$("#carpeta_compartida").fileTree({expandSpeed: 750, collapseSpeed: 750, multiFolder: false,
root: "../ardoraWorkFiles/filemanager3/filemanager3_Shared/",script: "php/jqueryFileTree.php" },function(file) { openFile(file);},function(dir) { openDir(dir);});getStateFolder(0);$("#bDelFolderS").hide();$("#folderName").html("/");}
function showPersonal(){$(".butBarVisor").hide();$("#infoFile").hide();$("#visor").empty();actualFile="";if (userType=="admin"){
rootP="../ardoraWorkFiles/filemanager3/filemanager3_Profesorado/"+userN+"/";}else{
rootP="../ardoraWorkFiles/filemanager3/filemanager3_Alumnado/"+userN+"/";}
$("#carpeta_personal").fileTree({expandSpeed: 750, collapseSpeed: 750, multiFolder: false,root: rootP,script:"php/jqueryFileTree.php"},function(file) { openFile(file);},function(dir) { openDir(dir);});getStateFolder(1);$("#bDelFolderPersonal").hide();}
function showAlumnado(){$(".butBarVisor").hide();$("#infoFile").hide();actualFile="";$("#visor").empty();$("#carpeta_alumnado").fileTree({expandSpeed: 750, collapseSpeed: 750, multiFolder: false,
root:"../ardoraWorkFiles/filemanager3/filemanager3_Alumnado/",script:"php/jqueryFileTree.php"},function(file) { openFile(file);},function(dir) { openDir(dir);});$("#bDelFolderAlumnado").hide();}
function openFile(file){$("#visor").empty();switch (file.substring(file.lastIndexOf(".")+1,400).toLowerCase()) {case "jpg": case "jpeg": case "png": case "bmp": case "svg": visorImage(file.substring(3,file.length));break;
case "pdf": case "pdf": case "odt": case "ods": case "odp": case "sxw": case "sxc": case "sxi":visorViewerJS(file.substring(3,file.length));break;
case "mp3": case "ogg":visorAudio(file.substring(3,file.length));break; case "mp4": case "ogv": visorVideo(file.substring(3,file.length));break;
case "txt": visorTxt(file.substring(3,file.length)); break; case "swf": visorSwf(file.substring(3,file.length)); break; default: visorNo();}
setButFunction(file.substring(3,file.length));$("#bDelFolderS").hide();$("#bDelFolderPersonal").hide();$("#bDelFolderAlumnado").hide();$("#infoFile").hide();actualFile=file;}
function openDir(dir){if ($("#tab1").is(":visible")) {$("#folderName").html(dir);if(dir.length>1){$("#bDelFolderS").show();}else{$("#bDelFolderS").hide();}}
if ($("#tab2").is(":visible")) {$("#folderNameUser").html(dir);if(dir.length>1){$("#bDelFolderPersonal").show();}else{$("#bDelFolderPersonal").hide();}}
if ($("#tab3").length){if ($("#tab3").is(":visible")) {$("#folderNameAlu").html(dir);}if (dir.length>1){$("#name_alu").html(allUser[dir.substr(1,dir.length-2)]);$("#bUpLoadAlumnado").show();$("#bNewFolderAlumnado").show();$("#bDelFolderAlumnado").show();}
else{$("#name_alu").html("");$("#bUpLoadAlumnado").hide();$("#bNewFolderAlumnado").hide();$("#bDelFolderAlumnado").hide();}}$(".butBarVisor").hide();$("#visor").empty();}
function getIdUser() {$.ajax({data: {"action": "getIdUser"}, type: "POST",dataType: "json",async: false,url: "php/ardoraFileManager.php",success: function(data, textStatus, jqXHR) {
userN=data["userN"];userName = data["userName"];userGru = data["userGru"];userCur = data["userCur"];allUser=data["allUser"];userType = "alu";if (data["userType"]=="profe" || data["userType"] == "admin") {userType = "admin";addAlus();}
$("#hUser").text(userName);
}});}
function getStateFolder(ft){$.ajax({data: {"action": "getStateFolder","fType":ft}, type: "POST",dataType: "json",url: "php/ardoraFileManager.php",success: function(data, textStatus, jqXHR) {
if (ft==0){maxShared=parseFloat(data["sMax"]);useShared=parseFloat(data["useSize"]);if (parseInt(data["useSize"])>parseInt(data["sMax"])){$("#bUpLoadS").hide();$("#estado_compartida").css("color","red");}
else{$("#estado_compartida").css("color","black");$("#bUpLoadS").show();} $("#estado_compartida").html(data["state"]);}
if (ft==1){maxUser=parseFloat(data["sMax"]);useUser=parseFloat(data["useSize"]);if (parseInt(data["useSize"])>parseInt(data["sMax"])){$("#bUpLoadPersonal").hide();$("#estado_personal").css("color","red");}else{$("#estado_personal").css("color","black");$("#bUpLoadPersonal").show();}
$("#estado_personal").html(data["state"]);}}});}
function addAlus(){$("#tabs").append('<li><a href="#" id="atab3" class="atab">Students</a></li>');$("#contentTabs").append('<div id="tab3" class="ctab"></div>');
$("#tab3").append('<div class="folderIcon"><img src="css/images/folder_open.png" width="16" height="16" alt="Carpeta aberta" longdesc="Imaxen de carpeta aberta"/></div>');
$("#tab3").append('<div id="folderNameAlu" class="cfolderName">/</div>');$("#tab3").append('<div id="arbol2"><div id="carpeta_alumnado" class="directorio"></div><div id="name_alu" class="barraEstado"></div></div>')
$("#tab3").append('<div class="butBarFM"><p id="bNewFolderAlumnado" class="butfont butLe but" title="New folder">m</p><p id="bDelFolderAlumnado" class="butfont butLe but" title="Delete folder">n</p><p id="bUpLoadAlumnado" class="butfont butRi but" title="">I</p><p id="bRefreshAlumnado" class="butfont butRi but" title="Recharge">Z</p></div>');
$("#bUpLoadAlumnado").hide();$("#bNewFolderAlumnado").hide();$("#bDelFolderAlumnado").hide();showAlumnado();}
function addFolder(fType){if (fType==0) {var tit=$("#folderName").text();}if (fType==1) {var tit=$("#folderNameUser").text();}if (fType==2) {var tit=$("#folderNameAlu").text();}
$("#modal-content").empty();$("#modal-content").css("height", "auto");var newHtml = '<div id="modalTit">New folder</div>';
newHtml = newHtml + '<div id="modalBody">Enter the folder name <b>' + tit + '</b> ';
newHtml = newHtml + '<input type="text" id="fName" name="fName" placeholder="Write here"><br>';
newHtml = newHtml + '<div id="modalBar"><p id="cancelBut" class="butfont butLe red" title="Cancel">E</p><p id="oKBut" class="butfont butRi green" title="Accept">g</p></div></div>';
$("#modal-content").append(newHtml);$("#modal-content,#modal-background").toggleClass("active");$("#modal-background, #modal-close, #cancelBut").click(function() {$("#modal-content,#modal-background").toggleClass("active");});
$("#modal-content").draggable();$("#oKBut").click(function() {if ($("#fName").val().length>0){var folder=$("#fName").val();folder=normalize(folder.replace(/^\s+/g,"").replace(/\s+$/g,""));
$.ajax({data: {"action": "makeDir","fName":tit.substr(1)+folder,"fType": fType},type: "POST",dataType: "json",async: false,url: "php/ardoraFileManager.php",success: function(data, textStatus, jqXHR) {}}); if (fType==0){showShared()} if (fType==1){showPersonal()} if (fType==2){showAlumnado()}
$("#modal-content,#modal-background").toggleClass("active");}else{$("#modal-content,#modal-background").toggleClass("active");}});}
function delFolder(fType){if (fType==0) {var tit=$("#folderName").text();}if (fType==1) {var tit=$("#folderNameUser").text();}if (fType==2) {var tit=$("#folderNameAlu").text();}$("#modal-content").empty();$("#modal-content").css("height", "auto");
var newHtml = '<div id="modalTit">Delete folder</div>';newHtml = newHtml + '<div id="modalBody">Delete folder <b>' + tit + '</b>? ';
newHtml = newHtml + '</div>';newHtml = newHtml + '<div id="modalBar"><p id="cancelBut" class="butfont butLe red" title="Cancel">E</p><p id="oKBut" class="butfont butRi green" title="Accept">g</p></div>';
$("#modal-content").append(newHtml);$("#modal-content,#modal-background").toggleClass("active");$("#modal-background, #modal-close, #cancelBut").click(function() {$("#modal-content,#modal-background").toggleClass("active");});$("#modal-content").draggable();
$("#oKBut").click(function() {var folder=tit.substr(1);folder=folder.substr(0,folder.length-1);
$.ajax({data: {"action": "deleteDir","fName":folder,"fType": fType},type: "POST",dataType: "json",async: false,url: "php/ardoraFileManager.php",success: function(data, textStatus, jqXHR) {}});
if (fType==0){showShared()} if (fType==1){showPersonal()} if (fType==2){showAlumnado()} $("#modal-content,#modal-background").toggleClass("active");});}
function upLoad(fType){indexCom=0;if (fType==0) {var tit=$("#folderName").text();} if (fType==1) {var tit=$("#folderNameUser").text();} if (fType==2) {var tit=$("#folderNameAlu").text();}$("#modal-content").empty();$("#modal-content").css("height", "auto");
var newHtml = '<div id="modalTit">Upload</div>';newHtml = newHtml + '<div id="modalBody">Save files <b>' + tit + '</b>';
newHtml = newHtml + '<div id="mulitplefileuploader">Upload</div><div id="status"></div>';
newHtml = newHtml + '<div id="modalBar"><p id="cancelBut" class="butfont butLe red" title="Cancel">E</p><p id="oKBut" class="butfont butRi green" title="Accept">g</p></div>';
$("#modal-content").append(newHtml);$("#modal-content,#modal-background").toggleClass("active");$("#modal-background, #modal-close, #cancelBut").click(function() {$("#modal-content,#modal-background").toggleClass("active");});$("#modal-content").draggable();
loadUploadForm(fType,tit.substr(1));$("#oKBut").click(function() {var totSize=0;$(".ajax-file-upload-filename").each(function(i) {var str=$(this).text().substring($(this).text().indexOf("(")+1);str=str.substring(0,str.length-1);
var val=str.substring(0,str.length-2);str=str.substring(str.length-2); if (str=="MB"){totSize=totSize+(parseFloat(val)*1024*1024);} if (str=="KB"){totSize=totSize+(parseFloat(val)*1024);}});
if (fType==0) {getStateFolder(0)}else{getStateFolder(1)}if (useShared+totSize<maxShared){$(".extrahtml").hide();$("#mulitplefileuploader").hide();extraObj.startUpload();}
else{var html="<div class='ajax-file-upload-error'>Folder size exceeded</div>"; $("#mulitplefileuploader").append(html);}});}
function loadUploadForm(fType,fol){extraObj = $("#mulitplefileuploader").uploadFile({url:"php/upload.php?fType="+fType+"&fol="+fol,fileName:"myfile",method: "POST",
allowedTypes:"jpg,gif,png,bmp,svg,mp4,ogv,swf,txt,pdf,mp3,ogg,odt,ods,odp,sxw,sxc,sxi,zip,rar,tar,7z,gzip,bz2,wav,flac,m4a",multiple: true,
maxFileSize:120*1048576,
maxFileCount:10,
maxFileCountErrorStr:"Maximum number of files ",
dragDropStr: "<span><b>Drop files to upload</b></span>",
abortStr:"E",cancelStr:"b",doneStr:"End",
multiDragErrorStr: "File type not allowed",extErrorStr:"File type not allowed --> ",
sizeErrorStr:"File too large ",uploadErrorStr:"Subida de archivos no permitida",uploadStr:"File",
afterUploadAll:function(obj){$("#status").html("End"); if (fType==0){showShared()} if (fType==1){showPersonal()} if (fType==2){showAlumnado()} $("#modal-content,#modal-background").toggleClass("active");},
onSuccess:function(files,data,xhr){for (i=0;i<extraObj.existingFileNames.length;i++){if (extraObj.existingFileNames[i]==files[0]){
$.ajax({data: {"action": "addXml","fName":fol,"fileName":files[0],"coment":$("#tags"+i.toString()).val(),"fType": fType}, type: "POST",dataType: "json",async: false,url: "php/ardoraFileManager.php",success: function(data, textStatus, jqXHR) {}});}}},
onError: function(files,status,errMsg){$("#status").html("<font color='red'>Error while saving the file</font>");},
extraHTML:function(){var html = "<div><b>Comment </b><input type='text' name='tags"+indexCom+"' id='tags"+indexCom+"' value='' class='coments' /> <br/>";html += "</div>";indexCom++;return html;},autoSubmit:false});}
function visorImage(file){$("#visor").width("auto");$("#visor").height("auto");$("#visor").append('<img id="ima" name="ima" src="'+file+'" alt="'+file.substring(file.lastIndexOf("/")+1,800)+'" />');
$("#ima").on("load", function () {imaResize();});$("#visor").on("resize", function () {imaResize();});$(window).resize(function () {imaResize();});$("#sidebar").resize(function () {imaResize();});}
function visorResize(){if ( $("#ima").length > 0 ) {imaResize()};}
function imaResize(){var wVal=($(".main-content").width() - 30)+"px";var hVal=($(".main-content").height() -90)+"px";$("#ima").css("maxWidth",wVal);$("#ima").css("maxHeight",hVal);}
function visorViewerJS(file){$("#visor").width("calc(100% - 40px)");$("#visor").height("100%");$("#visor").append('<iframe id="viewer" src = "ViewerJS/#../'+file+'" allowfullscreen webkitallowfullscreen></iframe>');}
function visorAudio(file){$("#visor").width("auto");$("#visor").height("auto");
if (file.substring(file.lastIndexOf(".")+1,400).toLowerCase()=="mp3"){$("#visor").append('<br><audio controls><source src="'+file+'" type="audio/mpeg"/></audio><br>');}
if (file.substring(file.lastIndexOf(".")+1,400).toLowerCase()=="ogg"){$("#visor").append('<br><audio controls><source src="'+file+'" type="audio/ogg"/></audio><br>');}}
function visorVideo(file){$("#visor").width("auto");$("#visor").height("auto");
if (file.substring(file.lastIndexOf(".")+1,400).toLowerCase()=="mp4"){$("#visor").append('<br><video controls><source src="'+file+'" type="video/mpeg"/></video><br>');}
if (file.substring(file.lastIndexOf(".")+1,400).toLowerCase()=="ogv"){$("#visor").append('<br><video controls><source src="'+file+'" type="video/ogg"/></video><br>');}}
function visorTxt(file){$("#visor").width("95%");$("#visor").height("95%");$.ajax({data: {"action": "getTxt","fName":file},type: "POST",dataType: "json",async: false,url: "php/ardoraFileManager.php",
success: function(data, textStatus, jqXHR){$("#visor").append('<div id="visorTXT">'+data["txt"]+'</div>')}});}
function visorSwf(file){$("#visor").width("100%");$("#visor").height("100%");$("#visor").append('<object id="swfO" type="application/x-shockwave-flash" data="'+file+'"></object>');}
function visorNo(){$("#visor").width("auto");$("#visor").height("auto");$("#visor").append('<p id="visorNo">Q</p>');}
function setButFunction(file){$("#divFull").empty();$("#bDownFile").unbind("click");$("#bInfoFile").unbind("click");$("#bSeeFile").unbind("click");$("#bDeleteFile").unbind("click");
$(".butBarVisor").show();$("#eFileName").text(file.substring(file.lastIndexOf("/")+1,800));var ext=file.substring(file.lastIndexOf(".")+1,400).toLowerCase();
if  (ext=="jpg" || ext=="jpeg" || ext=="png" || ext=="bmp" || ext=="svg"){$("#bSeeFile").show();$("#bSeeFile").click(function(){
$("#divFull").append('<img id="imaFull" name="ima" src="'+file+'" alt="'+file.substring(file.lastIndexOf("/")+1,800)+'" />');
$("#divFull").show(function() {var wVal=($("#divFull").width())+"px";var hVal=($("#divFull").height())+"px";$("#imaFull").css("maxHeight",hVal);$("#imaFull").css("maxWidth",wVal);});
launchFullScreen(document.getElementById("divFull"));document.addEventListener("fullscreenchange", function () {if(!document.fullscreen){$("#divFull").hide();$("#divFull").empty();}}, false);
document.addEventListener("mozfullscreenchange", function () {if (!document.mozFullScreen){$("#divFull").hide();$("#divFull").empty();}}, false);
document.addEventListener("webkitfullscreenchange", function () {if (!document.webkitIsFullScreen){$("#divFull").hide();$("#divFull").empty();}}, false);});} else{$("#bSeeFile").hide();}
$("#bDownFile").click(function(){parent.location.href="php/ardoraFileManager.php?action=download&&Arquivo="+file;});
$("#bInfoFile").click(function(){if (file!=actualFile){$.ajax({data: {"action": "getInfoFile","fName":file},type: "POST",dataType: "json",async: false,url: "php/ardoraFileManager.php",
success: function(data, textStatus, jqXHR){$("#infoFile").empty();html="<p>"+data["coment"]+"</p>";html=html+"<div id='dt'>";if (data["tab"]==0){html=html+data["usr"];}
html=html+"  "+data["datetime"];html=html+"</div>";$("#infoFile").append(html);}}); actualFile=file;} $("#infoFile").slideToggle( "slow");});
$("#bDeleteFile").click(function(){$("#modal-content").empty();$("#modal-content").css("height", "auto");var newHtml = '<div id="modalTit">Delete</div>';
newHtml = newHtml + '<div id="modalBody">Are you sure you want to delete it? <b>' + file.substring(file.lastIndexOf("/")+1,800) + ' ?</b><br>';
newHtml = newHtml + '<div id="modalBar"><p id="cancelBut" class="butfont butLe red" title="Cancel">E</p><p id="oKBut" class="butfont butRi green" title="Accept">g</p></div>';
$("#modal-content").append(newHtml);$("#modal-content,#modal-background").toggleClass("active");$("#modal-background, #modal-close, #cancelBut").click(function() {$("#modal-content,#modal-background").toggleClass("active");});
$("#modal-content").draggable();$("#oKBut").unbind("click");$("#oKBut").click(function() {$.ajax({data: {"action": "deleteFile","fName":file},type: "POST",dataType: "json",async: false,url: "php/ardoraFileManager.php",success: function(data, textStatus, jqXHR){}});
if ($("#tab1").css("display")!="none"){showShared()} if ($("#tab2").css("display")!="none"){showPersonal()} if ($("#tab3").css("display")!="none"){showAlumnado()} $("#modal-content,#modal-background").toggleClass("active");$("#visor").empty();});});}
function launchFullScreen(element) {if(element.requestFullScreen) {element.requestFullScreen();} else if(element.mozRequestFullScreen) {element.mozRequestFullScreen();} else if(element.webkitRequestFullScreen) {element.webkitRequestFullScreen();}}
function cancelFullscreen() {if(document.cancelFullScreen) {document.cancelFullScreen();} else if(document.mozCancelFullScreen) {document.mozCancelFullScreen();} else if(document.webkitCancelFullScreen) {document.webkitCancelFullScreen();}}
var normalize = (function() {var from = "ÃÀÁÄÂÈÉËÊÌÍÏÎÒÓÖÔÙÚÜÛãàáäâèéëêìíïîòóöôùúüûÑñÇç", to   = "AAAAAEEEEIIIIOOOOUUUUaaaaaeeeeiiiioooouuuunncc",mapping = {};
for(var i = 0, j = from.length; i < j; i++ )mapping[ from.charAt( i ) ] = to.charAt( i );return function( str ) {var ret = [];for( var i = 0, j = str.length; i < j; i++ ) {
var c = str.charAt(i); if( mapping.hasOwnProperty( str.charAt( i ) ) ) ret.push( mapping[ c ] );else ret.push( c );}return ret.join( "" ).replace( /[^-A-Za-z0-9]+/g, "_" );}})();
