var aTable;
function fnFormatDetails ( nTr )
{
	var iIndex = aTable.fnGetPosition( nTr );
	var aData = aTable.fnSettings().aoData[iIndex]._aData;
	aData[4] = aData[4] == "" ? "brak" : aData[4];
	
	switch (aData[6]) {
		case 'text/plain':
			filetype = 'text';
			break;
		case 'image/gif':
		case 'image/tiff':
		case 'image/png':
		case 'image/jpeg':
			filetype = 'image';
			break;
		case 'application/x-rar-compressed':
		case 'application/zip':
			filetype = 'archive';
			break;
		case 'application/pdf':
			filetype = 'pdf';
			break;
		case 'application/vnd.ms-powerpoint':
			filetype = 'excel';
			break;
		case 'application/vnd.ms-excel':
			filetype = 'excel';
			break;
		case 'application/msword':
			filetype = 'word';
			break;
		default:
			filetype = 'unknown';
			break;
		
	}
	
	var sOut = '<table width="100%" valign="top" class="'+iIndex+'" cellpadding="0" cellspacing="0" border="0" style="padding-left:50px; display: none;">';
	sOut += '<tr><td rowspan="5" width="150px"><img src="'+ path +'/images/file_type/'+filetype+'.png" alt="" /></td></tr>';
	sOut += '<tr><td width="100px">Opis:</td><td>'+aData[4]+'</td></tr>';
	sOut += '<tr><td>Plik:</td><td>'+aData[5]+'</td></tr>';
	sOut += '<tr><td>Wielkość:</td><td>'+aData[7]+'</td></tr>';
	sOut += '<tr><td>Typ:</td><td>'+aData[6]+'</td></tr>';

	sOut += '</table>';
	return sOut;
}

function fnOpenClose ( oSettings )
{
	$('.detal', aTable.fnGetNodes() ).each( function () {
		$(this).click( function () {
			var nTr = this.parentNode.parentNode;
			var position = aTable.fnGetPosition ( nTr );
			if ( this.src.match('details_close') ) {
				/* This row is already open - close it */
				this.src = path + "/images/details_open.png";
				var nRemove = $(nTr).next()[0];
				$("."+position).fadeOut ( 'fast', function () { nRemove.parentNode.removeChild( nRemove ) });
			} else {
				/* Open this row */
				this.src = path + "/images/details_close.png";
				$("."+position).hide ();
				aTable.fnOpen( nTr, fnFormatDetails(nTr), 'szczegoly', position );
				$("#"+position).hide();
				$("."+position).fadeIn ('slow');
				
			}
		} );
	} );
}

function deleteFiles() {
	var n = "";
	var t = "";
	$('input:checked' ).each( function () {
		n += $(this).val()+',';
		t += "- "+$(this).attr('name')+"\n";
	} );
	if ( t == "" ) {
		alert ("Musisz zaznaczyć conajmniej jeden plik!");
		return;
	}
		
	var is_confirmed = confirm( "Czy na pewno chcesz usunąć pliki: \n" + t);
	if (is_confirmed)
		document.location = path + "/files/delete?id="+n;
}

$(document).ready(function() {
	
	aTable = $('#table').dataTable( {
		"bAutoWidth":  false,
		"bProcessing": true,
		"bServerSide": true,
		"sAjaxSource": "files/getFileList",
		"fnServerData": function ( sSource, aoData, fnCallback ) {
			$.ajax( {
				"dataType": 'json', 
				"type": "POST", 
				"url": sSource, 
				"data": aoData, 
				"success": fnCallback
			} );
			
		},
		"sDom": 'rt<"search border_bottom_radius bottom_items"<"cancel">f><"pagination border_bottom_radius bottom_items"p>',
		"aoColumns": [
			{ "sWidth": "3%",  "sClass": "align_center", "bSortable": false },
			{ "sWidth": "3%",  "sClass": "align_center", "bSortable": false },
			{ "sWidth": "15%", "bSortable": false  },
			{ "bVisible":    false },
			{ "sWidth":      "55%" },
			{ "bVisible":    false },
			{ "bVisible":    false },
			{ "bVisible":    false },
			{ "bVisible":    false },
			{ "bVisible":    false },
			{ "bVisible":    false },
			{ "sWidth": "25%", "sClass": "align_center" },
			{ "sWidth": "5%",  "sClass": "align_center", "bSortable": false }
		],
		"oLanguage": {
			"sProcessing": "Proszę czekać...",
			"sLengthMenu": "Pokaż _MENU_ rekordów na stronę",
			"sZeroRecords": "Nic nie znaleziono",
			"sInfo": "Wyświetlanie od _START_ do _END_ z _TOTAL_ rekordów",
			"sInfoEmtpy": "Pusto :<",
			"sInfoFiltered": "(filtrowano z _MAX_ wszystkich)",
			"sInfoPostFix": "",
			"sSearch": "",
			"sUrl": "",
			"oPaginate": {
				"sFirst":    "Pierwsza",
				"sPrevious": "Poprzednia",
				"sNext":     "Następna",
				"sLast":     "Ostatnia"
			}
		},
		"sPaginationType": "full_numbers",
		"aaSorting": [[10, 'desc']],
		"fnDrawCallback": fnOpenClose		
	} );
		
	$('.search input').val( "szukaj w folderze" );
	$('.cancel').hide ();
	$('.search').hover (
		function () {
			if ( $('.search input').val() == 'szukaj w folderze' ) {
				$('.search input').val("");
				$('.search input').focus ();
				$('.search input').width ("155px");
				$('.search input').css ( { 'font-style': 'normal' } );
				$('.cancel').show ();
			}
	
		},
		function () {
			if ( $('.search input').val() == '' ) {
				$('.search input').val("szukaj w folderze");
				$('.search input').width ("175px");
				$('.search input').css ( { 'font-style': 'italic' } );
				$('.cancel').hide ();
			}
	
		}
	);
	
	$('.cancel').click ( function () {
		aTable.fnFilter( '' );
		$('.search input').val("");
	});
	
	$('body').keypress ( function () {
			if ( $('.search input').val() == 'szukaj w folderze' ) {
				$('.search input').val("");
				$('.search input').focus ();
				$('.search input').width ("155px");
				$('.search input').css ( { 'font-style': 'normal' } );
				$('.cancel').show ();
			}
	});
	
	$('#groups').change(function() {
		aTable.fnFilter( $(this).val(), 1, true );
	} );
	
} );
