var Series;

(
    function ()
    {
        // Borrowed from script.aculo.us.
        var REQUIRED_PROTOTYPE = '1.6.0.3';
        function convertVersionString (versionString) {
            var v = versionString.replace(/_.*|\./g, '');
            v = parseInt(v + '0'.times(4-v.length));
            return versionString.indexOf('_') > -1 ? v-1 : v;
        }

        if((typeof Prototype       == 'undefined') ||
           (typeof Element         == 'undefined') ||
           (typeof Element.Methods == 'undefined') ||
           (convertVersionString(Prototype.Version) < REQUIRED_PROTOTYPE)) {
            throw("Index library requires the Prototype JavaScript framework >= " + REQUIRED_PROTOTYPE);
        }

        // Begin Series class.
        Series = Class.create(
            {
                initialize:  function () {},
                __junk:      null
            }
        );
    }
)();

var ActivateTabs = function ()
{

    if (typeof(MT) == 'undefined') {
        var MyTabs= new mt('user_tabs','div.my_tab');

        MyTabs.removeTabTitles('h5.tab_title');

        MyTabs.addTab('t1','Get Started');
        MyTabs.addTab('t2','What\'s New?');
        MyTabs.addTab('t3','Classroom');
        MyTabs.addTab('t4','Training');
        MyTabs.makeActive('t1');
    }
};

function initHideAllShowParam (namespace)
{
    if (typeof namespace == 'undefined') {
        namespace = 'test';
    }

    HideAll(namespace);
    showContentById(namespace + '::1');
}

var allExpanded = true;

function HideAll (namespace)
{
    if (typeof namespace == 'undefined') {
        namespace = 'test';
    }

    var prefix = namespace + '-content-';
    var regex  = new RegExp('^' + prefix);

    $$('.accordion_content').each(
        function (d)
        {
            var id = d.id;
            if (id.indexOf(prefix) != 0) return;
            if (id == 'test-content-cmpl') return;
            if (id == 'test-content-proj_prev') return;
            if (id == 'test-content-adpt_ext') return;
            d.hide();
            id = id.replace(regex, '');
            if (id == 'sidebyside') {
                showExpandButton(namespace + '::' + id, 'left');
                showExpandButton(namespace + '::' + id, 'right');
            }
            else {
                showExpandButton(namespace + '::' + id);
            }
        }
    );
    updateMainHeight(namespace);
    allExpanded = false;
}

function ShowAll (namespace)
{
    if (typeof namespace == 'undefined') {
        namespace = 'test';
    }

    var prefix = namespace + '-content-';
    var regex  = new RegExp('^' + prefix);

    $$('.accordion_content').each(
        function (d)
        {
            var id = d.id;
            if (id.indexOf(prefix) != 0) return;
            d.show();
            id = id.replace(regex, '');
            if (id == 'sidebyside') {
                var div_left  = $('podlet_content_left_'  + id);
                var div_right = $('podlet_content_right_' + id);
                if (div_left)  div_left.show();
                if (div_right) div_right.hide();
                showCollapseButton(namespace + '::' + id, 'left');
                showExpandButton  (namespace + '::' + id, 'right');
            }
            else {
                showCollapseButton(namespace + '::' + id);
            }
        }
    );
    updateMainHeight(namespace);
    allExpanded = true;
}

function showContentById (id, side)
{
    var namespace = 'test';
    var subid     = id;
    var i = id.indexOf('::');
    if (i != -1) {
        namespace = id.substring(0, i);
        subid     = id.substring(i+2);
    }

    var div = $(namespace + '-content-' + subid);
    if (! div) return;

    div.show();

    if (side) {
        var div_left  = $('podlet_content_left_'  + subid);
        var div_right = $('podlet_content_right_' + subid);
        if (side == 'left') {
            if (div_left)  div_left.show();
            if (div_right) div_right.hide();
            showCollapseButton(id, 'left');
            showExpandButton  (id, 'right');
        }
        else if (side == 'right') {
            if (div_left)  div_left.hide();
            if (div_right) div_right.show();
            showExpandButton  (id, 'left');
            showCollapseButton(id, 'right');
        }
    }
    else {
        showCollapseButton(id);
    }

    if (namespace == 'test' && id != 'cmpl') showContentById('cmpl'); // for LAs

    updateMainHeight(namespace);
    return false;
}

function hideContentById (id)
{
    var namespace = 'test';
    var subid     = id;
    var i = id.indexOf('::');
    if (i != -1) {
        namespace = id.substring(0, i);
        subid     = id.substring(i+2);
    }

    var div = $(namespace + '-content-' + subid);
    if (! div) return;

    div.hide();

    if (id == 'sidebyside') {
        showExpandButton(id, 'left');
        showExpandButton(id, 'right');
    }
    else {
        showExpandButton(id);
    }
    return false;
}

function toggleContentById (id, side)
{
    var namespace = 'test';
    var subid     = id;
    var i = id.indexOf('::');
    if (i != -1) {
        namespace = id.substring(0, i);
        subid     = id.substring(i+2);
    }

    var div = $(namespace + '-content-' + subid);
    if (! div) return;

    if (namespace == 'test' && allExpanded) {
        if (div.visible()) {
            hideContentById(id);
        }
        else {
            showContentById(id, side);
        }
    }
    else {
        var show = ! div.visible();
        if (!show && side) {
            var visible = findSideBySideVisible(id);
            if (side != visible) show = true;
        }

        HideAll(namespace);
        if (show) {
            showContentById(id, side);
        }
    }
    updateMainHeight(namespace);
}

// Same as toggleContentById, except it only tests
// if the div will open, doesn't actually open it.
//
function toggleWillOpenById (id, side)
{
    var namespace = 'test';
    var subid     = id;
    var i = id.indexOf('::');
    if (i != -1) {
        namespace = id.substring(0, i);
        subid     = id.substring(i+2);
    }

    var div = $(namespace + '-content-' + subid);
    if (! div) return false;

    if (namespace == 'test' && allExpanded) {
        if (div.visible()) {
            return false;
        }
        else {
            return true;
        }
    }
    else {
        var show = ! div.visible();
        if (!show && side) {
            var visible = findSideBySideVisible(id);
            if (side != visible) show = true;
        }

        return show;
    }
}

function updateSideBySideHeaderSpacer (id)
{
    var spacer = $('podlet_header_spacer');

    var hdr_left  = $('podlet_header_left');
    var hdr_right = $('podlet_header_right');

    var visible = findSideBySideVisible(id);

    if (spacer) {
        spacer.removeClassName('podlet_header_spacer_left_inactive');
        spacer.removeClassName('podlet_header_spacer_right_inactive');
        spacer.removeClassName('podlet_header_spacer_both_inactive');
    }
    if (hdr_left)  hdr_left.removeClassName('podlet_header_left_inactive');
    if (hdr_right) hdr_right.removeClassName('podlet_header_right_inactive');

    if (visible == 'left') {
        if (hdr_right) hdr_right.addClassName('podlet_header_right_inactive');
        if (spacer) spacer.addClassName('podlet_header_spacer_right_inactive');
    }
    else if (visible == 'right') {
        if (hdr_left)  hdr_left.addClassName('podlet_header_left_inactive');
        if (spacer) spacer.addClassName('podlet_header_spacer_left_inactive');
    }
    else {
        if (spacer) spacer.addClassName('podlet_header_spacer_both_inactive');
    }
}

function findSideBySideVisible (id)
{
    var namespace = 'test';
    var subid     = id;
    var i = id.indexOf('::');
    if (i != -1) {
        namespace = id.substring(0, i);
        subid     = id.substring(i+2);
    }

    var div = $(namespace + '-content-' + subid);
    if (! div) return;

    if (! div.visible()) return 'none';

    var div_left  = $('podlet_content_left_'  + subid);
    var div_right = $('podlet_content_right_' + subid);

    if (div_left && div_left.visible()) {
        return 'left';
    }
    else if (div_right && div_right.visible()) {
        return 'right';
    }
    else {
        return 'none';
    }
}

function showExpandButton (id, side)
{
    var namespace = 'test';
    var subid     = id;
    var i = id.indexOf('::');
    if (i != -1) {
        namespace = id.substring(0, i);
        subid     = id.substring(i+2);
    }

    var cimg;
    var eimg;
    if (side) {
        updateSideBySideHeaderSpacer(id);
        cimg = $(namespace + '-collapse-img-' + side + '-' + subid);
        eimg = $(namespace + '-expand-img-'   + side + '-' + subid);
    }
    else {
        cimg = $(namespace + '-collapse-img-' + subid);
        eimg = $(namespace + '-expand-img-'   + subid);
    }
    if (cimg &&   cimg.visible()) cimg.hide();
    if (eimg && ! eimg.visible()) eimg.show();
}

function showCollapseButton (id, side)
{
    var namespace = 'test';
    var subid     = id;
    var i = id.indexOf('::');
    if (i != -1) {
        namespace = id.substring(0, i);
        subid     = id.substring(i+2);
    }

    var cimg;
    var eimg;
    if (side) {
        updateSideBySideHeaderSpacer(id);
        cimg = $(namespace + '-collapse-img-' + side + '-' + subid);
        eimg = $(namespace + '-expand-img-'   + side + '-' + subid);
    }
    else {
        cimg = $(namespace + '-collapse-img-' + subid);
        eimg = $(namespace + '-expand-img-'   + subid);
    }
    if (eimg &&   eimg.visible()) eimg.hide();
    if (cimg && ! cimg.visible()) cimg.show();
}

function updateMainHeight (namespace)
{
    return;    // ?????

    $$('div.column').each(
        function (col)
        {
            var sum  = col.childElements().inject(
                0,
                function (sum, el)
                {
                    if ((el.tagName != "DIV") ||
                        (el.id == "tutorialwrapper")) {
                        return sum;
                    }
                    return sum + el.getHeight();
                }
            );

            // console.log("sum: " + sum);
            col.setStyle({'height': '' + sum + 'px'});
        }
    );
}

document.observe('dom:loaded',
    function (e) {
        initHideAllShowParam();
        updateMainHeight();
    }
);
