// Constants
var END_Y = 228;
var MENU_WIDTH = 189;
var MENUSIGN_OFFSET = 15;
var MENUITEM_OFFSET = 35;
var SUBMENUITEM_OFFSET = 45;

// Menu corner pixel position (will be determined at onLoad)
var cornerX = 0;
var cornerY = 0;

// Indicates wheter the menu is being animated
var menuOpening = false;
var menuClosing = false;

// The currently opened menuitem
var openedMenuItem = -1;
var openMenuAfterClosing = -1;
var upperBottom = -1;
var lowerTop = -1;
var openCounter = -1;


function getPositionLeft( el ) {
	var x = el.offsetLeft;
	if ( el.offsetParent != null ) {
		x += getPositionLeft( el.offsetParent );
	}

	return x;
}

function getPositionTop( el ) {
	var y = el.offsetTop;
	if ( el.offsetParent != null ) {
		y += getPositionTop( el.offsetParent );
	}

	return y;
}

function justifyLayerToAnother( layerNameToJustify, anotherLayerName, offsetX, offsetY ) {
	var layerElm = document.getElementById( layerNameToJustify );
	var anotherLayerElm = document.getElementById( anotherLayerName );

	layerElm.style.left = getPositionLeft( anotherLayerElm ) + offsetX;
	layerElm.style.top = getPositionTop( anotherLayerElm ) + offsetY;
}

function moveLayerTo( layerName, x, y ) {
	var obj = document.getElementById( layerName );
	obj.style.left = x;
	obj.style.top = y;
}

function distributeMenuItems( x, y, space, start, end, hasLast ) {
	var nr = end - start + 1;

	var base = Math.floor( space / nr );
	var remain = space % nr;
	for ( var i = 0; i < nr; i++ ) {
		var height = base;
		if ( remain > 0 ) {
			height++;
			remain--;
		}

		moveLayerTo( "menuLine" + ( start + i ), x, y );

		var signLayerHeight = 10;
		moveLayerTo( "menuSign" + ( start + i ), x + MENUSIGN_OFFSET, y + ( height - signLayerHeight ) / 2 );

		var itemLayerHeight = 14;
		moveLayerTo( "menuItem" + ( start + i ), x + MENUITEM_OFFSET, y + ( height - itemLayerHeight ) / 2 );

		y += height;
	}

	if ( hasLast ) {
		moveLayerTo( "menuLineLast", x, y );
	}
}

function openSubmenu( menuItem ) {
	// Blur clicked link
	document.getElementById( "menuLink" + menuItem ).blur();

	if ( !menuOpening && !menuClosing ) {
		// Close previous menu if it is opened
		if ( openedMenuItem != -1 ) {
			// Store a menu item number to open after closing the currently open submenu
			openMenuAfterClosing = menuItem;

			// Close current submenu
			closeOpenedSubmenu();
		} else {
			reallyOpenSubmenu( menuItem );
		}

		/*// Do not open menu if it was opened
		if ( menuItem == openedMenuItem ) {
			openedMenuItem = -1;
			return;
		}*/
	}
}

function reallyOpenSubmenu( menuItem ) {
	if ( !menuOpening && !menuClosing ) {
		menuOpening = true;
		openMenuAfterClosing = -1;
		openedMenuItem = menuItem;

		var middleLine = parseInt( document.getElementById( "menuLine" + ( menuItem + 1 ) ).style.top );

		var sizeTop = middleLine - cornerY;
		var sizeBottom = END_Y - middleLine;

		openCounter = parseInt( document.getElementById( "submenu" + menuItem ).clientHeight ) / 2 + 5;
		shrinkOrExpand( menuItem, cornerY, sizeTop, middleLine, sizeBottom, -1, 1 );
	}
}

function closeOpenedSubmenu() {
	if ( !menuOpening && !menuClosing ) {
		if ( openedMenuItem != -1 ) {
			menuClosing = true;
			shrinkOrExpand( openedMenuItem, cornerY, upperBottom - cornerY, lowerTop, END_Y - lowerTop, 1, 1 );
		}
	}
}

function shrinkOrExpand( menuItem, topTop, topHeight, bottomTop, bottomHeight, expandSize, counter ) {
	var newExpandSize = expandSize;
	if ( menuItem != 0 ) {
		distributeMenuItems( cornerX, topTop, topHeight + newExpandSize, 0, menuItem, false );
	} else {
		newExpandSize = newExpandSize * 2;
	}

	distributeMenuItems( cornerX, bottomTop - newExpandSize, bottomHeight + newExpandSize, menuItem + 1, 6, true );

 	var submenuLayer = document.getElementById( "submenu" + menuItem );
 	if ( menuItem == 0 ) {
	 	if ( expandSize > 0 ) {
	 		submenuLayer.style.clip = "rect( 0px "+MENU_WIDTH+"px "+((openCounter-counter)*2)+"px 0px )";		// rect(top right bottom left)
	 	} else {
	 		submenuLayer.style.clip = "rect( 0px "+MENU_WIDTH+"px "+(counter*2)+"px 0px )";		// rect(top right bottom left)
	 	}
 	} else {
	 	var clipMiddle = Math.round( parseInt( submenuLayer.clientHeight ) / 2 );
	 	if ( expandSize > 0 ) {
	 		submenuLayer.style.clip = "rect( "+(clipMiddle-(openCounter-counter))+"px "+MENU_WIDTH+"px "+(clipMiddle+(openCounter-counter))+"px 0px )";		// rect(top right bottom left)
	 	} else {
	 		submenuLayer.style.clip = "rect( "+(clipMiddle-counter)+"px "+MENU_WIDTH+"px "+(clipMiddle+counter)+"px 0px )";		// rect(top right bottom left)
		}
	}
 	submenuLayer.style.visibility = "visible";

	counter++;
	if ( counter <= openCounter ) {
		setTimeout( "shrinkOrExpand( "+menuItem+", "+topTop+", "+(topHeight + newExpandSize)+", "+(bottomTop - newExpandSize)+", "+(bottomHeight + newExpandSize)+", "+expandSize+", "+counter+" );", 1 );
	} else {
		if ( menuOpening ) {
			menuOpening = false;

			upperBottom = topTop + topHeight + newExpandSize;
			lowerTop = bottomTop - newExpandSize;
		} else if ( menuClosing ) {
			menuClosing = false;

			openedMenuItem = -1;
			upperBottom = -1;
			lowerTop = -1;
			openCounter = -1;

			// Hide submenu layer
		 	var submenuLayer = document.getElementById( "submenu" + menuItem );
		 	submenuLayer.style.visibility = "hidden";

			// Open a submenu if needed (and if it isn't the closed menu)
			if ( openMenuAfterClosing != -1 && openMenuAfterClosing != menuItem ) {
				reallyOpenSubmenu( openMenuAfterClosing );
			}
		}
	}
}

function updateCornerPosition() {
	// Get position of menu corner pixel
	var cornerObj = document.getElementById( "menuCorner2" );
	cornerX = getPositionLeft( cornerObj ) + 1;		// Add one pixel as the border of the menu
	cornerY = getPositionTop( cornerObj );

	/*// Get window width
	var myWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		// Non-IE
		myWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		// IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		// IE 4 compatible
		myWidth = document.body.clientWidth;
	}*/

	/*// Website width is fixed
	var websiteWidth = 964;

	// Menu X offset in website
	var menuOffsetX = 756;

	// Calculate menu position
	cornerX = ( myWidth - websiteWidth ) / 2 + menuOffsetX + 1;
	cornerY = 50;*/
}

function documentLoaded() {
	updateCornerPosition();

	distributeMenuItems( cornerX, cornerY, END_Y - cornerY, 0, 6, true );

	// Show all layers
	for ( var i = 0; i < 7; i++ ) {
		document.getElementById( "menuLine" + i ).style.visibility = "visible";
		document.getElementById( "menuSign" + i ).style.visibility = "visible";
		document.getElementById( "menuItem" + i ).style.visibility = "visible";

		// Get submenu layer
		var submenuLayer = document.getElementById( "submenu" + i );
		if ( submenuLayer ) {
			submenuLayer.style.left = cornerX + SUBMENUITEM_OFFSET;

			var base = parseInt( document.getElementById( "menuLine" + ( i + 1 ) ).style.top );
			if ( i == 0 ) {
				submenuLayer.style.top = base;
			} else {
				var h = Math.round( parseInt( submenuLayer.clientHeight ) / 2 );
				submenuLayer.style.top = base - h;
			}
		}
	}
	document.getElementById( "menuLineLast" ).style.visibility = "visible";
}

function documentResized() {
	updateCornerPosition();

	/*// Get position of menu corner pixel
	var cornerObj = document.getElementById( "menuCorner" );
	cornerPixelX = getPositionLeft( cornerObj ) + 1;		// Add one pixel as the border of the menu
	cornerPixelY = getPositionTop( cornerObj );

	if ( cornerX != cornerPixelX ) {
		cornerX = cornerPixelX;
	}*/

	// Change X position of all layers
	for ( var i = 0; i < 7; i++ ) {
		document.getElementById( "menuLine" + i ).style.left = cornerX;
		document.getElementById( "menuSign" + i ).style.left = cornerX + MENUSIGN_OFFSET;
		document.getElementById( "menuItem" + i ).style.left = cornerX + MENUITEM_OFFSET;

		// Get submenu layer
		var submenuLayer = document.getElementById( "submenu" + i );
		if ( submenuLayer ) {
			submenuLayer.style.left = cornerX + SUBMENUITEM_OFFSET;
		}
	}
	document.getElementById( "menuLineLast" ).style.left = cornerX;
}

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length;
i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)
x.src=x.oSrc;
}

function MM_findObj(n, d) { //v3.0
var p,i,x; if(!d) d=document;
if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++)
x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++)
x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array;
for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc)
x.oSrc=x.src; x.src=a[i+2];}
}
