/*!
 * ExpressionEngine - by EllisLab
 *
 * @package		ExpressionEngine
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2003 - 2011, EllisLab, Inc.
 * @license		http://expressionengine.com/user_guide/license.html
 * @link		http://expressionengine.com
 * @since		Version 2.0
 * @filesource
 */

$.ee_filemanager=$.filemanager||{};$(document).ready(function(){$.ee_filemanager.file_uploader();$.ee_filemanager.datatables();$.ee_filemanager.image_overlay();$.ee_filemanager.date_range();$.ee_filemanager.toggle_all();$.ee_filemanager.directory_change();$(".paginationLinks .first").hide();$(".paginationLinks .previous").hide()});
$.ee_filemanager.file_uploader=function(){$.ee_fileuploader({type:"filemanager",load:function(){$.template("filemanager_row",$("#filemanager_row").remove())},open:function(){$.ee_fileuploader.set_directory_id($("#dir_id").val())},after_upload:function(d,a){a.replace==!0&&$(".mainTable tbody tr:has(td:contains("+a.file_id+")):has(td:contains("+a.file_name+"))").remove();$(".mainTable tbody tr:first").clone();a.actions="";$.each(EE.fileuploader.actions,function(b,c){var j=c.replace("[file_id]",a.file_id).replace("[upload_dir]",
a.upload_directory_prefs.id);if(b=="delete")a.action_delete=j;else if(b!="image"||a.is_image)a.actions+=j+"&nbsp;&nbsp;"});if(typeof a.title=="undefined")a.title=a.name;if(a.is_image){var b=$("<a>",{id:"",href:a.upload_directory_prefs.url+a.file_name,title:a.file_name,text:a.title,rel:"#overlay","class":"less_important_link overlay"});a.link=b.wrap("<div>").parent().html()}else a.link=a.title;$(".mainTable tbody").prepend($.tmpl("filemanager_row",a));$("td.dataTables_empty").size()&&$("td.dataTables_empty").parent().remove();
a.replace!=!0&&$("#file_uploader").dialog("option","position","center")},trigger:"#action_nav a:contains(Upload File)"})};
$.ee_filemanager.directory_change=function(){function d(b){a[b]===void 0&&(b=0);jQuery.each(a[b],function(a,b){$("select#cat_id").empty().append(b)})}var a=EE.file.directoryInfo,b=RegExp("!-!","g");$.each(a,function(i,c){$.each(c,function(c,d){var h=new String;$.each(d,function(a,c){h+='<option value="'+c[0]+'">'+c[1].replace(b,String.fromCharCode(160))+"</option>"});a[i][c]=h})});$("#dir_id").change(function(){d(this.value)})};
$.ee_filemanager.date_range=function(){function d(){if($("#custom_date_start").val()!="yyyy-mm-dd"&&$("#custom_date_end").val()!="yyyy-mm-dd")focus_number=$("#date_range").children().length,$("#date_range").append('<option id="custom_date_option">'+$("#custom_date_start").val()+" to "+$("#custom_date_end").val()+"</option>"),document.getElementById("date_range").options[focus_number].selected=!0,$("#custom_date_picker").slideUp("fast"),oTable.fnDraw()}$("#custom_date_start_span").datepicker({dateFormat:"yy-mm-dd",
prevText:"<<",nextText:">>",onSelect:function(a){$("#custom_date_start").val(a);d()}});$("#custom_date_end_span").datepicker({dateFormat:"yy-mm-dd",prevText:"<<",nextText:">>",onSelect:function(a){$("#custom_date_end").val(a);d()}});$("#custom_date_start, #custom_date_end").focus(function(){$(this).val()=="yyyy-mm-dd"&&$(this).val("")});$("#custom_date_start, #custom_date_end").keypress(function(){$(this).val().length>=9&&d()});$("#date_range").change(function(){$("#date_range").val()=="custom_date"?
($("#custom_date_start").val("yyyy-mm-dd"),$("#custom_date_end").val("yyyy-mm-dd"),$("#custom_date_option").remove(),$("#custom_date_picker").slideDown("fast")):$("#custom_date_picker").hide()})};$.ee_filemanager.toggle_all=function(){$(".toggle_all").toggle(function(){$("input.toggle").each(function(){this.checked=!0})},function(){$("input.toggle").each(function(){this.checked=!1})})};
$.ee_filemanager.image_overlay=function(){$("a.overlay").live("click",function(){$("#overlay").hide().removeData("overlay");$("#overlay .contentWrap img").remove();$("<img />").appendTo("#overlay .contentWrap").load(function(){var d=$(this).clone().appendTo(document.body).show(),a=d.width(),b=d.height(),i=$(window).width()*0.8,c=$(window).height()*0.8;i/=a;c/=b;c=i>c?c:i;d.remove();c<1&&(b*=c,a*=c,$(this).height(b).width(a));$("#overlay").overlay({load:!0,speed:100,top:"center"})}).attr("src",$(this).attr("href"));
return!1});$("#overlay").css("cursor","pointer").click(function(){$(this).fadeOut(100)})};
$.ee_filemanager.datatables=function(){function d(a,b,d){for(var f=0,h=a.length;f<h;f++)if(a[f].name==b)a[f].value=d}function a(a,b){for(var d=0,f=a.length;d<f;d++)if(a[d].name==b)return a[d].value;return null}var b={iCacheLower:-1};EE.file.tableColumns==9?(MyCols=[null,null,null,null,null,null,null,{bSortable:!1},{bSortable:!1},{bSortable:!1}],MySortCol=6):(MyCols=[null,null,null,null,null,null,{bSortable:!1},{bSortable:!1},{bSortable:!1}],MySortCol=5);oTable=$("#file_form .mainTable").dataTable({sPaginationType:"full_numbers",
bLengthChange:!1,aaSorting:[[MySortCol,"desc"],[0,"desc"]],bFilter:!1,sWrapper:!1,sInfo:!1,bAutoWidth:!1,iDisplayLength:+EE.file.perPage,aoColumns:MyCols,oLanguage:{sZeroRecords:EE.lang.noEntries,oPaginate:{sFirst:'<img src="'+EE.file.themeUrl+'images/pagination_first_button.gif" width="13" height="13" alt="&lt; &lt;" />',sPrevious:'<img src="'+EE.file.themeUrl+'images/pagination_prev_button.gif" width="13" height="13" alt="&lt; &lt;" />',sNext:'<img src="'+EE.file.themeUrl+'images/pagination_next_button.gif" width="13" height="13" alt="&lt; &lt;" />',
sLast:'<img src="'+EE.file.themeUrl+'images/pagination_last_button.gif" width="13" height="13" alt="&lt; &lt;" />'}},bProcessing:!0,bServerSide:!0,sAjaxSource:EE.BASE+"&C=content_files&M=file_ajax_filter&time="+(new Date).getTime(),fnServerData:function(i,c,j){function f(){if($(m).data("user_data")=="n")return"";return m.value}var h=+EE.file.pipe,l=!1,t=a(c,"sEcho"),e=a(c,"iDisplayStart"),k=a(c,"iDisplayLength"),g=e+k,m=document.getElementById("keywords"),n=document.getElementById("file_type"),o=
document.getElementById("dir_id"),p=document.getElementById("cat_id"),q=document.getElementById("date_range"),r=document.getElementById("search_in"),s=document.getElementById("file_type");c.push({name:"keywords",value:f()},{name:"type",value:n.value},{name:"dir_id",value:o.value},{name:"cat_id",value:p.value},{name:"date_range",value:q.value},{name:"search_in",value:r.value},{name:"file_type",value:s.value});b.iDisplayStart=e;if(b.iCacheLower<0||e<b.iCacheLower||g>b.iCacheUpper)l=!0;if(b.lastRequest&&
!l)for(var g=0,u=c.length;g<u;g++)if(c[g].name!="iDisplayStart"&&c[g].name!="iDisplayLength"&&c[g].name!="sEcho"&&c[g].value!=b.lastRequest[g].value){l=!0;break}b.lastRequest=c.slice();l?(e<b.iCacheLower&&(e-=k*(h-1),e<0&&(e=0)),b.iCacheLower=e,b.iCacheUpper=e+k*h,b.iDisplayLength=a(c,"iDisplayLength"),d(c,"iDisplayStart",e),d(c,"iDisplayLength",k*h),c.push({name:"keywords",value:f()},{name:"type",value:n.value},{name:"dir_id",value:o.value},{name:"cat_id",value:p.value},{name:"date_range",value:q.value},
{name:"search_in",value:r.value},{name:"file_type",value:s.value}),$.getJSON(i,c,function(a){b.lastJson=jQuery.extend(!0,{},a);b.iCacheLower!=b.iDisplayStart&&a.aaData.splice(0,b.iDisplayStart-b.iCacheLower);a.aaData.splice(b.iDisplayLength,a.aaData.length);j(a)})):(json=jQuery.extend(!0,{},b.lastJson),json.sEcho=t,json.aaData.splice(0,e-b.iCacheLower),json.aaData.splice(k,json.aaData.length),j(json))}});$("#keywords").keyup(function(){oTable.fnDraw()});$("select#dir_id, select#cat_id, select#file_type, select#date_range").change(function(){oTable.fnDraw()})};
