$(document).ready(function(){

    // Tweet form for logged out users
    $('div#new-user h2 a').click(function(){
        $(this).parent().parent().addClass('open');

        //$('div#new-user h3').html('Don\'t have an account? <a href="http://twitter.com/signup" target="_blank">Sign Up for Twitter &raquo;</a>');
        //$('div#new-user form fieldset').show();
        return false;
    });

    // Rotate the image left or right
    $('#rotateLeft').click(function() {
        $('#fullsize').rotateLeft();
        return false;
    });

    $('#rotateRight').click(function() {
        $('#fullsize').rotateRight();
        return false;
    });

    /* Show and hide the rotate actions on hover ---------- */
    $("div.media span").hover(function() {
        $("div.media span a.rotate").show();
    }, function() {
        $("div.media span a.rotate").hide();
    });

    // media actions
    $('#deleteLink').click(function() {
        if (confirm('This image will be deleted from Twitgoo. Do you want to continue?')) {
            $('#deleteForm').submit();
        }
        return false;
    });
    $('#abuseLink').click(function() {
        if (confirm('Are you sure you want to report this image for abuse? Do you want to continue?')) {
            $('#abuseForm').submit();
        }
        return false;
    });
    $('#syncLink').click(function() {
        if (confirm('Are you sure you wish to try to find this message on twitter?')) {
            $('#syncForm').submit();
        }
        return false;
    });

    $('#shareLink').click(function() {
        $('#divWildfirePost').fadeIn('fast');
        return false;
    });

    $('.getfocused').focus(function() {
        $(this).addClass('focused');
    }).blur(function() {
        $(this).removeClass('focused');
    });

    $('#twimage').colorbox({transition:"fade", speed:250, initialWidth:0, initialHeight:0, maxWidth:"85%", maxHeight:"85%"});
    $('li.enlarge a').click(function() {
        $('#twimage').trigger('click');
        return false;
    });

    $(document).keyup(function(e) {
        //alert(e.which); return;
        if ($('.focused').size() != 0) return;

        //generic
        if (typeof(e.keyCode) == 'number' && e.keyCode > 0) {
            switch (e.keyCode) {
                case 39: //right arrow
                document.location = $('li.next a').attr('href');
                break;
                case 37: //left arrow
                document.location = $('li.prev a').attr('href');
                break;
                case 76: //l larger
                $('#twimage').trigger('click');
                break;
                case 72: //h home
                document.location = '/';
                break;
                case 85: //u user
                document.location = '/u/'+$('#thistweet').attr('tg:username');
                break;
                case 82: //r reply
                document.location.replace(document.location.href.split('#')[0] + '#reply');
                $('.tweet .tweet_text').focus();
                break;
            }
        }
    });

    //colorbox extensions
    $('#cboxContent').append(
        $('<div id="cboxOrig" title="See Original">See Original</div>')
    );
    $('#cboxClose').attr('title', 'Close');
    $('#cboxOrig').click(function() { 
        window.open($.fn.colorbox.element().href); 
    }).mouseover(function() { 
        $(this).addClass('hover'); 
    }).mouseout(function() { 
        $(this).removeClass('hover'); 
    });
    $().bind('cbox_complete', function() {
        if (!$.fn.colorbox.element().href.match(/\.(gif|png|jpg|jpeg|bmp)(?:\?([^#]*))?(?:#(\.*))?$/i)) {
            $('#cboxOrig').addClass('hidden');
        }
    });

});

