$BLUEWEB = {
	page : null,
	node_logo : null,
	node_menu : null,
	node_main1 : null,
	node_main2 : null,
	node_achapnav : null,
	node_debug : null,
	node_selected : null,
	node_shadow : null,
	viewportWidth : 0,
	viewportHeight : 0,
	viewportScrollX : 0,
	viewportScrollY : 0,
	chapter_visible : null,
	modeNarrow : false,

	init : function() {
		$BLUEWEB.page = document.getElementById("page");
		$BLUEWEB.node_debug = document.getElementById("debug");
		if ($BLUEWEB.node_debug) $BLUEWEB.node_debug.innerHTML = "";
		$BLUEWEB.activeSize();
	},
	
	toggle : function(t) {
		chapter = document.getElementById(t);
		if (chapter) {
			if ($BLUEWEB.chapter_selected) {
				$BLUEWEB.chapter_selected.className += " hidden"; // hide whatever was on before
			}			
			chapter = chapter.className.replace(" hidden", "");
			if (chapter !== $BLUEWEB.chapter_selected) {
				$BLUEWEB.chapter_selected = chapter;
			}
		}
	},
	
	flip : function(t) {
		if ( $BLUEWEB.node_selected ) $BLUEWEB.node_selected.className = $BLUEWEB.node_selected.className.replace(" selected", ""); // flop whatever was on before
		if ( $BLUEWEB.node_shadow ) $BLUEWEB.node_shadow.parentNode.removeChild($BLUEWEB.node_shadow);
		$BLUEWEB.node_selected = document.getElementById(t);
		$BLUEWEB.node_selected.className += " selected";
		
		if (typeof pageTracker != "undefined") pageTracker._trackPageview('/questions/'+t);
		if (typeof sIFR != "undefined") if (sIFR.isActive) {
			sIFR.replace(bpreplay, {
				selector: '.topic.selected h1',
				css: ['.sIFR-root { color:#ffffff; }'],
				wmode: 'transparent'
			});
		}
		
		$BLUEWEB.adjust_node_selected();
		$BLUEWEB.page.className += " blur";
		document.body.className += " blur";
	},

	flop : function(t) {
		var node;
		if ( $BLUEWEB.node_shadow ) $BLUEWEB.node_shadow.parentNode.removeChild($BLUEWEB.node_shadow);
		node = document.getElementById(t);
		node.className = node.className.replace(" selected", "");
		$BLUEWEB.page.className = $BLUEWEB.page.className.replace(" blur", "");
		document.body.className = document.body.className.replace(" blur", "");
	},

	adjust_node_selected : function() {
		var pad_lr, pad_tb;
		var shOff = Math.min( 5, parseInt( Math.min( $BLUEWEB.viewportWidth, $BLUEWEB.viewportHeight ) / 80 ));
		if ( $BLUEWEB.node_selected ) {
			pad_lr = Math.max( 10, parseInt($BLUEWEB.viewportWidth / 5) );
			pad_tb = Math.max( 10, parseInt( ($BLUEWEB.viewportHeight - $BLUEWEB.node_selected.offsetHeight) / 2.5) );
			$BLUEWEB.node_selected.style.left = pad_lr.toString() + "px";
			$BLUEWEB.node_selected.style.right = pad_lr.toString() + "px";
			//$BLUEWEB.node_selected.style.top = pad_tb.toString() + "px";
			$BLUEWEB.node_selected.style.top = "100px";
		}
	},
	
	adjust_node_layout: function() {
		// adjust main content
		var n1 = $BLUEWEB.node_main1;
		var n2 = $BLUEWEB.node_main2;
		var n3 = $BLUEWEB.node_logo;
		var n4 = $BLUEWEB.node_menu;
		if ($BLUEWEB.viewportWidth < 810) {
			if (!$BLUEWEB.modeNarrow) {
				$BLUEWEB.modeNarrow = true;
				if (n1) n1.className = 'g11';
				if (n2) n2.className = 'g11';
				if (n3) n3.className = 'g11';
				if (n4) n4.className = 'g11';
			}
		} else {
			if ($BLUEWEB.modeNarrow) {
				$BLUEWEB.modeNarrow = false;
				if (n1) n1.className = 'g23';
				if (n2) n2.className = 'g13';
				if (n3) n3.className = 'g12';
				if (n4) n4.className = 'g12';
			}
		}
	},

	adjust_node_achapnav: function() {
		var fix_pos = 30;
		var abs_pos = 250;
		
		if ($BLUEWEB.viewportWidth < 810) abs_pos += 100; // compensate for liquid layout
		if ($BLUEWEB.viewportWidth < 500) abs_pos += 50; // compensate for liquid layout

		// adjust chapter navigator
		if ($BLUEWEB.viewportScrollY > (abs_pos - fix_pos)){
			$BLUEWEB.node_achapnav.style.top = fix_pos + 'px';
			$BLUEWEB.node_achapnav.style.position = 'fixed';
		} else {
			$BLUEWEB.node_achapnav.style.top = abs_pos + 'px';
			$BLUEWEB.node_achapnav.style.position = 'absolute';
		}
		arrow = document.getElementById("achapnavarrow");
		if (arrow) {
			arrow.style.top = -10 + Scrolling[1] / 12 + 'px';
		}
	},
	
	getViewportWH : function() {
		var vpW = 0, vpH = 0;
		// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
		if (typeof window.innerWidth != 'undefined') {
			vpW = window.innerWidth,
			vpH = window.innerHeight
		}
		// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
		else if (typeof document.documentElement != 'undefined' &&
				 typeof document.documentElement.clientWidth != 'undefined' &&
				 document.documentElement.clientWidth != 0) {
			vpW = document.documentElement.clientWidth,
			vpH = document.documentElement.clientHeight
		}
		// older versions of IE
		else {
			vpW = document.getElementsByTagName('body')[0].clientWidth,
			vpH = document.getElementsByTagName('body')[0].clientHeight
		}
		$BLUEWEB.viewportWidth = vpW;
		$BLUEWEB.viewportHeight = vpH;
		return [ vpW, vpH ];
	},
	
	getScrollXY : function() {
		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body &&
				 ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement &&
				 ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		$BLUEWEB.viewportScrollX = scrOfX;
		$BLUEWEB.viewportScrollY = scrOfY;
		return [ scrOfX, scrOfY ];
	},
	
	activeSize : function() {
		var aspectX, aspectY;
		
		$BLUEWEB.getViewportWH();
		aspectX = Math.max(50, parseInt($BLUEWEB.viewportWidth / 1004 * 100 * 1.1));
		aspectY = Math.max(50, parseInt($BLUEWEB.viewportHeight / 609 * 100 * 1.0));
		aspect = Math.min( aspectX, aspectY);
		
		$BLUEWEB.node_debug.innerHTML = "viewport width: " + $BLUEWEB.viewportWidth + " height: " + $BLUEWEB.viewportHeight + "<br/>";
		$BLUEWEB.node_debug.innerHTML += "aspect(X,Y): " + aspectX + "%, " + aspectY + "%" + "<br/>";
		
		$BLUEWEB.adjust_node_selected();
		$BLUEWEB.adjust_node_layout();
		if ($BLUEWEB.node_achapnav && $BLUEWEB.node_achapnav.style.display != 'none') {
			$BLUEWEB.adjust_node_achapnav();
		}
		/*
		$BLUEWEB.content = document.getElementById("ultoc");
		if ( $BLUEWEB.content != null ) $BLUEWEB.content.style.fontSize = aspect.toString() + "%";
		*/
	},
	
	activeScroll: function() {
		//$BLUEWEB.getScrollXY();
		if ($BLUEWEB.node_achapnav && $BLUEWEB.node_achapnav.style.display != 'none') {
			$BLUEWEB.getScrollXY();
			$BLUEWEB.adjust_node_achapnav();
		}
	}
};

// run when script loads
( function() {
	//$BLUEWEB.init();
	if (typeof window.addEventListener !== 'undefined') {
  		window.addEventListener('load', $BLUEWEB.init, false);
	} else if (typeof window.attachEvent !== 'undefined') {
 		window.attachEvent('onload', $BLUEWEB.init);
	}

	if (typeof window.addEventListener !== 'undefined') {
  		window.addEventListener('resize', $BLUEWEB.activeSize, false);
	} else if (typeof window.attachEvent !== 'undefined') {
 		window.attachEvent('onresize', $BLUEWEB.activeSize);
	}

	$BLUEWEB.node_logo = document.getElementById("identity");
	$BLUEWEB.node_menu = document.getElementById("menu");
	$BLUEWEB.node_main1 = document.getElementById("articles");
	$BLUEWEB.node_main2 = document.getElementById("shortcuts");
	$BLUEWEB.node_achapnav = document.getElementById("achapnav");
	
	if (typeof window.addEventListener !== 'undefined') {
		window.addEventListener('scroll', $BLUEWEB.activeScroll, false);
	} else if (typeof window.attachEvent !== 'undefined') {
		window.attachEvent('onscroll', $BLUEWEB.activeScroll);
	}
		
})();

