jQuery.fn.dataTableExt.oApi.fnLengthChange = function ( oSettings, iDisplay ){
    oSettings._iDisplayLength = iDisplay;
    oSettings.oApi._fnCalculateEnd( oSettings );
    if ( oSettings._iDisplayEnd == oSettings.aiDisplay.length ){
        oSettings._iDisplayStart = oSettings._iDisplayEnd - oSettings._iDisplayLength;
        if ( oSettings._iDisplayStart < 0 ){oSettings._iDisplayStart = 0;}
    }
    if ( oSettings._iDisplayLength == -1 ){oSettings._iDisplayStart = 0;}
    oSettings.oApi._fnDraw( oSettings );
    if ( oSettings.aanFeatures.l ){
        $('select', oSettings.aanFeatures.l).val( iDisplay );
    }
};


$(function(){
	var responsiveHelper = undefined;
	var breakpointDefinition = {
		tablet: 1024,
		phone: 480
	};
	var initTableWithSearch = function() {
        
		var table = $('#tableWithSearch');
         var menu = '<div class="block-area" id="custom-select">'
        +'<div class="row">'
        +'<div class="col-md-2 m-b-15">'
        +'<select class="select">'
        +'<option value="10">10</option>'
        +'<option value="20">20</option>'
        +'<option value="30">30</option>'   
        +'<option value="50">50</option>'
        +'<option value="100">100</option>'
        +'<option value="200">200</option>'
        +'</select>'
        +'</div>'
        +'</div>'
        +'</div>'
		var settings = {
			"sPaginationType": "bootstrap",
			 "pagingType": "full_numbers", 
            "destroy": true,
            "scrollCollapse": true,
            oLanguage: {sLengthMenu: menu},
            "iDisplayLength": 10
		};
		table.dataTable(settings);
        
		$('#search-table').on("keyup",function() {
			table.fnFilter($(this).val());
		});
        

	}
	var initTableWithDynamicRows = function() {
		var table = $('#tableWithDynamicRows');
		var settings = {
			"sDom": "<'table-responsive't><'row'<p i>>",
			"sPaginationType": "bootstrap",
			"destroy": true,
			"scrollCollapse": true,
			"oLanguage": {
				"sLengthMenu": "_MENU_ ",
				"sInfo": "Showing <b>_START_ to _END_</b> of _TOTAL_ entries"
			},
			"iDisplayLength": 5
		};
		table.dataTable(settings);
		$('#show-modal').click(function() {
			$('#addNewAppModal').modal('show');
		});
		$('#add-app').click(function() {
			table.dataTable().fnAddData([$("#appName").val(), $("#appDescription").val(), $("#appPrice").val(), $("#appNotes").val()]);
			$('#addNewAppModal').modal('hide');
		});
	}
	var initTableWithExportOptions = function() {
		var table = $('#tableWithExportOptions');
		var settings = {
			"sDom": "<'exportOptions'T><'table-responsive't><'row'<p i>>",
			"sPaginationType": "bootstrap",
			"destroy": true,
			"scrollCollapse": true,
			"oLanguage": {
				"sLengthMenu": "_MENU_ ",
				"sInfo": "Showing <b>_START_ to _END_</b> of _TOTAL_ entries"
			},
			"iDisplayLength": 5,
			"oTableTools": {
				"sSwfPath": "assets/plugins/jquery-datatable/extensions/TableTools/swf/copy_csv_xls_pdf.swf",
				"aButtons": [{
					"sExtends": "csv",
					"sButtonText": "<i class='pg-grid'></i>",
				}, {
					"sExtends": "xls",
					"sButtonText": "<i class='fa fa-file-excel-o'></i>",
				}, {
					"sExtends": "pdf",
					"sButtonText": "<i class='fa fa-file-pdf-o'></i>",
				}, {
					"sExtends": "copy",
					"sButtonText": "<i class='fa fa-copy'></i>",
				}]
			},
			fnDrawCallback: function(oSettings) {
				$('.export-options-container').append($('.exportOptions'));
				$('#ToolTables_tableWithExportOptions_0').tooltip({
					title: 'Export as CSV',
					container: 'body'
				});
				$('#ToolTables_tableWithExportOptions_1').tooltip({
					title: 'Export as Excel',
					container: 'body'
				});
				$('#ToolTables_tableWithExportOptions_2').tooltip({
					title: 'Export as PDF',
					container: 'body'
				});
				$('#ToolTables_tableWithExportOptions_3').tooltip({
					title: 'Copy data',
					container: 'body'
				});
			}
		};
		table.dataTable(settings);
	}
	initTableWithSearch();
	initTableWithDynamicRows();
	initTableWithExportOptions();
})