/********************************************************************************
*                                                                               *
*     M r . C   s c r i p t - o n l y   h o m e p a g e   f r a m e w o r k     *
*                                                                               *
*********************************************************************************
*                                                                               *
*                  Copyright (c) 2004 by Mr.C IT-Services                       *
*                                        a Mr.C company                         *
*                                                                               *
*                    Author : Wolfgang "Rick" Kutschera                         *
*                                                                               *
*********************************************************************************
*                                                                               *
* Version history :                                                             *
*                                                                               *
*         V1.0    : Misc    - Initial release                                   *
*         V1.1    : Bugfix  - Overlay menu disappears even with mouse over the  *
*                             menu if browsers scrollbar is not at top position *
*         V1.2    : Bugfix  - Startpage resizes dynamically even if blocked by  *
*                             stylesheet                                        *
*                   Feature - Dynamic content-width resize to prevent scrollbar *
*         V1.3    : Upgrade - Dynamic content-width now automatically adjusts   *
*                             to use at least the width needed by the top menu  *
*                   Misc    - Added version history to sourcecode               *
*         V1.4    : Misc    - Frontend cleanup                                  *
*         V1.5    : Feature - Added direct access to subpage via URL-parameter  *
*         V1.6    : Feature - Added capability to build sidemenu without        *
*                             subentries. (Like in topmenu)                     *
*         V1.7    : Misc    - File split - Script code is now stored in an      *
*                             extra file.                                       *
*         V1.8    : Feature - Added free content handling                       *
*                                                                               *
********************************************************************************/

// **********************************************************************
// *                             Structure                              *
// **********************************************************************

	function SideMenuEntry(Pic, RollOverPic, OverlayMenu, DefaultContent)
	{
		this.Pic = Pic;
		this.RollOverPic = RollOverPic;
		this.OverlayMenu = OverlayMenu;
		this.DefaultContent = DefaultContent;
	}
	
	function TopMenuEntry(Pic, RollOverPic, LeftPos, OverlayMenu, DefaultContent)
	{
		this.Pic = Pic;
		this.RollOverPic = RollOverPic;
		this.LeftPos = LeftPos;
		this.OverlayMenu = OverlayMenu;
		this.DefaultContent = DefaultContent;
	}

	function OverlayMenuEntry(Pic, RollOverPic, ContentEntry)
	{
		this.Pic = Pic;
		this.RollOverPic = RollOverPic;
		this.ContentEntry = ContentEntry;
	}
	
	function Position(X, Y, Width, Height)
	{
		this.X = X;
		this.Y = Y;
		this.Width = Width;
		this.Height = Height;
	}
	
	var W3CDom = (document.getElementById && !document.all) ? true:false
    var ieDom  = (document.all) ? true:false
    var nsDom  = (document.layers) ? true:false

	var Menu = new Array();
	var TopMenu = new Array();
	var LastMIndex = 0;
	var FreeCont = new Array();


// **********************************************************************
// *                         Helper functions                           *
// **********************************************************************

	function InitPage()
	{
		GenerateMenuArray();
		GenerateOverlayMenus();
		GenerateSideMenu();
		GenerateOverlayTopMenus();
		GenerateTopMenu();
	}

    function GetPos(ElementID)
    {
		var X=0;
		var Y=0;    
		var Width=0;
		var Height=0;
	    if (ieDom || W3CDom)
	    {
		    X = document.getElementById(ElementID).offsetLeft;
		    Y = document.getElementById(ElementID).offsetTop;
	    }
	    else if (nsDom)
	    {
		    X = document.getElementById(ElementID).x;
		    Y = document.getElementById(ElementID).y;
	    }
	    Width=document.getElementById(ElementID).clientWidth;
	    Height=document.getElementById(ElementID).clientHeight;
	    return new Position(X, Y, Width, Height);
    }
    
    function GetMousePos()
    {
    	var X=0;
    	var Y=0;
    	if (ieDom || W3CDom)
    	{
    		X=event.clientX + document.body.scrollLeft;
    		Y=event.clientY + document.body.scrollTop;
    	}
    	else if (nsDom)
    	{
    		X=event.screenX + window.pageXOffset;
    		Y=event.screenY + window.pageYOffset;
    	}
    	return new Position(X, Y, 0, 0);
    }
    
    function GetWindowDimensions()
    {
    	var TheWidth=0;
    	var TheHeight=0;
    	if (ieDom || W3CDom)
    	{
    		TheWidth=document.body.offsetWidth;
    		TheHeight=document.body.offsetHeight;
    	}
    	else if (nsDom)
    	{
    		TheWidth=window.innerWidth;
    		TheHeight=window.innerHeight;
    	}
    	return new Position(0, 0, TheWidth, TheHeight);
    }
    
    function GetLastPicEnd()
    {
    	var TheEnd=0;
    	if(TopMenu==null || TopMenu.length==0)
    	{
    		var Pos;
    		Pos=GetWindowDimensions();
    		return Pos.Width;
    	}
    	TheEnd=TopMenu[TopMenu.length-1].LeftPos;
    	TheEnd+=document.getElementById("TM"+(TopMenu.length-1)).clientWidth;
    	return TheEnd;
    }
	
	function SetContentWidth()
	{
		var WindowDim;
		var ActualWidth;
		var LastPicEnd;
		var MinWidth;
		
		LastPicEnd=GetLastPicEnd();
		MinWidth=LastPicEnd - ContentLeftPos - 30;
		WindowDim=GetWindowDimensions();
		ActualWidth=WindowDim.Width - ContentLeftPos - 30;
		if (ActualWidth<MinWidth) ActualWidth=MinWidth;
		if (ActualWidth>ContentMaxWidth) ActualWidth=ContentMaxWidth;
		
		for (var i=0;i<Menu.length;i++)
		{
			if (Menu[i].DefaultContent != null) Menu[i].DefaultContent.style.width=ActualWidth;
			if (Menu[i].OverlayMenu != null)
			{
				for(var j=0;j<Menu[i].OverlayMenu.length;j++)
				{
					Menu[i].OverlayMenu[j].ContentEntry.style.width=ActualWidth;
				}
			}
		}
		
		for(var i=0;i<TopMenu.length;i++)
		{
			if (TopMenu[i].DefaultContent != null) TopMenu[i].DefaultContent.style.width=ActualWidth;
			if (TopMenu[i].OverlayMenu != null)
			{
				for(var j=0;j<TopMenu[i].OverlayMenu.length;j++)
				{
					TopMenu[i].OverlayMenu[j].ContentEntry.style.width=ActualWidth;
				}
			}
		}
		
		for(var i=0;i<FreeCont.length;i++)
		{
			FreeCont[i].style.width=ActualWidth;
		}
			
	}
	
	function DeActivateContent(MenuVar)
	{
		for(var i=0;i<MenuVar.length;i++)
		{
			if (MenuVar[i].DefaultContent != null) MenuVar[i].DefaultContent.style.visibility="Hidden";
			if (MenuVar[i].OverlayMenu != null)
			{
				for(var j=0;j<MenuVar[i].OverlayMenu.length;j++)
				{
					MenuVar[i].OverlayMenu[j].ContentEntry.style.visibility="Hidden";
				}
			}
		}
	}
	
	function ActivateContent(TheEntry)
	{
		DeActivateContent(Menu);
		DeActivateContent(TopMenu);
		for(var i=0;i<FreeCont.length;i++)
			FreeCont[i].style.visibility="Hidden";

		TheEntry.style.visibility="Visible";
	}


	function DoInitialContentLoad()
	{
		var GivenURL;
		GivenURL=window.location.search;
		if(GivenURL != "")
		{
			GivenURL=GivenURL.substr(1,255);
		}
		if(GivenURL == "" || document.getElementById(GivenURL) == null)
			GivenURL=DefaultContent;
		ActivateContent(document.getElementById(GivenURL));
	}		

// **********************************************************************
// *                     Style building functions                       *
// **********************************************************************

	function BuildSMStyle(TheIndex, TheZIndex)
	{
		var s="";
		var TheTop=0;
		
		TheTop=TheIndex*SideMenuTopSpacer;
		TheTop+=SideMenuTopPos;
		s="position:absolute; left:"+SideMenuLeftPos+"px; top:"+TheTop+"px; z-Index:"+TheZIndex;
		return (s);
	}
	
	function BuildTMStyle(TheIndex, TheZIndex)
	{
		var s="";
		s="position:absolute; left:"+TopMenu[TheIndex].LeftPos+"px; top:"+TopMenuTopPos+"px; z-Index:"+TheZIndex;
		return (s);
	}

	function BuildOTMStyle(TheIndex)
	{
		var s="";
		s="position:absolute; left:"+TopMenu[TheIndex].LeftPos+"px; top:"+TopMenuTopPos+"px; z-Index:101;visibility:hidden";
		return (s);
	}

	function BuildOMStyle(TheIndex)
	{
		var s="";
		var TheTop=0;
		
		TheTop=TheIndex*OverlayMenuEntryTopSpacer;
		TheTop+=OverlayMenuEntryTopPos;
//		s="position:absolute; left:"+OverlayMenuEntryLeftPos+"px; top:"+TheTop+"px";
		return (s);
	}
	
	function BuildOMStyleDiv(TheIndex)
	{
		var s="";
		var TheTop=0;
		
		TheTop=TheIndex*SideMenuTopSpacer;
		TheTop+=SideMenuTopPos;
		TheTop+=OverlayMenuTopIntend;
		s=OverlayStyleInvisible+"position:absolute; left:"+OverlayMenuLeftPos+"px; top:"+TheTop+"px";
		return (s);
	}	

// **********************************************************************
// *                     Menu generator functions                       *
// **********************************************************************

	function GenerateSideMenu()
	{
		var s="";
		document.open();
		for(var i=0;i<Menu.length;i++)
		{
			s="<a href=\"#\"><img border=\"0\" id=\"SM"+i+"\" name=\"SM"+i+"\" OnMouseOver=\"SMMouseOver("+i+")\" OnMouseOut=\"SMMouseOut("+i+")\" ";
			s+="OnClick=\"SMMouseClick("+i+")\" src=\""+Menu[i].Pic+"\" style=\""+BuildSMStyle(i, 102)+"\"></a>";
			document.writeln(s);
		}
		document.close();
	}

	function GenerateTopMenu()
	{
		var s="";
		document.open();
		for(var i=0;i<TopMenu.length;i++)
		{
			s="<a href=\"#\"><img border=\"0\" id=\"TM"+i+"\" name=\"TM"+i+"\" OnMouseOver=\"TMMouseOver("+i+")\" OnMouseOut=\"TMMouseOut("+i+")\" ";
			s+="OnClick=\"TMMouseClick("+i+")\" src=\""+TopMenu[i].Pic+"\" style=\""+BuildTMStyle(i, 102)+"\"></a>";
			document.writeln(s);
		}
		document.close();
	}

	function GenerateOverlayMenus()
	{
		var OM;
		var s="";
//		if(Menu==null) return;
		document.open();
		for(var i=0;i<Menu.length;i++)
		{
			OM=Menu[i].OverlayMenu;
			if(OM!=null)
			{
				s="<div id=\"OM"+i+"\" name=\"OM"+i+"\" OnMouseOut=\"OMMouseOut("+i+", 0)\" style=\""+BuildOMStyleDiv(i)+"\">";
				s+="<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td><img width=\""+OverlayMenuEntryLeftPos+"px\" height=\"1px\"></td>";
				s+="<td><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"background-color:"+OverlayMenuBackgroundColor+"\">"
				for(var j=0;j<OM.length;j++)
				{
					s+="<tr height=\""+OverlayMenuEntryTopSpacer+"px\"><td width=\""+OverlayMenuEntryWidth+"px\">";
					s+="<a href=\"#\"><img border=\"0\" id=\"OM"+i+"E"+j+"\" name=\"OM"+i+"E"+j+"\" ";
					s+="OnMouseOver=\"OMEMouseOver("+i+", "+j+")\" ";
					s+="OnMouseOut=\"OMEMouseOut("+i+", "+j+")\" ";
					s+="OnClick=\"OMEMouseClick("+i+", "+j+")\" ";
					s+="src=\""+OM[j].Pic+"\" style=\""+BuildOMStyle(j)+"\" ></a>";
					s+="</td></tr>";
				}
				s+="</table></td></tr></table>";
				s+="</div>";
				document.writeln(s);
			}
		}
		document.close();
	}
	
	function GenerateOverlayTopMenus()
	{
		var OM;
		var s="";
		document.open();
		for(var i=0;i<TopMenu.length;i++)
		{
			OM=TopMenu[i].OverlayMenu;
			if (OM!=null)
			{
				s="<div id=\"OTM"+i+"\" name=\"OTM"+i+"\" OnMouseOut=\"OTMMouseOut("+i+", 0)\" style=\""+BuildOTMStyle(i)+"\">";
				s+="<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"\">"
				s+="<tr height=\""+TopMenuHeight+"pc\"><td></td></tr>";
				for(var j=0;j<OM.length;j++)
				{
					s+="<tr height=\""+TopMenuEntryHeight+"px\" style=\"background-color:"+OverlayTopMenuBackgroundColor+"\"><td>";
					s+="<a href=\"#\"><img border=\"0\" id=\"OTM"+i+"E"+j+"\" name=\"OTM"+i+"E"+j+"\" ";
					s+="OnMouseOver=\"OTMEMouseOver("+i+", "+j+")\" ";
					s+="OnMouseOut=\"OTMEMouseOut("+i+", "+j+")\" ";
					s+="OnClick=\"OTMEMouseClick("+i+", "+j+")\" ";
					s+="src=\""+OM[j].Pic+"\" style=\"\" ></a>";
					s+="</td></tr>";
				}
				s+="</table>";
				s+="</div>";
				document.writeln(s);
			}
		}
		document.close();
	}

// **********************************************************************
// *                     Event handling functions                       *
// **********************************************************************

// **********************************************************************
// *                            Side Menu                               *
// **********************************************************************

	function SMMouseOver(MenuIndex)
	{
		for (var i=0;i<Menu.length;i++)
		{
			document.getElementById("SM"+i).src=Menu[i].Pic;
			document.getElementById("SM"+i).style.zIndex=102;
			if(Menu[i].OverlayMenu!=null) document.getElementById("OM"+i).style.visibility="Hidden";
		}
	
		document.getElementById("SM"+MenuIndex).src=Menu[MenuIndex].RollOverPic;
		
		document.getElementById("SM"+MenuIndex).style.zIndex=100;
		if(Menu[MenuIndex].OverlayMenu!=null) document.getElementById("OM"+MenuIndex).style.visibility="Visible";
	}


	function SMMouseOut(MenuIndex)
	{
		if(Menu[MenuIndex].OverlayMenu==null) 
		{
			document.getElementById("SM"+MenuIndex).src=Menu[MenuIndex].Pic;
			document.getElementById("SM"+MenuIndex).style.zIndex=102;
		}
	}

	function SMMouseClick(MenuIndex)
	{
		ActivateContent(Menu[MenuIndex].DefaultContent);
	}

// **********************************************************************
// *                             Top Menu                               *
// **********************************************************************

	function TMMouseOver(MenuIndex)
	{
		for (var i=0;i<TopMenu.length;i++)
		{
			document.getElementById("TM"+i).src=TopMenu[i].Pic;
			document.getElementById("TM"+i).style.zIndex=102;
			if(TopMenu[i].OverlayMenu!=null) document.getElementById("OTM"+i).style.visibility="Hidden";
		}
	
		document.getElementById("TM"+MenuIndex).src=TopMenu[MenuIndex].RollOverPic;
		
		document.getElementById("TM"+MenuIndex).style.zIndex=100;
		if(TopMenu[MenuIndex].OverlayMenu!=null) document.getElementById("OTM"+MenuIndex).style.visibility="Visible";
	}
	
	function TMMouseOut(MenuIndex)
	{
		if(TopMenu[MenuIndex].OverlayMenu==null) 
		{
			document.getElementById("TM"+MenuIndex).src=TopMenu[MenuIndex].Pic;
			document.getElementById("TM"+MenuIndex).style.zIndex=102;
		}
	}
	
	function TMMouseClick(MenuIndex)
	{
		ActivateContent(TopMenu[MenuIndex].DefaultContent);
	}

// **********************************************************************
// *                           Overlay Menu                             *
// **********************************************************************
	
	function OMMouseOut(MenuIndex, Force)
	{
		var ThePos=new Position(0,0,0,0);
		var MPos=new Position(0,0,0,0);
		if(Force==0)
		{
			// Check Mouse Position
			ThePos=GetPos("OM"+MenuIndex);
			MPos=GetMousePos();
//			alert(MPos.X+":"+MPos.Y+" "+ThePos.X+":"+ThePos.Y+":"+ThePos.Width+":"+ThePos.Height);
			if ((MPos.X>=ThePos.X && MPos.X<=(ThePos.X+ThePos.Width)) && (MPos.Y>=ThePos.Y && MPos.Y<=(ThePos.Y+ThePos.Height)))
			{
				// Still within Menu
				return false;
			}
		}
		document.getElementById("SM"+MenuIndex).src=Menu[MenuIndex].Pic;


		document.getElementById("OM"+MenuIndex).style.visibility="Hidden";
		document.getElementById("SM"+MenuIndex).style.zIndex=102;
	}
	
	function OMEMouseOver(SideIndex, MenuIndex)
	{
		document.getElementById("OM"+SideIndex+"E"+MenuIndex).src=Menu[SideIndex].OverlayMenu[MenuIndex].RollOverPic;
	}

	function OMEMouseOut(SideIndex, MenuIndex)
	{
		document.getElementById("OM"+SideIndex+"E"+MenuIndex).src=Menu[SideIndex].OverlayMenu[MenuIndex].Pic;
	}	
	
	function OMEMouseClick(SideIndex, MenuIndex)
	{
		OMMouseOut(SideIndex, 1);
		ActivateContent(Menu[SideIndex].OverlayMenu[MenuIndex].ContentEntry);
	}
	
	function OTMMouseOut(MenuIndex, Force)
	{
		var ThePos=new Position(0,0,0,0);
		var MPos=new Position(0,0,0,0);
		if(Force==0)
		{
			// Check Mouse Position
			ThePos=GetPos("OTM"+MenuIndex);
			MPos=GetMousePos();
			if ((MPos.X>=ThePos.X && MPos.X<=(ThePos.X+ThePos.Width)) && (MPos.Y>=ThePos.Y && MPos.Y<=(ThePos.Y+ThePos.Height)))
			{
				// Still within Menu
				return false;
			}
		}
		document.getElementById("TM"+MenuIndex).src=TopMenu[MenuIndex].Pic;




		document.getElementById("OTM"+MenuIndex).style.visibility="Hidden";
		document.getElementById("TM"+MenuIndex).style.zIndex=102;
	}
	
	function OTMEMouseOver(SideIndex, MenuIndex)
	{
		document.getElementById("OTM"+SideIndex+"E"+MenuIndex).src=TopMenu[SideIndex].OverlayMenu[MenuIndex].RollOverPic;
	}

	function OTMEMouseOut(SideIndex, MenuIndex)
	{
		document.getElementById("OTM"+SideIndex+"E"+MenuIndex).src=TopMenu[SideIndex].OverlayMenu[MenuIndex].Pic;
	}	
	
	function OTMEMouseClick(SideIndex, MenuIndex)
	{
		OTMMouseOut(MenuIndex, 1);
		ActivateContent(TopMenu[SideIndex].OverlayMenu[MenuIndex].ContentEntry);
	}
	
