/* Dependencies:
* jQuery 1.3.1+
* jQuery plugin 'scrollTo' 1.4.0+
*/
var allElements = null; 

$j(document).ready(function(){
    // Profiling shows that the internal jQuery call curCSS() speeds up from ~160ms on first exec to ~6ms on all consecutive runs.
    // A wild guess is that this is related to caching, but
    // either way, we'll speed things up before the user clicks anything so he or she won't notice.
    // We basically want to make sure Interals/jQuery.curCSS has been ran once.
    allElements = $j("ul.categoryGroup").children();    
    $j("a.category").bind("click", expand);
    // If we have an anchor reference in the URL we'll fake a click on the corresponding anchor to trigger the right event
    if(document.location.hash != '')
    {
        $j('a.category[href="' + document.location.hash.toLowerCase() + '"]').click();
    }
});

function expand(event)
{
    
    var list = $j(event.currentTarget).parent().parent().parent();
    if($j(list).hasClass("has-expansion"))
    {
        $j(list).children("li.expansionRow").children("ul.expansion").fadeOut("fast");
        $j(list).children("li.expansionRow").slideUp("normal", function(){
            $j(list).children("li.expandedSibling").removeClass("expandedSibling");
            $j(list).children("li.expansionRow").remove();
            var sameElementClicked = $j(event.currentTarget).parent().parent().hasClass("expanded");
            $j(list).children("li.expanded").removeClass("expanded");
            $j(list).removeClass("has-expansion");
            if(!sameElementClicked)
            {
                showExpansion(event);
            }
        });
    }
    else
    {
        showExpansion(event);
    }
    
    
    return true;
}

function logExpansion(expansion, categoryName, categoryId)
{
    var entries = { category : categoryName, categoryId: categoryId};
    var i = 1;
    $j(expansion).children("li.expansion").each(function(){
        entries['documentId[' + $j(this).attr('name') + ']'] = i;
        i++;
    });

    $j.get('logViews.php', entries);
}

function showExpansion(event)
{

    // saving yottabytes 
    var expansion = $j(event.currentTarget).parent().parent().children("ul.expansion");
    var currentExpansion = $j(event.currentTarget).parent().parent();
    logExpansion(expansion, $j(event.currentTarget).text(), $j(currentExpansion).attr('id'));
    allElements = $j(currentExpansion).parent().children();     
    
    
    // Locate where to add a new row, then add it and clone in a copy of the expansion ul residing in the li that was selected
    var addAfter;
    if($j(expansion).parent().hasClass("lastInRow"))
    {
        addAfter = $j(expansion).parent();
    }
    else
    {
        addAfter = $j(expansion).parent().nextAll("li.lastInRow").eq(0);
    }
    $j(addAfter).after('<li class="expansionRow"></li>');
    
    $j(expansion).clone().appendTo($j(expansion).parent().nextAll("li.expansionRow").eq(0));
    

    
    
    // Our cloned ul has a default display property of none as the original element we cloned it from is not supposed to be shown, ever (it's there for us to copy and for semantic reasons).
    // The clone, on the other hand, should be shown
    $j(expansion).parent().nextAll("li.expansionRow").eq(0).children("ul.expansion").show();
    // Show new expansion row
    $j.scrollTo(currentExpansion, {easing: 'swing', duration: 500, offset: {top: -100}, onAfter: function()
    {
        $j(expansion).parent().nextAll("li.expansionRow").eq(0).slideDown("normal");
    }});
    
    // Setting class on the li that was selected (it should appear as the selected tab)
    $j(event.currentTarget).parent().parent().addClass("expanded");
    // To avoid having to perform a hasClass on all children of the topmost ul (it can has lotz of children!!11) we'll just mark the ul with a class indicating that it has an expansionRow element in it instead
    // This is somewhat a performance hack
    $j(event.currentTarget).parent().parent().parent().addClass("has-expansion");

    
    // Apply setting on neighbours (typically siblings that need border-bottom and such for the new row to appear as a tab)
    if(!$j(currentExpansion).hasClass("lastInRow"))
    {
        $j(allElements).slice(
            $j(allElements).index(currentExpansion)+1, 
            $j(allElements).index($j(currentExpansion).nextAll("li.lastInRow").eq(0))+1
            ).addClass("expandedSibling");      
    }
    if(!$j(currentExpansion).hasClass("firstInRow"))
    {
        $j(allElements).slice(
            $j(allElements).index($j(currentExpansion).prevAll("li.firstInRow").eq(0)),
            $j(allElements).index(currentExpansion)
            ).addClass("expandedSibling");      
    }       
    
    
}