<?php
//define this module's name and path
$mn = basename(__FILE__ , ".php");
include("modules/modules.php");

/*
Available variables:
All system classes
All request variables
$p (the template path)
$mp (the module path)
$sufixID (id sufix for this module's divs)
*/

$marginTop = ($moduleParameters['marginTop'] == "__EMPTY__" || empty($moduleParameters['marginTop'])) ? 0 : $moduleParameters['marginTop'];
$marginRight = ($moduleParameters['marginRight'] == "__EMPTY__" || empty($moduleParameters['marginRight'])) ? 0 : $moduleParameters['marginRight'];
$marginBottom = ($moduleParameters['marginBottom'] == "__EMPTY__" || empty($moduleParameters['marginBottom'])) ? 0 : $moduleParameters['marginBottom'];
$marginLeft = ($moduleParameters['marginLeft'] == "__EMPTY__" || empty($moduleParameters['marginLeft'])) ? 0 : $moduleParameters['marginLeft'];
$paddingTop = ($moduleParameters['paddingTop'] == "__EMPTY__" || empty($moduleParameters['paddingTop'])) ? 0 : $moduleParameters['paddingTop'];
$paddingRight = ($moduleParameters['paddingRight'] == "__EMPTY__" || empty($moduleParameters['paddingRight'])) ? 0 : $moduleParameters['paddingRight'];
$paddingBottom = ($moduleParameters['paddingBottom'] == "__EMPTY__" || empty($moduleParameters['paddingBottom'])) ? 0 : $moduleParameters['paddingBottom'];
$paddingLeft = ($moduleParameters['paddingLeft'] == "__EMPTY__" || empty($moduleParameters['paddingLeft'])) ? 0 : $moduleParameters['paddingLeft'];
$useModuleContainer = ($moduleParameters['useModuleContainer'] == "__EMPTY__" || empty($moduleParameters['useModuleContainer'])) ? 0 : (int)$moduleParameters['useModuleContainer'];
$widthInPixels = ($moduleParameters['widthInPixels'] == "__EMPTY__" || empty($moduleParameters['widthInPixels'])) ? null : (int)$moduleParameters['widthInPixels'];


if(empty($marginTop) && empty($marginRight) && empty($marginBottom) && empty($marginLeft)){
	$Mmargin = "0";
}else{
	$Mmargin = $marginTop." ".$marginRight." ".$marginBottom." ".$marginLeft." ";
}
if(empty($paddingTop) && empty($paddingRight) && empty($paddingBottom) && empty($paddingLeft)){
	$Mpadding = "0";
}else{
	$Mpadding = $paddingTop." ".$paddingRight." ".$paddingBottom." ".$paddingLeft." ";
}

$menuID = "menu".uniqid();

$dataSource = $moduleParameters['dataSource'];
$dataSource = ($dataSource == "auto" || empty($dataSource)) ? $section : $dataSource;
if($system->getTableSectionID($dataSource)) { // only if a valid section
if(!preg_match('/_categories$/', $dataSource)) $dataSource.="_categories"; // apend the _categories sufix if not present


$wrapStartHTML = $moduleParameters["wrapStartHTML"];
$wrapEndHTML = $moduleParameters["wrapEndHTML"];
$overrideCSS = ($moduleParameters['overrideCSS'] == "__EMPTY__") ? 0 : $moduleParameters['overrideCSS'];
$cssContent = $moduleParameters["cssContent"];
$customClass = $moduleParameters["customClass"];
$idCustom = $moduleParameters["idCustom"];
$currentClass = $moduleParameters["currentClass"];
$wrapMenuOption = $moduleParameters["wrapMenuOption"];
$menuType = $moduleParameters["menuType"];
$activeOnly = $moduleParameters["activeOnly"];
$showTitle = ($moduleParameters['showTitle'] == "__EMPTY__") ? 0 : $moduleParameters['showTitle'];
$allowMultiOpen = ($moduleParameters['allowMultiOpen'] == "__EMPTY__") ? 0 : $moduleParameters['allowMultiOpen'];
$accordionSpeed = ($moduleParameters['accordionSpeed'] == "__EMPTY__" || empty($moduleParameters['accordionSpeed'])) ? 0 : $moduleParameters['accordionSpeed'];
$plusIcon = $moduleParameters["plusIcon"];
$minusIcon = $moduleParameters["minusIcon"];
$activeCategories = ($moduleParameters['activeCategories'] == "__EMPTY__" || empty($moduleParameters['activeCategories'])) ? false : true;
$rootOptionsUppercase = ($moduleParameters['rootOptionsUppercase'] == "__EMPTY__" || empty($moduleParameters['rootOptionsUppercase'])) ? false : true;

if($useModuleContainer){

?>

<div id="<?php echo $mn.$sufixID;?>" data-editable="editable">
<?php
if($moduleParameters['moduleAlign'] && $moduleParameters['moduleHR']){ // there are system parameters...
?>
<style type="text/css">
#<?php echo $mn.$sufixID;?>{
	<?php 
	$alignRule = ($moduleParameters['moduleAlign'] == "center") ? "text-align" : "float";
	echo $alignRule.": ".$moduleParameters['moduleAlign'].";".PHP_EOL;
	?>
	margin : <?php echo $Mmargin;?>;
	padding : <?php echo $Mpadding;?>;
<?php
if($widthInPixels){
	?>
	width: <?php echo $widthInPixels;?>px;
	<?php 
}
if($moduleParameters['moduleHR'] == "top" || $moduleParameters['moduleHR'] == "bottom"){
?>
	border-<?php echo $moduleParameters['moduleHR'];?> : 1px solid black;
<?php 
}
?>
}
</style>
<?php 
}
}
echo $variables->restoreModuleValue($wrapStartHTML);
if($showTitle){
	$dataSourceID = $system->getTableSectionID(str_replace("_categories", "", $dataSource));	
	$menuTitle = $system->getTableSectionTitle($dataSourceID);
	?>
	<h3 class="module_menu_title"><?php echo t(str_replace("MENU ", "", $menuTitle));?></h3>
	<?php
}
if($menuType == "accordion"){
$amo = ($allowMultiOpen) ? "accordion:true ," : NULL;	
$templatePlusIcon = $p."images/plus.gif";
$templateMinusIcon = $p."images/minus.gif";
$plusIcon = (is_file($templatePlusIcon)) ? $templatePlusIcon : "images/".$plusIcon;
$minusIcon = (is_file($templateMinusIcon)) ? $templateMinusIcon : "images/".$minusIcon;
?>
<style type="text/css">
#nav_<?php echo $menuID;?> > ul{
	padding: 0;
}
<?php
if($overrideCSS && $cssContent){ // load custom css only if overrideCSS has been selected and a cssContent has been found
	echo $variables->restoreModuleValue($cssContent);
}else{ 
	include("menu_accordion.css.php"); // load accordion menu styles from db 
}
if($rootOptionsUppercase){
?>
#<?php echo $menuID;?> > li > a{
	text-transform: uppercase !important;
}
<?php	
}
if($activeCategories){
?>
#nav_<?php echo $menuID;?>{
	display:none;
}
<?php	
}
?>
</style>
<script type="text/javascript" src="js/multilevel-accordion.js"></script> 
<script language="JavaScript">
$(document).ready(function(){
	

	
<?php

if($activeCategories){
	if(preg_match('/^menu_/', $dataSource)){ // it's a menu, parse for callMenu_ string part
	?>

	$('#<?php echo $menuID;?> [id^="callMenu_"]').each(function(){
		var dataSource = $(this).attr("id").replace("callMenu_", "") + "_categories";
		var activeCats = <?php echo $menuID;?>.getActiveCategories(dataSource);
		$(this).find('li').each(function(){
			if(!<?php echo $menuID;?>.inArray($(this).attr("data-optionid"), activeCats)) $(this).hide();
		});
	});
	<?php	
	}else{ // native categories
	?>
	$('#<?php echo $menuID;?>').each(function(){
		var dataSource = "<?php echo $dataSource;?>";
		var activeCats = <?php echo $menuID;?>.getActiveCategories(dataSource);
		$(this).find('li').each(function(){
			if(!<?php echo $menuID;?>.inArray($(this).attr("data-optionid"), activeCats)) $(this).hide();
		});
	});		
	<?php	
	}
	?>
	$('#nav_<?php echo $menuID;?>').show(); // show the menu after rendering the active options
<?php
}

?>	
	$("#nav_<?php echo $menuID;?>").accordion({ 
		<?php echo $amo;?>speed: <?php echo $accordionSpeed;?>, 
		closedSign: '<img src="<?php echo $plusIcon;?>">', 
		openedSign: '<img src="<?php echo $minusIcon;?>">'
	});
	
	
});

// NAMESPACE for this module instance
;<?php echo $menuID;?> = {
	getActiveCategories: function(dataSource){
		window.activeCats = [];
		if(!dataSource) return window.activeCats;
		jQuery.ajax({
			type: "POST",
			url: "<?php echo $mp;?>getActiveCategories.php",
			data: "dataSource=" + dataSource,
			async: false,
			success: function(response){
				//console.log(response);
				var cats = response.trim();
				window.activeCats = cats.split(",");
			}
		});
		return window.activeCats;	
	},
	
	inArray: function(needle, haystack) {
		var length = haystack.length;
		for(var i = 0; i < length; i++) {
			if(haystack[i] == needle) return true;
		}
		return false;
	}
};


</script>
<nav id="nav_<?php echo $menuID;?>">
<?php
	$menu->displayMenu("accordion", $dataSource, false, false, NULL, $menuID, false);	
}
?>
</nav>
<?php


if($menuType == "custom"){
	global $optionsHTML;
	$optionsHTML = array();
	$menu->displayMenu(NULL, $dataSource, true, false, $customClass, $idCustom, false); // get the global array $optionsHTML with html markup as an unordered list
	$markup = $optionsHTML;
	$tags = explode("option", $wrapMenuOption);
	$startTag = ($tags[0]) ? $tags[0] : NULL ;
	$endTag = ($tags[1]) ? $tags[1] : NULL ;
	$tabs = -8;	
	$LItabs = 0;
	foreach($markup as $line){
		if($currentClass) $line = preg_replace('/current/', $currentClass, $line);
		$line = preg_replace('/--startWrapText--/', $startTag, $line);
		$line = preg_replace('/--endWrapText--/', $endTag, $line);
		if(preg_match('/^\<li/', ltrim($line)) && preg_match('/^<ul/', $prev)){
			$LItabs+=8;
		}
		if(preg_match('/^\<\/li/', ltrim($line)) && $prev == "</ul>"){
			$LItabs-=8;
			echo str_pad("", $LItabs, " ", STR_PAD_LEFT);
		}		
		if(preg_match('/^\<ul/', $line)){
			$tabs+=8;
			echo PHP_EOL;
			echo str_pad("", $tabs, " ", STR_PAD_LEFT);
		}
		if($line) echo $line;
		$prev = trim($line);
	}
}
echo $variables->restoreModuleValue($wrapEndHTML);


if($useModuleContainer ){
?>
</div>
<?php
}
}
?>