<?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 == "[ .. ]")
				{
					history.go(-1);
				}
}

function selectOk()
{
        var list = menuItems();
        var node = list[mMenuSelected].childNodes[0].parentNode;
		
		// Go back
		if(node.title == "..")
		{
			history.go(-1);
		}
		// Folder
        else if(node.innerHTML.substring(0,2) == "[ " && node.innerHTML.substring(node.innerHTML.length-2,node.innerHTML.length) == " ]")
        {
            newLocation(mRoot + "/svtPlay.php" + node.title);
        }
        // File
        else
        {
			parent.document.getElementById('url').src = mRoot + "/svtPlayUrl.php?url=" + node.title;
			top.preRenderVlc(node.innerHTML);
        }
}

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

</head>
<body onLoad="load()">
<iframe id="svtPlayUrl" name="svtPlayUrl" src="" height="0" width="0" frameborder="10" marginwidth="0" marginheight="0" scrolling="no"></iframe>
<div id="mainMenu">
<?php

// Start page
if( !isset($_GET['menu']) )
{
	$src = file_get_contents("http://www.svt.se/svt/road/Classic/shared/mediacenter/navigation.jsp?&frameset=true");
}
// Sub menu
else
{	
	$src = file_get_contents("http://www.svt.se/svt/road/Classic/shared/mediacenter/navigation.jsp?d={$_GET['d']}&group={$_GET['g']}");
	echo "<div class=\"menuBar\" title=\"..\">[ .. ]</div>"; // Go back
}
$src = cleanSrc($src);

// Parse Folders
if( !isset($_GET['menu']) )
{
	preg_match_all("~<a\s+href=\"navigation.jsp\?d=(\d+)&amp;group=(\d+)\".*?>(.*?)</a>~", $src, $match);

	for($i=0;$i<count($match[0]);$i++)
	{
		echo "<div class=\"menuBar\" title=\"?d={$match[1][$i]}&g={$match[2][$i]}&menu=true\">[ {$match[3][$i]} ]</div>";
	}	
}
else
{
	preg_match_all("~<a\s+(?:href=\"navigation.jsp\?d=(\d+)\"|onClick=\"shL\(\);\")\s+(?:href=\"navigation.jsp\?d=(\d+)\"|onClick=\"shL\(\);\")>([\w\d\såäöÅÄÖ,;.:-_\!\?&\\\*\"'%#\(\)\[\]\=]+)</a>(?:\s+|)</div>~", $src, $match);
	
	$index = (empty($match[1][0])) ? 2 : 1;
	
	for($i=0;$i<count($match[0]);$i++)
	{
		echo "<div class=\"menuBar\" title=\"?d={$match[$index][$i]}&menu=true\">[ {$match[3][$i]} ]</div>";
	}
}

// Parse videos
preg_match_all("~href=\"/svt/road/Classic/shared/mediacenter/player.jsp\?a=(\d+)&d=(\d+)\" onclick=\"chC\(\d+\);shL\('cont'\);\">(.*?)</a>~", $src, $match);

if(!empty($match[1][0]))
{
	for($i=0;$i<count($match[0]);$i++)
	{
		echo "<div class=\"menuBar\" title=\"{$match[1][$i]}|{$match[2][$i]}\">{$match[3][$i]}</div>";
	}
}

function cleanSrc($src)
{
	return preg_replace("~\r|\n|\t~", "", $src);
}

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