/*************************************************************************
    This code is from Dynamic Web Coding at dyn-web.com
    Copyright 2009 by Sharon Paine 
    See Terms of Use at www.dyn-web.com/business/terms.php
    regarding conditions under which you may use this code.
    This notice must be retained in the code as is!
*************************************************************************/

// requires: dw_event.js, dw_cookies.js (2009 version)

function dw_Tabs(tabSetId, bUseCookies) {
    this.id = tabSetId; this.useCookies = bUseCookies;
    dw_Tabs.col[tabSetId] = this;
    this.init(tabSetId);
}

dw_Tabs.prototype = {
    current: null, // id of currently active tab

    init: function(tabSetId) {
        var ar, tabId, cur, paneId;
        // get the links in ul.tabnavs inside tabSetId
        var tabnavs = dw_Util.getElementsByClassName( 'tabnavs', 'ul', document.getElementById(tabSetId) );
        var tab_links = tabnavs[0].getElementsByTagName('a');
        for (var i=0; tab_links[i]; i++) {
            ar = tab_links[i].href.split('#');
            if (ar.length > 1 ) {
                tabId = tab_links[i].id = ar[1] + '__' + tabSetId;
                dw_Event.add( tab_links[i], 'click', dw_Tabs.showClicked );
            }
        }
        if ( this.useCookies ) {
            cur = dw_Tabs.checkCookie(tabSetId);
        }
        this.current = cur || tab_links[0].id;
        if ( this.current ) {
            paneId = this.current.substr(0, this.current.lastIndexOf('__') );
            dw_Util.addClass( document.getElementById(this.current), 'activeTab');
            dw_Util.addClass( document.getElementById(paneId), 'activePane');
        }
    }

}

dw_Tabs.col = {};

dw_Tabs.hasBrowserSupport = function () {
    if ( document.getElementById && document.getElementsByTagName 
         && typeof decodeURI !== 'undefined'
         && (document.addEventListener || document.attachEvent) ) {
        return true;
    }
    return false;
}

// last 2 args for use when called from event handler attr
dw_Tabs.showClicked = function(e, paneId, tabSetId) {
    var tabId = '';
    if (e) {
        var tgt = dw_Event.getTarget(e); 
        tabId = tgt.id;
        // extract tabset id from target's id ('paneId__tabSetId')
        // and use it to obtain instance and retrieve currently active tab
        tabSetId = tabId.substr(tabId.lastIndexOf('__') + 2 );
        // get id of pane (first portion of id of target link)
        paneId = tabId.substr(0, tabId.lastIndexOf('__') );
    } else {
        tabId = paneId + '__' + tabSetId;
    }
    
    var _this = dw_Tabs.col[tabSetId]; 
    dw_Tabs.hideCurrent(_this.current);
    dw_Util.addClass( document.getElementById(tabId), 'activeTab');
    dw_Util.addClass(document.getElementById(paneId), 'activePane');
    _this.current = tabId;
    if ( _this.useCookies ) {
        dw_Tabs.setCookie(tabSetId, tabId);
    }
    if (e) {
        e.preventDefault();
    }
    return false;
}

dw_Tabs.hideCurrent = function(cur) {
    var paneId = cur.substr(0, cur.lastIndexOf('__') );
    dw_Util.removeClass( document.getElementById(cur), 'activeTab');
    dw_Util.removeClass(document.getElementById(paneId), 'activePane');
}

dw_Tabs.checkCookie = function(tabSetId) {
    var c, tab_cookies = dw_Cookie.get('dw_Tabs');
    if ( tab_cookies ) {
        var cookies = tab_cookies.split(',');
        for (var i=0; cookies[i]; i++) {
            c = cookies[i];
            if ( c.indexOf(tabSetId + ':') === 0 ) {
                return decodeURI( c.slice(tabSetId.length + 1, c.length) );
            }
        }
    }
    return null;
}

dw_Tabs.setCookie = function(tabSetId, tabId) {
    // format for cookies (multiple tabsets supported): dw_Tabs=tabSetId:tabId,tabSetId:tabId;
    var new_tab_cookies = '';
    var tab_cookies = dw_Cookie.get('dw_Tabs');
    if ( tab_cookies ) {
        var cookies = tab_cookies.split(',');
        for (var i=0; cookies[i]; i++) {
            if ( cookies[i].indexOf(tabSetId + ':') === 0 ) {
                cookies[i] = tabSetId + ':' + tabId;
                new_tab_cookies = cookies.join(',');
                break;
            }
        }
        if ( !new_tab_cookies ) { // if no match for this tabSetId
            new_tab_cookies = tab_cookies + ',' + tabSetId + ':' + tabId;
        }
    } else { // no dw_Tabs set yet
        new_tab_cookies = tabSetId + ':' + tabId;
    }
    dw_Cookie.set('dw_Tabs', new_tab_cookies, null, '/');
}

/////////////////////////////////////////////////////////////////////
//  utility functions

var dw_Util; 
if (!dw_Util) dw_Util = {};

dw_Util.normalizeString = function (str) {
    var re = /\s\s+/g;
    return dw_Util.trimString(str).replace(re, " ");
}

dw_Util.trimString = function (str) {
    var re = /^\s+|\s+$/g;
    return str.replace(re, "");
}

dw_Util.addClass = function (el, cl) {
    el.className = dw_Util.trimString( el.className + ' ' + cl );
}

dw_Util.removeClass = function (el, cl) {
    el.className = dw_Util.normalizeString( el.className.replace(cl, " ") );
}

// what className attached to what element type in what container element (default: document)
dw_Util.getElementsByClassName = function (sClass, sTag, oCont) {
    var result = [], list, i;
    var re = new RegExp("\\b" + sClass + "\\b", "i");
    oCont = oCont? oCont: document;
    if ( document.getElementsByTagName ) {
        if ( !sTag || sTag == "*" ) { // for ie5
            list = oCont.all? oCont.all: oCont.getElementsByTagName("*");
        } else {
            list = oCont.getElementsByTagName(sTag);
        }
        for (i=0; list[i]; i++) 
            if ( re.test( list[i].className ) ) result.push( list[i] );
    }
    return result;
}

// Alternate functions are available that use DOM methods instead of document.write
dw_Util.writeStyleSheet = function(file, bScreen) {
    var screen = (bScreen != false)? '" media="screen" />\n': '"/>\n';
    document.write( '\n<link rel="stylesheet" href="' + file + screen);
}

dw_Util.writeStyleRule = function(rule, bScreen) {
    var screen = (bScreen != false)? ' media="screen">': '>';
    document.write( '\n<style type="text/css"' + screen + rule + '</style>');
}