/*******************************
* Workplace Hero Sidebar JS
* (c) 2010, 2Catalyze, Inc. All rights reserved
********************************/

$(document).ready(function() {

/*******************************************************************************
* Leaderboard
*******************************************************************************/

    $('#modes').tabs();

    $('#domain_filter').submit(function(){
        updateDomain();
        return false;
    });

    // Update the Domain tab
    updateDomain('mozilla.com');
    
    // Update the Global tab
    $.getJSON('http://' + window.location.host + '/wp-content/themes/workplacehero/leaderboard.php',function(users){
        if(users){
            drawList('global', users);
        }else{
            showMessage('global','Hey! Looks like you\'re the first one here. <strong><a href="https://rypple.com/feedback/a/?app=kudos" title="Enter now!">Enter now!</a></strong>');
        }        
    });
    
    // Update photos
    if($('#sidebar_photo').length > 0){
        $.getJSON('http://' + window.location.host + '/wp-content/themes/workplacehero/twitterPhotos.php?all',function(photos){
            if(photos){
                photos = photos['photos'][0];
                $('#sidebar_photo').append('<a href="' + photos['link'] + '" title="See the photo"><img src="' + photos['thumburl'] + '" alt="' + photos['description'] + '" /></a>');
                $('#sidebar_photo').append('<p><strong><a href="http://twitter.com/' + photos['username'] + '" title="Check out ' + photos['username'] + '\'s Twitter profile">@' + photos['username'] + '</a> says:</strong> ' + photos['description'] + '</p>');
            }
        });
    }
});

function updateDomain(target){
    // Delete the old table in case there is one
    $('#leaderboard_domain_list').remove();
    $('#leaderboard_domain p.loading').css('display','block');

    if(target == null){
        target = $('#domain_name').val();
    }
    
    $.getJSON('http://' + window.location.host + '/wp-content/themes/workplacehero/leaderboard.php?domain=' + target,function(users){
        if(users == 'freemail'){
            showMessage('domain','<strong>Sorry!</strong> Valid company email required.');
        }else if(users){
            drawList('domain', users);
        }else{
            showMessage('domain','Hey! Looks like you\'re the first one here. <strong><a href="https://rypple.com/feedback/a/?app=kudos" title="Enter now!">Enter now!</a></strong>');
        }
    });
}

function drawList(type, users){
     // Hide the loading animation
    $('#leaderboard_' + type + ' p.loading').css('display','none');
    $('#leaderboard_' + type + ' p.message').css('display','none');
    
    $('#leaderboard_' + type).append('<table id="leaderboard_' + type + '_list" class="leaderboard_listing"><thead><tr><th colspan="2">Name</th><th>Points</th></tr></thead><tbody>');
    for(var i=0; i<users.length; i++){
        $('#leaderboard_' + type + '_list').append('<tr><td class="leaderboard_ranking">' + (i+1) + '</td><td class="leaderboard_name">' + users[i]['name'] + ' <br/><span class="leaderboard_domain">' + users[i]['domain'] + '</span></td><td class="leaderboard_points">' + users[i]['points'] + '</td></tr>');
    }
    $('#leaderboard_' + type).append('</tbody></table>');
    
    $('#leaderboard_' + type + ' tr:even td').addClass('altRow');
}

function showMessage(type, message){
     // Hide the loading animation
    $('#leaderboard_' + type + ' p.loading').css('display','none');
    
    // Hide old messages
    $('#leaderboard_' + type + ' p.message').remove();
    
    $('#leaderboard_' + type).append('<p class="message">' + message + '</p>');
}
