﻿/// <reference path="jquery-1.3.2.js" />

$(function() {
    $('.video_links a').click(function() {
        var count = $(this).parent().children().size();
        var t = $(this).text();

        // 'i' will be used outside the for loop, so it is declared here
        var i;
        for (i = 1; i < count; i++) {
            var t1 = $(this).parent().children(':nth-child(' + i + ')').text();

            if (t == t1) {
                break;
            }
        } /* 'i' is now equal to the 1-based index of the links, so if the second link
             was clicked, 'i' is now 2 */

        // Find the appropriate .additional_info div
        var infoDiv = $(this).parent().parent().children('.additional_info');

        if ($(infoDiv).hasClass('expanded')) {
            // Slide the info div of out view
            $(infoDiv).animate({ height: '0', opacity: '0%' }, 'fast',
                function() {
                    finishAnim(infoDiv, i);
                }
            );
        } else {
            $(infoDiv).addClass('expanded');
            $(infoDiv).animate({ height: '0', opacity: '0%' }, 0,
                function() {
                    finishAnim(infoDiv, i);
                }
            );
        }


    });
})

function finishAnim(infoDiv, i) {    
    // Hide any currently visible divs within infoDiv
    $(infoDiv).children().hide();

    // Show in the 'i' div
    $(infoDiv).children(':nth-child(' + i + ')').show();
    var height = $(infoDiv).children(':nth-child(' + i + ')').height();

    // Slide the info div into view
    $(infoDiv).animate({ height: height, opacity: '100%' }, 'fast');
}
