/*******************************************************************************
*  ruthsarian_utilities.js : 2005.03.07
* -----------------------------------------------------------------------------
*  A group of useful JavaScript utilities that can aid in the development
*  of webpages.
*******************************************************************************/

/* event_attach() takes care of attaching event handlers (functions) to events. this 
 * simplifies the process of attaching multiple handlers to a single event
 */
function event_attach( event , func )
{
	if ( ( typeof( func ) ).toLowerCase() != 'function' )
	{
		return;
	}
	if ( ( typeof( document.event_handlers ) ).toLowerCase() == 'undefined' )
	{
		document.event_handlers = new Array();
	}
	if ( ( typeof( document.event_handlers[ event ] ) ).toLowerCase() == 'undefined' )
	{
		document.event_handlers[ event ] = new Array();
	}
	if ( window.attachEvent )
	{
		window.attachEvent( event , func );
	}
	else
	{
		if ( ( typeof( eval( 'window.' + event ) ) ).toLowerCase() != 'function' )
		{
			eval( 'window.' + event + ' = function () { if ( ( typeof( document.event_handlers[ \'' + event + '\' ] ) ).toLowerCase() != \'undefined\' ) { for ( i = 0 ; i < document.event_handlers[ \'' + event + '\' ].length ; i++ ) { document.event_handlers[ \'' + event + '\' ][ i ](); } } } ' );
		}
		document.event_handlers[ event ][ document.event_handlers[ event ].length ] = func;
	}
}

/* browser_detect() creates a simple object that can be used to determine what
 * browser the client is using. this is useful in many areas.
 */
function browser_detect() 
{
	var ua			= navigator.userAgent.toLowerCase(); 
	this.ua			= ua;
	this.isGecko		= ( ua.indexOf( 'gecko' ) != -1 );
	this.isMozilla		= ( this.isGecko && ua.indexOf( "gecko/" ) + 14 == ua.length);
	this.isIE		= ( ( ua.indexOf( "msie" ) != -1 ) && ( ua.indexOf( "opera" ) == -1) && ( ua.indexOf( "webtv" ) == -1 ) );
	this.isOpera		= ( ua.indexOf( "opera" ) != -1 );
	this.isKonqueror	= ( ua.indexOf( "konqueror" ) != -1 );
	this.isIcab		= ( ua.indexOf( "icab" ) != -1 );
	this.isAol		= ( ua.indexOf( "aol" ) != -1 );
	this.isWebtv		= ( ua.indexOf( "webtv" ) != -1 );
	this.isOmniweb		= ( ua.indexOf( "omniweb" ) != -1 );
	this.isDreamcast	= ( ua.indexOf( "dreamcast" ) != -1 );
	this.versionMinor	= parseFloat( navigator.appVersion );
	if ( this.isNS && this.isGecko ) 
	{
		this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf( '/' ) + 1 ) );
	} 
	else if ( this.isIE && this.versionMinor >= 4 ) 
	{
		this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'msie ' ) + 5 ) );
	} 
	else if ( this.isOpera ) 
	{
		if ( ua.indexOf( 'opera/' ) != -1 ) 
		{
			this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'opera/' ) + 6 ) );
		}
		else 
		{
			this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'opera ' ) + 6 ) );
		}
	} 
	else if ( this.isKonqueror ) 
	{
		this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'konqueror/' ) + 10 ) );
	} 
	else if ( this.isIcab ) 
	{
		if ( ua.indexOf( 'icab/' ) != -1 ) 
		{
			this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'icab/' ) + 6 ) );
		}
		else 
		{
			this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'icab ' ) + 6 ) );
		}
	} 
	else if ( this.isWebtv )
	{
		this.versionMinor = parseFloat( ua.substring( ua.indexOf( 'webtv/' ) + 6 ) );
	}
	this.versionMajor	= parseInt( this.versionMinor ); 
	this.isWin		= ( ua.indexOf( 'win' ) != -1 );
	this.isWin32		= ( this.isWin && ( ua.indexOf( '95' ) != -1 || ua.indexOf( '98' ) != -1 || ua.indexOf( 'nt' ) != -1 || ua.indexOf( 'win32' ) != -1 || ua.indexOf( '32bit' ) != -1 ) );
	this.isMac		= ( ua.indexOf( 'mac' ) != -1 );
	this.isUnix		= ( ua.indexOf( 'unix' ) != -1 || ua.indexOf( 'linux' ) != -1 || ua.indexOf( 'sunos' ) != -1 || ua.indexOf( 'bsd' ) != -1 || ua.indexOf( 'x11' ) != -1 );
	this.isIE55		= ( this.isIE && this.versionMinor == 5.5 );
	this.isIE5up		= ( this.isIE && this.versionMajor >= 5 );
	this.isIE6up		= ( this.isIE && this.versionMajor >= 6 );
}

/* opacity() will enable PNG transparency for Internet Explorer
 */
function opacity( strId , strPath , intWidth , intHeight , strClass , strAlt )
{	
	if ( document.pngAlpha )
	{
		document.write( '<div style="height:'+intHeight+'px;width:'+intWidth+'px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+strPath+'.png\', sizingMethod=\'scale\')" id="'+strId+'" class="'+strClass+'"></div>' );
	}
	else if ( document.pngNormal )
	{
		document.write( '<img src="'+strPath+'.png" width="'+intWidth+'" height="'+intHeight+'" name="'+strId+'" border="0" class="'+strClass+'" alt="'+strAlt+'" />' );
	}
	else if ( document.layers )
	{
		return( '<img src="'+strPath+'.gif" width="'+intWidth+'" height="'+intHeight+'" name="'+strId+'" border="0" class="'+strClass+'" alt="'+strAlt+'" />' );
	}
	else
	{
		document.write( '<img src="'+strPath+'.gif" width="'+intWidth+'" height="'+intHeight+'" name="'+strId+'" border="0" class="'+strClass+'" alt="'+strAlt+'" />' );
	}
	return( '' );
}

/* opacity_init() handles some variable initializations required for opacity() to function
 */
function opacity_init()
{
	var browser = new browser_detect();
	document.pngAlpha = false;
	document.pngNormal = false;
	document.strExt = ".gif";

	if ( ( browser.isIE55 || browser.isIE6up ) && browser.isWin32 )
	{
		document.pngAlpha = true;
		document.strExt = ".png";
	}
	else if ( 
			( browser.isGecko ) || 
			( browser.isIE5up && browser.isMac ) || 
			( browser.isOpera && browser.isWin && browser.versionMajor >= 6 ) || 
			( browser.isOpera && browser.isUnix && browser.versionMajor >= 6 ) || 
			( browser.isOpera && browser.isMac && browser.versionMajor >= 5 ) || 
			( browser.isOmniweb && browser.versionMinor >= 3.1 ) || 
			( browser.isIcab && browser.versionMinor >= 1.9 ) || 
			( browser.isWebtv ) || 
			( browser.isDreamcast ) 
		)
	{
		document.pngNormal = true;
		document.strExt = ".png";
	}
}

/* handler for Netscape Navigator clients that screw up the display
 * of CSS pages when reloaded
 */
function NN_reloadPage( init )
{
	if ( init == true ) with ( navigator )
	{
		if ( ( appName == "Netscape" ) && ( parseInt ( appVersion ) == 4 ) )
		{
			document.NN_pgW = innerWidth;
			document.NN_pgH = innerHeight;
			event_attach ( 'onresize' , NN_reloadPage );
		}
	}
	else if ( innerWidth != document.NN_pgW || innerHeight != document.NN_pgH )
	{
		location.reload();
	}
}

/* set_min_width() initializes min-width-like functionality
 */
function set_min_width( obj_name , min_width , ieOnly )
{

	if ( ( typeof( ieOnly ) ).toLowerCase() == 'undefined' )
	{
		ieOnly = true;
	}
	if ( ieOnly == false || ( document.getElementById && navigator.appVersion.indexOf( "MSIE" ) > -1 && !window.opera ) )
	{
		document.min_width_obj_name = obj_name;
		document.min_width_size = min_width;
		document.resizing = false;
		event_attach( 'onload' , control_min_width );
		event_attach( 'onresize' , control_min_width );
	}
}

/* control_min_width() is the event handler setup and used by set_min_width()
 * it is based on the Project Seven min-width code
 */
function control_min_width()
{

	var cw , w , pl , pr , ml , mr , br , bl , ad , theDiv = document.min_width_obj_name;
	var g = document.getElementById( theDiv );
	w = parseInt(document.min_width_size);
	if ( g && document.body && document.body.clientWidth )
	{
		gs = g.currentStyle;
		cw = parseInt( document.body.clientWidth );
		pl = parseInt( gs.paddingLeft );
		pr = parseInt( gs.paddingRight );
		ml = parseInt( gs.marginLeft );
		mr = parseInt( gs.marginRight );
		bl = parseInt( gs.borderLeftWidth );
		br = parseInt( gs.borderRightWidth );
		ml = ml ? ml : 0;
		mr = mr ? mr : 0;
		pl = pl ? pl : 0;
		pr = pr ? pr : 0;
		bl = bl ? bl : 0;
		br = br ? br : 0;
		ad = pl + pr + ml + mr + bl + br;
		if ( cw <= w )
		{
			w -= ad;
			g.style.width = w + "px";
		}
		else
		{
			g.style.width = "auto";
		}
	}
}

/* set_min_width_rs() takes a different approach to emulating min-width functionality
 */
function set_min_width_rs( element_id , min_width )
{
	if ( document.getElementById && navigator.appVersion.indexOf( "MSIE" ) > -1 && !window.opera )
	{
		document.mw_element_id = element_id;
		document.mw_min_width = min_width;
		event_attach( 'onload' , control_min_width_rs );
		event_attach( 'onresize' , control_min_width_rs );
	}
}

/* control_min_width_rs() is the event handler used by set_min_width_rs. This method fixes a couple bugs with
 * the p7 approach that can lead to the browser crashing. This method specifically targets IE although perhaps
 * it could be modified to work with other browsers later.
 */
function control_min_width_rs()
{
	var e = document.getElementById( document.mw_element_id );
	var offset = parseInt( 0 + e.currentStyle.paddingLeft ) + parseInt( 0 + e.currentStyle.paddingRight ) + parseInt( 0 + e.currentStyle.borderLeftWidth ) + parseInt( 0 + e.currentStyle.borderRightWidth );

	if ( ( document.body.clientWidth - ( e.offsetLeft * 2 ) ) <= ( document.mw_min_width - offset ) && parseInt( 0 + e.style.width ) != document.mw_min_width )
		e.style.width = document.mw_min_width + "px";
	else if ( document.body.clientWidth > ( document.mw_min_width + offset + e.offsetLeft * 2 ) )
		e.style.width = "auto";
}

/* Son of Suckerfish drop-down CSS-based menus. This javascript will setup mouse event handlers
 * for LI elements under IE that adds a class to the class attribute on hover and removes
 * it on mouseout. This replicates the :hover pseudo attribute which IE6 and below don't understand
 *
 * Use the following Javascript inside your webpage to get the drop downs to work.
 * Just replace "dropnav" with whatever the ID for the UL/OL element containing the
 * menu.
 *
 *	if (window.attachEvent)
 *	{
 *		window.attachEvent("onload", function () { sfHover( "dropvnav" ); } );
 *
 *	}
 */
function sfHover( objID ) 
{
	if ( document.getElementById( objID ) == null )
	{
		return;
	}

	var sfEls = document.getElementById( objID ).getElementsByTagName( "LI" );
	for (var i=0; i<sfEls.length; i++)
	{
		sfEls[i].onmouseover = function()
		{
			this.className+=" sfhover"; 
		}
		sfEls[i].onmouseout = function() 
		{
			this.className=this.className.replace(new RegExp(" sfhover\\b"), ""); 
		} 
	} 
}
