(function($){$.attachments={initiator:function(){$.attachments.buttonsUI($('div.attachments'));$('div.attachments button.loadattachments').click(function(){var articleid_element=$(this).attr('id').split('_');articleid=articleid_element[1]||0;$.attachments.enableDialog($(this))})},enableDialog:function(clickable){var enableDialog=$('body > .'+$.attachments.settings.theme+'');if(enableDialog.length)return false;$('<div id="attachments"></div>').appendTo('body');var attachment=$('#attachments');attachment.dialog({title:$.attachments.language.CDATTACHMENTS,height:400,width:600,draggable:false,resizeable:false,position:'center',stack:false,buttons:{'close':function(){$(this).dialog('close')},'files':function(){$.attachments.filemgmt(attachment)},'addnew':function(){$.attachments.addnew(attachment)},'back':function(){$.attachments.load(attachment)}},open:function(){clickable.attr('disabled',true).addClass('ui-state-disabled');$.attachments.theme($(this));$.attachments.load($(this));$.attachments.buttonTitles();$.attachments.buttonRoutine()},close:function(){clickable.attr('disabled',false).removeClass('ui-state-disabled');$.attachments.removeDialog($(this));$('body > #attachments').remove()}})},load:function(container){$.ajax({type:'POST',dataType:'html',timeout:8000,cache:true,data:'attachments=getItems'+'&articleid='+articleid+'&'+$.attachments.settings.token+'=1',beforeSend:function(){$.attachments.loading();container.dialog('disable')},success:function(msg){container.html(msg);$.attachments.buttonsUI(container);$.attachments.fileinformationicon(container);$.attachments.deleteIcon(container);$.attachments.editIcon(container);var form=container.find('form[name="downloadfile"]');form.submit(function(e){var password=$(this).find('input[name="filepassword"]:hidden');if(!password.length)return true;var pass=$.trim(prompt($.attachments.language.CDATTACHMENTS_FILE_IS_PROTECTED,''));if(pass!=null&&pass!=''){password.val(pass)}else{e.preventDefault();return false}})},error:function(){alert($.attachments.language.CDATTACHMENTS_ERROR);return false},complete:function(){$.attachments.buttonRoutine();$.attachments.loading();container.dialog('enable')}})},filemgmt:function(container){$.ajax({type:'POST',dataType:'html',timeout:8000,cache:true,data:'attachments=filemgmt&'+$.attachments.settings.token+'=1',beforeSend:function(){$.attachments.loading();container.dialog('disable')},success:function(msg){$.attachments.buttonRoutine('filemgmt');container.html(msg);$.attachments.buttonsUI(container);container.find('form[name="removefile"]').submit(function(e){e.preventDefault();if($(this).find('select[name="file"]').val()){$.ajax({type:'POST',dataType:'html',timeout:8000,cache:true,data:'attachments=removeFile'+'&file='+$(this).find('select[name="file"]').val()+'&'+$.attachments.settings.token+'=1',beforeSend:function(){$.attachments.loading();container.dialog('disable')},success:function(msg){$.attachments.buttonRoutine('files');container.html(msg)},error:function(){alert($.attachments.language.CDATTACHMENTS_ERROR);return false},complete:function(){$.attachments.loading();container.dialog('enable')}})}else{alert($.attachments.language.CDATTACHMENTS_SELECT_FILE);return false}});container.find('form[name="uploadfile"]').submit(function(e){if($(this).find('input[name="file"]').val()){return true}else{alert($.attachments.language.CDATTACHMENTS_SELECT_FILE);e.preventDefault();return false}})},error:function(){alert($.attachments.language.CDATTACHMENTS_ERROR);return false},complete:function(){$.attachments.loading();container.dialog('enable')}})},addnew:function(container){$.ajax({type:'POST',dataType:'html',timeout:8000,cache:true,data:'attachments=fileTemplate'+'&'+$.attachments.settings.token+'=1',beforeSend:function(){$.attachments.loading();container.dialog('disable')},success:function(msg){$.attachments.buttonRoutine('addnew');container.html(msg);$.attachments.buttonsUI(container);var form=container.find('form[name="fileform"]');form.find('input[name="filetitle"]').focus();form.submit(function(e){e.preventDefault();var emptylang=$.attachments.language.CDATTACHMENTS_EMPTY_VALUE;var filetitle=$(this).find('input[name="filetitle"]');if(filetitle.val()==''){alert(emptylang);filetitle.focus();return false}var file=$(this).find('select[name="file"]');if(file.val()==''){alert(emptylang);file.focus();return false}$(this).find('input[name="articleid"]:hidden').val(articleid);$.ajax({type:'POST',dataType:'html',timeout:8000,cache:true,data:$(this).serialize(),beforeSend:function(){$.attachments.loading();container.dialog('disable')},success:function(msg){if(msg){alert(msg);return false}else{var count=$('button[id="'+articleid+'"]').children('span');count.text(count.text()*1+1);$.attachments.successInfo(container,$.attachments.language.CDATTACHMENTS_SAVED);$.attachments.buttonRoutine('success')}},error:function(){alert($.attachments.language.CDATTACHMENTS_ERROR);return false},complete:function(){$.attachments.loading();container.dialog('enable')}})})},error:function(){alert($.attachments.language.CDATTACHMENTS_ERROR);return false},complete:function(){$.attachments.loading();container.dialog('enable')}})},deleteIcon:function(container){container.find('div.document div.fileicons div.fileicondelete').click(function(){var really=confirm($.attachments.language.CDATTACHMENTS_REALLY);if(!really)return false;var document=$(this).parent('div.fileicons').parent('div.document');var fileid=document.find('div.downloadbutton').find('input[name="fileid"]:hidden').val();$.ajax({type:'POST',dataType:'html',timeout:8000,cache:true,data:'attachments=deleteAttachment'+'&fileid='+fileid+'&'+$.attachments.settings.token+'=1',beforeSend:function(){container.dialog('disable');$.attachments.loading()},success:function(msg){if(msg){alert(msg);return false}else{$.attachments.successInfo(container,$.attachments.language.CDATTACHMENTS_DELETED);var count=$('button[id="'+articleid+'"]').children('span');count.text(count.text()*1-1);$.attachments.buttonRoutine('success');$.attachments.buttonRoutine('success')}},error:function(){alert($.attachments.language.CDATTACHMENTS_ERROR);return false},complete:function(){container.dialog('enable');$.attachments.loading()}})})},editIcon:function(container){container.find('div.document div.fileicons div.fileiconedit').click(function(){var document=$(this).parent('div.fileicons').parent('div.document');var fileid=document.find('div.downloadbutton').find('input[name="fileid"]:hidden').val();$.ajax({type:'POST',dataType:'html',timeout:8000,cache:true,data:'attachments=fileTemplate'+'&fileid='+fileid+'&'+$.attachments.settings.token+'=1',beforeSend:function(){$.attachments.loading();container.dialog('disable')},success:function(msg){$.attachments.buttonRoutine('edit');container.html(msg);$.attachments.buttonsUI(container);var form=container.find('form[name="fileform"]');form.find('input[name="filetitle"]').focus();form.submit(function(e){e.preventDefault();var emptylang=$.attachments.language.CDATTACHMENTS_EMPTY_VALUE;var filetitle=$(this).find('input[name="filetitle"]');if(filetitle.val()==''){alert(emptylang);filetitle.focus();return false}var file=$(this).find('select[name="file"]');if(file.val()==''){alert(emptylang);file.focus();return false}$.ajax({type:'POST',dataType:'html',timeout:8000,cache:true,data:$(this).serialize(),beforeSend:function(){$.attachments.loading();container.dialog('disable')},success:function(msg){if(msg){alert(msg);return false}else{$.attachments.successInfo(container,$.attachments.language.CDATTACHMENTS_EDITED);$.attachments.buttonRoutine('success')}},error:function(){alert($.attachments.language.CDATTACHMENTS_ERROR);return false},complete:function(){$.attachments.loading();container.dialog('enable')}})})},error:function(){alert($.attachments.language.CDATTACHMENTS_ERROR);return false},complete:function(){$.attachments.loading();container.dialog('enable')}})})},fileinformationicon:function(element){if(!element.length)return false;var isDialogOpen=false;element.find('div.document div.fileicons div.fileiconinfo').click(function(){if(isDialogOpen)return true;var fileinfo=$(this).parent('div.fileicons').parent('div.document').find('div.fileinformation').clone();var panelbuttons=element.next('div.ui-dialog-buttonpane').children('button');fileinfo.dialog({title:$.attachments.language.CDATTACHMENTS_FILEINFORMATION,height:150,width:350,draggable:false,resizeable:false,position:'center',stack:true,buttons:{'close':function(){$(this).dialog('close')}},open:function(){element.dialog('disable');panelbuttons.attr('disabled',true).addClass('ui-state-disabled');$.attachments.buttonTitles();$.attachments.theme($(this));isDialogOpen=true},close:function(){element.dialog('enable');panelbuttons.attr('disabled',false).removeClass('ui-state-disabled');isDialogOpen=false;$.attachments.removeDialog($(this));$('body > div.fileinformation').remove()}})})},loading:function(container){var buttonpanel=$('div.ui-dialog div.ui-dialog-buttonpane');if(buttonpanel.children('span.attachments_loading').length){buttonpanel.children('span.attachments_loading').remove();buttonpanel.find('button').attr('disabled',false).removeClass('ui-state-disabled')}else{buttonpanel.find('button').attr('disabled',true).addClass('ui-state-disabled');buttonpanel.prepend('<span class="attachments_loading"></span>')}},buttonsUI:function(container){var buttons=container.find('button');buttons.addClass('ui-state-default ui-corner-all');if(buttons.is(':disabled')){container.find('button').addClass('ui-state-disabled');return false}buttons.hover(function(){$(this).addClass('ui-state-hover');$(this).click(function(){$(this).removeClass('ui-state-hover')})},function(){$(this).removeClass('ui-state-hover')})},theme:function(container){var wrapper='<div class="'+$.attachments.settings.theme+'"></div>';container.parent('div.ui-dialog').wrap(wrapper);$('div.ui-widget-overlay').wrap(wrapper)},removeDialog:function(container){container.parent('div.ui-dialog').parent('div.'+$.attachments.settings.theme+'').remove()},buttonRoutine:function(layout){var buttons=$('div.ui-dialog div.ui-dialog-buttonpane');switch(layout){case'addnew':case'edit':case'success':case'filemgmt':buttons.find('button.back').show();buttons.find('button.addnew, button.files').hide();break;case'files':buttons.find('button.files').show();buttons.find('button.back, button.addnew').hide();break;default:buttons.find('button.back').hide();buttons.find('button.addnew, button.files').show();break}},buttonTitles:function(){var buttons=$('div.ui-dialog div.ui-dialog-buttonpane button');buttons.each(function(){switch($(this).text()){case'close':$(this).text($.attachments.language.CDATTACHMENTS_CLOSE).attr('title',$.attachments.language.CDATTACHMENTS_CLOSE);break;case'addnew':if($.attachments.settings.addnewauth!='1'){$(this).remove();return}$(this).text($.attachments.language.CDATTACHMENTS_ADDNEW).attr('title',$.attachments.language.CDATTACHMENTS_ADDNEW).addClass('addnew');break;case'files':if($.attachments.settings.filemanagerauth!='1'){$(this).remove();return}$(this).text($.attachments.language.CDATTACHMENTS_FILES).attr('title',$.attachments.language.CDATTACHMENTS_FILES).addClass('files');break;case'back':$(this).text($.attachments.language.CDATTACHMENTS_BACK).attr('title',$.attachments.language.CDATTACHMENTS_BACK).addClass('back');break;default:break}})},successInfo:function(container,msg){container.html($('<div id="attachments_success"><p><span style="margin: 0pt 7px 50px 0pt; float: left;" class="ui-icon ui-icon-check"/></span>'+msg+'</p></div>'))}}})(jQuery);jQuery(document).ready(function($){if($.attachments)$.attachments.initiator()});
