<?php
	function showArray($array) {					
		echo "<div id=\"FULLARRAY\" style=\"border: 0px dotted #906F27;position:static;padding-left:80px;\">";
		echo "ARRAY():".count($array);				
		processArray($array,1);
		echo "</div>";
	}		
	function processArray($a,$p) {
		echo "<div style=\"border-left: 1px dotted #A9863A;position:static;padding-left:15px;\">";
		//echo "ARRAY: nivel:$p";			
		foreach($a as $b=>$c) {				
			if(is_array($c)) {
				$i = count($c);
				$count = $p+1;
				//echo "<div onclick=\"hDIV('$b$count')\" style=\"color:#295077;\">$b : ($count) ARRAY </div>";
				echo "<table onclick=\"hDIV('$b$count')\" style=\"cursor:pointer;\" ><tr><td style=\"color:#245F0D;\">$b($i)</td>".
								"<td>:</td><td style=\"color:#B48968;\">ARRAY($count)</td></tr></table>";								
				echo "<div ID=\"$b$count\" style=\"display:block;border: 0px solid #4A545E;position:static;padding-left:15px;\">";
				processArray($c,$count);			
				echo "</div>";		
			} else {
				echo "<table><tr><td style=\"color:navy;\">$b</td><td>=</td><td style=\"color:red;\">$c</td></tr></table>";					
			}
		}
		//echo "}";
		echo "</div>";
	}
	?>
	
	<script>
	function hDIV(ID) {
		if(document.getElementById(ID).style.display==\"none\");
			document.getElementById(ID).style.display = \"block\";
		else
			document.getElementById(ID).style.display = \"none\";
	}	 
	</script>		
	
	
	
	
	
	
	
	<?php	
	function showOnlyArray($array) {					
		echo "<div id=\"FULLARRAY\" style=\"border: 0px dotted #906F27;position:static;padding:8px;\">";
		createArray($array,1,"");
		echo "</div>";		
	}		
	function editArray($array) {					
		echo "<div id=\"FULLARRAY\" style=\"border: 0px dotted #906F27;position:static;padding:8px;\">";
		echo "ARRAY():".count($array);	
		echo "<div ID=\"RSAVE\"></div>";		
		echo "<div><input type=\"submit\" onclick=\"".jsSave()."('Saved')\"  value=\"Salvar\"></div>";	
		//echo "<div><input type=\"submit\" onclick=\"jssave('Saved')\"  value=\"Only test\"></div>";
		echo "<form action=\"\" id=\"formDATA\" method=\"post\">";
		createArray($array,1,"");
		echo "</form>";
		echo "</div>";		
	}		
	function createArray($a,$p,$name) {
		echo "<div style=\"border-left: 0px dotted #A9863A;position:static;padding-left:15px;\">";
		//echo "ARRAY: nivel:$p";	
		$fc=1;		
		foreach($a as $b=>$c) {				
			if(is_array($c)) {
				$i = count($c);
				$count = $p+$fc;
				//echo "<div class=\"CTRLS\" onclick=\"hDIV('$b$count')\">$b:($i)</div>";			
				//echo "<div class=\"CTRLS\" onclick=\"(function(ID){alert(ID);})('$b$count')\">$b:($i) - $b$count</div>";	
				$label=str_replace(["/","."],[" "," "],"$name.$b:($i)");		
				$ID="h$label$count";
				$ID=uniqid($label,true);
				//$label=str_replace("."," ",$label);			
				echo "<div class=\"CTRLS\" onclick=\"".jsArray()."('$ID')\">$label</div>";					
				//style=\"display:none;border: 2px solid #4A545E;position:static;padding-left:15px;\"
				echo "<div ID=\"$ID\" class=\"CTRLS-SUB\">"; //h$label$count
				createArray($c,$count,"$name/$b");			
				echo "</div>";	
				$fc++;				
			} else {
				//echo "<table><tr><td style=\"color:navy;\">$b</td><td>=</td><td style=\"color:red;\">$c</td></tr></table>";		
				echo jsControl("text","$b","$b",$c,"$name/$b");			
			}
		}
		//echo "}";
		echo "</div><br>";
	}
	function jsSave()
	{
		return 
			"(function(ID){".
				"var form = document.forms.namedItem('formDATA');".
				"var request = new XMLHttpRequest();".
				"request.onreadystatechange = function() {".
					"if (this.readyState == 4 && this.status == 200) { ".
						"document.getElementById('RSAVE').innerHTML = this.responseText;".						
					"}".
				"};".							
				"request.open('POST','opsave.php');".
				"request.send(new FormData(form));".
			"})";
	}	
	function jsArray()
	{
		return 
			"(function(ID){".
				"var C = document.getElementById(ID);".
				//" alert(ID+' : ['+C.style.display+'] => '+C); ".			
				"if(C.style.display=='' || C.style.display=='none') {".
					" C.style.display = 'block'; ".
				"} else {".
					" C.style.display = 'none'; ".
			"}})";
	}
	function jsControl($type,$name,$label,$value,$full)
	{
		if(strtoupper($type)=="TEXT") {
			$ff = "<font size=\"1\" style=\"color:#95009E;\">$full</font>";
			$r = "<table class=\"CTRLS\" onclick=\"\" style=\"padding:0;width:95%;border-bottom:1px solid #E2E2E2;\" ><tr>".
					 "<table onclick=\"\" style=\"width:95%;border:0px solid #E2E2E2;\"><tr class=\"CTRLS\">".			
					 "<td class=\"CTRLS\" style=\"color:#245F0D;width:30%;\">$label<br>$ff</td>".					 
					 "<td class=\"CTRLS\" style=\"color:#B48968;width:70%;\">".	
					 "<input class=\"CTRLS\" type=\"$type\" name=\"$full\" value=\"$value\">".
					 "</td>".
					 "</tr></table>".				 
					 "</tr class=\"CTRLS\"><tr>".		
					 "<td class=\"CTRLS\" style=\"color:#245F0D;width:100%;font-size:6px;\"></td>".					 
					 "</tr></table>";
			return $r;//"<label class=\"CTRLS\" for=\"$name\">$label</label><br><br>";
		}
	}	



	
	
	
	
	


	function parse_xml_file($filename) {
		$content = file_get_contents($filename);
		$tmp = new SimpleXMLElement($content);
		$json = json_encode($tmp);
		return json_decode($json,TRUE);
		 
	}


	function parse_ini_content($str) {   
			if(empty($str)) return false;
			$lines = explode("\n", $str);
			$ret = Array();
			$inside_section = false;
			foreach($lines as $line) {       
					$line = trim($line);
					//if(!$line || $line[0] == "" || $line[0] == ";") continue;       
					if($line[0] == "[" && $endIdx = strpos($line, "]")){
							$inside_section = substr($line, 1, $endIdx-1);
							continue;
					}
					if($line[0] == "#") {
											
					}        
					if(!strpos($line, '=')) continue;
					$tmp = explode("=", $line, 2);
					if($inside_section) {           
							$key = rtrim($tmp[0]);
							$value = ltrim($tmp[1]);
							if(preg_match("/^\".*\"$/", $value) || preg_match("/^'.*'$/", $value)) {
									$value = mb_substr($value, 1, mb_strlen($value) - 2);
							}
							$t = preg_match("^\[(.*?)\]^", $key, $matches);
							if(!empty($matches) && isset($matches[0])) {
									$arr_name = preg_replace('#\[(.*?)\]#is', '', $key);
									if(!isset($ret[$inside_section][$arr_name]) || !is_array($ret[$inside_section][$arr_name])) {
											$ret[$inside_section][$arr_name] = array();
									}
									if(isset($matches[1]) && !empty($matches[1])) {
											$ret[$inside_section][$arr_name][$matches[1]] = $value;
									} else {
											$ret[$inside_section][$arr_name][] = $value;
									}
							} else {
									$ret[$inside_section][trim($tmp[0])] = $value;
							}     
					} else {           
							$ret[trim($tmp[0])] = ltrim($tmp[1]);
					}
			}
			return $ret;
	}











	function buildList($path,$filetype,$filter,$init)
	{
		echo "<div id=\"TITLE-LIST\" class=\"TITLE-LIST\" >";	
		echo "<div class=\"TITLE-BANNER\" >VIDEOS</div>";	
			echo "<table class=\"TITLE-MODES\"><tr>";	
			echo "<td class=\"TITLE-MODES\" onclick=\"changeView('GALLERY')\" >Galery</td>";
			echo "<td class=\"TITLE-MODES\" onclick=\"changeView('COMPACT-VIEW')\" >Compact</td>";	
			echo "<td class=\"TITLE-MODES\" onclick=\"changeView('LIST-VIEW')\" >ListView</td>";		
		echo "</tr></table>";
		echo "</div>";		
		//echo "<div id=\"FULL-LIST\">";	
		//getDirContents($path,$filetype,$filter,$init,true);		
		//echo "</div>";
	}

	function getDirContents($dir, $type, $filter, $init, $showprint, &$results = array()) 
	{
		if(!is_dir($dir))return $results;
		if(!isset($showprint))$showprint=false;
		$files = scandir($dir);
		foreach ($files as $key => $value) {
			$path = realpath($dir . DIRECTORY_SEPARATOR . $value);			
			if (!is_dir($path)) {
				$path_parts = pathinfo($path);
				if(isFileType($type,$path_parts['extension']))
				{
					$path_parts['URL']=$init;
					$path_parts['FILTER']=$filter;
					$path_parts['HOST']=$_SERVER['HTTP_HOST'];
					$results[] = $path_parts;
					if($showprint) showItemContents($path_parts);
				}
			} else if ($value != "." && $value != "..") {
				$rel=$init;
				if(substr($rel,-1)!='/')$rel=$rel.'/';				
				getDirContents($path,$type,$filter,$rel.$value,$showprint,$results);
			}
		}
		return $results;
	}
	
	function isFileType($types,$type) 
	{
		foreach($types as $t) {
			if(strtoupper($t)==strtoupper($type)) return true;
		}
		return false;
	}
	
	function showItemContents($d) 
	{
		$id=getFullFilename($d);
		$id=str_replace("'","\'",$id);		
		$fn="B001";	
		$url=getURL($d);			
		$css="GALLERY";
		//$css="COMPACT-VIEW";	
		//id=\"$fn\"
		$cid = getTitle($d);
		echo "<div id=\"$cid\" class=\"$css\" onmouseenter=\"MouseEnter('$id')\" onmouseleave=\"MouseLeave('$id')\"   >";				
			echo "<div class=\"$css-CONTROLS\">";			
				echo "<h1 class=\"$css\">".$d['extension']."</h1>";						
				echo "<p class=\"$css\"   >";
					echo getTitle($d);	
				echo "</p>";			
				$data = array ("teste"=>"valor 1","name"=>"abcd4321");
				echo insertDetails($css,$data);
				echo "<table class=\"$css-TOOLBAR\" data-id-type=\"TOOLBAR\"><tr>";					
				echo "<td id=\"DELETE\" class=\"$css-TOOLBAR\" data-id-type=\"TOOLBAR\" onclick=\"showViewer('delete.php','$id')\"></td>";				
				echo "<td id=\"ASK\" 		class=\"$css-TOOLBAR\" data-id-type=\"TOOLBAR\" onclick=\"showViewer('id3info.php','$id')\"></td>";
				echo "<td id=\"INFO\" 	class=\"$css-TOOLBAR\" data-id-type=\"TOOLBAR\" onclick=\"showViewer('plot.php','$id')\"></td></td>";	
				echo "<td id=\"PLAY\" 	class=\"$css-TOOLBAR\" data-id-type=\"TOOLBAR\" onclick=\"loadPLAYER('$url')\"></td>";	
				echo "<td id=\"PLAY\" 	class=\"$css-TOOLBAR\" data-id-type=\"TOOLBAR\" onclick=\"loadFrame('player.php','$url')\"></td>";							
				echo "</tr></table>";										
			echo "</div>";						
		echo "</div>";		
	}	
	
	function insertDetails($css,$data) 
	{
		if(!$data)return "";
		$tmp = "<table class=\"$css-DETAILS\"  data-id-type=\"DETAILS\" >";			
		$healder="<tr class=\"$css-DETAILS\"  data-id-type=\"DETAILS\">";
		$rowData="<tr class=\"$css-DETAILS\"  data-id-type=\"DETAILS\">";
		foreach($data as $n=>$i){
			$healder=$healder."<th class=\"$css-DETAILS\"  data-id-type=\"DETAILS\" >$n</th>";
			$rowData=$rowData."<td class=\"$css-DETAILS\"  data-id-type=\"DETAILS\" >$i</td>";		
		}	
		//$tmp=$tmp."<td class=\"$css-DETAILS\"  data-id-type=\"DETAILS\" >S04</td>";		
		$tmp=$tmp.$healder."</tr>".$rowData."</tr></table>";
		return $tmp;
	}
	
	function showDirContents($dir) 
	{
		foreach($dir as $d) {
			showItemContents($d);
		}
	}			
	
	function listDirContents($dir) 
	{
		foreach($dir as $d) {
			echo "<br>";print_r($d);
		}
	}	
	
	//function getRelPath($d){
		//return $d['URL'];
	//}	
	
	function getURL($d){
		$rel=$d['URL'];
		if(substr($rel,-1)!='/')$rel=$rel.'/';	
		return str_replace("'","\'",$rel.$d['basename']);
	}		
	
	function getTitle($d){
		return str_replace("."," ",$d['filename']);//$d['filename'];
	}
		
	function getShortFilename($d){
		return $d['basename'];
	}
	
	function getFullFilename($d){
		return $d['dirname']."/".$d['basename'];
	}
	

function createControl($type,$name,$label,$value)
{
	if(strtoupper($type)=="TEXT") {
		return "<label class=\"CTRLS\" for=\"$name\">$label</label><input class=\"CTRLS\" type=\"$type\" name=\"$name\" value=\"$value\"><br><br>";
	} else if(strtoupper($type)=="CHECKBOX") {
		return "<input class=\"CTRLS\" type=\"$type\" name=\"$name\" checked=\"true\"><label for=\"$name\">$label</label><br><br>";		
	} else if(strtoupper($type)=="SUBMIT") {
		return "<input class=\"CTRLS\" type=\"$type\" name=\"$name\" value=\"$label\"><br>";
	}
}
?>
