if ( !BODOG ) { var BODOG = {} ; } ;
if ( !BODOG.components ) { BODOG.components = {} ; } ;

BODOG.components.tournamentSchedule = {

    updateFilter: function( v ) {
        
        $('.filter #schedule-status').show() ;
        
        if ( v == "" ) {
            // Show all Tournaments
            $('#schedule-filtered').empty() ;
            $('#schedule-filtered').hide() ;
            $('#schedule select').attr('selectedIndex', 0) ;
            $('.filter #schedule-status').hide() ;
            $('#schedule').show() ;
            return ;
        }
        
        // Retrieve Tournaments that match filter
     
        that = this ;
        
        $.ajax({
            type: "POST",
            url: '/client-game-info/poker-tournament-schedule-get.jsp',
            data: 'filter='+encodeURIComponent( v ),
            success: function( response, status ) {
                that.processRequest( response, status, that ) ;
            }
        });
    },
    
    processRequest: function( response, status, parent ) {
        // Display filtered results only
        
        $('#schedule-filtered').empty() ;
        $('#schedule').hide() ;
        $( response ).appendTo('#schedule-filtered') ;
        
        parent.formatDays('#schedule-filtered') ;
        parent.addRowStriping('#schedule-filtered') ;
        parent.initializeTooltips('#schedule-filtered') ;
        
        $('.filter #schedule-status').hide() ;
        $('#schedule-filtered').show() ;
        
    },
    
    initializeTooltips: function( id ) {
        $(id + ' a.load-local').mouseover( function(){
            if ( $(this).attr('ttloaded') == undefined ) {
                $(this).attr('ttloaded', true) ;
                
                $(this).cluetip({
                    sticky: true,
                    positionBy: ($.browser.opera) ? "bottomTop" : "auto",
                    closePosition: 'title',
                    arrows: true,
                    activation:'click',
                    local:true
                }) ;
            }
        });
    },
    
    addRowStriping: function( id ) {
        $(id + ' .tournament-schedule tbody tr:even').addClass("alt") ;
    },
    
    formatDays: function( id ) {
        var that = this ;
        $(id + ' .tournament-schedule thead th').each(function() {
            var stamp = $(this).text().toUpperCase() ;
            if ( stamp != "TIME" ) {
                $(this).text( that.getDayStr( stamp ) ) ;
            }
        }) ;
    },
    
    getDayStr: function( day ) {
        // Format YYYYMMDD to example: MON JUN 02
        var y = day.substring(0,4) ;
        var m = day.substring(4,6) ; m-- ;
        var d = day.substring(6,8) ;
        
        var newday = new Date(y,m,d).toString() ;
        if ( $.browser.opera ) {
            // Opera handles the date string slightly differently
            newday = newday.replace(/,/, '') ;
        }
        
        var a = newday.split(' ') ;
        return a[0]+' '+a[1]+' '+a[2] ;
    },
    
    init: function() {
        this.formatDays('#schedule') ;
        this.addRowStriping('#schedule') ;
        this.initializeTooltips('#schedule') ;
    
        $('.filter #schedule-status').hide() ;
        $('#schedule').show()
        $('#schedule-filtered').empty().hide() ;
    }
    
} ;



$(document).ready(function() {
    BODOG.components.tournamentSchedule.init() ;
}) ;