$.fn.tabs = function () {

    return this.each(function () {

        var $tabwrapper = $(this);
        var $panels = $tabwrapper.find('> div div');    // Search down the DOM to get all child divs within div#tabwrapper
        var $tabs = $tabwrapper.find('li > a');         // Search down the DOM to get all child links within div#tabwrapper
        
        $tabs.click(function(){
                                    
            $tabs.removeClass('selected')               // Remove selected class from all tabs
                 .filter('[hash=' + this.hash + ']')    // Filter tab selection to the clicked one using its hash
                 .addClass('selected');                 // Add selected class to the tab
                 
            $panels.hide()                              // Hide all panels
                   .filter(this.hash)                   // Filter panel selection by clicked tab's hash (tab's href/hash == id selector of panel)
                   .show();                             // Show the panel
                   
            return false;
            
        }).filter(window.location.hash ? '[hash=' + window.location.hash + ']' : ':first').click(); // Simulate click on first tab or from URL hash
    });
}

$(document).ready(function() {
    $('div#mediaTabsContainer').tabs();
});
