// FILE browser.js
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


// FILE menu.js

	var ie6 = false;
	if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version == '6') ie6 = true;
	jQuery.event.add(window, "scroll", scrollLeftNavMenu);
	jQuery.event.add(window, "resize", resizeFrame);
	// autorun
	jQuery(function() { addSectionTitles();autoSelectMenuItem(); });
	
	var menuHeight = $('#core-leftmenunav').height();
	var menuTopInitial = $('#core-leftmenunav').offset().top;
	var menuLeftInitial = $('#core-leftmenunav').offset().left;
	//var menuTopInitial = $('#core-leftmenunav').position().top;
	//var menuLeftInitial = $('#core-leftmenunav').position().left;
	var menuBottomInitial = menuTopInitial + menuHeight;
	if (ie6)
		var marginTopInitial = $('#core-leftmenunav').css('marginTop');
	
	function addSectionTitles() {
		$('#core-leftmenunav .current_subtab').each(function() {
			var id = $(this).attr('link_tag').replace(/\-/, '.').replace(/\./, '.');
			var item = $(document.getElementById(id));
			//if (item.length > 0) {
				item.addClass('m-title').attr('link_tag', id);
		  /*} else {
		  	console.log('for item '+id+' header not found');
		  }*/
		});
	}
	
	function resizeFrame() {
		scrollLeftNavMenu();
	}
	
	function menuHighlightByIndex(index) {
		$('#core-leftmenunav .current_subtab').css('background-color', '#FFFFFF');
		$('#core-leftmenunav .current_subtab').css('font-weight', 'normal');
		//console.log('Highlight section '+index);
		if ($('#core-leftmenunav .current_subtab')[index]) {
			$($('#core-leftmenunav .current_subtab')[index]).css('background-color', '#F5F7E9');
			$($('#core-leftmenunav .current_subtab')[index]).css('font-weight', 'bold');
		}
	}
	
	function autoSelectMenuItem() {
		var selected = null;
		var index = -1;
		var onscreen = false;
		var items = jQuery('.m-title');
		var item = null;
		
		for(index=0; index < items.length; index++) {
			item = jQuery(items[index]);
			var top = parseInt(item.offset().top)+2;//-1;
			
			//jQuery(item).html(jQuery(document).scrollTop() + " " + jQuery(window).height() + " " + parseInt(jQuery(item).offset().top));  // debug
			
			if ( jQuery(document).scrollTop() + jQuery(window).height() < top) {
				index -= 1;
				break;
			}
			
			if (jQuery(document).scrollTop() < top) {
				break;
			}
		
		};
		
		if (index >= items.length) index -= 1;
		
		if (index <= -1)
		{
			index = 0;
		}
		
		menuHighlightByIndex(index);
	}
	
	function scrollLeftNavMenu() {
		autoSelectMenuItem();

		windowTop = $(window).scrollTop();
		windowHeight = $(window).height();
		windowBottom = windowTop + windowHeight;
		menuTop = $('#core-leftmenunav').offset().top;
		//menuTop = document.getElementById('core-leftmenunav').style.top;
		menuBottom = menuTop + menuHeight;

		// fits on screen
		if (menuHeight < windowHeight) {
			// stick to top
			if (windowTop >= menuTopInitial) {
				if (ie6)
					$('#core-leftmenunav').css('marginTop', (document.documentElement.scrollTop-menuTopInitial+12) + "px");
				else {
					$('#core-leftmenunav').css('position', 'fixed');
					$('#core-leftmenunav').css('top', '0');
				}
				$('#core-leftmenunav').css('left', menuLeftInitial);
			}
			// or return to normal
			else if (windowTop < menuTopInitial) {
				$('#core-leftmenunav').css('position', 'static');
				if (ie6)
					$('#core-leftmenunav').css('marginTop', marginTopInitial);
				else
					$('#core-leftmenunav').css('top', menuTopInitial);
			}
		}
		
		// doesn't fit on screen
		if (menuHeight >= windowHeight) {
			//stick menu to the screen bottom by changing menu.top
			if (windowBottom >= menuBottomInitial) {
				marginTop = parseInt($('#core-leftmenunav').css('marginTop').replace("px", ""));
				if (ie6) {
					//if ( document.documentElement.scrollTop-menuTopInitial-(menuHeight - windowHeight)-12 + document.documentElement.clientHeight+100 < document.body.scrollHeight)
					newMarginTop = (document.documentElement.scrollTop-menuTopInitial-(menuHeight - windowHeight)-12);
					if ( newMarginTop + menuHeight + $('#core-footer').height() +75 < $('#core-footer').offset().top )
						$('#core-leftmenunav').css('marginTop', newMarginTop + "px");
				}
				else {
					$('#core-leftmenunav').css('position', 'fixed');
					//$('#core-leftmenunav').css('top', -(menuHeight - windowHeight) );
					addon_margin = 10;
					if ($('#citableToggle').length > 0) addon_margin += 150;
					if ($('#core-footer').length > 0 && windowBottom > $('#core-footer').offset().top) addon_margin += (windowBottom - $('#core-footer').offset().top);
					$('#core-leftmenunav').css('top', -(menuHeight - windowHeight) - addon_margin );
				}
				$('#core-leftmenunav').css('left', menuLeftInitial);
			}
			// return to normal
			else {
				$('#core-leftmenunav').css('position', 'static');
				if (ie6)
					$('#core-leftmenunav').css('marginTop', marginTopInitial);
				else
					$('#core-leftmenunav').css('top', menuTopInitial);
			}
		}
		
		// BUGFIX: menu sticks to top of page
		if (menuTop < menuTopInitial-1) {
			if (ie6)
				$('#core-leftmenunav').css('marginTop', marginTopInitial);
			else
				$('#core-leftmenunav').css('top', menuTopInitial);
		}
	}


