
mediaBrowser={};lang.media={en:{uploading:"Uploading File...",new_file:"New File",del:"Delete",cancel:"Cancel",insert:"Insert",upload:"Upload",add_fol:"Add Folder",saving_to_system:"Saving file to system..."},es:{uploading:"Subiendo Archivo...",new_file:"Nuevo",del:"Borrar",cancel:"Cancelar",insert:"Usar",upload:"Subir",add_fol:"Agregar Carpeta",saving_to_system:"Guardando archivo en sistema..."}}
MediaBrowserComponent=Class.create();MediaBrowserComponent.prototype={initialize:function(app,app_root,up_id,parent,path_type,options){this.id='media_browser';this.self=$(this.id);this.constants={MB_FOLDER_LABEL_LIMIT:14,MB_FILE_LABEL_LIMIT:18,MB_LABEL_LIMIT_DEFAULT:20,MB_LABEL_TAIL_LENGTH:10};this.options=options||{};var now=new Date();this.up_id=Math.round(now.getTime()*(Math.random()+0.1))+'.'+Math.round(now.getTime()*(Math.random()+0.1));this.parent=parent;this.cur_path_type=path_type||'files';this.status=new StatusComponent('media_status',this.options.color);this.btn={};this.btn.cancel=new ButtonComponent(this,'media_btn_cancel','/view/theme/'+theme+'/media/shared/icon_cancel.gif',lang.media[cur_lang].cancel);this.btn.save=new ButtonComponent(this,'media_btn_save','/view/theme/'+theme+'/media/shared/icon_ok.gif',lang.media[cur_lang].insert);this.btn.save.disable();this.btn.upload_cancel=new ButtonComponent(this,'media_btn_upload_cancel','/view/theme/'+theme+'/media/shared/icon_cancel.gif',lang.media[cur_lang].cancel);this.btn.upload=new ButtonComponent(this,'media_btn_upload','/view/theme/'+theme+'/media/shared/icon_ok.gif',lang.media[cur_lang].upload);this.upload_btns=$('media_upload_btns');this.group_btns=$('media_group_btns');this.file_btns=$('media_file_btns');this.folder_btns=$('media_folder_btns');this.folder_id=$('folder_id');this.browser=$('media_columns');this.form_wrapper=$('media_form_wrapper');this.path=$('media_path');this.search=$('media_search');this.form_title=$('form_title');this.form=$('media_form');$('media_upload_id_field').value=this.up_id;this.form.action=app+"?cmd=SaveMedia&UPLOAD_IDENTIFIER="+this.up_id;this.form.target="media_hidden_frame";if(console)console.log('FORM_ACTION: '+this.form.action);this.modules_holder=$('media_module_column').getElementsByTagName('div')[2];this.partition_holder=$('partition_folder_holder').getElementsByTagName('div')[0];this.folder_holder=$('partition_folder_holder').getElementsByTagName('div')[4];this.file_holder=$('media_files_column').getElementsByTagName('div')[3];this.preview_holder=$('media_preview_column').getElementsByTagName('div')[1];this.media_filter=$('media_filter');this.old_filter='';this.elmtsOn=new Array();this.elmtsOff=new Array();this.cur_module={};this.cur_partition={};this.cur_folder={};this.cur_file={};this.app=app;this.injectBehaviours();this.initAjax();},injectBehaviours:function(){this.btn.save.setAction(this.save.bind(this));this.btn.cancel.setAction(this.closeBrowser.bind(this));this.btn.upload_cancel.setAction(this.cancelUpload.bind(this));this.btn.upload.setAction(this.triggerUpload.bind(this));},initAjax:function(){ajaxEngine.registerRequest(this.id+'_request',this.app);ajaxEngine.registerAjaxObject(this.id+'_init',this);ajaxEngine.registerAjaxObject(this.id+'_filelisting',this);ajaxEngine.registerAjaxObject(this.id+'_folderlisting',this);ajaxEngine.registerAjaxObject(this.id+'_filepreview',this);ajaxEngine.registerAjaxObject(this.id+'_plugin',this);ajaxEngine.registerAjaxObject(this.id+'_uploadstatus',this);ajaxEngine.registerAjaxObject(this.id+'_uploadresults',this);ajaxEngine.registerAjaxObject(this.id+'_shareresults',this);ajaxEngine.registerAjaxObject(this.id+'_deleteresults',this);ajaxEngine.registerAjaxObject(this.id+'_moveresults',this);},callRicoAjaxEngine:function(callParams){var additionalParams=this.options.requestParameters||[];for(var i=0;i<additionalParams.length;i++)callParams.push(additionalParams[i]);if(console)console.log('PARAMS: '+callParams);ajaxEngine.sendRequest.apply(ajaxEngine,callParams);},initBrowser:function(){var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=InitBrowser');callParams.push('id='+this.id);this.callRicoAjaxEngine(callParams);},deleteFile:function(){this.status.show('');var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=DeleteMedia');callParams.push('id='+this.id);callParams.push('file='+this.cur_file.value);callParams.push('file_index='+this.cur_file.id);this.callRicoAjaxEngine(callParams);},saveFolder:function(){if((ApexUtil.trim(this.folder_name.value)).length==0){this.folder_holder.removeChild(this.folder_holder.lastChild);}else{if(console)console.log('saving folder...');var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=SaveFolder');callParams.push('id='+this.id);callParams.push('type='+this.cur_module.value);callParams.push('partition='+this.cur_partition.value);callParams.push('folder='+this.folder_name.value);this.callRicoAjaxEngine(callParams);}},updateFolder:function(name){if((ApexUtil.trim(this.folder_name.value)).length==0){this.folder_name.value=this.old_edit;this.folder_name.focus();}else{if(console)console.log('updating folder...');var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=UpdateFolder');callParams.push('id='+this.id);callParams.push('type='+this.cur_module.value);callParams.push('folder='+this.folder_name.value);callParams.push('folder_id='+this.cur_folder.value);callParams.push('partition='+this.cur_partition.value);this.callRicoAjaxEngine(callParams);}},deleteFolder:function(){var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=DeleteFolder');callParams.push('id='+this.id);callParams.push('type='+this.cur_module.value);callParams.push('folder='+this.cur_folder.value);callParams.push('partition='+this.cur_partition.value);this.callRicoAjaxEngine(callParams);},addFolder:function(){this.createInput('add');},createInput:function(type){var m_item=document.createElement('div');m_item.id='media_folder_new';var state=" on";m_item.className="media_item"+state;m_item.value='';var icon=document.createElement('div');icon.className="media_icon folder";var label=document.createElement('div');var input=document.createElement('input');input.id='folder_name';input.style.width="80%";label.appendChild(input);m_item.onmouseover=function(){this.style.cursor='pointer';}
m_item.onmouseout=function(){this.style.cursor='';}
m_item.appendChild(icon);m_item.appendChild(label);if(type=='edit'){this.folder_wrapper=this.folder_holder.childNodes[this.cur_folder.id];this.folder_wrapper.style.display='none';input.value=this.folder_wrapper.lastChild.longname;this.old_edit=this.folder_wrapper.lastChild.innerHTML;this.folder_wrapper.removeChild(this.folder_wrapper.firstChild);this.folder_wrapper.replaceChild(input,this.folder_wrapper.firstChild);this.folder_wrapper.style.display='';}else{this.folder_holder.appendChild(m_item);}
this.folder_name=$('folder_name');this.folder_name.onblur=this.blurFolder.bindAsEventListener(this,type);this.folder_name.onkeyup=this.keyupHandler.bindAsEventListener(this,this.folder_name);this.folder_name.focus();},keyupHandler:function(e,elmt){if(e.keyCode==13){elmt.blur();}},getFileListing:function(){this.status.show('');var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=GetFileListing');callParams.push('id='+this.id);callParams.push('type='+this.cur_module.value);callParams.push('partition='+this.cur_partition.value);var folder=(typeof(this.cur_folder.value)!='undefined')?this.cur_folder.value:'';callParams.push('folder='+folder);this.callRicoAjaxEngine(callParams);},getFolderListing:function(){this.status.show('');var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=GetFolderListing');callParams.push('id='+this.id);callParams.push('type='+this.cur_module.value);callParams.push('partition='+this.cur_partition.value);this.callRicoAjaxEngine(callParams);},getFilePreview:function(file_id){if(!this.moving_file){this.status.show('');var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=GetPreview');callParams.push('id='+this.id);callParams.push('file_id='+file_id);callParams.push('type='+this.cur_module.value);callParams.push('partition='+this.cur_partition.value);this.callRicoAjaxEngine(callParams);}},loadPlugin:function(filename){this.status.show('');var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=LoadPlugin');callParams.push('id='+this.id);callParams.push('filename='+filename);callParams.push('partition='+this.cur_partition.value);var folder=(typeof(this.cur_folder.value)!='undefined')?this.cur_folder.value:'';callParams.push('folder_id='+folder);this.callRicoAjaxEngine(callParams);},uploadStatus:function(){var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=UploadStatus');callParams.push('id='+this.id);callParams.push('UPLOAD_IDENTIFIER='+this.up_id);this.callRicoAjaxEngine(callParams);},deleteTmpUpload:function(){var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=CancelUpload');callParams.push('id='+this.id);callParams.push('UPLOAD_IDENTIFIER='+this.up_id);this.callRicoAjaxEngine(callParams);},getUploadResult:function(){clearTimeout(this.timer);if(console)console.log('CALLING UPLOAD RESULTS');var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=GetUploadResults');callParams.push('id='+this.id);var folder=(typeof(this.cur_folder.id)!='undefined')?this.cur_folder.id:'';callParams.push('folder='+folder);this.callRicoAjaxEngine(callParams);},moveFile:function(file,folder){this.moving_file=true;$(file.id).style.display='none';var callParams=[];callParams.push(this.id+'_request');callParams.push('cmd=MoveFile');callParams.push('id='+this.id);callParams.push('file='+file.value);callParams.push('folder='+folder.value);callParams.push('partition='+this.cur_partition.value);this.callRicoAjaxEngine(callParams);},ajaxUpdate:function(ajaxResponse){switch(ajaxResponse.getAttribute('id')){case this.id+'_init':this.browserStartup(ajaxResponse);break;case this.id+'_filelisting':this.updateFileListing(ajaxResponse);break;case this.id+'_folderlisting':this.updateFolderListing(ajaxResponse);break;case this.id+'_filepreview':if(!this.moving_file)this.updateFilePreview(ajaxResponse);break;case this.id+'_plugin':this.displayPlugin(ajaxResponse);break;case this.id+'_uploadstatus':this.displayUploadStatus(ajaxResponse);break;case this.id+'_uploadresults':this.targetNewFile(ajaxResponse);break;case this.id+'_moveresults':this.moving_file=false;break;case this.id+'_shareresults':this.shareResults(ajaxResponse);break;case this.id+'_deleteresults':this.updateAfterDelete(ajaxResponse);break;case this.id+'_moveresults':this.moving_file=false;this.status.hide();break;}},browserStartup:function(ajaxResponse){this.status.hide();this.self.style.visibility='hidden';this.self.style.display='';this.centerPane();this.upload_btns.style.display='none';this.file_btns.style.display='';this.group_btns.style.display='';this.folder_btns.style.display='';this.form_title.style.display='none';this.path.style.display='';this.search.style.display='';this.form_wrapper.style.display='none';this.browser.style.display='';this.injectModules(ajaxResponse.getElementsByTagName('module'));this.injectPartitions(ajaxResponse.getElementsByTagName('partition'));this.injectFolders(ajaxResponse.getElementsByTagName('folder'));this.injectFiles(ajaxResponse.getElementsByTagName('file'));this.updateFilePath();this.show();},injectModules:function(modules){this.modules_holder.innerHTML='';if(modules){this.cur_module={id:0,value:modules[0].getAttribute('code')};for(var i=0;i<modules.length;++i){var m=modules[i];var mod=document.createElement('div');var state=(i==this.cur_module.id)?"on":"off";mod.className="media_module clearfix "+state;var icon=document.createElement('div');icon.className="module_icon "+m.getAttribute('icon');var label=document.createElement('div');label.className="module_label";label.appendChild(document.createTextNode(RicoUtil.getContentAsString(m)));mod.onmouseover=function(){this.style.cursor='pointer';}
mod.onmouseout=function(){this.style.cursor='';}
mod.onclick=this.clickModule.bindAsEventListener(this,[i,m.getAttribute('code')]);mod.appendChild(icon);mod.appendChild(label);this.modules_holder.appendChild(mod);}}},injectPartitions:function(partitions){this.own_partition={id:0,value:partitions[0].getAttribute('id')};this.clearFileList();this.elmtsOff=[];this.elmtsOn=[];this.partition_holder.innerHTML='';if(partitions){this.cur_partition={id:0,value:partitions[0].getAttribute('id')};for(var i=0;i<partitions.length;++i){var p=partitions[i];var name=RicoUtil.getContentAsString(p);var m_item=document.createElement('div');m_item.id='media_partition_'+i;var state=(i==this.cur_partition.id)?" on":"";if(i==this.cur_partition.id)this.cur_partition.name=name;m_item.className="media_item"+state;m_item.value=p.getAttribute('id');var icon=document.createElement('div');if(i==0){icon.className="media_icon partition_home";}else{icon.className="media_icon partition_shared";}
var label=document.createElement('div');label.appendChild(document.createTextNode(name));m_item.onmouseover=function(){this.style.cursor='pointer';}
m_item.onmouseout=function(){this.style.cursor='';}
m_item.onclick=this.clickPartition.bindAsEventListener(this,[i,p.getAttribute('id')]);m_item.appendChild(icon);m_item.appendChild(label);this.partition_holder.appendChild(m_item);}}},injectFolders:function(folders){this.elmtsOff=[];this.elmtsOn=[];this.folder_holder.innerHTML='';if(folders.length>0){for(var i=0;i<folders.length;++i){var fol=folders[i];var m_item=document.createElement('div');m_item.id='media_folder_'+i;m_item.className="media_item";m_item.className="media_item";m_item.value=fol.getAttribute('id');var trash=document.createElement('div');trash.className="deletethisflat";trash.onclick=this.deleteFolder.bindAsEventListener(this);var icon=document.createElement('div');icon.className="media_icon folder";var label=document.createElement('div');label.className="media_label "+fol.getAttribute('type');label.longname=RicoUtil.getContentAsString(fol);label.appendChild(document.createTextNode(this.formatLabel(unescape(label.longname),this.constants.MB_FOLDER_LABEL_LIMIT)));var regxp=new RegExp("\(\\d+\)");regxp.test(label.longname);if(console)console.log('REG MATCH: '+RegExp["$1"]);label.longname=label.longname.replace(" ("+RegExp["$1"]+")",'');m_item.onmouseover=function(){this.style.cursor='pointer';}
m_item.onmouseout=function(){this.style.cursor='';}
m_item.ondblclick=this.dblclickFolder.bindAsEventListener(this,[i,fol.getAttribute('id')]);m_item.onclick=this.clickFolder.bindAsEventListener(this,[i,fol.getAttribute('id')]);m_item.appendChild(icon);m_item.appendChild(trash);m_item.appendChild(label);this.folder_holder.appendChild(m_item);Droppables.add(m_item.id,{accept:'media_item',hoverclass:'media_droppable',onDrop:this.moveFile.bind(this)});}
this.clickedFolder=false;}},injectFiles:function(files){this.initSearch(files);this.file_holder.innerHTML='';this.cur_file=this.cur_file||{};if(console)console.log(this.cur_file.id+" :: "+this.cur_file.value);if(files.length>0){for(var i=0;i<files.length;++i){var f=files[i];var m_item=document.createElement('div');m_item.id='media_file_'+i;m_item.className="media_item";m_item.value=f.getAttribute('value');if(this.cur_file.value){if(this.cur_file.value==m_item.value){this.cur_file.id=i;}}
var icon=document.createElement('div');icon.className="media_icon "+f.getAttribute('ext');var label=document.createElement('div');label.className="media_label";label.longname=RicoUtil.getContentAsString(f);label.appendChild(document.createTextNode(this.formatLabel(unescape(label.longname),this.constants.MB_FILE_LABEL_LIMIT)));var trash=document.createElement('div');trash.className="deletethisflat";trash.onclick=this.deleteFile.bindAsEventListener(this);m_item.onmouseover=function(){this.style.cursor='pointer';}
m_item.onmouseout=function(){this.style.cursor='';}
m_item.onclick=this.clickFile.bindAsEventListener(this,[i,f.getAttribute('value')]);m_item.appendChild(icon);m_item.appendChild(trash);m_item.appendChild(label);this.file_holder.appendChild(m_item);m_item.drag=new Draggable(m_item.id,{revert:true,handle:'media_icon',snap:true,zindex:1500,ghosting:true});}
if(this.cur_file.value){this.clickFile(null,[this.cur_file.id,this.cur_file.value]);}}},injectFilePreview:function(attributes){this.preview_holder.innerHTML=RicoUtil.getContentAsString(attributes[0]);},formatLabel:function(label,limit){var lim=limit||this.constants.MB_LABEL_LIMIT_DEFAULT;var len=label.length;if(len>lim){var diff=len-lim;var start=label.substr(0,lim-this.constants.MB_LABEL_TAIL_LENGTH);var end=label.substr(len-this.constants.MB_LABEL_TAIL_LENGTH,this.constants.MB_LABEL_TAIL_LENGTH);label=start+"..."+end;}
return label;},blurFolder:function(e,type){this.clearFilePreview();this.resetSearch();if(type=='edit'){this.updateFolder();this.updateFileListing();}else{this.saveFolder();}
this.clickedFolder=false;},clickModule:function(e,args){var cur=this.modules_holder.childNodes[this.cur_module.id];cur.className=ApexUtil.replaceClass(cur,'on','off');var new_mod=this.modules_holder.childNodes[args[0]];new_mod.className=ApexUtil.replaceClass(new_mod,'off','on');this.cur_module={id:args[0],value:args[1]};this.cur_folder={};this.cur_file={};this.getFolderListing();this.getFileListing();this.updateFilePath();this.clearFilePreview();this.resetSearch();},clickPartition:function(e,args){var cur=this.partition_holder.childNodes[this.cur_partition.id];cur.className=ApexUtil.removeClass(cur,'on');var new_mod=this.partition_holder.childNodes[args[0]];new_mod.className=ApexUtil.addClass(new_mod,'on');var label=new_mod.childNodes[1];this.cur_partition={id:args[0],value:args[1],name:label.innerHTML};this.cur_folder={};this.cur_file={};this.getFolderListing();this.getFileListing();this.updateFilePath();this.clearFilePreview();this.resetSearch();},clickFolder:function(e,args){var cur=this.folder_holder.childNodes[this.cur_folder.id];if(cur!=undefined){cur.className=ApexUtil.removeClass(cur,'on');}
var new_mod=this.folder_holder.childNodes[args[0]];new_mod.className=ApexUtil.addClass(new_mod,'on');this.cur_folder={id:args[0],value:args[1]};if(console)console.log('ID: '+this.cur_folder.id+' value: '+this.cur_folder.value);this.cur_file={};if(this.folder_select){this.btn.save.enable();}else{this.btn.save.disable();}
if(this.btn.share_file)this.btn.share_file.disable();this.getFileListing();this.updateFilePath();this.clearFilePreview();this.resetSearch();},clickFile:function(e,args){if(this.cur_file.value&&this.cur_file.id!=args[0]){var cur=this.file_holder.childNodes[this.cur_file.id];cur.className=ApexUtil.removeClass(cur,'on');}
var new_mod=this.file_holder.childNodes[args[0]];new_mod.className=ApexUtil.addClass(new_mod,'on');this.cur_file={id:args[0],value:args[1]};this.updateFilePath();this.getFilePreview(args[1]);this.btn.save.enable();},dblclickFolder:function(e,args){if(!this.clickedFolder){this.clickedFolder=true;this.createInput('edit');}},initSearch:function(files){this.elmtsOn=[];this.elmtsOff=[];for(var i=0;i<files.length;i++){var f=files[i];var nEntry={name:RicoUtil.getContentAsString(f),divId:("media_file_"+i)};this.elmtsOn.push(nEntry);}},printArray:function(arr){var str='';for(var i=0;i<arr.length;i++){str+=' Name: '+arr[i].name;}
return str;},filterFiles:function(){var oldstr="^"+this.old_filter;var newstr="^"+this.media_filter.value;var oldExp=new RegExp(oldstr);var newExp=new RegExp(newstr);var newExpStr=this.media_filter.value;var newPattern=new RegExp(newExpStr,"i");if(console)console.log('new: '+this.media_filter.value);if(console)console.log('old: '+this.old_filter);if(!oldExp.test(this.media_filter.value)){if(newExp.test(this.old_filter)){if(console)console.log('something deleted...');for(var i=this.elmtsOff.length-1;i>=0;i--){if(newPattern.test(this.elmtsOff[i].name)){if(console)console.log(this.elmtsOff[i].name+' switch to ON iterator: '+i);this.divSwitchOn=$(this.elmtsOff[i].divId);this.divSwitchOn.style.display='';this.elmtsOn.push(this.elmtsOff[i]);this.elmtsOff.splice(i,1);}}}else{if(console)console.log('something totally new...');this.elmtsOn=this.elmtsOn.concat(this.elmtsOff);this.elmtsOff=[];if(console)console.log('size: '+this.elmtsOn.length);for(var i=this.elmtsOn.length-1;i>=0;i--){this.divSwitchOn=$(this.elmtsOn[i].divId);this.divSwitchOn.style.display='';if(!newPattern.test(this.elmtsOn[i].name)){if(console)console.log(this.elmtsOn[i].name+' switch to OFF iterator: '+i);this.divSwitchOff=$(this.elmtsOn[i].divId);this.divSwitchOff.style.display='none';this.elmtsOff.push(this.elmtsOn[i]);this.elmtsOn.splice(i,1);}else{if(console)console.log(this.elmtsOn[i].name+" matches with "+newExpStr);}}}}else{if(console)console.log('something added...');for(var i=this.elmtsOn.length-1;i>=0;i--){if(!newPattern.test(this.elmtsOn[i].name)){if(console)console.log(this.elmtsOn[i].name+' switch to OFF iterator: '+i);this.divSwitchOff=$(this.elmtsOn[i].divId);this.divSwitchOff.style.display='none';this.elmtsOff.push(this.elmtsOn[i]);this.elmtsOn.splice(i,1);}}}
if(console)console.log("OFF: "+this.printArray(this.elmtsOff));if(console)console.log("ON: "+this.printArray(this.elmtsOn));this.old_filter=this.media_filter.value;},updateFilePath:function(){var module=RicoUtil.getContentAsString(this.modules_holder.childNodes[this.cur_module.id].childNodes[1]);var partition=this.cur_partition.name;var file;if(this.cur_file.value){file=unescape(this.file_holder.childNodes[this.cur_file.id].childNodes[2].longname);}else{file="";}
this.cur_path="/"+this.cur_path_type+"/"+partition+"/"+file;this.path.innerHTML=this.cur_path;},centerPane:function(){var w=this.self.offsetWidth;var h=this.self.offsetHeight;var pageScroll=ApexUtil.getPageScroll();var left=((screen_overlay.arrayPageSize[2]-w)/2);var top=((screen_overlay.arrayPageSize[3]-h)/2);if(console)console.log('page coords: width:'+w+', height:'+h);this.self.style.top=(top<0)?"0px":(pageScroll[1]+top)+"px";this.self.style.left=(top<0)?"0px":left+"px";},clearFilePreview:function(){this.preview_holder.innerHTML='';},clearFileList:function(){this.file_holder.innerHTML='';},resetSearch:function(){this.media_filter.value='';},updateFileListing:function(ajaxResponse){this.injectFiles(ajaxResponse.getElementsByTagName('file'));this.status.hide();},updateFolderListing:function(ajaxResponse){this.injectFolders(ajaxResponse.getElementsByTagName('folder'));this.status.hide();},updateFilePreview:function(ajaxResponse){this.injectFilePreview(ajaxResponse.getElementsByTagName('attributes'));this.status.hide();},updateAfterDelete:function(ajaxResponse){this.cur_file={};this.clearFilePreview();this.getFileListing();},shareResults:function(ajaxResponse){this.status.hide();},displayPlugin:function(ajaxResponse){var error=ajaxResponse.getElementsByTagName('error')[0];if(error){var msg=RicoUtil.getContentAsString(error);this.status.show(msg,'error');$('upload_file').value='';}else{var form=ajaxResponse.getElementsByTagName('plugin_form')[0];this.form_wrapper.innerHTML=unescape(RicoUtil.getContentAsString(form));this.file_btns.style.display='none';this.group_btns.style.display='none';this.folder_btns.style.display='none';this.upload_btns.style.display='';var title=ajaxResponse.getElementsByTagName('plugin_title')[0];this.form_title.innerHTML=RicoUtil.getContentAsString(title);this.path.style.display='none';this.search.style.display='none';this.form_title.style.display='';this.status.hide();this.switchViews(this.browser,this.form_wrapper);}},cancelUpload:function(){this.deleteTmpUpload();this.upload_btns.style.display='none';this.file_btns.style.display='';this.group_btns.style.display='';this.folder_btns.style.display='';this.form_title.style.display='none';this.path.style.display='';this.search.style.display='';this.switchViews(this.form_wrapper,this.browser);clearTimeout(this.stat_timer);clearTimeout(this.timer);this.upload_complete=true;this.status.hide();$('media_upload_file').value='';},checkStatus:function(){if(this.finishedUpload){this.finishedUpload=false;this.upload_complete=false;clearTimeout(this.stat_timer);this.status.resetLoader();this.timer=setTimeout(this.getUploadResult.bind(this),1000);}else{this.timer=setTimeout(this.checkStatus.bind(this),1000);if(!this.stat_timer)this.stat_timer=setTimeout(this.uploadStatus.bind(this),500);}},triggerUpload:function(){this.finishedUpload=false;this.upload_complete=false;this.timer=null;this.stat_timer=null;this.status.resetLoader();this.form.submit();this.checkStatus();},displayUploadStatus:function(ajaxResponse){if(!this.upload_complete){var msg=ajaxResponse.getElementsByTagName('msg')[0];var perc=RicoUtil.getContentAsString(ajaxResponse.getElementsByTagName('perc')[0]);this.status.show(RicoUtil.getContentAsString(msg),'loader',{perc:perc});if(perc<98){this.stat_timer=setTimeout(this.uploadStatus.bind(this),1000);}else{clearTimeout(this.stat_timer);this.upload_complete=true;this.status.show(lang.media[cur_lang].saving_to_system);this.status.resetLoader();}}},targetNewFile:function(ajaxResponse){var m=RicoUtil.getContentAsString(ajaxResponse.getElementsByTagName('module')[0]);m=m.split(':');var cur=this.modules_holder.childNodes[this.cur_module.id];if(cur!=undefined){cur.className=ApexUtil.replaceClass(cur,'on','off');}
var new_mod=this.modules_holder.childNodes[m[0]];if(new_mod){new_mod.className=ApexUtil.replaceClass(new_mod,'off','on');this.cur_module={id:m[0],value:m[1]};}
var fol=RicoUtil.getContentAsString(ajaxResponse.getElementsByTagName('folder')[0]);fol=fol.split(':');if(this.cur_folder.id!=undefined){var cur=this.folder_holder.childNodes[this.cur_folder.id];if(cur!=undefined||cur)cur.className=ApexUtil.removeClass(cur,'on');}
if(fol[0]!=undefined){var new_mod=this.folder_holder.childNodes[fol[0]];if(new_mod!=undefined||new_mod){new_mod.className=ApexUtil.addClass(new_mod,'on');this.cur_folder={id:fol[0],value:fol[1]};}else{this.cur_folder={};}}
var selected=ajaxResponse.getElementsByTagName('files')[0].getAttribute('selected');this.cur_file={value:selected};this.injectFiles(ajaxResponse.getElementsByTagName('file'));this.cancelUpload();},open:function(parent,fullPath,path_type,folder){this.parent=parent;this.return_full_path=fullPath;this.cur_path_type=path_type||'files';this.folder_select=folder||false;screen_overlay.show();screen_overlay.parent=this;this.status.hide();this.cur_module={};this.cur_folder={};this.cur_file={};this.clearFilePreview();this.clearFileList();this.initBrowser();},closeBrowser:function(){screen_overlay.hide();},save:function(){this.closeBrowser();if(this.parent){if(this.return_full_path){this.parent.mediaInsert(this.cur_path);}else{if(this.cur_file.value){var name=this.file_holder.childNodes[this.cur_file.id].childNodes[2].longname;this.parent.mediaInsert(this.cur_file.value+'|'+name);}else{if(this.cur_folder.value&&this.folder_select){var name=this.folder_holder.childNodes[this.cur_folder.id].childNodes[2].longname;this.parent.mediaInsert(this.cur_folder.value+':folder'+'|'+name);}}}}},show:function(){this.self.style.display='none';this.self.style.visibility='';var effect=new Effect.Appear(this.self,{duration:0.25,transition:Effect.Transitions.linear,from:0.0,to:1.0});this.firstLoad=true;},hide:function(){this.self.style.display='none';},switchViews:function(cur,target,type){var me=this;switch(type){case'show':var effect=new Effect.Appear(target,{duration:0.25,transition:Effect.Transitions.linear,from:0.0,to:1.0});break;default:var effect=new Effect.Fade(cur,{duration:0.25,transition:Effect.Transitions.linear,from:1.0,to:0.0,afterFinish:function(){me.switchViews(cur,target,'show');}});break;}}};
