/*
*	Packages Menu, Analyzing
*	menu.complete.js		2001.04.12
*	Victor A.Spirin,	victor_aspirin@mail.ru
*/



var RES = {out: /\-off\./, over: /\-on\./};
var SOURCES = {out: '-off.', over: '-on.'};

var analyzeManager = new AnalyzeManager();

analyzeManager.addAnalyzer( new RolloverImageAnalyzer('rollover', RolloverImage, {res: RES, sources: SOURCES}) );
analyzeManager.addAnalyzer( new LinkHashAnalyzer('menu', LinkMenuActivator, {res: RES, sources: SOURCES}) );

window.onload = function ()
	{
	analyzeManager.analyzeAll();
	}










function Menu( el, ActivatorContext )
	{
	var DELAY_MOUSE_OUT = 300;
	var DEFAULT_LAYOUT = 'bottom';

	el = new baseElement( el );
	var parentMenu;
	var layout;
	var isTopLevel;

	var thread;

		if( document.layers != null )
		{
		window.getSize = function ()
			{
			return {width: window.innerWidth, height: window.innerHeight};
			}

		function cancel()
			{
			return false;
			}

		var s = document.ids[el.id];
		layout = s != null && s.menuLayout != null ? s.menuLayout : DEFAULT_LAYOUT;
		}
		else if( document.all != null )
		{
		window.getSize = function ()
			{
			return {width: document.body.scrollWidth, height: document.body.offsetHeight};
			}

		function cancel()
			{
			event.cancelBubble = true;
			return false;
			}

		el.onmousedown = cancel;
		el.onmouseup = cancel;

		layout = el.style['menu-layout'] || DEFAULT_LAYOUT;
		}

	el.getParentMenu = function ()
		{
			if( isTopLevel == null )
			{
				if( ActivatorContext != null && ActivatorContext.getParentMenu != null )
				{
				parentMenu = ActivatorContext;
				isTopLevel = false;
				}
				else isTopLevel = true;
			}
		return parentMenu;
		}

	function getContext()
		{
		var m = el;
			while( m.getParentMenu() != null )
			{
			m = m.getParentMenu();
			}
		return m.parentLayer != null && m.parentLayer != window ? new baseElement( m.parentLayer ) : window;
		}

	function placePiece( size, outBegin, outSize, inBegin, inSize )
		{
		return inBegin + inSize + size < outBegin + outSize ? inBegin + inSize : inBegin - size;
		}

	function alignPiece( size, outBegin, outSize, inBegin, inSize )
		{
		return inBegin + size < outBegin + outSize ? inBegin : inBegin + inSize - size;
		}

	function restrictPiece( size, outBegin, outSize, inBegin, inSize )
		{
		return inBegin + size < outBegin + outSize ? inBegin : outBegin + outSize - size;
		}

	el.place = function ( position, size )
		{
		var c = getContext();
		var o = c.getAbsolutePosition != null ? c.getAbsolutePosition() : {X: 0, Y: 0};
		var a = c.getSize();

		var s = el.getSize();

		var x;
		var y;
			if( isTopLevel )
			{
				if( layout == 'top' || layout == 'bottom'  )
				{
				x = restrictPiece( s.width, o.X, a.width, position.X, size.width );
				y = placePiece( s.height, o.Y, a.height, position.Y, size.height );
				}
				else if( layout == 'left' || layout == 'right' )
				{
				x = placePiece( s.width, o.X, a.width, position.X, size.width );
				y = alignPiece( s.height, o.Y, a.height, position.Y, size.height );
				}
			}
			else
			{
			x = placePiece( s.width, o.X, a.width, parentMenu.getAbsolutePosition().X, parentMenu.getSize().width );
			y = alignPiece( s.height, o.Y, a.height, position.Y, size.height );
			}
		el.moveToAbsolute( x, y );
		}

	el.hideAfter = function ( delay )
		{
		thread = setTimeout( el.hide, delay )
		}

	el.clearHide = function ()
		{
			if( thread != null ) clearTimeout( thread );
		thread = null;
		}

	el.onmouseover = function f1( ev )
		{
			if( document.all == null || !el.contains(event.fromElement) )
			{
			var m = el;
				while( m != null )
				{
				m.clearHide();
				m.show();
				m = m.getParentMenu();
				}
			}
		return cancel();
		}

	el.onmouseout = function f2( ev )
		{
			if( document.all == null || !el.contains(event.toElement) )
			{
			el.hide();
			var m = el.getParentMenu();
				while( m != null )
				{
				m.hideAfter( DELAY_MOUSE_OUT );
				m = m.getParentMenu();
				}
			}
		return cancel();
		}

	return el;
	}










function baseElement( el )
	{
		if( document.layers != null )
		{
		el.show = function show( forced )
			{
			el.visibility = forced ? 'show' : 'inherit';
			}

		el.hide = function hide()
			{
			el.visibility = 'hide';
			}

		el.getAbsolutePosition = function ()
			{
			return {X: el.pageX, Y: el.pageY};
			}

		el.getSize = function ()
			{
			return {width: el.clip.width, height: el.clip.height};
			}
		}
		else if( document.all != null )
		{
		el.show = function show( forced )
			{
			el.style.visibility = forced ? 'visible' : 'inherit';
			}

		el.hide = function hide()
			{
			el.style.visibility = 'hidden';
			}

		el.getAbsolutePosition = function ()
			{
			var x = 0;
			var y = 0;
			var e = el;
				while( e != null )
				{
				x += e.offsetLeft;
				y += e.offsetTop;
				e = eE.offsetParent;
				}
			return {X: x, Y: y};
			}

		el.getSize = function ()
			{
			return {width: (el.style.posWidth != 0 ? el.style.pixelWidth : el.offsetWidth), height: (el.style.posHeight != 0 ? el.style.pixelHeight : el.offsetHeight)};
			}

		el.moveToAbsolute = function ( x, y )
			{
			var _x = 0;
			var _y = 0;
			var p = el.offsetParent;
				while( p != null )
				{
				_x += p.offsetLeft;
				_y += p.offsetTop;
				p = p.offsetParent;
				}
			el.style.pixelLeft = x - _x;
			el.style.pixelTop = y - _y;
			}
		}

	return el;
	}










function LinkMenuActivator( Link, container, id, data )
	{
	var DELAY_MOUSE_OUT = 120;

	var menu;
	var image;

	menu = findLayer( id, container.window.document );
		if( menu == null ) return null;

		if( document.layers != null )
		{
		var is = container.document.images;
			for( var i = 0 ; i < is.length ; i++ )
				if( is[i].x == Link.x && is[i].y == Link.y )
				{
				image = is[i];
				break;
				}

		Link.getAbsolutePosition = function ()
			{
				if( container == container.window ) return {X: Link.x, Y: Link.y};
				else return {X: Link.x + container.pageX, Y: Link.y + container.pageY};
			}

		function getValueOf( str )
			{
			var m = str.match( /([0-9]+\.?[0-9]*)([a-zA-Z]*)/ );
				switch( m[2] )
				{
				case 'px': return parseInt( m[1] );
				case 'pt': return parseInt( m[1] )*96/72;
				case 'mm': return parseInt( m[1] )*96/25.4;
				case 'cm': return parseInt( m[1] )*96/2.54;
				}
			}

		Link.getSize = function ()
			{
				if( image != null ) return {width: image.width, height: image.height};

			var s = 16;
				if( document.tags['a'].fontSize != null ) s = getValueOf( document.tags['a'].fontSize );
				else if( document.tags['body'].fontSize != null ) s = getValueOf( document.tags['body'].fontSize );
			return {width: Math.round(s*(0.5*(Link.text != null ? Link.text.length : 8) + 0.2)), height: Math.round(s*1.5)};
			}

		Link.getContext = function ()
			{
			return container;
			}

		function cancel()
			{
			return false;
			}
		}
		else if( document.all != null )
		{
			if( Link.all.length == 1 && Link.all[0].tagName == 'IMG' ) image = Link.all[0];

		Link.getAbsolutePosition = function ()
			{
			var x = 0;
			var y = 0;
			var e = Link;
				while( e != null )
				{
				x += e.offsetLeft;
				y += e.offsetTop;
				e = e.offsetParent;
				}
			return {X: x, Y: y};
			}

		Link.getSize = function ()
			{
			return {width: Link.offsetWidth, height: Link.offsetHeight};
			}

		Link.getContext = function ()
			{
				for( var p = Link.offsetParent ; p.tagName != 'BODY' ; p = p.offsetParent )
					if( p.tagName != 'TABLE' && p.tagName != 'TR' && p.tagName != 'TD' ) return p;
			return container.window;
			}

		function cancel()
			{
			event.cancelBubble = true;
			return false;
			}
		}

	function findLayer( id, doc )
		{
		var ls = doc.layers;
			if( ls == null ) return doc.all[id];
			if( ls[id] != null ) return ls[id];
		var l;
			for( var i = 0 ; i < ls.length ; i++ )
			{
			l = findLayer( id, ls[i].document );
				if( l != null ) return l;
			}
		return null;
		}

	Link.onmouseover = function onMouseOver( ev )
		{
		menu.clearHide();
		menu.place( Link.getAbsolutePosition(), Link.getSize() );
		menu.show();
			if( image != null ) image.src = image.src.replace( data.res.out, data.sources.over );
		return cancel();
		}

	Link.onmouseout = function onMouseOut( ev )
		{
		menu.hideAfter( DELAY_MOUSE_OUT );
			if( image != null ) image.src = image.src.replace( data.res.over, data.sources.out );
		return cancel();
		}

	Link.href = window.document.URL;

	menu = new Menu( menu, Link.getContext() );
	menu.place( Link.getAbsolutePosition(), Link.getSize() );

	return Link;
	}










function LinkHashAnalyzer( marker, ObjectConstructor, data )
	{
	var type = marker != null ? marker.toLowerCase() : null;

	this.getType = function ()
		{
		return type;
		}

	this.findArray = function findArray( container )
		{
			if( document.layers != null ) return container.document.links;
			else if( document.all != null ) return (container != window ? container : document).all.tags( 'a' );
		}

	function getPath( a )
		{
		var p = unescape( a.href );
		var i = p.indexOf( '#' );
			if( 0 < i ) p = p.substring(0, i);
			if( a.pathname == '/' && p.charAt(p.length - 1) != '/' ) p += '/';
		return p; 
		}

	function getHash( a )
		{
		var h = unescape( a.href );
		var i = h.indexOf( '#' );
		return (0 < i && i + 1 < h.length) ? h.substring(i + 1) : null; 
		}

	this.accept = function accept( el, container )
		{
		var h = getHash( el );
		var d = container.window.document;
			if( h == null || getPath(el) != getPath(d.location) ) return false;

		var as = d.anchors;
			for( var i = 0 ; i < as.length ; i++ )
				if( as[i].name == h ) return false;

		return (type == null || h.toLowerCase().search(type) != -1);
		}

	this.createObject = function createObject( el, container )
		{
		var o = new ObjectConstructor( el, container, getHash(el), data );
		}
	}










function RolloverImageAnalyzer( marker, ObjectConstructor, data )
	{
	var type = marker != null ? marker.toLowerCase() : null;

	this.getType = function ()
		{
		return type;
		}

	this.findArray = function findArray( container )
		{
			if( document.layers != null ) return container.document.images;
			else if( document.all != null ) return (container != window ? container : document).all.tags( 'img' );
		}

	this.accept = function accept( el, container )
		{
		return (el.src.search(data.res.out) != -1 || el.src.search(data.res.over) != -1);
		}

	this.createObject = function createObject( el, container )
		{
		var o = new ObjectConstructor( el, container, el.name, data );
		}
	}

function RolloverImage( image, container, name, data )
	{
	var link;
		if( document.all != null )
		{
			if( image.parentElement.tagName == 'A' ) link = image.parentElement;
		}
		else if( document.layers != null )
		{
		var ls = container.document.links;
			for( var i = 0 ; i < ls.length ; i++ )
				if( ls[i].x == image.x && ls[i].y == image.y )
				{
				link = ls[i];
				break;
				}
		}

		if( link == null ) return null;

	link.onmouseover = function onMouseOver( ev )
		{
		image.src = image.src.replace( data.res.out, data.sources.over );
		return false;
		}

	link.onmouseout = function onMouseOut( ev )
		{
		image.src = image.src.replace( data.res.over, data.sources.out );
		return false;
		}

	return link;
	}










function AnalyzeManager()
	{
	var analyzers;
	var This = this;

	this.addAnalyzer = function addAnalyzer( a )
		{
			if( a.getType != null && a.findArray != null && a.accept != null && a.createObject != null )
			{
				if( analyzers == null ) analyzers = {};
			var t = a.getType();
				if( t == null ) t = 'type_' + Math.random();
			analyzers[t] = a;
			}
		}

	this.analyze = function ( type, container )
		{
		var c = container || window;
		var a = analyzers[type];

		var es = a.findArray( c );
			for( var j = 0 ; j < es.length ; j++ )
				if( a.accept(es[j], c) ) a.createObject( es[j], c );

		var d = c.document;
			if( d != null && d.layers != null )
				for( var k = 0 ; k < d.layers.length ; k++ )
				This.analyze( type, d.layers[k] );
		}

	this.analyzeAll = function ( container )
		{
			for( var i in analyzers )
			This.analyze( i, container );
		}
	}

