var dtable = '';
var ctable = '';
$(document).ready(function() {

    //Init the table controller
    dtable = $('table#datatable').dataTable({
        "bLengthChange": false,
        "bFilter": false,
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "fnInitComplete": function() {
            $('div.fg-toolbar').addClass('clearfix');
            $('div.fg-toolbar:empty').hide();
        }
    });

    //Add span to end of datatable wrapper and clearfix to fake pagination background
    /*$('div#datatable_wrapper').addClass('clearfix');

    var info = ('.dataTables_info', dtable),
    paginate = ('.dataTables_paginate', dtable);
    //$('div#datatable_wrapper').append('<span class="faux_bg"></span>');*/

    //Resource filter
    $('select.resource_filter').change(function() {
        var a = $('select.resource_filter :selected').val();
        if (a != "All Resource Types") {
            dtable.fnFilter(a, 1);
        } else {
            dtable.fnFilter('', 1);
        }
    })

    //Product filter
    $('select.product_filter').change(function() {
        var a = $('select.product_filter :selected').val();
        if (a != "All Products") {
            dtable.fnFilter(a, 2);
        } else {
            dtable.fnFilter('', 2);
        }
    })

    //Keyword search on the Name table
    $('.submit_btn .send').click(function() {
        var filter = $('#keyword_filter input').val();
        dtable.fnFilter(filter, 0);
        return false;
    })

    //Clear the keyword search and reset the form
    $('.submit_btn .reset').click(function() {
        $('#keyword_filter input').val('keyword');
        dtable.fnFilter('', 0);
        return false;
    })
    
    
    // Corporate Customer DataTables
    //Init the table controller
    ctable = $('table#customertable').dataTable({
        "sDom": 'f<"fg-toolbar"ipr>t',
        "bSort": false,
        "bLengthChange": false,
        "bFilter": false,
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "fnInitComplete": function() {
            $('div.fg-toolbar').addClass('clearfix');
            $('div.fg-toolbar:empty').hide();
        },
        "fnDrawCallback": function ( oSettings ) {
                    
			if ( oSettings.aiDisplay.length == 0 )
			{
				return;
			}
            
            // Get All Rows
            var nTrs = $('#customertable tbody tr');
            
            // Construct replacement Html            
            var custHTML = "<tr>";            
            var iCount = 0;
            for( var i = 0; i < nTrs.length; i++)
            {
                iCount = i + 1;
            
                var iDisplayIndex = oSettings._iDisplayStart + i;
				var sGroup = oSettings.aoData[ oSettings.aiDisplay[iDisplayIndex] ]._aData[0];
                                
                custHTML = custHTML + "<td>" + sGroup + "</td>";
                
                if((iCount % 2) == 0)
                {
                    custHTML = custHTML + "</tr><tr>";
                }
                                            
            }
            
            if((iCount % 2) != 0)
            {
                custHTML = custHTML + "<td>&nbsp;</td>";
            }
            
            custHTML = custHTML + "</tr>";
                        
            $("#customertable").html(custHTML);                        
        }        
    });
    
    //Product filter
    $('select.cust_product_filter').change(function() {        
        var a = $('select.cust_product_filter :selected').val();
        if (a != "All Products") {
            ctable.fnFilter(a, 1);
        } else {
            ctable.fnFilter('', 1);
        }
    })    
    
    
});
