$(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;
    });

    $('#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).keypress(function(e) {
        //alert(e.which); return;
        if ($('.focused').size() != 0) return;

        //generic
        if (typeof(e.keyCode) == 'number' && e.keyCode > 0) {
            //special chars
            switch (e.keyCode) {
                case 39: //right arrow
                document.location = $('li.next a').attr('href');
                //document.location=;
                break;
                case 37: //left arrow
                document.location = $('li.prev a').attr('href');
                break;
            }
        }
        if (typeof(e.which) == 'number' && e.which > 0) {
            //regular chars
            switch (e.which) {
                case 108: //l larger
                $('#twimage').trigger('click');
                break;
                case 104: //h home
                document.location = '/';
                break;
                case 117: //u user
                document.location = '/u/'+$('#thistweet').attr('tg:username');
                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');
        }
    });

});
