_ = function ( element_id ) { return document.getElementById( element_id ) || false }

function Base () {
    /**
     * FOR TAB.CLASS
     */
    this.tab_css_active     = "active";
    this.tab_css_current    = "current";
    this.tab_css_base       = "list_ol_tab";
    this.tab_prefix_link    = "_link_";
    this.tab_prefix_ul      = "_ul";
    this.tab_block_div      = "_block_";
    this.tab_tag_container  = "ol";
    this.tab_tag_element    = "li";
    this.tab_timeout        = 1000;
    this.tab_delta_id       = 0;
    this.Browser = {
        IE:     !!(window.attachEvent && !window.opera),
        Opera:  !!window.opera,
        WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
        Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
        MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
    };
    this.json_url           = '/exec/json.php';

    /**
     * FOR CAROUSEL.CLASS
     */
    this.carousel_css_normal= "mrt_null";
    this.carousel_block_name= "block";

    /**
     * Methods
     */
    this.in_array = function (el, arr) {
        for (var i = 0; i < arr.length; i++) {
            if (el == arr[i])
                return true;
        }
        return false;
    }

}

function Tab ( element_id, tab_tag_container, tab_delta_id ) {

    var timer;
    this.obj = null;
    var element_id = element_id;

    var use_link = true;

    if( tab_delta_id ) this.tab_delta_id = tab_delta_id ;
    if( tab_tag_container ) this.tab_tag_container = tab_tag_container ;

    var that = this;

    if( _( element_id ) ) {
        var tab = _( element_id ).getElementsByTagName( this.tab_tag_element );
        for ( var i = this.tab_delta_id; i < tab.length; i++ ) {
    if( use_link == false ) {
            tab[i].onmouseover = function( e ) {
                obj = this;
                timer = setTimeout(that.timeCount, that.tab_timeout);
            }
            tab[i].onmouseout = function( e ) {
                clearTimeout(timer)
            }
    } else {
        tab[i].onclick = function( e )
        {
            that.obj = this;
            var obj_link = that.obj.getElementsByTagName('a')[0];

            obj_link.onclick = function (e)
            {
                return false;
            }

            that.timeCount();
            return false;
        }
        }
    }
        this.timeCount = function () {
            if ( that.obj ){
                for( var i=that.tab_delta_id; i < tab.length; i++ ) {
                    tab[i].className = "";
                    _( element_id + that.tab_prefix_link + i ).className = '';
                    if( tab_tag_container ) {
                        _( element_id + that.tab_block_div + i).style.display = (i == that.obj.id) ? "block" : "none";
                    }
                }
                _( element_id + that.tab_prefix_link + that.obj.id ).parentNode.className = that.tab_css_current;

                if ( typeof(_('r')) == 'object' || typeof(_('c')) == 'object')
                {
                    if (element_id == 'calendar')
                    {

                        if (_( element_id + that.tab_prefix_link + that.obj.id ).parentNode.id == 0)
                        {
                            if (typeof(_('r')) == 'object')
                                _('r').style.display = "block";

                            if (typeof(_('c')) == 'object')
                                _('c').style.display = "none";
                        }

                        if (_( element_id + that.tab_prefix_link + that.obj.id ).parentNode.id == 1)
                        {
                            if (typeof(_('r')) == 'object')
                                _('r').style.display = "none";

                            if (typeof(_('c')) == 'object')
                                _('c').style.display = "block";

                        }
                     }
                }

                if ( typeof(_('com')) == 'object' || typeof(_('search')) == 'object')
                {
                    if (element_id == 'tab')
                    {
                        if (_( element_id + that.tab_prefix_link + that.obj.id ).parentNode.id == 1)
                        {
                            if (typeof(_('com')) == 'object')
                                _('com').style.display = "block";

                            if (typeof(_('search')) == 'object')
                                _('search').style.display = "none";
                        }

                        if (_( element_id + that.tab_prefix_link + that.obj.id ).parentNode.id == 2)
                        {
                            if (typeof(_('com')) == 'object')
                                _('com').style.display = "none";

                            if (typeof(_('search')) == 'object')
                                _('search').style.display = "block";
                        }
                     }
                }

                if ( typeof(_('medals')) == 'object' )
                {
                    if (element_id == 'olimp')
                    {
                        if (_( element_id + that.tab_prefix_link + that.obj.id ).parentNode.id == 1)
                        {
                            if (typeof(_('medals')) == 'object')
                                _('medals').style.display = "block";
                        }

                        if (_( element_id + that.tab_prefix_link + that.obj.id ).parentNode.id == 2)
                        {
                            if (typeof(_('medals')) == 'object')
                            {
                                _('medals').style.display = "none";
                            }
                        }

                        if (_( element_id + that.tab_prefix_link + that.obj.id ).parentNode.id == 3)
                        {
                            if (typeof(_('medals')) == 'object')
                                _('medals').style.display = "none";
                        }
                     }
                }

                if( !tab_tag_container ) {
                    var tabul = _( element_id + that.tab_prefix_ul ).getElementsByTagName( that.tab_tag_container );
                    for( var i=that.tab_delta_id; i<tabul.length; i++ ) {
                        tabul[i].className = that.tab_css_base;
                    }
                    tabul[that.obj.id].className = that.tab_css_base + ' ' + that.tab_css_active;
                }
            }
            return false;
        }
    }
}

Tab.prototype = new Base();

// prepare the block on DOM ready
$().ready(function(){
    $('#tab_stat_ul_hp').children().each(function(i,el) {
        var $trs = $(this).find('table tbody').children().each(function(i,el) {
            if (i > 8) $(this).remove();
        });
    });
});
