/* installed file=/include/faToCmnu.js
 *
 *	Copyright © 2010-2011 by FKE Internet.  All rights reserved.
 *
 *	$Id: /include/faToCmnu.js,v $
 */
/**
*	Javascript for the Floating accordian Table of Contents menu system
*
*	Author:			Fred Koschara
*	Creation Date:	June twentieth, 2011
*	Last Modified:  July 31, 2011 @ 1:45 am
*
*	Revision History:
*	   Date		  by		Description
*	2011/07/31	wfredk	use content width as "left" if content wider than window
*	2011/07/31	wfredk	use zero as height if no site_banner ID on the page
*	2011/07/02	wfredk	original development
*		|						|
*	2011/06/20	wfredk	original development
*/
//setup - all values in pixels
var bAlertNoSiteBanner=true;
var imgClose='/graphics/imgClose.gif';
var imgOpen='/graphics/imgOpen.gif';
var navWidth=320;
var ulIndent=10;
var wideWidth=navWidth+(3*ulIndent)+12;

var mN="#floatMenu";	// floating menu id name
var tN="#toc";			// ToC id name
var mX=0;				//
var mY=0;				// menu Y location
var scrollMax=0;

/** jquery.dimensions
* Copyright (c) 2007 Paul Bakaus (paul.bakaus@googlemail.com)
* and Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Requires: jQuery 1.2+
*/
(function($)
{	$.dimensions={ version: '@VERSION' };

	// Create innerHeight,innerWidth,outerHeight and outerWidth methods
	$.each(['Height','Width'],function(i,name)
	{	var torl,borr;
		if (name=='Height')
		{	torl='Top';
			borr='Bottom';
		}
		else
		{	torl='Left';
			borr='Right';
		}

		// innerHeight and innerWidth
		$.fn['inner'+name]=function()
		{	if (!this[0]) return;
			return this.css('display')!='none'
				 ? this[0]['client'+name]
				 : num(this,name.toLowerCase())
					+num(this,'padding'+torl)+num(this,'padding'+borr);
		};

		// outerHeight and outerWidth
		$.fn['outer'+name]=function(options)
		{	if (!this[0]) return;
			options=$.extend({ margin: false },options || {});
			var val=this.css('display')!='none'
				   ? this[0]['offset'+name]
				   : num(this,name.toLowerCase())
					+num(this,'border'+torl+'Width')
					+num(this,'border'+borr+'Width')
					+num(this,'padding'+torl)+num(this,'padding'+borr);
			return val+(options.margin
						? (num(this,'margin'+torl)+num(this,'margin'+borr))
						: 0);
		};
	});

	// Create scrollLeft and scrollTop methods
	$.each(['Left','Top'],function(i,name)
	{	$.fn['scroll'+name]=function(val)
		{	if (!this[0]) return;
			return val!=undefined
				 ?	// Set the scroll offset
					this.each(function()
					{	this==window || this==document
						?	window.scrollTo
							(	name=='Left' ? val : $(window)['scrollLeft'](),
								name=='Top'  ? val : $(window)['scrollTop' ]()
							)
						: this['scroll'+name]=val;
					})
				 :	// Return the scroll offset
					this[0]==window || this[0]==document
					?	self[(name=='Left' ? 'pageXOffset' : 'pageYOffset')]
						|| $.boxModel && document.documentElement['scroll'+name]
						|| document.body['scroll'+name]
					:	this[0]['scroll'+name];
		};
	});

	$.fn.extend(
	{	position: function()
		{	var left=0,top=0,elem=this[0],offset,parentOffset,offsetParent,results;
			if (elem)
			{	// Get *real* offsetParent
				offsetParent=this.offsetParent();
				// Get correct offsets
				offset=this.offset();
				parentOffset=offsetParent.offset();
				// Subtract element margins
				offset.top -=num(elem,'marginTop');
				offset.left-=num(elem,'marginLeft');
				// Add offsetParent borders
				parentOffset.top +=num(offsetParent,'borderTopWidth');
				parentOffset.left+=num(offsetParent,'borderLeftWidth');
				// Subtract the two offsets
				results=
				{	top:  offset.top -parentOffset.top,
					left: offset.left-parentOffset.left
				};
			}
			return results;
		},
		offsetParent: function()
		{	var offsetParent=this[0].offsetParent;
			while (offsetParent && (!/^body|html$/i.test(offsetParent.tagName)
				&& $.css(offsetParent,'position')=='static'))
				offsetParent=offsetParent.offsetParent;
			return $(offsetParent);
		}
	});

	function num(el,prop)
	{	return parseInt($.curCSS(el.jquery?el[0]:el,prop,true))||0;
	};

})(jQuery);

// Accordian menu plugin created by Brock Nusser - http://mlamenu.blogspot.com/
//$(function()
function accordian_menu()
{	var pathname=window.location.pathname;

//	$('#nav').css('width',(navWidth+(3*ulIndent)+12)+'px');
	$('#nav').css('width',(wideWidth-14)+'px');
	$('#nav ul').css('width',navWidth+'px');
	$('#nav ul').css('margin-left',ulIndent+'px');

	$('#nav a').each(function()
	{	var level=$(this).parents('ul').length;
		var liWidth=navWidth-(ulIndent*level)-((4*ulIndent));//+12);//+30;
		$(this).parent('li').css('width',liWidth+'px');
	});

	//prepend expand/collapse icons
	$('#nav li').each(function()
	{	if ($(this).children('ul').length > 0)
		{	if ($(this).children('ul').is(":visible"))
				$(this).prepend('<img src="'+imgOpen+'" />');
			else $(this).prepend('<img src="'+imgClose+'" />');
		}
	});

	var slideSpeed='slow'; // 'slow', 'normal', 'fast', or miliseconds
	$('#nav a').each(function()
	{	var thisHref=$(this).attr('href')
		if ((window.location.pathname.indexOf(thisHref)==0)
		||	(window.location.pathname.indexOf('/'+thisHref)==0))
			$(this).addClass('Current');
	});

	$('.Current').parent('li').children('ul').show();
	$('.Current').parents('ul').show();

	//prepend expand/collapse icons
	$('#nav li').each(function()
	{	if ($(this).children('ul').length>0
		&&	$(this).children('ul').is(":visible"))
			$(this).children('img').attr('src',imgOpen);
	});

	$('#nav img').click(function()
	{	if ($(this).parent('li').children('ul').html() != null)
		{	$(this).parent('li').parent('ul').children('li').children('ul').hide(slideSpeed);
			$(this).parent('li').parent('ul').children('li').children('img').attr('src',imgClose);
			$(this).delay(100).is(':hidden');
			if ($(this).parent('li').children('ul').css('display')=="block")
			{	$(this).parent('li').children('ul').hide(slideSpeed);
				$(this).attr('src',imgClose);
			}
			else
			{
//		var liWidth=navWidth-(ulIndent*level)-100;//((3*ulIndent)+12);//+30;
//		$(this).parent('li').css('width',liWidth+'px');
				$(this).parent('li').children('ul').show(slideSpeed);
				$(this).attr('src',imgOpen);
			}
			return false;
		}
	});

	$('#nav li').click(function()
	{	if ($(this).children('a').length==0
		&&	$(this).children('ul').html()!=null)
		{	$(this).parent('ul').children('li').children('ul').hide(slideSpeed);
			if ($(this).children('ul').css('display')=="block")
				$(this).children('ul').hide(slideSpeed);
			else $(this).children('ul').show(slideSpeed);
		}
	});
//});
};

function initFloatMenu()
{	var content=document.getElementById("sf_page_contents");
	var floatMenu=document.getElementById("floatMenu");
	var site_banner=document.getElementById("site_banner");
	var toc=document.getElementById("toc");
	var factor=2*((4*ulIndent)+12);
	var wnWidth=document.body.clientWidth;
	navWidth=floatMenu.offsetWidth;
	wideWidth=navWidth+factor;
	toc.width=wideWidth+2*factor;
	floatMenu.width=wideWidth-16;
//	mX=document.getElementById("content").offsetWidth;
//alert("content.offsetWidth = "+content.offsetWidth+"  wnWidth = "+wnWidth);
	if (content.offsetWidth>wnWidth) mX=content.offsetWidth;
	else
	{	mX=wnWidth-wideWidth-2;
		content.width=mX;//-factor;
	}
//	$(mN).css("left",(mX/*-factor*/)+"px");
	$(mN).css("width",(wideWidth-16)+"px");
	$(tN).css("left",(mX/*-factor*/)+"px");
	$(tN).css("width",(wideWidth/*-factor*/)+"px");
	var bodyHeight;
	var myHeight=0;
	var ht=content.offsetHeight;
	var th=toc.offsetHeight;
	if (ht>th)
	{	toc.style["height"]=ht+"px";
		bodyHeight=ht;
	}
	else
	{	if (th>ht) content.style["height"]=th+"px";
		bodyHeight=th;
	}
/*	if (typeof(window.innerHeight)=='number')	// not IE
		myHeight=window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		myHeight=document.documentElement.clientHeight;	// IE 6+ in 'standards compliant mode'
	else if (document.body && document.body.clientHeight)	// IE 4 compatible
		myHeight=document.body.clientHeight;
	else myHeight=0;
	if (site_banner)
	{	mY=site_banner.offsetHeight;
		$(mN).css("top",mY+"px");
	}
	else mY=parseInt($(mN).css("top").substring(0,$(mN).css("top").indexOf("px"))); */
	if (site_banner)
		scrollMax=bodyHeight-site_banner.offsetHeight;//(myHeight-mY);
	else
	{	scrollMax=bodyHeight;
		if (bAlertNoSiteBanner)
			alert("No site_banner element on the page");
	}
	$(window).resize(function()
	{	mX=document.getElementById("sf_page_contents").offsetWidth;
//		$(mN).css("left",mX+"px");
		$(tN).css("left",mX+"px");
	});
	$(window).scroll(function()
	{	if (typeof(window.pageYOffset)=='number')	// Netscape compliant
			scrollTop=window.pageYOffset;
		else if (document.body && document.body.scrollTop)	// DOM compliant
			scrollTop=document.body.scrollTop;
		else if (document.documentElement && document.documentElement.scrollTop)
			scrollTop=document.documentElement.scrollTop;	// IE6 standards compliant mode
		offset=Math.min(scrollMax,(mY+scrollTop))+"px";
		$(mN).animate({top:offset},{duration:250,queue:false});
//		$(mN).css("top",offset);
	});
}

addLoadEvent(initFloatMenu);
addLoadEvent(accordian_menu);
//
// EOF: faToCmnu.js

