<?php
include($O__libPath . "/functions/server/O__backupData.php");

// Load settings
$O__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/configs/settings.xml", 0, 0);
$O__defaultInput = O__XMLgetData($O__fileData,"/settings/setting[@id='defaultInput']/value");
$O__scrollAmount = O__XMLgetData($O__fileData,"/settings/setting[@id='scrollAmount']/value");
$O__menuTextLength = O__XMLgetData($O__fileData,"/settings/setting[@id='menuTextLength']/value");
$O__fadeRate = O__XMLgetData($O__fileData,"/settings/setting[@id='fadeRate']/value");
$O__pathIndexers = $O__DBpath . "/pageTypes/" . $O__PT . O__XMLgetData($O__fileData,"/settings/setting[@id='storagePathIndexers']/value");
$O__pathMenus = $O__DBpath . "/pageTypes/" . $O__PT . O__XMLgetData($O__fileData,"/settings/setting[@id='storagePathMenus']/value");
unset($O__fileData);

$O__yLength = 0;
$O__menu = $O__URLpath[1];

function O__indexer(){
	global $O__DBpath, $O__PT, $O__pathMenus, $O__fileData, $O__menu;
	
	// Load settings
	$O__backup = O__XMLgetData($O__fileData,"/indexer/setting[@id='backup']/value");
	$O__sort = O__XMLgetData($O__fileData,"/indexer/setting[@id='sort']/value");
	$O__pathFiles = O__XMLgetData($O__fileData,"/indexer/setting[@id='pathFiles']/value");
	$O__pathImgs = O__XMLgetData($O__fileData,"/indexer/setting[@id='pathImgs']/value");
	$O__pathImgHref = O__XMLgetData($O__fileData,"/indexer/setting[@id='pathImgsHref']/value");
	$O__imgDefault = O__XMLgetData($O__fileData,"/indexer/setting[@id='imgDefault']/value");
	$O__pathLauncher = O__XMLgetData($O__fileData,"/indexer/setting[@id='pathLauncher']/value");
	$O__pathLauncherBin = O__XMLgetData($O__fileData,"/indexer/setting[@id='pathLauncherBin']/value");
	$O__launcherBinArgs = O__XMLgetData($O__fileData,"/indexer/setting[@id='launcherBinArgs']/value");
	$O__wait = O__XMLgetData($O__fileData,"/indexer/setting[@id='wait']/value");
	unset($O__fileData);
	
	// Build arrays of files and associated images
	$O__filesArray = O__dirToArray($O__pathFiles);
	$O__imgArray = O__dirToArray($O__pathImgs);
	
	// Sorting
	if ($O__sort == 1){ sort($O__filesArray); 
		} else if ($O__sort == 2){ rsort($O__filesArray); }
	
	// Create new XML
	$O__dataXML = new DomDocument("1.0", "UTF-8");
	$O__XMLroot = $O__dataXML->createElement("menu", "");	

	// Add "back" menu item to menu	
	$O__menuItem = $O__dataXML->createElement("item", "");
		$O__menuItem->appendChild($O__dataXML->createElement("active", "1"));
		$O__menuItem->appendChild($O__dataXML->createElement("name", "Back"));
		$O__menuItem->appendChild($O__dataXML->createElement("info", "Go to previous screen."));
		$O__menuItem->appendChild($O__dataXML->createElement("href", "javascript:history.go(-1);"));
		$O__menuItem->appendChild($O__dataXML->createElement("path", ""));
		$O__menuItem->appendChild($O__dataXML->createElement("bin", ""));
		$O__menuItem->appendChild($O__dataXML->createElement("args", ""));
		$O__menuItem->appendChild($O__dataXML->createElement("wait", ""));
		$O__menuItem->appendChild($O__dataXML->createElement("img", "/files/images/back_600x600.jpg"));
	$O__XMLroot->appendChild($O__menuItem); // add menu item to parent			

	// Add indexed items
	for($i=0; $i<count($O__filesArray); $i++){
		$O__filename = pathinfo($O__filesArray[$i][0], PATHINFO_FILENAME);
		$O__imgPath = $O__imgDefault;
		
		for($j=0;$j<count($O__imgArray);$j++){ // Check to see if there is an image for it
			$O__imgName = pathinfo($O__imgArray[$j][0], PATHINFO_FILENAME);
			
			if ($O__imgName == $O__filename){
				$O__imgPath = $O__pathImgHref . "/" . $O__imgArray[$j][0];
			}
		}
		
		$O__menuItem = $O__dataXML->createElement("item", "");
			$O__menuItem->appendChild($O__dataXML->createElement("active", "1"));
			$O__menuItem->appendChild($O__dataXML->createElement("name", $O__filename));
			$O__menuItem->appendChild($O__dataXML->createElement("info", "Launch " . $O__filename));
			$O__menuItem->appendChild($O__dataXML->createElement("href", ""));
			$O__menuItem->appendChild($O__dataXML->createElement("path", $O__pathLauncher));
			$O__menuItem->appendChild($O__dataXML->createElement("bin", $O__pathLauncherBin));
			$O__menuItem->appendChild($O__dataXML->createElement("args", $O__launcherBinArgs  . " " . $O__pathFiles . "/" . $O__filesArray[$i][0]));
			$O__menuItem->appendChild($O__dataXML->createElement("wait", $O__wait));
			$O__menuItem->appendChild($O__dataXML->createElement("img", $O__imgPath));
		$O__XMLroot->appendChild($O__menuItem); // add menu item to parent
		
	}
	
	// Wrap up and save XML
	$O__dataXML->appendChild($O__XMLroot);
	if ($O__backup == 1) { O__backupData($O__pathMenus . "/" . $O__menuList . ".xml",0); }
	O__XMLdata($O__pathMenus . "/" . $O__menu . ".xml", $O__dataXML, 0);
	clearstatcache(); // clear cache so newest version can be loaded

}

function O__createOutput(){
	global $O__pathMenus, $O__menu, $O__yLength;
	
	// Load menu data
	$O__fileData = O__XMLdata($O__pathMenus . "/" . $O__menu . ".xml", 0, 0);
	$O__output = "";		
				
	for ($i=0; $i< count($O__fileData); $i++){
		$O__menuActive = $O__fileData->item[$i]->active;
				
		if ($O__menuActive == 1){
			$O__menuName = $O__fileData->item[$i]->name;
			$O__menuInfo = $O__fileData->item[$i]->info;
			$O__menuHref = $O__fileData->item[$i]->href;
			$O__menuBinPath = $O__fileData->item[$i]->path;
			$O__menuBin = $O__fileData->item[$i]->bin;
			$O__menuBinArgs = $O__fileData->item[$i]->args;
			$O__menuWait = $O__fileData->item[$i]->wait;
			$O__menuImg = $O__fileData->item[$i]->img;
		
			$O__output .= "<div id=\"O__itemContainer" . $i . "\" class=\"O__menuItem\" onmouseover=\"O__selectItem(" . $i . ");\" data-info=\"" . $O__menuInfo . "\">";
			$O__output .= "<a href=\"javascript:O__prepAppInfo('" . $O__menuName . "','" . $O__menuInfo . "','" . $O__menuHref . "','" . $O__menuBinPath . "','" . $O__menuBin . "','" . $O__menuBinArgs . "','" . $O__menuWait . "','" . $O__menuImg . "');\" id=\"link" . $i . "\">";
			$O__output .= "<div class=\"O__menuItemImage\"><img src=\"" . $O__menuImg . "\" width=\"30\" height=\"30\" vspace=\"5\" border=\"0\" name=\"menuItemImage" . $i . "\"></div>";
			$O__output .= "<div class=\"O__menuItemText\" id=\"menuItem" . $i . "\">" . $O__menuName . "</div>";
			$O__output .= "</a></div>";	
		}
	}
	
	$O__yLength = count($O__fileData);
	
	//Add form to process app launch
	$O__output .= "<form name=\"O__appInfo\" action=\"/O__launch\" method=\"post\">";
	$O__output .= "<input name=\"O__appName\" type=\"hidden\" value=\"\">";
	$O__output .= "<input name=\"O__appPathStart\" type=\"hidden\" value=\"\">";
	$O__output .= "<input name=\"O__appBin\" type=\"hidden\" value=\"\">";
	$O__output .= "<input name=\"O__appParameters\" type=\"hidden\" value=\"\">";
	$O__output .= "<input name=\"O__appWaitTime\" type=\"hidden\" value=\"\">";
	$O__output .= "<input name=\"O__appImage\" type=\"hidden\" value=\"\">";
	$O__output .= "</form>";
	
	return $O__output;
}	

if(file_exists($O__pathIndexers . "/" . $O__menu. ".xml")){	
	// Load indexer settings
	$O__fileData = O__XMLdata($O__pathIndexers . "/" . $O__menu . ".xml", 0, 0);
	$O__auto = O__XMLgetData($O__fileData,"/indexer/setting[@id='auto']/value");
	if ($O__auto == 0) {
			if (isset($O__PTATinputs["reindex"])) { O__indexer(); }
		} else if ($O__auto == 1) { 
			O__indexer(); 
		} else { 
			unset($O__fileData); 
		}
}

$O__output = O__createOutput();
?>

<script>
var O__menuItemsText = new Array();
var O__scrollAmount = <?php echo($O__scrollAmount); ?>;
var O__menuTextLength = <?php echo($O__menuTextLength); ?>;
var O__position = 0;
var O__lastPosition = 0;
var O__xLength = 0;
var O__yLength = <?php echo($O__yLength); ?>;
var	O__menuImgArea = "O__menuImageDisplay";
var	O__menuInfoArea = "O__menuItemDesc";
var	O__menuDisplayArea = "O__menuItemContainer";

function O__selectItem(O__positionInput){	
	// Change to position of selected item
	O__lastPosition = O__position;
	O__position = O__positionInput;
	
	// Highlight selected item
	document.getElementById("O__itemContainer" + O__lastPosition).className = "O__menuItem";
	document.getElementById("O__itemContainer" + O__position).className = "O__menuItemSelected";

	// Swap visuals
	document.getElementById(O__menuImgArea).src = eval("document.menuItemImage" + O__position + ".src");
		document.getElementById(O__menuImgArea).height = document.getElementById(O__menuImgArea).width; // match img height with width
	document.getElementById(O__menuInfoArea).innerHTML = document.getElementById("O__itemContainer" + O__position).getAttribute("data-info");
}

function O__move(direction){
	var directionMath;
	
	// Move selected item
	if (direction == "d") { directionMath = 1; } else { directionMath = -1; }
	var O__positionNext = O__position + directionMath;
	if(!(O__positionNext < 0) && !(O__positionNext > (O__yLength-1))){ O__selectItem(O__positionNext); } // select item if within range
	
	// Scroll menu display area
	document.getElementById(O__menuDisplayArea).scrollTop = document.getElementById(O__menuDisplayArea).scrollTop + (O__scrollAmount * directionMath);
}

function O__keyPress(O__value) {
	var O__temp = O__value.which;
	if(O__temp==38){ O__move("u", O__position); } //arrow up
	if(O__temp==40){ O__move("d", O__position); } //arrow down
	//if(O__temp==37){ O__move("l", O__position); } //arrow left
	//if(O__temp==39){ O__move("r", O__position); } //arrow right
	//if(O__temp==32){ O__move("", O__position); } //spacebar
	if(O__temp==13){ location.href=document.getElementById("link" + O__position).href; } //enter key
}

function O__prepAppInfo(title,desc,URL,path,bin,param,wait,img){
	if(URL!=""){
		location.href = URL;
	} else {
		document.O__appInfo.O__appName.value = title;
		document.O__appInfo.O__appPathStart.value = path;
		document.O__appInfo.O__appBin.value = bin;
		document.O__appInfo.O__appParameters.value = param;
		document.O__appInfo.O__appWaitTime.value = wait;
		document.O__appInfo.O__appImage.value = img;
		document.O__appInfo.submit();
	}
}

// Set up listeners: select first item on load, key presses, mouse wheel
window.addEventListener("load", function(){ O__selectItem(0); });
document.onkeydown = O__keyPress;
window.addEventListener("DOMMouseScroll", function(O__value){ if (-O__value.detail/3 < 0) { O__move("d",O__position); } else { O__move("u",O__position); }}, false);
</script>
