#!/usr/local/bin/Resource/www/cgi-bin/php
<?php echo "<?xml version='1.0' encoding='UTF8' ?>"; ?>
<rss version="2.0">
<onEnter>
  startitem = "middle";
  setRefreshTime(1);
</onEnter>

<onRefresh>
  setRefreshTime(-1);
  itemCount = getPageInfo("itemCount");
</onRefresh>

<mediaDisplay name="threePartsView"
	sideLeftWidthPC="0"
	sideRightWidthPC="0"
	
	headerImageWidthPC="0"
	selectMenuOnRight="no"
	autoSelectMenu="no"
	autoSelectItem="no"
	itemImageHeightPC="0"
	itemImageWidthPC="0"
	itemXPC="8"
	itemYPC="25"
	itemWidthPC="45"
	itemHeightPC="8"
	capXPC="8"
	capYPC="25"
	capWidthPC="45"
	capHeightPC="64"
	itemBackgroundColor="255:127:36"
	itemPerPage="8"
  itemGap="0"
	bottomYPC="90"
	backgroundColor="255:127:36"
	showHeader="no"
	showDefaultInfo="no"
	imageFocus=""
	sliding="no" idleImageXPC="5" idleImageYPC="5" idleImageWidthPC="8" idleImageHeightPC="10"
>

  	<text align="center" offsetXPC="0" offsetYPC="0" widthPC="100" heightPC="20" fontSize="30" backgroundColor="139:155:85" foregroundColor="255:255:255">
		  <script>getPageInfo("pageTitle");</script>
		</text>

  	<text redraw="yes" offsetXPC="85" offsetYPC="12" widthPC="10" heightPC="6" fontSize="20" backgroundColor="139:155:85" foregroundColor="255:255:255">
		  <script>sprintf("%s / ", focus-(-1))+itemCount;</script>
	</text>
	
	
		<text align="center" redraw="yes"
          lines="8" fontSize=15
		      offsetXPC=55 offsetYPC=70 widthPC=40 heightPC=26
		      backgroundColor=139:155:85 foregroundColor=255:255:255>
			<script>print(annotation); annotation;</script>
		</text>

		<image  redraw="yes" offsetXPC=55 offsetYPC=30 widthPC=40 heightPC=39>
  <script>print(img); img;</script>
		</image>
		
		<image offsetXPC=1 offsetYPC=3 widthPC=21 heightPC=14>
<script>channelImage;</script>

<widthPC>
<script>
          41 * screenYp / screenXp;
</script>
</widthPC>

<offsetXPC>
<script>
          1 + 55 * (1 - screenYp / screenXp) / 2;
</script>
</offsetXPC>
</image>

<image offsetXPC=1 offsetYPC=19 widthPC=21 heightPC=14>
<script>channelImage1;</script>

<widthPC>
<script>
          30 * screenYp / screenXp;
</script>
</widthPC>

<offsetXPC>
<script>
          13 + 99 * (1 - screenYp / screenXp) / 2;
</script>
</offsetXPC>
</image>

		
		<idleImage> image/POPUP_LOADING_01.png </idleImage>
		<idleImage> image/POPUP_LOADING_02.png </idleImage>
		<idleImage> image/POPUP_LOADING_03.png </idleImage>
		<idleImage> image/POPUP_LOADING_04.png </idleImage>
		<idleImage> image/POPUP_LOADING_05.png </idleImage>
		<idleImage> image/POPUP_LOADING_06.png </idleImage>
		<idleImage> image/POPUP_LOADING_07.png </idleImage>
		<idleImage> image/POPUP_LOADING_08.png </idleImage>

		<itemDisplay>
			<text align="left" lines="1" offsetXPC=0 offsetYPC=0 widthPC=100 heightPC=100>
				<script>
					idx = getQueryItemIndex();
					focus = getFocusItemIndex();
					if(focus==idx)
					{
                      img = getItemInfo(idx,"image");
					  annotation = getItemInfo(idx, "annotation");
					  durata = getItemInfo(idx, "durata");
					  pub = getItemInfo(idx, "pub");
					  titlu = getItemInfo(idx, "title");
					}
					getItemInfo(idx, "title");
				</script>
				<fontSize>
  				<script>
  					idx = getQueryItemIndex();
  					focus = getFocusItemIndex();
  			    if(focus==idx) "16"; else "14";
  				</script>
				</fontSize>
			  <backgroundColor>
  				<script>
  					idx = getQueryItemIndex();
  					focus = getFocusItemIndex();
  			    if(focus==idx) "139:155:85"; else "-1:-1:-1";
  				</script>
			  </backgroundColor>
			  <foregroundColor>
  				<script>
  					idx = getQueryItemIndex();
  					focus = getFocusItemIndex();
  			    if(focus==idx) "255:255:255"; else "255:255:255";
  				</script>
			  </foregroundColor>
			</text>

		</itemDisplay>

<onUserInput>
<script>
ret = "false";
userInput = currentUserInput();

if (userInput == "pagedown" || userInput == "pageup")
{
  idx = Integer(getFocusItemIndex());
  if (userInput == "pagedown")
  {
    idx -= -8;
    if(idx &gt;= itemCount)
      idx = itemCount-1;
  }
  else
  {
    idx -= 8;
    if(idx &lt; 0)
      idx = 0;
  }

  print("new idx: "+idx);
  setFocusItemIndex(idx);
	setItemFocus(0);
  redrawDisplay();
  "true";
}
if (userInput == "two" || userInput == "2")
	{
     showIdle();
     url="<?php echo $host; ?>" + "/modules/podcast/podcast_me.php?file=" + getItemInfo(getFocusItemIndex(),"download");
     movie=getUrl(url);
     cancelIdle();
	 topUrl = "http://127.0.0.1:80/media/sda1/scripts/MyxLive/modules/links/download.cgi?link=" + movie + ";name=" + getItemInfo(getFocusItemIndex(),"name");
	 dlok = loadXMLFile(topUrl);
	 "true";
}
if (userInput == "three" || userInput == "3")
   {
    jumpToLink("destination");
    "true";
}
ret;
</script>
</onUserInput>

	</mediaDisplay>
	<item_template>
		<mediaDisplay  name="threePartsView" idleImageXPC="5" idleImageYPC="5" idleImageWidthPC="8" idleImageHeightPC="10">
        <idleImage>image/POPUP_LOADING_01.png</idleImage>
        <idleImage>image/POPUP_LOADING_02.png</idleImage>
        <idleImage>image/POPUP_LOADING_03.png</idleImage>
        <idleImage>image/POPUP_LOADING_04.png</idleImage>
        <idleImage>image/POPUP_LOADING_05.png</idleImage>
        <idleImage>image/POPUP_LOADING_06.png</idleImage>
        <idleImage>image/POPUP_LOADING_07.png</idleImage>
        <idleImage>image/POPUP_LOADING_08.png</idleImage>
		</mediaDisplay>

	</item_template>
	
		
<script>
    channelImage = "/tmp/usbmounts/sda1/scripts/MyxLive/themes/osd/veetle.png";
  </script>	
 
<script>
    channelImage1 = "/tmp/usbmounts/sda1/scripts/MyxLive/themes/logo/myxtremer_logo.png";
  </script>	
<channel>
<?php
$query = $_GET["query"];
if($query) {
   $queryArr = explode(',', $query);
   $page = $queryArr[0];
   $search = $queryArr[1];
}
echo "<title>".$search."</title>";

$host = "http://127.0.0.1:80/media/sda1/scripts/MyxLive";
$page1=($page-1)*9;
$link="http://veetle.com/index.php/listing/index/".$search."/popular/".$page1;
$html = file_get_contents($link);
if($page > 1) { ?>

<item>
<?php
$sThisFile = 'http://127.0.0.1'.$_SERVER['SCRIPT_NAME'];
$url = $sThisFile."?query=".($page-1).",";
if($search) { 
  $url = $url.$search; 
}
?>
<title>Previous Page</title>
<link><?php echo $url;?></link>
<annotation>Previous Page</annotation>
<image>/tmp/usbmounts/sda1/scripts/MyxLive/themes/osd/left.png</image>
<mediaDisplay name="threePartsView"/>
</item>


<?php } ?>

<?php
function str_between($string, $start, $end){ 
	$string = " ".$string; $ini = strpos($string,$start); 
	if ($ini == 0) return ""; $ini += strlen($start); $len = strpos($string,$end,$ini) - $ini; 
	return substr($string,$ini,$len); 
}
function getRewriteString($string) {
    $string    = htmlentities($string);
    $string    = preg_replace("/&amp;(.)(acute|cedil|circ|ring|tilde|uml|horn);/", "$1", $string);
    return $string;
}
function getRewriteString1($string) {
    $string    = htmlentities($string);
    $string    = preg_replace("/&(.)(acute|cedil|circ|ring|tilde|uml|horn|);/", "$1", $string);
    return $string;
}
function c($title) {
     $title = str_replace("&ordm;","s",$title);
     $title = str_replace("&Ordm;","S",$title);
     $title = str_replace("&thorn;","t",$title);
     $title = str_replace("&Thorn;","T",$title);
     $title = str_replace("&icirc;","i",$title);
     $title = str_replace("&Icirc;","I",$title);
     $title = str_replace("&atilde;","a",$title);
     $title = str_replace("&Atilde;","I",$title);
     $title = str_replace("&ordf;","S",$title);
     $title = str_replace("&acirc;","a",$title);
     $title = str_replace("&Acirc;","A",$title);

     return $title;
}
$videos = explode('div class="grid', $html);

unset($videos[0]);
$videos = array_values($videos);

foreach($videos as $video) {
    $t1 = explode("flatRedirect('", $video);
    $t2 = explode("'", $t1[1]);
    $link = $t2[0];

    $t1 = explode('src="', $video);
    $t2 = explode('"', $t1[1]);
    $image = $t2[0];

    $t1 = explode('title="', $video);
    $t2 = explode('"', $t1[2]);
    $title = str_replace("&nbsp;","",$t2[0]);
    $title = str_replace("&amp;","&",$title);
    $title = str_replace('"',"",$title);
    $title = getRewriteString1($title);
    $title = getRewriteString($title);
    $title = str_replace("&amp;","&",$title);
    $title=c($title);
    
    $t1 = explode('title="', $video);
    $t2 = explode('"', $t1[1]);
    $description = str_replace("&nbsp;","",$t2[0]);
    $description = str_replace("&amp;","&",$description);
    $description = getRewriteString($description);

    echo '
    <item>
    <title>'.$title.'</title>
    <onClick>
    <script>
    showIdle();
    url="http://127.0.0.1:80/media/sda1/scripts/MyxLive/modules/vod/veetle/veetle_link.php?file='.$link.'";
    movie=getUrl(url);
    cancelIdle();
    playItemUrl(movie,10);
    </script>
    </onClick>
    <annotation>'.$description.'</annotation>
    <image>'.$image.'</image>
    <media:thumbnail url="'.$image.'" />
    </item>
    ';
}

?>

<item>
<?php
$sThisFile = 'http://127.0.0.1'.$_SERVER['SCRIPT_NAME'];
$url = $sThisFile."?query=".($page+1).",";
if($search) { 
  $url = $url.$search; 
}
?>
<title>Next Page</title>
<link><?php echo $url;?></link>
<annotation>Next Page</annotation>
<image>/tmp/usbmounts/sda1/scripts/MyxLive/themes/osd/right.png</image>
<mediaDisplay name="threePartsView"/>
</item>

</channel>
</rss>
