/*jslint white:true, browser:true, undef:true, eqeqeq:true */
/*extern Cookies*/
/*
Szybkie i proste semantyczne zakladki
Copyright (c) 2007 Radoslaw Litman, Grupa Wydawnicza INFOR S.A.

dodano aktywatory zakładek - ppraga 30/08/2007
*/

function eventTrigger( e ) {
    if ( !e ) {
        e = event;
    }
    return e.target || e.srcElement;
}

function TabSwitcher( list, mode, onActivate )
{
    this.list = list;
    //this.setAction = setAction;

    if ( !mode ) {
        mode = 'href';
    }

    var hrefs = this.list.getElementsByTagName( 'a' );// uwaga - to nie jest prawdziwa tablica, tylko NodeList: http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-536297177
    var tmp = [];
    var i;
    var tab_id;

    this.cookie_name = '';
    this.testmode = false;

    for ( i = 0; i < hrefs.length; i++ ) {
        if ( /tab_switcher/.test( hrefs[i].className ) ) {
            tmp.push( hrefs[i] );
        }
    }
    hrefs = tmp;

    this.hide_all_tabs = function () {
        var i;
        for ( i = 0; i < hrefs.length; i++ ) {
            hrefs[i].tab.style.display = 'none';
            if ( this.testmode ) {
                alert( hrefs[i].tab.tagName );
            }
            hrefs[i].className =  hrefs[i].className.replace( / *selected/ , '' );
            if ( hrefs[i].parentNode.tagName.toUpperCase() === 'LI' ) {
                hrefs[i].parentNode.className = hrefs[i].parentNode.className.replace( / *selected/ , '' );
            }
        }
    };

    this.switch_tab = function ( e ) {
        var href = eventTrigger( e );
        href.tab_switcher.hide_all_tabs();
        if ( href.tab_switcher.cookie_name !== '' ) {
            Cookies.set( href.tab_switcher.cookie_name, href.index );
        }
        href.tab.style.display = '';
        if ( !/ *selected/.test( href.className ) ) {
            href.className = href.className + ' selected';
            if ( href.parentNode.tagName.toUpperCase() === 'LI' ) {
            	href.parentNode.className = href.parentNode.className + ' selected';
            	if (typeof onActivate === 'object' && typeof onActivate[href.tab.id] === 'function' ){
            		onActivate[href.tab.id]();
            	}
            }
        }
        return false;
    };

    this.activate = function ( i ) {
        hrefs[i].onclick( { target: hrefs[i] } );
    };

    this.run = function () {
        for ( i = 0; i < hrefs.length; i++ ) {
            tab_id = hrefs[i][mode].replace( /^.*?#/, '' );
            hrefs[i].tab = document.getElementById( tab_id );
            hrefs[i].tab_switcher = this;
            hrefs[i].onclick = this.switch_tab;
            hrefs[i].index = i;
        }

        if ( hrefs.length > 0 ) {
            i = 0;
            if ( this.cookie_name !== '' ) {
                i = Cookies.get( this.cookie_name );
                if ( !/^\d+$/.test( i ) ) {
                    i = 0;
                } else {
                    i = Number( i );
                }
                if ( i > hrefs.length - 1 ) {
                    i = 0;
                }
            }
            hrefs[i].onclick( { target: hrefs[i] } );
        }
    };
}

