<?php

/***************************************************************************

 topmenu.php
 ------------
 copyright : (C) 2002-2003 The ezContents Development Team

 ***************************************************************************/

/***************************************************************************
 The ezContents Development Team offers no warranties on this script.
 The owner/licensee of the script is solely responsible for any problems
 caused by installation of the script or use of the script.

 All copyright notices regarding ezContents must remain intact on the
 scripts and in the HTML for the scripts.

 For more info on ezContents,
 visit http://www.ezcontents.org/

/***************************************************************************

/***************************************************************************
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the License which can be found within the
 *	zipped package.
 *
 ***************************************************************************/

$GLOBALS["rootdp"] = './';
require_once ($GLOBALS["rootdp"]."include/config.php");
require_once ($GLOBALS["rootdp"]."include/db.php");
require_once ($GLOBALS["rootdp"]."include/session.php");

// The global gsUseFrames is empty when framed.
//		settings.php will decide if it should be set or not
// The session variable noframesbrowser is set when viewing
//		a frame-configured site in a non-frames browser.

if (($GLOBALS["gsUseFrames"] == '') && ($EZ_SESSION_VARS["noframesbrowser"] != True)) {
	include ($GLOBALS["rootdp"]."include/settings.php");
	include ($GLOBALS["rootdp"]."include/functions.php");
	includeLanguageFiles('main');
	force_page_refresh();
}


$GLOBALS["bTopMenuExists"] = ReadTopMenuStructure();
if ($HTTP_GET_VARS["topgroupname"] != "") { $menuref = $HTTP_GET_VARS["topgroupname"];
} else { $menuref = $GLOBALS["gsHomepageTopGroup"]; }
frmTopMenu($menuref);



function jsTopMenuImage($prefix,$group,$image,$postfix)
{
	echo $prefix.$group.$postfix." = new Image(0,0) ;\n";
	echo $prefix.$group.$postfix.".src = './".$GLOBALS["image_home"].$image."' ;\n";
} // jsMenuImage()



function frmTopMenu($menuref)
{
	global $EZ_SESSION_VARS;

	// If we're in frames mode, output the page header data
	// In non-frames mode, this is handled by control.php
	if (($GLOBALS["gsUseFrames"] == 'Y') && ($EZ_SESSION_VARS["noframesbrowser"] != True)) {
		HTMLHeader('topmenu');
		StyleSheet();
	}
	?>
	<script language="JavaScript" type="text/javascript">
		<!-- Begin
		var currentTopMenu = '<?php echo $menuref; ?>' ;

		function inTopMenuArray( testArray, testElement ) {
			var r = 0 ;
			for ( var i = 0; i < testArray.length; i++ ) {
				if ( testArray[i] == testElement ) {
					r = 1 ;
				}
			}
			return r ;
		}

		function ChangeFrames( imgDocID, TopGroupName ) {
			parent.left.location.href="<?php echo BuildLink('menu.php'); ?>&topgroupname=" + TopGroupName ;
			parent.contents.location.href="<?php echo BuildLink('showcontents.php'); ?>&topgroupname=" + TopGroupName ;
			if ( TopMenuImageObj ) {
				var iTest = inTopMenuArray( topmenuImageArray, currentTopMenu );
				if ( iTest ) {
					document.images[imgDocID].src = eval( 'gt' + currentTopMenu + '.src' ) ;
				}
			}
			currentTopMenu = TopGroupName ;
			if ( TopMenuImageObj ) {
				var iTest = inTopMenuArray( topmenuImageArray, currentTopMenu );
				if ( iTest ) {
					var iref = eval('topmenu' + currentTopMenu) ;
					iref.src = eval( 'gt' + currentTopMenu + 'c.src' ) ;
				}
			}
		}

		function changeTopMenuImageOver( imgDocID, imgObjName ) {
			if ( TopMenuImageObj ) {
				var iTest = inTopMenuArray( topmenuImageArray, imgObjName );
				if ( iTest ) {
					if ( imgObjName == currentTopMenu ) {
						document.images[imgDocID].src = eval( 'gt' + imgObjName + 'c.src' ) ;
					}
					else {
						document.images[imgDocID].src = eval( 'gt' + imgObjName + 'a.src' ) ;
					}
				}
			}
		}

		function changeTopMenuImageOut( imgDocID, imgObjName ) {
			if ( TopMenuImageObj ) {
				var iTest = inTopMenuArray( topmenuImageArray, imgObjName );
				if ( iTest ) {
					if ( imgObjName == currentTopMenu ) {
						document.images[imgDocID].src = eval( 'gt' + imgObjName + 'b.src' ) ;
					}
					else {
						document.images[imgDocID].src = eval( 'gt' + imgObjName + '.src' ) ;
					}
				}
			}
		}

		var TopMenuImageObj = 0 ;
		if ( document.images ) {
			TopMenuImageObj = 1 ;
			var topmenuImageArray = new Array ;
			<?php
			if ($GLOBALS["bTopMenuExists"] > 0) {
				$i = 0;
				reset($GLOBALS["topgroups"]);
				while (list($topi,$val) = each($GLOBALS["topgroups"])) {
					if ($GLOBALS["topgroups"][$topi]["topmenuimage1"] != "") {
						echo "topmenuImageArray[".$i."] = '".$GLOBALS["topgroups"][$topi]["topgroupname"]."' ;\n";
						jsTopMenuImage('gt',$GLOBALS["topgroups"][$topi]["topgroupname"],$GLOBALS["topgroups"][$topi]["topmenuimage1"],'');
						if ($GLOBALS["topgroups"][$topi]["topmenuimage2"] != "")
						{
							jsTopMenuImage('gt',$GLOBALS["topgroups"][$topi]["topgroupname"],$GLOBALS["topgroups"][$topi]["topmenuimage2"],'a');
						} else {
							jsTopMenuImage('gt',$GLOBALS["topgroups"][$topi]["topgroupname"],$GLOBALS["topgroups"][$topi]["topmenuimage1"],'a');
						}
						if ($GLOBALS["topgroups"][$topi]["topmenuimage3"] != "") {
							jsTopMenuImage('gt',$GLOBALS["topgroups"][$topi]["topgroupname"],$GLOBALS["topgroups"][$topi]["topmenuimage3"],'b');
							if ($GLOBALS["topgroups"][$topi]["topmenuimage4"] != "") {
								jsTopMenuImage('gt',$GLOBALS["topgroups"][$topi]["topgroupname"],$GLOBALS["topgroups"][$topi]["topmenuimage4"],'c');
							} else {
								jsTopMenuImage('gt',$GLOBALS["topgroups"][$topi]["topgroupname"],$GLOBALS["topgroups"][$topi]["topmenuimage3"],'c');
							}
						} else {
							jsTopMenuImage('gt',$GLOBALS["topgroups"][$topi]["topgroupname"],$GLOBALS["topgroups"][$topi]["topmenuimage1"],'b');
							if ($GLOBALS["topgroups"][$topi]["topmenuimage2"] != "") {
								jsTopMenuImage('gt',$GLOBALS["topgroups"][$topi]["topgroupname"],$GLOBALS["topgroups"][$topi]["topmenuimage2"],'c');
							} else {
								jsTopMenuImage('gt',$GLOBALS["topgroups"][$topi]["topgroupname"],$GLOBALS["topgroups"][$topi]["topmenuimage1"],'c');
							}
						}
						$i++;
					}
				}
			}
			?>
		}
		//  End -->
	</script>
	<?php
	if (($GLOBALS["gsUseFrames"] == 'Y') && ($EZ_SESSION_VARS["noframesbrowser"] != True)) {
		?>
		</head>
		<body marginwidth="0" marginheight="0" leftmargin="5" rightmargin="5" topmargin="10" class="topmenuback">
		<?php
	} else {
		?>
		<table border="0" cellspacing="0" cellpadding="0" width="100%" height="100%">
		<tr><td valign="top" align="<?php echo $GLOBALS["left"]; ?>" class="topmenuback" width="100%" height="100%">
		<?php
	}

	$twidth = '';
	if ($GLOBALS["gsTopMenuAlign"] == 'C') { $talign = 'center';
	} elseif ($GLOBALS["gsTopMenuAlign"] == 'J') {
		$talign = 'center';
		$twidth = '100%';
	} elseif ($GLOBALS["gsTopMenuAlign"] == 'R') { $talign = 'right';
	} else { $talign='left'; }
	?>

	<table border="0" cellspacing="0" cellpadding="0" width="100%">
	<tr class="topmenu"><td align="<?php echo $talign; ?>">
	<table border="<?php echo $GLOBALS["gsTopMenuBorder"]; ?>" align="<?php echo $talign; ?>" cellspacing="0" cellpadding="4" <?php if ($twidth != "") { echo 'width="'.$twidth.'"'; } ?>>
	<tr class="topmenu">
	<?php
	if ($GLOBALS["bTopMenuExists"] > 0) {
		RenderTopGroups($menuref,$talign);
	}
	?>
	</tr>
	</table>
	</td></tr>
	</table>
	<?php

	// If we're in frames mode, output the page footer data
	// In non-frames mode, this is handled by control.php
	if (($GLOBALS["gsUseFrames"] == 'Y') && ($EZ_SESSION_VARS["noframesbrowser"] != True)) {
		?>
		</body>
		</html>
		<?php
	} else {
		?>
		</td></tr></table>
		<?php
	}
} // function frmTopMenu()




function RenderTopGroups($menuref,$talign)
{
	global $EZ_SESSION_VARS;

	$lines = ceil($GLOBALS["bTopMenuExists"] / $GLOBALS["gsTopMenuRows"]);
	$cols = $GLOBALS["bTopMenuExists"] % $GLOBALS["gsTopMenuRows"];
	$BlankColumns = $GLOBALS["gsTopMenuRows"] - $cols;
	if ($BlankColumns == $GLOBALS["gsTopMenuRows"]) { $BlankColumns = 0; }

	$separator_bar = explode('?',$GLOBALS["gsTopMenuSeparator"]);
	if ($GLOBALS["topgroups"]) { reset($GLOBALS["topgroups"]); }

	$linecount = 1;
	$rowcount = 1;
	$barcount = 0;
	if (($talign == 'right') && ($linecount == $lines)) {
		for ($i = 1; $i < $BlankColumns; $i++) {
			echo '<td class="topmenu"></td>';
			if (trim($separator_bar[2]) != '') { echo '<td class="topmenu"></td>'; }
		}
	}
	if (trim($separator_bar[0]) != '') { echo '<td align="center" valign="top">'.trim($separator_bar[0]).'</td>'; }
	while (list($topi,$val) = each($GLOBALS["topgroups"])) {
		if (($GLOBALS["gsPrivateMenus"] == 'L') || ($GLOBALS["topgroups"][$topi]["loginreq"] != 'Y') || ($EZ_SESSION_VARS["PasswordCookie"] != '')) {
			if ($rowcount > $GLOBALS["gsTopMenuRows"]) {
				if (trim($separator_bar[2]) != '') { echo '<td align="center" valign="top">'.trim($separator_bar[2]).'</td>'; }
				$rowcount = 1;
				$barcount = 0;
				?>
				</tr><tr class="topmenu">
				<?php
				$linecount++;
				if (($linecount == $lines) && ($talign == 'right')) {
					for ($i = 0; $i < $BlankColumns; $i++) {
						echo '<td class="topmenu"></td>';
						if (trim($separator_bar[2]) != '') { echo '<td class="topmenu"></td>'; }
						if ((trim($separator_bar[1]) != '') && (trim($separator_bar[2]) == '')) { echo '<td class="topmenu"></td>'; }
					}
				}
				if (trim($separator_bar[0]) != '') { echo '<td align="center" valign="top">'.trim($separator_bar[0]).'</td>'; }
			}
			if (($barcount != 0) && (trim($separator_bar[1]) != '')) { echo '<td align="center" valign="top">'.trim($separator_bar[1]).'</td>'; }
			?>
			<td align="<?php echo $talign; ?>" valign="top">
			<?php
			$GLOBALS["topgroups"][$topi]["topgrouplink"] = privatemenu($GLOBALS["topgroups"][$topi]["loginreq"],$GLOBALS["topgroups"][$topi]["usergroups"],$GLOBALS["topgroups"][$topi]["topgrouplink"]);
			$menustring = '<a class="topmenulink" ';
			if ($GLOBALS["gsMenuHover"] == "Y") {
				if ($GLOBALS["topgroups"][$topi]["tophovertitle"] != '') { $hovertext= $GLOBALS["topgroups"][$topi]["tophovertitle"];
				} else { $hovertext= $GLOBALS["topgroups"][$topi]["topgroupdesc"]; }
				$menustring .= 'title="'.$hovertext.'" ';
			}
			$menustring .= 'href="';
			if ($GLOBALS["topgroups"][$topi]["topgrouplink"] == '') {
				if (($GLOBALS["gsUseFrames"] == 'Y') && ($EZ_SESSION_VARS["noframesbrowser"] != True)) {
					$menustring .= 'javascript:ChangeFrames(\'topmenu\' + currentTopMenu,\''.$GLOBALS["topgroups"][$topi]["topgroupname"].'\');"';
				} else {
					$menustring .= BuildLink('control.php').'&topgroupname='.$GLOBALS["topgroups"][$topi]["topgroupname"].'"';
				}
			} else {
				if (($GLOBALS["gsUseFrames"] == 'Y') && ($EZ_SESSION_VARS["noframesbrowser"] != True)) {
					if (($GLOBALS["topgroups"][$topi]["topopeninpage"] == 'Y') || ($GLOBALS["topgroups"][$topi]["topgrouplink"] == 'loginreq.php') || ($GLOBALS["topgroups"][$topi]["topgrouplink"] == 'loginreq2.php')) {
						if ((substr($GLOBALS["topgroups"][$topi]["topgrouplink"],0,7) == 'http://') || (substr($GLOBALS["topgroups"][$topi]["topgrouplink"],0,8) == 'https://')) {
							$menustring .= $GLOBALS["topgroups"][$topi]["topgrouplink"].'" target="contents"';
						} else {
							$menustring .= BuildLink('module.php').'&link='.$GLOBALS["topgroups"][$topi]["topgrouplink"].'&topgroupname='.$GLOBALS["topgroups"][$topi]["topgroupname"].'" target="contents"';
						}
					} else {
						$menustring .= $GLOBALS["topgroups"][$topi]["topgrouplink"].'" target="_blank"';
					}
				} else {
					if (($GLOBALS["topgroups"][$topi]["topopeninpage"] == 'Y') || ($GLOBALS["topgroups"][$topi]["topgrouplink"] == 'loginreq.php') || ($GLOBALS["topgroups"][$topi]["topgrouplink"] == 'loginreq2.php')) {
						if ((substr($GLOBALS["topgroups"][$topi]["topgrouplink"],0,7) == 'http://') || (substr($GLOBALS["topgroups"][$topi]["topgrouplink"],0,8) == 'https://')) {
							$menustring .= $GLOBALS["topgroups"][$topi]["topgrouplink"].'"';
						} else {
							$menustring .= BuildLink('control.php').'&link='.$GLOBALS["topgroups"][$topi]["topgrouplink"].'&topgroupname='.$GLOBALS["topgroups"][$topi]["topgroupname"].'"';
						}
					} else {
						$menustring .= $GLOBALS["topgroups"][$topi]["topgrouplink"].'" target="_blank"';
					}
				}
			}
			if ($GLOBALS["topgroups"][$topi]["topmenuimage1"] == '') {
				$menustring .= BuildLinkMouseOver($GLOBALS["topgroups"][$topi]["topgroupdesc"]).'>';
				$menustring .= $GLOBALS["topgroups"][$topi]["topgroupdesc"].'</a>';
			} else {
				$menustring .= ' onMouseOver="changeTopMenuImageOver(\'topmenu'.$GLOBALS["topgroups"][$topi]["topgroupname"].'\',\''.$GLOBALS["topgroups"][$topi]["topgroupname"].'\'); window.status=\''.str_replace("'","\'",$GLOBALS["topgroups"][$topi]["topgroupdesc"]).'\'; return true;"';
				$menustring .= ' onMouseOut="changeTopMenuImageOut(\'topmenu'.$GLOBALS["topgroups"][$topi]["topgroupname"].'\',\''.$GLOBALS["topgroups"][$topi]["topgroupname"].'\'); window.status=\'\'; return true;">';
				if (($GLOBALS["topgroups"][$topi]["topgroupname"] == $menuref) && ($GLOBALS["topgroups"][$topi]["topmenuimage3"] != '')) {
					$menustring .= '<img src="./'.$GLOBALS["image_home"].$GLOBALS["topgroups"][$topi]["topmenuimage3"].'" border="0" name="topmenu'.$GLOBALS["topgroups"][$topi]["topgroupname"].'"></a>';
				} else {
					$menustring .= '<img src="./'.$GLOBALS["image_home"].$GLOBALS["topgroups"][$topi]["topmenuimage1"].'" border="0" name="topmenu'.$GLOBALS["topgroups"][$topi]["topgroupname"].'"></a>';
				}
			}
			if (($GLOBALS["topgroups"][$topi]["loginreq"] == 'Y') && ($GLOBALS["gsSecureIcon"] != '')) {
				$menustring .= '&nbsp;&nbsp;'.imagehtmltag($GLOBALS["image_home"],$GLOBALS["gsSecureIcon"],$GLOBALS["tPrivateOption"],0,'');
			}

			echo $menustring;
			$barcount++;
			$rowcount++;
			?>
			</td>
			<?php
		}
	}
	if (trim($separator_bar[2]) != '') { echo '<td align="center" valign="top">'.trim($separator_bar[2]).'</td>'; }
	if ($talign != 'right') {
		for ($i = 0; $i < $BlankColumns; $i++) {
			echo '<td class="topmenu"></td>';
			if (trim($separator_bar[2]) != '') { echo '<td class="topmenu"></td>'; }
			if ((trim($separator_bar[1]) != '') && (trim($separator_bar[2]) == '')) { echo '<td class="topmenu"></td>'; }
		}
	}
} // function RenderTopGroups()




function ReadTopMenuStructure()
{
	$topi = 0;
	// We always list all menu items in the default site language; but if the user language is different we
	//		include any menu items in that language as well, sorted so that the user language items will be
	//		processed first.... then we filter out the default site language items when we transfer the
	//		retrieved list to the $GLOBALS array to avoid duplication.
	if ($GLOBALS["gsLanguage"] == $GLOBALS["gsDefault_language"]) {
		$strQuery = "SELECT * FROM ".$GLOBALS["eztbTopgroups"]." WHERE topmenuvisible='Y' AND language='".$GLOBALS["gsLanguage"]."' ORDER BY topgrouporderid";
	} else {
		$lOrder = '';
		if ($GLOBALS["gsLanguage"] > $GLOBALS["gsDefault_language"]) { $lOrder = ' DESC'; }
		$strQuery = "SELECT * FROM ".$GLOBALS["eztbTopgroups"]." WHERE topmenuvisible='Y' AND (language='".$GLOBALS["gsLanguage"]."' OR language='".$GLOBALS["gsDefault_language"]."') ORDER BY topgrouporderid,language".$lOrder;
	}
	$result = dbRetrieve($strQuery,true,0,0);

	$nTopGroupName = '';
	while ($rs = dbFetch($result)) {
		//  Suppress duplicates (ensuring we only get the appropriate language versions)
		if ($rs["topgroupname"] != $nTopGroupName) {
			$nTopGroupName = $rs["topgroupname"];
			//  Suppress hidden entries if the user doesn't have privilege to see them.
			//  Locked entries are filtered by the RenderTopGroups() function
			if (!hiddenmenu($rs["loginreq"],$rs["usergroups"])) {
				$GLOBALS["topgroups"][$topi]["topgroupname"]	= $rs["topgroupname"];
				$GLOBALS["topgroups"][$topi]["topgroupdesc"]	= $rs["topgroupdesc"];
				$GLOBALS["topgroups"][$topi]["topgrouplink"]	= $rs["topgrouplink"];
				$GLOBALS["topgroups"][$topi]["topgrouporderid"] = $rs["topgrouporderid"];
				$GLOBALS["topgroups"][$topi]["topmenuimage1"]	= $rs["topmenuimage1"];
				$GLOBALS["topgroups"][$topi]["topmenuimage2"]	= $rs["topmenuimage2"];
				$GLOBALS["topgroups"][$topi]["topmenuimage3"]	= $rs["topmenuimage3"];
				$GLOBALS["topgroups"][$topi]["topmenuimage4"]	= $rs["topmenuimage4"];
				$GLOBALS["topgroups"][$topi]["topmenuvisible"]  = $rs["topmenuvisible"];
				$GLOBALS["topgroups"][$topi]["topmenuorderby"]  = $rs["topmenuorderby"];
				$GLOBALS["topgroups"][$topi]["topmenuorderdir"] = $rs["topmenuorderdir"];
				$GLOBALS["topgroups"][$topi]["tophovertitle"]	= $rs["tophovertitle"];
				$GLOBALS["topgroups"][$topi]["topopeninpage"]	= $rs["topopeninpage"];
				$GLOBALS["topgroups"][$topi]["loginreq"]		= $rs["loginreq"];
				$GLOBALS["topgroups"][$topi]["usergroups"]		= $rs["usergroups"];
				$topi++;
			} // hidden filter
		} // duplicate filter
	}
	dbFreeResult($result);
	return $topi;
} // function ReadTopMenuStructure()

?>
