<?php include("config.php"); ?>
<html>
<head>
<style type="text/css">
<!--

body {
 color:#fff;
 margin: 0;
 padding: 0;
 font-size: 18px;
 font-weight: bold
}

#mainMenu {
 position: absolute;
 left: 0px;
 top: 0px;

 width: 510px;
 height: 420px;
 overflow:auto;
}

#mainMenu a {
 font-size: 18px;
 color: #fff;
 text-decoration: none;
 font-weight: bold
}

.menuBar {
 background-image: url('./design/menuBar.png');
 height: 20px;
 width: 479px;
 padding: 5px;
 margin-bottom: 5px;
line-height: 1;
}

.menuBar input {
 border: 0;
 background-color: #808481;
 color: #fff;
 font-size: 18px;
 color: #fff;
 font-weight: bold;
 width: 479px;
 margin-top: -3px;
 margin-left: 3px;
}

.menuBar_s {

 background-image: url('./design/menuBar_selected.png');
 height: 20px;
 width: 479px;
 padding: 5px;
 margin-bottom: 5px;
 line-height: 1;
}

.menuBar_s input {
 border: 0;
 background-color: #808481;
 color: #fff;
 font-size: 18px;
 color: #fff;
 font-weight: bold;
 width: 479px;
 margin-top: -3px;
 margin-left: 3px;
}

-->
</style>

<script type="text/javascript">
/* Menu items */
var mRoot = '<?php echo $globalRoot; ?>';
var mMenuSelected = 0;
var mMenuScroll = 0;

function load()
{
	goDown();
}

function menuItems()
{
        return document.getElementById( "mainMenu" ).childNodes;
}

function goUp()
{
        var list = menuItems();

        if(mMenuSelected > 1)
        {
                list[mMenuSelected-1].className = 'menuBar_s';
                list[mMenuSelected].className = 'menuBar';

                mMenuSelected--;
        }

        if(mMenuScroll <= 1)
        {
                document.getElementById("mainMenu").scrollTop -=  35;

        }
        else
        {
                mMenuScroll--;
        }
}

function goDown()
{
        var list = menuItems();

        if(mMenuSelected < list.length)
        {
                list[mMenuSelected+1].className = 'menuBar_s';
                list[mMenuSelected].className = 'menuBar';

                mMenuSelected++;
        }


        if(mMenuScroll >= 12)
        {
                document.getElementById("mainMenu").scrollTop +=  35;

        }
        else
        {
                mMenuScroll++;
        }
}

function goBack()
{
				var list = menuItems();
				if(list[1].childNodes[0].value == "[ .. ]")
				{
	                var newLocation = mRoot + "/listDir.php?subdir=" + list[1].childNodes[0].name;
	                parent.mainFrame.location = newLocation;
				}
}

function selectOk()
{
        var list = menuItems();
        var node = list[mMenuSelected].childNodes[0].parentNode;
		
		// Folder
        if(node.innerHTML.substring(0,2) == "[ " && node.innerHTML.substring(node.innerHTML.length-2,node.innerHTML.length) == " ]")
        {
                newLocation(mRoot + "/listDir.php?subdir=" + node.title);
        }
        // File
        else
        {
                top.preRenderVideo(node.title, node.innerHTML);
        }
}

function newLocation(location)
{
                parent.mainFrame.location = location;
}
</script>

</head>
<body onLoad="load()">
<div id="mainMenu">
<?php

include("config.php");

// Favourites
if(isset($_GET['favourites']))
{
        printFavourites($dirFavourites);
}
// Sub dir
else if(isset($_GET['subdir']))
{
        printDir($_GET['subdir']);
}
else if(isset($_GET['fotos']))
{
        printDir($_GET['subdir'],"foto");
}
// Standard dir
else if(!isset($_GET['subdir']))
{
        printDir($dirMain);
}

?>
</div>
</body>
</html>
<?php

function short($text)
{
        $short = 32;
        $returnString = $text;

        if(strlen($text) > $short)
        {
			$extension = "";
				if(substr($text,-4,1) == ".")
				{
					$extension = substr($text,-3);
				}
                $returnString = substr($text, 0, $short)."...".$extension;
        }

        return $returnString;
}


function listDir($sDir)
{
        $type = array("dir" => array(), "file" => array());

        preg_match('~(.*)/.*$~',$sDir,$vLink); // Make so you can go back to previous folder

        if(file_exists($sDir)) // If folder exists
        {
                if ($sHandle = opendir($sDir)) // Open folder
                {
                        while (false !== ($sFile = readdir($sHandle))) // Loop folder
                        {
                                if ($sFile != '.') // Hiding Files/Folders
                                {

                                        if($sFile == '..' || is_dir($sDir.'/'.$sFile))
                                        {
                                                $type['dir'][] = $sFile;
                                        }
                                        else
                                        {
                                                $type['file'][] = $sFile;
                                        }
                                }
                        }
                closedir($sHandle); // Close folder
                }
        }

        return $type;
}

function printFavourites($favourite)
{
	foreach($favourite as $key => $val)
	{
		echo "<div class=\"menuBar\" title=\"{$val}\">[ ".short($key)." ]</div>";
	}
}

function printDir($sDir,$tipo="pelicula")
{
	$type = listDir($sDir);
	for($i=0;$i<count($type['dir']);$i++)
	{
		echo "<div class=\"menuBar\" title=\"{$sDir}/{$type['dir'][$i]}\">[ ".short($type['dir'][$i])." ]</div>";
	}

	for($i=0;$i<count($type['file']);$i++)
	{
		switch($tipo)
		{
			case "pelicula":
				if( (substr($type['file'][$i],-4) == ".mpg") ||
					(substr($type['file'][$i],-4) == ".avi") ||
					(substr($type['file'][$i],-5) == ".mpeg") )	echo "<div class=\"menuBar\" title=\"{$sDir}/{$type['file'][$i]}\">".short($type['file'][$i])."</div>";
				break;
			case "foto":
				if( (substr($type['file'][$i],-4) == ".bmp") ||
					(substr($type['file'][$i],-4) == ".jpg") ||
					(substr($type['file'][$i],-5) == ".gif") )	echo "<div class=\"menuBar\" title=\"{$sDir}/{$type['file'][$i]}\">".short($type['file'][$i])."</div>";
				break;
		}
	}
}

function urlFix($url)
{
        $find = array('Å','å','Ä','ä','Ö','ö',' ');
        $replace = array('%C5','%E5','%C4','%E4','%D6','%F6','%20');

        $url = str_replace($find,$replace,$url);

        return $url;
}
?>
