var KEY_PGUP	= 33;
var KEY_PGDN	= 34;
var KEY_END	= 35;
var KEY_HOME	= 36;

var KEY_LEFT	= 37;
var KEY_UP	= 38;
var KEY_RIGHT	= 39;
var KEY_DOWN	= 40;

if ( !self.SMM ) self.SMM = new Object();
self.SMM.base = {
 menus: [],
 enl: [],
 curr_enlno: null,
 setup: function () {
	var o = this;
	var mdiv = document.getElementById( 'main' );
	var tdiv = document.getElementById( 'top' );
	if ( !mdiv || !tdiv || !mdiv.setAttribute ) return;
	mdiv.setAttribute( 'role', 'main' );
	tdiv.setAttribute( 'role', 'banner' );

	o.ns = document.documentElement.getAttribute( 'xmlns' );
	o.useeval = document.evaluate && XPathResult.ORDERED_NODE_SNAPSHOT_TYPE;
//	if ( o.useeval ) alert( 'eval' );

	var bdiv = document.getElementById( 'bottom' );
	if ( bdiv ) {
		bdiv.setAttribute( 'role', 'contentinfo' );
	}
	var bc = document.getElementById( 'breadcrumbs' );
	if ( bc ) {
		bc.setAttribute( 'role', 'navigation' );
		bc.setAttribute( 'aria-labelledby', 'yah' );
	}
	var mmnu = document.getElementById( 'm-0' );
	var menu = mmnu;
	var level = 1;
	while ( menu ) {
		menu.setAttribute( 'role', 'navigation' );
		menu.setAttribute( 'aria-label', menu.title );
		menu.removeAttribute( 'title' );
		menu = document.getElementById( 'm-' + level );
		level++;
	}
	if ( !'test'.match ) return;
	if ( self.SMM.form ) self.SMM.form.setup();

	if ( o.getComputedStyle( mmnu, 'position' ) == 'absolute' ) { //not true for @media handheld
		var lis = o.gEbtn( mmnu, 'li', 'hassub' );
		for ( var i = 0; i < lis.length; i++ ) { o.setup_menuhover( lis[i] ); }
		o.setup_tabcont( mdiv, tdiv );
		o.setup_enlarge( mdiv );
	}

	var tables = o.gEbtn( mdiv, 'table', 'pres' );
	for ( var i = 0; i < tables.length; i++ ) { tables[i].setAttribute( 'role', 'presentation' ) }
 },
 setup_menuhover: function( li ) {
	var o = this;
//	var a = li.firstChild;
	var cls = li.className;
	cls = cls.replace( 'hassub', 'jssub' );
	li.className = cls;
//	a.removeAttribute( 'href' );
	var ul = o.gEbtn( li, 'ul' )[0];
	if ( ul ) {
		var menuno = o.menus.length;
		//o.menus[ menuno ] = [ ul, a ];
		o.menus[ menuno ] = [ ul ];
		li.menuno = menuno; // EXPANDO
		li.onmouseover = o.onmouseover_openmenu;
		li.onmouseout = o.onmouseout_closemenu;
		o.closemenu( menuno );
	}
 },
 setup_enlarge: function ( mdiv ) {
	var o = this;
	if ( !mdiv.removeChild || !mdiv.replaceChild || !document.body || !document.body.offsetWidth ) return;
	var imgs = o.gEbtn( mdiv, 'img', 'enl' );
	for ( var i = 0; i < imgs.length; i++ ) {
		var img = imgs[i];
		var a = img.parentNode;
		if ( !a || !a.href ) continue;
		a.className = '';
		var enlno = o.enl.length;
		var big = o.cElt( 'div', '', 'big' );
		var img2 = o.cElt( 'img' );
		img2.title = 'Enlarged Image';
		img2.tabIndex = -1;
		img2.alt = img.alt + ' (Enlarged)';

		var div = o.cElt( 'div', '', 'cls' )
		var clse = o.cElt( 'a' );
		clse.title = 'Close Enlarged Image';
		clse.href = '#';
		div.appendChild( clse );
		var ldg = o.cElt( 'div', '', 'load', 'Loading Image...' );
		big.appendChild( ldg );
		big.appendChild( div );
		var cls = a.parentNode.className;
		if ( cls == 'prodtd' ) img2.prodtd = 1; // EXPANDO 

		o.enl[ enlno ] = [ a, big, clse, img2, img ];
		a.enlno = clse.enlno = img2.enlno = enlno; // EXPANDO
		a.onclick = o.onclick_enlargeimg;
	}
 },
 setup_tabcont: function ( mdiv, tdiv ) {
	var o = this;
	var divs = o.gEbtn( mdiv, 'div', 'tloff' );
	if ( !divs ) return;
	var d = divs[0];
	if ( !d || !d.offsetHeight || !d.insertBefore || !d.id ) return;
	var items = []; var id2num = {};
	var itemno = 0;
	var kids = d.childNodes;
	for ( var j = 0; j < kids.length; j++ ) {
		var kid = kids[j];
		var cls = kid.className;
		if ( !cls ) continue;
		if ( cls == 'tab' ) {
			if ( !kid.id ) continue;
			items[ itemno ] = { tab: kid };
			id2num[ kid.id ] = itemno;
		}
		else if ( cls == 'tabpanel' ) {
			items[ itemno ].tabpanel = kid;
			++itemno; 
		}
	}
	o.tabcont = { tlon: d, items: items, defitemno: 0, id2num: id2num, toppx: 0 };

	var tl = o.tabcont;
//	    tl.tlon.setAttribute( 'role', 'application' );
	tl.tlon.className = 'tlon';
	var items = tl.items;
	var owns = [];
	for ( var j = 0; j < items.length; j++ ) {
		var tab = items[j].tab;
		var tabpanel = items[j].tabpanel;
		tab.id = 'T'+tab.id;
		var tabid = tab.id;

		var pd  = o.cElt( 'a', '', 'padd' );
		pd.setAttribute( 'role', 'presentation' );
		pd.href = '#';
		pd.tabIndex = -1;
		var kids = tab.childNodes;
		for ( var k = 0; k < kids.length; k++ ) { pd.appendChild( kids[k] ) }
		tab.appendChild( pd );

		owns[ owns.length ] = tabid;
		tab.itemno = tabpanel.itemno = j; //EXPANDO
		tab.lastitem = tabpanel.lastitem = items.length - 1; //EXPANDO
		tab.style[ 'top' ] = tl.toppx + 'px';
		tl.toppx += ( tab.offsetHeight + 10 );

		var tabpanelid = tabid + '-p';
		tabpanel.id = tabpanelid;

		tab.setAttribute( 'role', 'tab' );
		tab.setAttribute( 'aria-controls', tabpanelid );
		tabpanel.setAttribute( 'role', 'tabpanel' );
		tabpanel.setAttribute( 'aria-labelledby', tabid );

		tab.onkeydown = o.onkeydown_intab;
		tab.onkeypress = o.onkeypress_intab;
		tabpanel.onkeydown = o.onkeydown_intabpanel;
		tabpanel.onkeypress = o.onkeypress_intabpanel;
		tab.onclick = o.onclick_tabchange;

		o.hidetab( j );
	}
	var tablist = o.cElt( 'div' );
	tablist.setAttribute( 'role', 'tablist' );
	tablist.setAttribute( 'aria-owns', owns.join( ' ' ) );
	tl.tlon.insertBefore( tablist, tl.tlon.firstChild );
	tl.tlon = null;

	var items = tl.items;
	for ( var j = 0; j < items.length; j++ ) {
		var tabpanel = items[j].tabpanel;
		tabpanel.style.minHeight = tl.toppx + 'px';
	}
	o.process_hash( true );
	if ( window.onhashchange ) { o.onhashchange_tabchange }
	else { o.oldhash = location.hash; o.pollhash(); }
 },
 pollhash: function () {
	var o = this;
	var newhash = location.hash;
	if ( o.oldhash != newhash ) { o.oldhash = newhash; o.process_hash() }
	window.setTimeout( 'self.SMM.base.pollhash()', 200 );
 },
 process_hash: function ( isinit ) {
	var o = this;
	var h = location.hash;
	var tl = o.tabcont;
	if ( h && tl ) {
		h = h.substring(1);
		if ( h in tl.id2num ) { newitemno = tl.id2num[ h ] }
		else { newitemno = 0; h = null; }
		tl.newitemno = newitemno;
	}
	var itemno = tl.newitemno != null ? tl.newitemno : tl.defitemno;
	tl.newitemno = null;
	var cf = h ? 1 : 0;
	if ( isinit ) { o.showtab( itemno, null, cf ) }
	else { o.tabchange( itemno, null, 1 ) }
 },
 showtab: function ( itemno, changehash, changefocus ) {
	var o = this;
	var tl = o.tabcont;
	var tab = tl.items[ itemno ].tab;
	tab.tabIndex = 0;
	tab.className = 'tab thistab';
	tab.setAttribute( 'aria-selected', 'true' );
	var tabpanel = tl.items[ itemno ].tabpanel;
	tabpanel.setAttribute( 'aria-hidden', 'false' );
	tabpanel.className = 'thistabpanel';
	tl.curritemno = itemno;
	if ( changehash ) {
		var tabid = tab.id;
		var frid = '#' + tabid.substring(1);
		if ( window.onhashchange ) { location.hash = frid }
		else { location.href = o.oldhash = frid }
	}
	if ( changefocus && tab.focus ) tab.focus();
 },
 hidetab: function ( itemno ) {
	var o = this;
	var tl = o.tabcont;
	var tab = tl.items[ itemno ].tab;
	tab.tabIndex = -1;
	tab.className = 'tab';
	tab.setAttribute( 'aria-selected', 'false' );
	var tabpanel = tl.items[ itemno ].tabpanel;
	tabpanel.setAttribute( 'aria-hidden', 'true' );
	tabpanel.className = 'tabpanel';
 },
 tabchange: function ( itemno, changehash, changefocus ) {
	var o = this;
	var tl = o.tabcont;
	if ( tl.curritemno == itemno ) return;
	o.hidetab( tl.curritemno );
	o.showtab( itemno, changehash, changefocus );
 },
 gEbtn: function ( base, tag, cls ) {
	tag = tag || '*';
	var elts = [], re;
	if ( cls ) re = new RegExp( '(?:^| )'+ cls +'(?: |$)' );
	if ( !this.useeval ) {
		var tmp = base.getElementsByTagName( tag );
		for ( var i = 0; i < tmp.length; i++ ) {
			var t = tmp[i];
			var tcls = t.className;
			if ( cls && ( !tcls || !tcls.match( re ) ) ) continue;
			elts[ elts.length ] = t;
		}
		return elts;
	}
	var q = './/'+tag;
	if ( cls ) q = q + '[@class]';
	var xpathres = document.evaluate( q, base, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
	for ( var i = 0; i < xpathres.snapshotLength; i++ ) { 
		var t = xpathres.snapshotItem(i);
		var tcls = t.className;
		if ( cls && ( !tcls || !tcls.match( re ) ) ) continue;
		elts[ elts.length ] = t;
	}
	return elts;
 },
 cElt: function ( eltname, id, cls, txt ) {
        var o = this;
        var elt;
        if ( document.createElementNS && o.ns ) { elt = document.createElementNS( o.ns, eltname ) }
        else { elt = document.createElement( eltname ) }
        if ( id ) { elt.id = id }
        if ( cls ) { elt.className = cls }
        if ( txt ) { o.aTxt( elt, txt ) }
        return elt;
 },
 aTxt: function ( elt, txt ) {
	elt.appendChild( document.createTextNode( txt ) );
 },
 getComputedStyle: function ( elt, prop ) {
	var computedStyle;
	if ( document.defaultView && document.defaultView.getComputedStyle ) {
		computedStyle = document.defaultView.getComputedStyle( elt, null );
	}
	else if ( typeof elt.currentStyle != 'undefined' ) {
		computedStyle = elt.currentStyle;
	}
	else {
		return null;
	}
	return computedStyle[ prop ];
 },
 get_elt_pos: function ( elt ) {
	var l = 0, t = 0;
	if ( elt.offsetParent != null ) {
		do {
			l += elt.offsetLeft;
			t += elt.offsetTop;
			elt = elt.offsetParent;
		} while ( elt );
	}
	return [ l, t ];
 },
 stopprop: function( e ) {
	e.cancelBubble = true; e.returnValue = false;
	if ( e.stopPropagation ) { e.stopPropagation(); e.preventDefault(); }
 },
 closeimg: function( enlno ) {
	var o = this;
	var enl = o.enl[ enlno ];
	var a = enl[0]; var big = enl[1]; var clse = enl[2]; var img = enl[3];
	clse.onclick = null;
	big.parentNode.removeChild( big );
	a.className = '';
	if ( img.prodtd ) {
		a.parentNode.parentNode.className = 'prodtr';
		a.parentNode.style.paddingBottom = '0px';
	}
	o.curr_enlno = null;
 },
 focusimg: function( enlno ) {
	var o = this;
	var img = o.enl[ enlno ][3];
	if ( img.focus && img.parentNode ) img.focus();
 },
 onclick_enlargeimg: function () {
	var a = this;
	var o = self.SMM.base;
	var enl =  o.enl[ a.enlno ];
	var big = enl[1]; var clse = enl[2]; var img = enl[3];
	if ( o.curr_enlno == null || a.enlno != o.curr_enlno ) {
		if ( o.curr_enlno != null ) o.closeimg( o.curr_enlno );
		if ( img.prodtd ) a.parentNode.parentNode.className = 'prodtr thisprodtr';
		if ( img.pbott ) a.parentNode.style.paddingBottom = img.pbott;
		a.className = 'this';
		a.parentNode.appendChild( big );
		clse.onclick = o.onclick_closeimg;
		o.curr_enlno = a.enlno;
		if ( img.hasloaded ) { o.focusimg( a.enlno ); }
		else { img.onload = o.onload_img; img.src = a.href; }
	}
	return false;
 },
 onload_img: function () {
	var o = self.SMM.base;
	var enl = o.enl[ this.enlno ];
	var a = enl[0]; var big = enl[1]; var clse = enl[2]; var img = enl[3]; var imgth = enl[4];
	img.hasloaded = 1; // EXPANDO
	img.onload = null;
	var wd = img.width;
	if ( !wd ) return;
	var ht = img.height;
	img.style.width = big.style.width = wd + 'px';
	img.style.height = ht + 'px';
	var extral = 0;
	if ( img.prodtd ) {
		img.pbott = ( ht + 30 ) + 'px'; // EXPANDO
		a.parentNode.style.paddingBottom = img.pbott;
		extral = 0.5 * ( wd + 2 - a.parentNode.offsetWidth );
		big.style.left = '-' + extral + 'px';
		big.style[ 'top' ] = ( imgth.offsetHeight + 12 ) + 'px';
	}
	big.replaceChild( img, big.firstChild );
	var l = o.get_elt_pos( img )[0];
	var r = l + wd + 5;
	var bw = document.body.offsetWidth;
	if ( !self.IS_IELT8 && r > bw ) { //if offscreen, adjust
		var left = r - bw;
		if ( ( l - left ) < 1 ) left = l - 1;
		big.style.left = '-' + (left+extral) + 'px';
	}
	o.focusimg( this.enlno );
 },
 onclick_closeimg: function () {
	var o = self.SMM.base;
	var a = o.enl[ this.enlno ][0];
	o.closeimg( this.enlno );
	if ( a.focus ) a.focus();
	return false;
 },
 onhashchange_tabchange: function () {
	self.SMM.base.process_hash();
 },
 onclick_tabchange: function () {
	self.SMM.base.tabchange( this.itemno, 1, null );
	return false;
 },
 onkeydown_intabpanel: function ( e ) {
	var o = self.SMM.base;
	if ( window.event ) e = window.event;
	var keycode = ( e.keyCode ) ? e.keyCode : e.which;
	if ( e.ctrlKey && keycode == KEY_PGUP ) {
		var newitem = this.itemno == 0 ? 0 : this.itemno - 1;
		o.tabchange( newitem, 1, 1 );
	}
	else if ( e.ctrlKey && keycode == KEY_PGDN ) {
		var newitem = this.itemno == this.lastitem ? this.itemno : this.itemno + 1;
		o.tabchange( newitem, 1, 1 );
	}
	else { return true }
	o.stopprop(e);
        return false;
 },
 onkeypress_intabpanel: function ( e ) {
	var o = self.SMM.base;
	if ( window.event ) e = window.event;
	var keycode = ( e.keyCode ) ? e.keyCode : e.which;
	if ( ( e.ctrlKey && keycode == KEY_PGUP ) || ( e.ctrlKey && keycode == KEY_PGDN ) ) {
		o.stopprop(e);
		return false;
	}
	else { return true }
 },
 onkeydown_intab: function ( e ) {
	var o = self.SMM.base;
	if ( window.event ) e = window.event;
	var keycode = ( e.keyCode ) ? e.keyCode : e.which;
	if ( keycode == KEY_UP || keycode == KEY_LEFT ) {
		var newitem = this.itemno == 0 ? 0 : this.itemno - 1;
		o.tabchange( newitem, 1, 1 );
	}
	else if ( keycode == KEY_DOWN || keycode == KEY_RIGHT ) {
		var newitem = this.itemno == this.lastitem ? this.itemno : this.itemno + 1;
		o.tabchange( newitem, 1, 1 );
	}
	else if ( keycode == KEY_HOME ) {
		o.tabchange( 0, 1, 1 );
	}
	else if ( keycode == KEY_END ) {
		o.tabchange( this.lastitem, 1, 1 );
	}
	else if ( e.ctrlKey && keycode == KEY_PGUP ) {
		var newitem = this.itemno == 0 ? 0 : this.itemno - 1;
		o.tabchange( newitem, 1, 1 );
	}
	else if ( e.ctrlKey && keycode == KEY_PGDN ) {
		var newitem = this.itemno == this.lastitem ? this.itemno : this.itemno + 1;
		o.tabchange( newitem, 1, 1 );
	}
	else { return true }
	o.stopprop(e);
        return false;
 },
 onkeypress_intab: function ( e ) {
	var o = self.SMM.base;
	if ( window.event ) e = window.event;
	var keycode = ( e.keyCode ) ? e.keyCode : e.which;
	if ( keycode == KEY_UP || keycode == KEY_LEFT || keycode == KEY_DOWN || keycode == KEY_RIGHT ||
		keycode == KEY_HOME || keycode == KEY_END ||
		( e.ctrlKey && keycode == KEY_PGUP ) || ( e.ctrlKey && keycode == KEY_PGDN ) ) {
		o.stopprop(e);
		return false;
	}
	else { return true }
 },
openmenu: function ( menuno ) {
	var o = this;
	o.menus[ menuno ][0].className = 'open';
	o.menus[ menuno ][0].setAttribute( 'aria-hidden', 'false' );
 },
 closemenu: function ( menuno ) {
	var o = this;
	o.menus[ menuno ][0].className = '';
	o.menus[ menuno ][0].setAttribute( 'aria-hidden', 'true' );
 },
 onmouseover_openmenu: function () {
	self.SMM.base.openmenu( this.menuno );
 },
 onmouseout_closemenu: function () {
	self.SMM.base.closemenu( this.menuno );
 },
 onload_window:	function () { if ( arguments.callee.done ) { return }; arguments.callee.done = true; self.SMM.base.setup(); },
 onload_fallback: function () { window.setTimeout( 'self.SMM.base.onload_window()', 100 ); },
 set_onload_window: function () {
	if ( !document.getElementById || !document.getElementsByTagName || !document.createElement || !document.createTextNode ) return;
	if ( !window.addEventListener && !window.attachEvent ) return;
	if ( window.addEventListener ) { window.addEventListener( 'DOMContentLoaded', this.onload_window, false ); window.addEventListener( 'load', this.onload_fallback, false ) }
	else if ( window.attachEvent ) { window.attachEvent( 'onload', this.onload_window ) }
 }
};
self.SMM.base.set_onload_window();

