$(document).ready(function() {

    var content = '';
    var f = '';
    var g = '';
    /* ------------------------------------------------------------------------------------------------------*/
    /* make tabs */
    $('#tf_content_tabbed > div > .secHeader').each(function(i) {
        $(this).hide();
        if (i == 0) {
            f = 'first ';
            g = ' es_open';
        } else {
            f = '';
            g = '';
        }
        content += '<li class="' + f + 'es_closed' + g + '"><span>' + $(this).children('h1:first').text() + '</span></li>';
    });
    content = '<div class="mainTabs"><ul>' + content + '</ul></div>';
    $('#tf_content_tabbed').prepend(content);
    /*------------------------------------------------------------------------------------------------------*/
    /* reclink */
    /* we use addClass/removeClass to store the current cmd-string in the page */
    /* we get the orig-rec-url from spacial a */
    /* we append the rec-hash as get-param at the end of the recLink-get-params, */
    /* to convert it in the rec-form back to #hash */

    var recTab_last = '';
    var recTab_key = 'recTab';
    var recUrl = $('#rec a#recCmd_initUrl').attr('href');

    function setCmdTab(i) {
        recTab = recTab_key + '.' + i;
        $('#recCmd').addClass(recTab);
        recTab_last = recTab;
        updateRecLinkTab();
    }
    function clearCmdTab() {
        if (recTab_last != '') {
            $('#recCmd').removeClass(recTab_last);
            recTab_last = '';
        }
        updateRecLinkTab();
    }
    function updateRecLinkTab() {
        var hash = $('#recCmd').attr('class');
        hash = jQuery.trim(hash);
        if (hash) {
            hash = '&recCmd=' + hash;
            hash = hash.replace(' ', '_');
        }
        $('#recCmd').html(hash);
        var recUrl_update = recUrl + hash;
        $('div a.jqRecCmd').attr('href', recUrl_update);
    }
    function parseIncomingHashTab() {
        var inHash = window.location.hash;
        $('#inHash').html(inHash);
        var inCmd_i = '';
        if (inHash) {
            inHash = inHash.substr(1);
            inHash = inHash.split('_');
            inHash = jQuery.makeArray(inHash);
            inHash = jQuery.grep(inHash,
            function(v, i) {
                var v_arr = v.split('.');
                if (v_arr[0] == recTab_key) {
                    return (v);
                }
            });
            inCmd_i = inHash[0].split('.');
            inCmd_i_number = Number(inCmd_i[1]);
            return (inCmd_i_number);
        }
    }
    /* ------------------------------------------------------------------------------------------------------*/
    /* show */

    var tabs = new Object();
    tabs.tabs = $('#tf_content_tabbed .mainTabs li');
    tabs.sections = $('#tf_content_tabbed > div > .sec');

    function tabHideShow(i) {
        clearCmdTab();
        setCmdTab(i);
        // tabs
        tabs.tabs.removeClass('es_open');
        tabs.tabs.eq(i).addClass('es_open');
        // section
        tabs.sections.hide();
        tabs.sections.eq(i).show();
    }


    /* ------------------------------------------------------------------------------------------------------*/
    /* tabs action */
    function tabInit(k) {
        tabs.tabs.each(function(index) {

            tabHideShow(k);

            $(this).click(function() {

                tabHideShow(index);

            }).mouseover(function() {
                $(this).addClass('over');
            }).mouseleave(function() {
                $(this).removeClass('over');
            });
        });
    }
    var in_i_tab = parseIncomingHashTab();
    if (in_i_tab == null) in_i_tab = 0;
    tabInit(in_i_tab);
});
