title, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "        description=\"" .
	   htmlspecialchars($this->description, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "        image=\"" . $this->image . "\">\n");
    
    fwrite($fp, "        type=\"" . $this->type . "\"\n");
    fwrite($fp, "        paypalkey=\"" . $this->paypalkey . "\">\n");
    fwrite($fp, "  \n");
    */
  }
  
  public function show()
  {
    $str = "";
    
		$str .= "          url . "\">\n";
		$str .= "            ". $this->title . "\n";
		$str .= "          \n";
    return $str;
  }
  public function DownloadSource($id, $title, $url)
  {
		$this->id = $id;
    $this->title = $title;
		$this->url = $url;
  }
}
class DownloadItem {
	public $id;
  public $title;
  public $description;
  public $type;
	public $value;
  public $sources = array();
  public function write($fp)
  {
    /*
    fwrite($fp, "  title, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "        description=\"" .
	   htmlspecialchars($this->description, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "        image=\"" . $this->image . "\">\n");
    
    fwrite($fp, "        type=\"" . $this->type . "\"\n");
    fwrite($fp, "        paypalkey=\"" . $this->paypalkey . "\">\n");
    fwrite($fp, "  \n");
    */
  }
  
  public function show()
  {
		global $VIDEO_WIDTH;
		global $VIDEO_HEIGHT;
		$VIDEO_WIDTH = 530;
		$VIDEO_HEIGHT = 400;
    $str = "";
    
    $str .= "      
\n";
    $str .= "        
\n";
    $str .= "      
\n";
		$str .= "    
".$this->title."
\n";
    $str .= "    
\n";
		if($this->items) {
			foreach($this->items as $i) {
				$str .= $i->show();
			}
		}
    $str .= "    
\n";
    $str .= "  
\n";
    
    //foreach($params as $param => $value) {}
    
    if($this->groups) {
      foreach($this->groups as $group) {
				$str .= $group->show();
      }
    }
    
    $str .= "
\n";
    
    return $str;
  }
  public function add($group) {
    $key = $group->id;
    $this->groups[$key] = $group;
  }
	
  public function write()
  {
    /*
    $fp = fopen($this->file, "w");
    fwrite($fp, "\n");
    fwrite($fp, "\n");
    foreach($this->discs as $disc) {
			$disc->write($fp);
    }
    fwrite($fp, "\n");
    fclose($fp);
    */
  }
	
  private function read()
  {
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->load($this->file);
    $groups = $dom->getElementsByTagName('group');
    if($groups) {
      foreach($groups as $g) {
				$group = new DownloadGroup($g->getAttribute('id'),
																	 $g->getAttribute('title'));
				
				$items = $g->getElementsByTagName('item');
				if($items) {
					foreach($items as $i) {
						$item = new DownloadItem($i->getAttribute('id'),
																		 $i->getAttribute('title'),
																		 $i->getAttribute('description'),
																		 $i->getAttribute('type'),
																		 $i->getAttribute('value'));
						$sources = $i->getElementsByTagName('source');
						if($sources) {
							foreach($sources as $s) {
								$source = new DownloadSource($s->getAttribute('id'),
																						 $s->getAttribute('title'),
																						 $s->getAttribute('url'));
								$item->addSource($source);
							}
						}
						ksort($item->sources);
						$group->addItem($item);
					}
				}
				
				ksort($group->items);
				$this->add($group);
      }
    }
		ksort($this->groups);
  }
  public function Downloads($file)
  {
    $this->file =  $file;
    if(file_exists($file)) $this->read();
  }
}
function downloads_init()
{
  global $DATA_DIR;
  return new Downloads($DATA_DIR . "/downloads.xml");
}
?>