//// pagination, sorting, search
$(document).ready(function(e) {

	

var items = $("table.tableActive tbody tr.tableData");
var itemsNoteTr = $("table.tableActive tbody tr.notetr");

var numItems = items.length;

var perPage = 50;

var pageNumber = 1;

var totalPages = Math.ceil(numItems/perPage);
if(totalPages == 0 ){
	pageNumber = 0;
}
var itemsInActive = $("table.tableInActive tbody tr.tableDataHide");
var itemsInActiveNoteTr = $("table.tableInActive tbody tr.notetr");

var numItemsInActive = itemsInActive.length;

var perPageInActive = 50;

var pageNumberInActive = 1;

var totalPagesInActive = Math.ceil(numItemsInActive/perPageInActive);

if(totalPagesInActive == 0 ){
	pageNumberInActive = 0;
}   

// only show the first 2 (or "first per_page") items initially

items.slice(perPage).hide();
itemsNoteTr.slice(perPage).hide();
itemsInActive.slice(perPageInActive).hide();
itemsInActiveNoteTr.slice(perPageInActive).hide();	

    $('#SearchMeasure').keyup(function(e) {

		//console.log('keyup');

		$searchData = $(this).val().toLowerCase();

			$('.tableData').each(function(index, element) {

                $t = $(this);

				//console.log('each');

				$status = false;

				$(this).find('td.tdData').each(function(index, element) {

                    $value = $(this).html();

					if ($value.toLowerCase().indexOf($searchData) >= 0){

						$status = true;

						//console.log('find');

					}

                });

				

				if($status){

					$t.show();

					$t.next('tr').show();

				}else{

					$t.hide();

					$t.next('tr').hide();

				}

            });

	});
	
	$('#findproject').change(function(e) {
		//console.log('keyup');
		
		$searchData = $(this).val().toLowerCase();
			$('.tableData').each(function(index, element) {
                $t = $(this);
				//console.log('each');
				$status = false;
				$(this).find('td.srtProject').each(function(index, element) {
                    $value = $(this).html();
					if ($value.toLowerCase().indexOf($searchData) >= 0){
						$status = true;
						//console.log('find');
					}
                });
				if($status){
					$t.show();
					$t.next('tr').show();
				}else{
					$t.hide();
					$t.next('tr').hide();
				}
            });
	});

	$('#SearchMeasureHide').keyup(function(e) {

		//console.log('keyup');

		$searchData = $(this).val().toLowerCase();

			$('.tableDataHide').each(function(index, element) {

                $t = $(this);

				//console.log('each');

				$status = false;

				$(this).find('td.tdDataHide').each(function(index, element) {

                    $value = $(this).html();

					if ($value.toLowerCase().indexOf($searchData) >= 0){

						$status = true;

						//console.log('find');

					}

                });

				

				if($status){

					$t.show();
					$t.next('tr').show();

				}else{

					$t.hide();
					$t.next('tr').hide();

				}

            });

	});	
	
	
	$('#findprojectHide').change(function(e) {
		//console.log('keyup');
		
		$searchData = $(this).val().toLowerCase();
			$('.tableDataHide').each(function(index, element) {
                $t = $(this);
				//console.log('each');
				$status = false;
				$(this).find('td.srtProject').each(function(index, element) {
                    $value = $(this).html();
					if ($value.toLowerCase().indexOf($searchData) >= 0){
						$status = true;
						//console.log('find');
					}
                });
				if($status){
					$t.show();
					$t.next('tr').show();
				}else{
					$t.hide();
					$t.next('tr').hide();
				}
            });
	});

	var currentColumn='';

	$('table.tableActive th.sorting').click(function(e) {

		pageNumber = 1;
		if(totalPages == 0){
			pageNumber = 0;
		}
		
		$('.ActiveTablePaging .pagingInfo').html('Showing Page ' + pageNumber + ' of ' + totalPages + ' Pages for a Total of ' + numItems + ' entries');

		$("table.tableActive tbody tr.tableData").show();
		$("table.tableActive tbody tr.notetr").show();

		

		 var rows = $('table.tableActive tbody tr.tableData').get();
		 

		  currentColumn = $(this).attr("sorting");

			if($(this).hasClass('my_sorted_reverse')){

				$(this).removeClass('my_sorted_reverse');

				rows.sort(table_dec_sort);

			}else{

				$(this).addClass('my_sorted_reverse');

				rows.sort(table_asc_sort);

			}

		

		  $.each(rows, function(index, row) {

			$('table.tableActive tbody').append(row);

			

			$trId = $(row).attr('trid');
			$value = $('table.tableActive tbody tr.trid'+$trId).get();

			$('table.tableActive tbody tr.trid'+$trId).remove();

			$('table.tableActive tbody').append($value);

			

		  });

		  items = $("table.tableActive tbody tr.tableData");
		  itemsNoteTr = $("table.tableActive tbody tr.notetr");

		  items.slice(perPage).hide();
		  itemsNoteTr.slice(perPage).hide();

	});

	

	$('table.tableInActive th.sorting').click(function(e) {

		pageNumberInActive = 1;

		if(totalPagesInActive == 0){
			pageNumberInActive = 0;
		}
		
		$('.InActiveTablePaging .pagingInfo').html('Showing Page ' + pageNumberInActive + ' of ' + totalPagesInActive + ' Pages for a Total of ' + numItemsInActive + ' entries');

		$("table.tableInActive tbody tr.tableDataHide").show();
		$("table.tableInActive tbody tr.notetr").show();

		

		 var rows = $('table.tableInActive tbody tr.tableDataHide').get();

	

		  currentColumn = $(this).attr("sorting");

			if($(this).hasClass('my_sorted_reverse')){

				$(this).removeClass('my_sorted_reverse');

				rows.sort(table_dec_sort);

			}else{

				$(this).addClass('my_sorted_reverse');

				rows.sort(table_asc_sort);

			}

		

		  $.each(rows, function(index, row) {

			$('table.tableInActive tbody').append(row);
			

			$trId = $(row).attr('trid');

			$value = $('table.tableInActive tbody tr.trid'+$trId).get();

			$('table.tableInActive tbody tr.trid'+$trId).remove();

			$('table.tableInActive tbody').append($value);

			

		  });

		  itemsInActive = $("table.tableInActive tbody tr.tableDataHide");
		  itemsInActiveNoteTr = $("table.tableInActive tbody tr.notetr");

		  itemsInActive.slice(perPageInActive).hide();
		  itemsInActiveNoteTr.slice(perPageInActive).hide();
	});

	function table_asc_sort(a, b){
        return ($(b).find("."+currentColumn).text()) < ($(a).find("."+currentColumn).text()) ? 1 : -1;    
    }



 // decending sort



    function table_dec_sort(a, b){
        return ($(b).find("."+currentColumn).text()) > ($(a).find("."+currentColumn).text()) ? 1 : -1;    
    }

/***************** start paging ****************************/
	$('.ActiveTablePaging .pagingInfo').html('Showing Page '+ pageNumber +' of ' + totalPages + ' Pages for a Total of ' + numItems + ' entries');

	$('.tableActiveFilter #PagingRows').change(function(e) {

		$ParPageVal = $(this).val();

		//console.log("val-"+$ParPageVal);

		perPage = ($ParPageVal == 'all'?999999999999999:$ParPageVal);

		//console.log(perPage);

		pageNumber = 1;

		totalPages = Math.ceil(numItems/perPage);
		if(totalPages == 0){
			pageNumber = 0;
		}
		$('.ActiveTablePaging .pagingInfo').html('Showing Page '+ pageNumber +' of ' + totalPages + ' Pages for a Total of ' + numItems + ' entries');

		items.show();
		itemsNoteTr.show();
		items.slice(perPage).hide();
		itemsNoteTr.slice(perPage).hide();

		

	});

	$('.ActiveTablePaging .BtnPrev').click(function(e) {

		if(pageNumber > 1){

			 pageNumber -=1;

			 //console.log("prev perPage-"+ perPage);

			 var showFrom = perPage * (pageNumber - 1);

			 var showTo = parseInt(showFrom) + parseInt(perPage);

			// console.log("prev-"+showFrom+"-"+showTo);

			 $('.ActiveTablePaging .pagingInfo').html('Showing Page ' + pageNumber + ' of ' + totalPages + ' Pages for a Total of ' + numItems + ' entries');

			 items.hide() // first hide everything, then show for the new page

						 .slice(showFrom, showTo).show();
			 itemsNoteTr.hide().slice(showFrom, showTo).show();

		}

	});

	$('.ActiveTablePaging .BtnNext').click(function(e) {

		

		if(pageNumber != totalPages){

			

			 pageNumber +=1;

			 //console.log("next perPage-"+perPage);

			 var showFrom = perPage * (pageNumber - 1);

			 var showTo = parseInt(showFrom) + parseInt(perPage);

			// console.log("next-"+showFrom+"-"+showTo);

			 $('.ActiveTablePaging .pagingInfo').html('Showing Page ' + pageNumber + ' of ' + totalPages + ' Pages for a Total of ' + numItems + ' entries');

			 items.hide() // first hide everything, then show for the new page

						 .slice(showFrom, showTo).show();
			 itemsNoteTr.hide().slice(showFrom, showTo).show();

		}

	});

	/*****************/

	$('.InActiveTablePaging .pagingInfo').html('Showing Page '+ pageNumberInActive +' of ' + totalPagesInActive + ' Pages for a Total of ' + numItemsInActive + ' entries');

    

	

	$('.tableInActiveFilter #PagingRows').change(function(e) {

		

		$ParPageVal = $(this).val();

		perPageInActive = $ParPageVal == 'all'?999999999999999:$ParPageVal;

		//console.log(perPage);

		pageNumberInActive = 1;
		
		totalPagesInActive = Math.ceil(numItemsInActive/perPageInActive);
		if(totalPagesInActive == 0){
			pageNumberInActive = 0;
		}
		$('.InActiveTablePaging .pagingInfo').html('Showing Page '+ pageNumberInActive +' of ' + totalPagesInActive + ' Pages for a Total of ' + numItemsInActive + ' entries');

		itemsInActive.show();
		itemsInActiveNoteTr.show();

		itemsInActive.slice(perPageInActive).hide();
		itemsInActiveNoteTr.slice(perPageInActive).hide();

		

	});

	$('.InActiveTablePaging .BtnPrev').click(function(e) {

		if(pageNumberInActive > 1){

			 pageNumberInActive -=1;
			 var showFrom = perPageInActive * (pageNumberInActive - 1);

			 var showTo = parseInt(showFrom) + parseInt(perPageInActive);

			 $('.InActiveTablePaging .pagingInfo').html('Showing Page ' + pageNumberInActive + ' of ' + totalPagesInActive + ' Pages for a Total of ' + numItemsInActive + ' entries');

			 itemsInActive.hide() // first hide everything, then show for the new page

						 .slice(showFrom, showTo).show();
			itemsInActiveNoteTr.hide().slice(showFrom, showTo).show();

		}

	});

	$('.InActiveTablePaging .BtnNext').click(function(e) {

		

		if(pageNumberInActive != totalPagesInActive){
			 pageNumberInActive +=1;
			 var showFrom = perPageInActive * (pageNumberInActive - 1);

			 var showTo = parseInt(showFrom) + parseInt(perPageInActive);

			 $('.InActiveTablePaging .pagingInfo').html('Showing Page ' + pageNumberInActive + ' of ' + totalPagesInActive + ' Pages for a Total of ' + numItemsInActive + ' entries');

			 itemsInActive.hide() // first hide everything, then show for the new page

						 .slice(showFrom, showTo).show();
		     itemsInActiveNoteTr.hide().slice(showFrom, showTo).show();

		}

	});

  /***************** end paging ****************************/ 

});