$(document).ready(function() {
    /* stickies */
    $('#stickies .stickies_close_area a')
        .click(function() {
            $(this).parent().parent().fadeOut('slow');
            return false;
        });

    /* form */
    $('.form .field')
        .click(function() {
            $(this)
                .parents('li').addClass('focused')
                .siblings('li').removeClass('focused');
        })
        .focus(function() {
            $(this)
                .parents('li').addClass('focused')
                .siblings('li').removeClass('focused');
        })
        .blur(function() {
            $(this).parents('li').removeClass('focused');
        });
});

var linkwithin_div_class="linkwithin";
var linkwithin_site_id = 86323;

(function() {
    function logout() {
        var $x = $("<form action='/logout' type='post'></form>")
        $("<input type='hidden'>").attr("name", window._auth_token_name).attr("value", window._auth_token).appendTo($x);
        $x.appendTo(document.body).submit();
    };

    window.techbang_logout = function () {
        try {
            FB.Connect.get_status().waitUntilReady(function(status) {
                switch(status) {
                case FB.ConnectState.connected:
                    FB.Connect.logout(function() {
                        logout();
                    });
                    break;
                case FB.ConnectState.appNotAuthorized:
                case FB.ConnectState.userNotLoggedIn:
                    logout();
                    break;
                }
            });
        }
        catch(e) {
            logout();
        }
        return false;
    }
})();

$(function() {
    try {
        if (location.hash) {
            $(location.hash).get(0).scrollIntoView();
        }
    } catch(e) {}

    // A minimal tabs implementation.
    $(".technique-posts .tabs,.highlight .tabs").each(function() {
        var self = this;
        $("li", this).removeClass("current");
        $("li:first", this).addClass("current");

        var show_only_current_tab = function() {
            $(self).find("li a").each(function() {
                var s = $(this).attr("href");
                if ( $(this).parent("li").is(".current") ) {
                    $(s).show();
                } else {
                    $(s).hide();
                }
            });
        };
        show_only_current_tab();

        $("li a", this).bind("click", function() {
            $(this).parent("li").siblings().removeClass("current");
            $(this).parent("li").addClass("current");
            show_only_current_tab();
            return false;
        });
    });

    if ($(".linkwithin").size() > 0) {
        $.ajax({
            'url': "http://www.linkwithin.com/widget.js",
            'cache': true,
            'dataType': 'script',
            'error': function() {
                $(".linkwithin").remove();
            }
        });
    }

    if ($("a[name=fb_share]").size() > 0) {
        $.getScript("http://static.ak.fbcdn.net/connect.php/js/FB.Share");
    }
});
