
DRHorton = DRHorton || {};

DRHorton.community = DRHorton.community || {};

DRHorton.community.tableSorter = DRHorton.community.tableSorter || {};


$(function(){
    
    // add qmi widgets
    $.tablesorter.addWidget({
        id: "qmi",
        format: function (table) {
            if (table.config.debug) {
                var time = new Date();
            }
            var $tr, row = -1,
                odd;
                
            // loop through the visible rows
            $("tr:visible.BaseDataGirdItem", table.tBodies[0]).each(function (i) {                
                $tr = $(this);
                // style children rows the same way the parent
                // row was styled
                if (!$tr.hasClass(table.config.cssChildRow)) row++;
                odd = (row % 2 == 0);
                $tr.removeClass(
                table.config.widgetZebra.css[odd ? 0 : 1]).addClass(
                table.config.widgetZebra.css[odd ? 1 : 0])
            });
            if (table.config.debug) {
                $.tablesorter.benchmark("Applying Zebra widget", time);
            }
        }
    });
    
})
    
    
    
; (function($) {
    
    var
        _defaultPager = "<div id=\"pager\" class=\"pager\"><img src=\"/App_Themes/DRHortonSite/js/jquery.tablesorter/bw/first.png\" class=\"first\" style=\"display:none;\"/><img src=\"/App_Themes/DRHortonSite/js/jquery.tablesorter/bw/prev.png\" class=\"prev\" style=\"display:none;\"/><div class=\"pagelinks\" style=\"margin: 4px;\"></div><input type=\"hidden\" class=\"pagedisplay\"/><img src=\"/App_Themes/DRHortonSite/js/jquery.tablesorter/bw/next.png\" class=\"next\" style=\"display:none;\"/><img src=\"/App_Themes/DRHortonSite/js/jquery.tablesorter/bw/last.png\" class=\"last\" style=\"display:none;\"/><input class=\"pagesize\" type=\"hidden\" value=\"3\" /></div>",
        _defaultTextExtraction = function(node) {
            
            if($(node).text().replace(/[^a-z0-9.]/img, "").indexOf('miles') > -1)
                return parseInt($(node).text().replace(/[^a-z0-9.]/img, ""))
            else 
                return $(node).text().replace(/[^a-z0-9.]/img, "");
        },
        _prepareQMI = function() {
            
            var table = $('#QuickMoveInTable');
            
            //table.after(_defaultPager)
            table.find('tfoot tr:first td:first').append(_defaultPager)
            
            
            table
                .sortableHomePrices()
                .tablesorter({
                    textExtraction: _defaultTextExtraction,
                    headers: {
                        0: {
                            sorter: false
                        },
                        1: {
                            sorter: false
                        }
                    },
                    widgets: ['qmi']
                })
                /*
                .tablesorterPager({
                    positionFixed: false,
                    size: 3,
                    container: $("#pager"),
                    widgets: ['qmi']
                });
                */
            
            
            $('#QuickMoveInTable div.pagelinks, #QuickMoveInTable div.pagelinks a').css({
                "font-size": "14px",
                "letter-spacing": "3px",
                "text-decoration": "none"
            });
                        
            $('li[id] a[onclick*=ContainerQMI]').unbind('click', _prepareQMI);
            
        },

        _preparePlanList = function() {
            var table;

            $('#tabContainerList_PlanList table').each(function(){
                table = $(this)
                $('a[href]', table)
                .toast("<div style=\"margin-top: 8px; position:absolute; display:block; width: 134px; height: 103px; background-image: url(/App_Themes/DRHortonSite/images/calloutright.png); background-repeat: no-repeat; padding: 16px;padding-bottom:19px; z-index:2000;\"></div>")
                
                
                table
                    .sortableHomePrices()
                    .tablesorter({
                        textExtraction: _defaultTextExtraction,
                        headers: {
                            0: {
                                sorter: false
                            }
                        },
                        widgets: ['zebra'],
                        sortList: [[2,0]]
                    });
                    
                            
                    // give the last two headers some space away from sort icons            
                    table.find('th:gt(6)').css({
                        "padding-left": "0",
                        "padding-right": "16px"
                    });
                    
            });

            $('li[id] a[onclick*=ContainerPlanList]').unbind('click', _preparePlanList);
            
        },

        _prepareNearbyList = function() {
        
            var table = $('#NearbyCommunitiesTable');
        
            $('a[href]', table)
                //.toast("<div style=\"display:block; width: 134px; height: 103px; background-image: url(/App_Themes/DRHortonSite/images/calloutright.png); background-repeat: no-repeat; padding: 16px;padding-bottom:19px;  z-index:1000;\"></div>")
                .toast("<div style=\"display:block; width: 134px; height: 87px; background-image: url(/App_Themes/DRHortonSite/images/calloutright.png); background-repeat: no-repeat; padding: 16px 0 0 12px; z-index:2000;\"></div>")
        
            table
                .sortableHomePrices()
                .tablesorter({
                    textExtraction: _defaultTextExtraction,                        
                    headers: {
                        0: {
                            sorter: false
                        }
                    },
                    widgets: ['zebra']
                });
        
            // give the last two headers some space away from sort icons            
            table.find('th:gt(3)').css({
                "padding-left": "0",
                "padding-right": "5px"
            });
            
            $('li[id] a[onclick*=ContainerNearbyList]').unbind('click', _prepareNearbyList);
            
            
        }
        
    ;

    DRHorton.community.tableSorter.init = function() {
        
        var tabDefault = DRHorton.cookies.getCookie("TabDefault");

        $('li[id] a[onclick*=ContainerQMI]').bind('click', _prepareQMI)
        $('li[id] a[onclick*=ContainerPlanList]').bind('click', _preparePlanList)
        $('li[id] a[onclick*=ContainerNearbyList]').bind('click', _prepareNearbyList)
        
        switch (tabDefault) {
            case ("ContainerQMI"): 
                {
                    _prepareQMI();
                    break;
                }
            case ("ContainerPlanList"): 
                {
                    _preparePlanList();
                    break;
                }
            case ("ContainerNearbyList"): 
                {
                    _prepareNearbyList();
                    break;
                }
        }
        
    }

})(jQuery);


$(function() {
    DRHorton.community.tableSorter.init();
});

