var Sharer = {
    shareWindow: function(base_url, window_title, width, height, args) {
        width = width || 550;
        height = height || 550;
        args = args || {};
        base_url = base_url.replace('{:location}', encodeURIComponent(args.location || location.href))
            .replace('{:title}', encodeURIComponent(args.title || document.title));
        window.open(base_url, window_title, 'toolbar=no,width=' + width + ',height=' + height);
    }
};

$(function() {
    if($('.flash').length > 0) {
        setTimeout(function() {
            var hideTimeout = setTimeout(function() {
                $('.flash').hide('slide', {direction: 'down'});
            }, 5000);
            $('.flash').show('slide', {direction: 'down'}).one('click', function() {
                clearTimeout(hideTimeout);
                $(this).hide('slide', {direction: 'down'});
            });
        }, 1000);
    }
    
    $('input[type=checkbox]').each(function() {
        $(this).wrap(function() {
            $(this).hide();
            return ($(this).is(':checked')) ? '<div class="input-checkbox checked" />' : '<div class="input-checkbox" />';
        });
    });

    $('.input-checkbox').click(function () {
        $(this).toggleClass('checked');
        if($(this).hasClass('checked')) {
            $(this).find('input[type=checkbox]').attr('checked', 'checked');
        } else {
            $(this).find('input[type=checkbox]').removeAttr('checked');
        }
    });
    
    $('.share-buttons .share.delicious').click(function() {
        Sharer.shareWindow('http://delicious.com/save?v=5&noui&jump=close&url={:location}&title={:title}', 'delicious');
        return false;
    });
    
    $('.share-buttons .share.facebook').click(function() {
        Sharer.shareWindow('http://www.facebook.com/sharer.php?u={:location}&t={:title}', 'facebook', 550, 450);
        return false;
    });
    
    $('.share-buttons .share.twitter').click(function() {
        Sharer.shareWindow('http://twitter.com/?status=RT%20%40RailsMX%3A%20{:title} %20%2F%2F%20 {:location}', 'twitter', 800, 200, {
            location: $(this).attr('data-value')
        });
        return false;
    });
    
    $('.share-buttons .share.digg').click(function() {
        Sharer.shareWindow('http://digg.com/submit?url={:location}&title={:title}', 'Digg', 800);
        return false;
    });
});

