lazy load app (horizontal)

var lazyload_app = function(){
 
    function init(){
        $('#mousewheel_example').scroll(function() {
            var $this = $(this);
            var $table = $this.find('table');

            var cw = $this.width();
            var tw = $table.width();
            var left = $this.scrollLeft();
            //console.log($this.width());
            //console.log($table.width());
            //console.log($this.scrollLeft());
            if(cw+left == tw){
                //console.log('Right!');
                var index = get_column_index();
                get_column_data(index);
            }
        });
    }

    function fetch(column_index){
        get_column_data(column_index);
    }

    function get_column_index(){
        var $table = $('#itable');
        //-2: 減掉input column, 然後再減掉1才是index
        var column_index = $table.find('tr:first td').length - 2;

        //Return next index
        return column_index + 1;
    }

    function get_column_data(column_index){
        var params = {
            from: $('#from_date').val(), 
            to: $('#end_date').val(), 
            vital_type: $('#vital_type').val(), 
            patient_id: $('#patient_id').val(), 
            column_index:column_index
        }

        $.ajax({
            type: 'get',
            url: base_url + 'inquire/ajax_column_data',
            data: params,
            dataType: 'json',
            success: function(res){
                if(typeof res.status == 'undefined'){
                    alert(res);
                }else if(res.status == 'fail'){
                    alert(res.message);
                }else{
                    console.log(res);
                    show_columns(res.data);
                    syncscroll_app.refresh();
                }
            },
            error: function(res){
                //var errors = res.responseJSON;
                //show_response_error(errors);
                if(typeof res.responseText != 'undefined'){
                    alert(res.responseText);
                }else{
                    alert(res);
                }
            }
        });
    }

    function show_columns(data){
        var $table = $('#itable');
        for(var i = 0; i < data.length; i++){
            var col_data = data[i];

            $table.find('tr').each(function(index){
                var $this = $(this);
                $this.append($(col_data[index]));
            });
        }
    }

    return {
        init: init,
        fetch: fetch
    }
}();