var findfred = (new function ($) {
    var emptyCls    = 'empty-no-data';
    var disabledCls = 'disabled';
    var inactiveCls = 'text-field-inactive';
    var _reg        = [];
    
    $(document).ajaxError(function(event, request) {
            if(request.status == '403') {
                var port = window.location.port ? ':' + window.location.port : '';
                window.location.href = 'http://' + window.location.hostname + port;
            }
        }).ready(function() {
        $.each(_reg, function(i, o) {
            o.call();
        });
    });
    
    return {
        /**
        * @description registers ready function
        */
        reg : function(f) {
            _reg.push(f);
        },
        /**
        * @description array of objects, with el
        */
        tts : function(arr) {  
            $.each(arr, function(i, o) {
                $('#' + o.el).bind('click', {
                    def: o.def,
                    inactiveCls: o.inactiveCls || inactiveCls
                }, findfred._tt);
                $('#' + o.el).bind('blur', {
                    def: o.def,
                    inactiveCls: o.inactiveCls || inactiveCls
                }, findfred._tt);
            });
        },
        _tt : function(e) {
			e.target.value = e.type != 'click' ? (!e.target.value ? e.data.def : e.target.value) : (e.target.value != e.data.def ? e.target.value : '');
			if(e.target.value == e.data.def) { 
				$(e.target).addClass(e.data.inactiveCls);
			} else { 
				$(e.target).removeClass(e.data.inactiveCls);
			}
		},
        // o: {
        //      totalPage : total number of pages
        //      baseURI : base URI for requests
        //      currentPage : the cursor (current page) of the resultset
        //      start: where in the resultset the count starts
        //      end: where in the resultset the count ends
        //      count: total number of records in this set
        //      totalCount: total count of all matching records
        //      selectEl : the id of the select element
        //      countEl : the id of the count element
        //      lessEl : the id of the less element
        //      moreEl : the id of the more element
        //      displayEl : the id of the display element
        //      containerEl : the container which to append data to
        //      less : if there are less pages
        //      more : if there are more pages
        //      disabledCls : class name to apply to less/more elements if there are no more/less pages
        //      emptyCls: the class to apply to container if no records to display
        // }
        /**
        * @description updates pager
        */
        updatePager : function(o, data) {
            // handle select
            for(var i=0; i<o.totalPage; i++) {
                $('#' + o.selectEl).empty();
                $('#' + o.selectEl).append('<option value="' + o.baseURI + i + '" ' + (o.currentPage == i ? 'selected="selected" ' : ' ') + '>' + i + '</option>');
            }
            // handle number of pages
            $('#' + o.countEl).html(totalPages);
            // handle class of previous page icon
            if(o.less) {
                $('#' + o.lessEl).addClass(o.disabledClass);
            } else {
                $('#' + o.lessEl).removeClass(o.disabledClass);
            }
            // handle class of next page icon
            if(o.more) {
                $('#' + o.moreEl).addClass(o.disabledClass);
            } else {
                $('#' + o.moreEl).removeClass(o.disabledClass);
            }
            // update display and data
            if(o.count) {
                $('#' + o.containerEl).removeClass(o.emptyCls);
                $('#' + o.containerEl).empty();
                $('#' + o.containerEl).append(data);
                // Displaying records 1-10 of 500 (page 1 of 50)
                $('#' + o.displayEl).html = 'Displaying records ' + o.start + '-' + o.end + ' of ' + o.totalCount + ' (page ' + o.currentPage + ' of ' + o.totalPage + ')';
            } else {
                $('#' + o.containerEl).empty();
                $('#' + o.containerEl).addClass(o.emptyCls);
            }
        }
        
    }
}(jQuery));
 

