var BrowserDetect = Class.create();
BrowserDetect.prototype = {
	initialize: function() {
		var ua = navigator.userAgent.toLowerCase(); 

		this.isIE = (ua.indexOf('msie') != -1 && (ua.indexOf('opera') == -1) && (ua.indexOf('webtv') == -1) ); 
		// browser version
		this.versionMinor = parseFloat(navigator.appVersion); 
		if (this.isIE && this.versionMinor >= 4) {
			this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
		}
		this.versionMajor = parseInt(this.versionMinor);
		this.isIE5x = (this.isIE && this.versionMajor == 5);
	}
}
browser = new BrowserDetect();

var sC = {
	aNode: false,
	nodes: false,
	container: false,
	
	init: function(){
		if(browser.isIE5x) return false;
		sC.container = $('pageContainer');
		var elems = document.getElementsByClassName('scrollbox', sC.container);
		sC.nodes = $A(elems);
		sC.nodes.each(function(node){
			node.scroller = new Scroller('scroller_'+node.id, node, document.getElementsByClassName('scrollerContent', node)[0], {heightCorr: 3});
			Element.hide(node);
			node.style.visibility = 'visible';
		});
		Element.hide(sC.container);
		sC.container.style.visibility = 'visible';	
		
		startEl = $('m_values').getElementsByTagName('A')[0];
		setTimeout(function(){sC.show(startEl)},800);	
	},

	show: function(el){
		var targetId = el.href.split('#')[1];
		var target = $(targetId);
		if (sC.aNode.id != target.id) {
			if(sC.aNode) {
				new Effect.Parallel(
					[ new Effect.Fade(sC.container, { sync:true }),
					  new Effect.Fade(sC.aNode, { sync:true }) ],
					{ duration:0.5, afterFinish:function(){ 
									Element.hide(sC.aNode); 
									sC._show(target); } });
			} else {
				sC._show(target);
			}
			sC.clearCurrent();
			Element.addClassName(el, 'isCurrent');
		}
	},
	
	_show: function(target){
		new Effect.Parallel(
			[ new Effect.Appear(sC.container, { sync:true }),
			  new Effect.Appear(target, { sync:true }) ],
			{ duration:0.3 });
		sC.aNode = target;
	},

	clearCurrent: function(){
		var elems = document.getElementsByClassName('isCurrent', $('subnav'));
		var nodes = $A(elems);
		nodes.each(function(node){
			Element.removeClassName(node, 'isCurrent');
		});
	}
}


var myBehaviours = {
	'#subnav a' : function(element){
		
		if (browser.isIE5x) return false;

		element.onclick = function(){
			sC.show(this);
			return false;
		}
		
		element.onkeypress = function(){
			sC.show(this);
			return false;				
		}
	}
};
Behaviour.register(myBehaviours);


Event.observe(window, 'load', sC.init);


function trace( msg ){
	if( typeof( jsTrace ) != 'undefined' ){
		jsTrace.send( msg );
	}
}
