(function($){$.fn.jDownload=function(settings){var config={root:"/",filePath:null,event:"click",dialogTitle:"jDownload",dialogDesc:'Download the file now?',dialogWidth:400,dialogHeight:'auto',dialogModal:true,showfileInfo:true,start:null,stop:null,download:null,cancel:null}
settings=$.extend(config,settings);var dialogID="jDownloadDialog_"+$('.jDownloadDialog').length;var iframeID="jDownloadFrame_"+$('.jDownloadFrame').length;var iframeHTML='<iframe class="jDownloadFrame" src="" id="'+iframeID+'"></iframe>';var dialogHTML='<div class="jDownloadDialog" title="'+settings.dialogTitle+'" id="'+dialogID+'"></div>';$('body').append(iframeHTML+dialogHTML);var iframe=$('#'+iframeID);var dialog=$('#'+dialogID);iframe.css({"height":"0px","width":"0px","visibility":"hidden"});dialog.dialog({autoOpen:false,buttons:{"Cancel":function(){if($.isFunction(settings.cancel)){settings.cancel();}
$(this).dialog('close');},"Download":function(){if($.isFunction(settings.download)){settings.download();}
start_download();}},width:settings.dialogWidth,height:settings.dialogHeight,modal:settings.dialogModal,close:($.isFunction(settings.stop))?settings.stop:null});$(this).bind(settings.event,function(){if($.isFunction(settings.start)){settings.start();}
var _this=$(this);dialog.html("");var filePath=(settings.filePath==null)?$(this).attr('href'):settings.filePath;dialog.html('<p>Fetching File...</p><img src="'+settings.root+'jdownload/loader.gif" alt="Loading" />');$.ajax({type:'POST',url:settings.root+'jdownload/jdownload.php',data:'action=download&path='+filePath,error:function(XMLHttpRequest,textStatus,errorThrown){dialog.html("<p class=\"jDownloadError\">Fatal Error.</p>");},success:function(data){setTimeout(function(){if(data=="error"){dialog.html("<p class=\"jDownloadError\">File cannot be found.</p>");}else{if(settings.showfileInfo==true){var url=settings.root+'jdownload/jdownload.php?action=info&path='+filePath;$.getJSON(url,function(data){if(data.error=='denied'){dialog.html('<p class=\"jDownloadError\">This file type is not allowed.</p>');}else{var html="<div class=\"jDownloadInfo\">";html+="<p><span>File Name:</span> "+data.filename+"</p>";html+="<p><span>File Type:</span> "+data.filetype+"</p>";html+="<p><span>File Size:</span> "+data.filesize+" KB</p>";html+="</div>";$('.jDownloadInfo, .jDownloadError').remove();var desc=(_this.attr('title').length>0)?_this.attr('title'):'Download the file now?';dialog.html('<p>'+desc+'</p>'+html);}});}}},200);}});dialog.data('jDownloadData',{filePath:filePath}).dialog('open');return false;});function start_download(i){iframe.attr('src',settings.root+'jdownload/jdownload.php?action=download&path='+dialog.data('jDownloadData').filePath);dialog.dialog('close');return false;}}})(jQuery);
