/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


// global variable on page to control <add> and <edit> operations in page
var addEditFlag = true;

function addRowComplete(dataTableId) {

    // click on edit button of last row
    jQuery(dataTableId + ' .ui-datatable-data > tr')
            .last()
            .find('span.ui-icon-pencil')
            .each(function () {
                jQuery(this).click();
            });

    // hide all edit buttons of other rows in all datatables in the page
    jQuery('.ui-datatable-data > tr')
            .find('a.ui-row-editor-pencil')
            .each(function () {
                jQuery(this).css('visibility', 'hidden');
            });
    jQuery('.ui-datatable-data > tr')
            .find('.deleteBtn')
            .each(function () {
                jQuery(this).css('visibility', 'hidden');
            });
    var scroll_pos = jQuery(dataTableId + ' tr:last').position();
    jQuery(dataTableId + ' .ui-datatable-scrollable-body').scrollTop(scroll_pos.top);

    // set flag to false to prevent any other event
    addEditFlag = false;
}
//when i want to check some business condition befor add 
function addRowOnCondComplete(args, dataTableId) {

    if (args.addSuccess === true) {
        // click on edit button of last row
        jQuery(dataTableId + ' .ui-datatable-data > tr')
                .last()
                .find('span.ui-icon-pencil')
                .each(function () {
                    jQuery(this).click();
                });

        // hide all edit buttons of other rows in all datatables in the page
        jQuery('.ui-datatable-data > tr')
                .find('a.ui-row-editor-pencil')
                .each(function () {
                    jQuery(this).css('visibility', 'hidden');
                });
        jQuery('.ui-datatable-data > tr')
                .find('.deleteBtn')
                .each(function () {
                    jQuery(this).css('visibility', 'hidden');
                });

        var scroll_pos = jQuery(dataTableId + ' tr:last').position();
        jQuery(dataTableId + ' .ui-datatable-scrollable-body').scrollTop(scroll_pos.top);

        // set flag to false to prevent any other event
        addEditFlag = false;
    }
}

function rowEditInit() {

    // on row edit initialization hide all edit icons in the page
    jQuery('.ui-datatable-data > tr')
            .find('a.ui-row-editor-pencil')
            .each(function () {
                jQuery(this).css('visibility', 'hidden');
            });
    jQuery('.ui-datatable-data > tr')
            .find('.deleteBtn')
            .each(function () {
                jQuery(this).css('visibility', 'hidden');
            });
    // set flag to false to prevent any other event
    addEditFlag = false;
}

function rowEditFinish(args, datatableId) {

    // after finishing edit of any row
    // if call back arguments <editSuccess> was sent from backbean and true
    // then edit success and show all edit icons in page
    if (args.editSuccess === true) {
        jQuery('.ui-datatable-data > tr')
                .find('a.ui-row-editor-pencil')
                .each(function () {
                    jQuery(this).css('visibility', 'visible');
                });
        jQuery('.ui-datatable-data > tr')
                .find('.deleteBtn')
                .each(function () {
                    jQuery(this).css('visibility', 'visible');
                });
        // set flag to true to enable events again
        addEditFlag = true;
    } else if (args.editSuccess === false && args.rowIndex !== undefined) {
        // if edit failed then re-open current cell using sent row index from backbean
        jQuery(datatableId + ' .ui-datatable-data > tr')
                .find('span.ui-icon-pencil')
                .eq(args.rowIndex)
                .each(function () {
                    jQuery(this).click();
                });
    }
}

function cancelEdit() {

    // if edit was canceled show edit icons again
    jQuery('.ui-datatable-data > tr')
            .find('a.ui-row-editor-pencil')
            .each(function () {
                jQuery(this).css('visibility', 'visible');
            });
    jQuery('.ui-datatable-data > tr')
            .find('.deleteBtn')
            .each(function () {
                jQuery(this).css('visibility', 'visible');
            });

    // set flag to true to enable events again
    addEditFlag = true;
}
function test(cu) {
console.log( cu);

   alert('here');
   
}
//
//$(window).on('load ', function () {
//
//    var win = $(this);
//    if (win.width() > 640) {
//        $('.ui-datatable').removeClass('ui-datatable-reflow');
//    } else {
//        $('.ui-datatable').addClass('ui-datatable-reflow');
//    }
//});
//$(window).on('resize', function () {
//
//    var win = $(this);
//    if (win.width() > 640) {
//        $('.ui-datatable').removeClass('ui-datatable-reflow');
//    } else {
//        $('.ui-datatable').addClass('ui-datatable-reflow');
//    }
//});