function add_to_onload( func ) {
    var old_onload = window.onload;
    window.onload = function() {
        if ( typeof old_onload == 'function' ) {
        	old_onload();
        }
        func();
    }
}

function jscss( action, obj, c1, c2 )
{
    // perform operations on an element's classname
    // IN: a, the action to be performed:
    //       'swap' one classname for another
    //       'add' a classname
    //       'remove' a classname
    //       'check' to see if a class name is there
    switch ( action ){
        case 'swap':
            obj.className = 
                !jscss( 'check', obj, c1) ?
                obj.className.replace( c2, c1 ) : 
                obj.className.replace( c1, c2 ) ;
            break;
        case 'add':
            if ( !jscss( 'check', obj, c1 ) ) {
                obj.className += obj.className ? ' ' + c1 : c1;
            }
            break;
        case 'remove':
            var rep = obj.className.match( ' ' + c1 ) ? ' ' + c1 : c1;
            obj.className = obj.className.replace( rep,'' );
            break;
        case 'check':
            return new RegExp( '\\b' + c1 + '\\b' ).test( obj.className )
            break;
    }
}


function TabSet () {
    /* tabset manages a set of tabs and targets. 
       Each tab is associated with a target.
       When a tab is selected, its target becomes active or visible;
       all other tabs become unselected, and all other targets become 
       inactive or invisible.
    */
    this.selected = null;
    this.tabs = new Object();
    
    this.class_name = {
        selected   : "selected",
        unselected : "unselected",
        visible    : "visible",
        hidden     : "hidden"
    };
};
TabSet.prototype.handleclick = function ( el_id ) {        
    if ( this.selected != el_id ) { // if it's already selected, do nothing
        this.hide     ( this.tabs[ this.selected ]  );
        this.unselect ( this.selected               );
        this.select   ( el_id                       );
        this.show     ( this.tabs[ el_id ]          );
    }
};
TabSet.prototype.select = function ( el_id ) {        
    if ( el = document.getElementById( el_id ) ) {
        this.selected = el_id;
        jscss('swap', el, this.class_name.unselected, this.class_name.selected );
    }
};
TabSet.prototype.unselect = function ( el_id ) {
    if ( el = document.getElementById( el_id ) ) {
        if (this.selected == el_id ) {
            this.selected = null;
            jscss('swap', el, this.class_name.unselected, this.class_name.selected );

        }
    }
};
TabSet.prototype.show = function ( el_id ) {
    if (el = document.getElementById( el_id )) {
        jscss('remove', el, this.class_name.hidden ); 
        jscss('add',    el, this.class_name.visible );
    }
};

TabSet.prototype.hide = function ( el_id ) {
    if (el = document.getElementById( el_id ) ) {
        jscss('remove',   el, this.class_name.visible );
        jscss('add',      el, this.class_name.hidden ); 
    }
};

TabSet.prototype.add = function ( tab_id, target_id ) {
    if (el = document.getElementById( tab_id ) ) {
        this.tabs[ tab_id ] = target_id;
    }
};

TabSet.prototype.remove = function ( tab_id, target_id ) {
    delete this.tabs[ tab_id ];
};










 
var Tabs = new TabSet();

function WindowOnload(f) {
    var prev=window.onload;
    window.onload=function(){ if(prev)prev(); f(); }
}



set_tabs = function () {
    // call old window.onload function
    
    var tabs = Array( 
        // tabs on main pages
        "popular-downloads",
        "popular-articles", 
        "newest-articles",
        
        // tabs on article pages
        "related-Articles",
        "related-Whitepapers",
        "related-Products",
        "related-Industrynews"
    );
    for (i=0; i<tabs.length; i++ ) {
        Tabs.add( "tab-"+tabs[i], tabs[i] );
        
    }
    Tabs.select ( "tab-popular-articles" ); // these two elements shouldn't be on the same page
    Tabs.select ( "tab-related-Articles" );
}

WindowOnload( set_tabs );
