var TabMenu = function ( ) { this.initializer.apply( this, arguments ); };
TabMenu.prototype.initializer = (function ( ) {
return function ( menuId, contentsId, cbFunc, selectedCSS, defaultNo, saveDay ) {
this.id = menuId;
this.menu = document.getElementById( menuId ).getElementsByTagName( 'li' );
this.contents = document.getElementById( contentsId ).getElementsByTagName( 'li' );
this.cbFunc = cbFunc;
this.selectedCSS = selectedCSS;
this.defaultNo = ( 'number' == typeof defaultNo ) ? defaultNo: 0;
this.saveDay = ( 'number' == typeof saveDay ) ? saveDay: 100;
this.setPage();
};})();
TabMenu.prototype.setPage = (function ( getCookie, setCookie ) {
return function ( menuNo, contentsNo, flag ) {
var cnt, li;
if( 'number' != typeof menuNo )
menuNo = parseInt( getCookie( this.id ) ) || this.defaultNo;
setCookie( this.id, menuNo %= this.contents.length, this.saveDay );
if( 'undefined' === typeof contentsNo )
contentsNo = menuNo;
for( cnt = 0; li = this.menu[ cnt ]; cnt++ )
li.className = cnt == menuNo ? this.selectedCSS: '';
for( cnt = 0; li = this.contents[ cnt ]; cnt++ )
li.style.display = cnt == contentsNo ?
'list-item' : 'none';
if( 'function' == typeof this.cbFunc && !flag ) this.cbFunc( menuNo );
return this.contents[ contentsNo ];
};
})(
function ( name ) {
name = encodeURIComponent( name ).replace( /([.*()]) /g, '\\$1' );
var value = document.cookie.match( RegExp( name + '\\s*=\\s*(.*?)(?:[\\s;,]|$)' ) );
return value ? decodeURIComponent( value[1] ): '';
},
function ( name, value, day, path, domain ) {
return document.cookie = encodeURIComponent( name ) + '=' + encodeURIComponent( value ) +
'; ' + 'expires=' + new Date( ( new Date ) - 86400000 * -day ).toUTCString( ) +
'; ' + ( path ? 'path=' + encodeURI( path ) + '; ': '' ) +
(domain ? 'domain=' + encodeURI ( domain ) + '; ': '' );
}
);
TabMenu.handler = (function ( getParent, getNo ) {
return function ( evt ) {
var e = evt. target ;
var ul = getParent( e, 'nodeName', 'UL' );
var li = getParent( e, 'nodeName', 'LI' );
var tb;
if( ul && ul.id ) {
tb = TabMenu.memo[ ul.id ];
tb && tb.setPage( getNo( li ) );
}
};
})(
function ( node, type, val ) {
return node ? (val == node[type]) ? node: arguments.callee( node.parentNode, type, val ): null;
},
function ( node ) {
var r = 0, n;
if( node )
for( n = node.nodeName; node = node.previousSibling; )
if( node.nodeName == n ) r += 1;
return r;
}
);
TabMenu.create = function ( menuId, contentsId, cbFunc, selectedCSS, defaultNo, saveDay ) {
if( 'undefined' == typeof this.memo ) {
this.memo = [ ];
document. addEventListener(
'click', TabMenu.handler, false );
}
if( !this.memo[ menuId ] )
this.memo[ menuId ] = new this( menuId, contentsId, cbFunc, selectedCSS, defaultNo, saveDay );
return this.memo[ menuId ];
};