\n";
  $str .= str_replace("\n", " \n", $tail);
  return $str;
}
class Track {
  public $title;
  public $number;
  public $playtime;
  public $previewurl;
  public $lyrics;
  public function write($fp)
  {
    fwrite($fp, "      title, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "             number=\"" . $this->number . "\"\n");
    fwrite($fp, "             playtime=\"" . $this->playtime . "\"\n");
    fwrite($fp, "             previewurl=\"" . $this->previewurl . "\">\n");
    fwrite($fp, "        ".
	   htmlspecialchars($this->lyrics, ENT_QUOTES, "UTF-8")." \n");
    fwrite($fp, "       \n");
  }
  public function showLyrics()
  {
    $str = "";
    if($this->lyrics) {
      $str .= createNewlines(htmlspecialchars_decode($this->lyrics));
    }
    return $str;
  }
  public function show($disc)
  {
    global $GLOBALS;
    $page = $GLOBALS["page"];
    $str = "";
    $str .= "      \n";
    $str .= "        
\n";
    if($this->previewurl) {
      $str .= "          \n";
      $str .= "             \n"; 
      $str .= "             previewurl."&showslider=0&width=25&height=12&".
	"skin=design/rotr/chicken.jpg\"/>\n"; 
      $str .= "           \n";
    } else {
    }
    $str .= "         \n";
    $str .= "        
".$this->number.".  \n";
    $str .= "        
".$this->title." \n";
    $str .= "        
".time2str($this->playtime).
      " \n";
    if($this->lyrics) {
      $str .= "      
number."\">Lyrics \n"; 
    }
    $str .= "      
 \n";
    return $str;
  }
  public function Track($title, $number, $playtime, $previewurl, $lyrics)
  {
    $this->title = $title;
    $this->number = $number;
    $this->playtime = $playtime;
    $this->previewurl = $previewurl;
    $this->lyrics = $lyrics;
  }
}
class Disc {
  public $title;
  public $releasetime;
  public $description;
  public $cover;
  public $releaser;
  public $tracks = array();
  //  public $reviews = array();
  public $refs;
  public function write($fp)
  {
    fwrite($fp, "  title, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "        releasetime=\"" . $this->releasetime . "\"\n");
    fwrite($fp, "        description=\"" .
	   htmlspecialchars($this->description, ENT_QUOTES, "UTF-8") . "\"\n");
    fwrite($fp, "        cover=\"" . $this->cover . "\"\n");
    fwrite($fp, "        releaser=\"" . $this->releaser . "\">\n");
    fwrite($fp, "    \n");
    if($this->tracks) {
      foreach($this->tracks as $track) {
	$track->write($fp);
      }
    }
    fwrite($fp, "     \n");
    if($this->refs) $this->refs->write($fp, "    ");
    fwrite($fp, "   \n");
  }
  public function showLyrics($number)
  {
    $str = "";
    if($this->tracks) {
      foreach($this->tracks as $track) {
	if($track->number == $number) {
	  $str .= $track->showLyrics();
	  break;
	}
      }
    }
    return $str;
  }
  public function show()
  {
    $str = "";
    $str .= "  \n"; 
    $str .= "    
".$this->title." (".
      date("Y", $this->releasetime).") \n"; 
    $str .= "    
\n";
    $str .= "    
";		
    if($this->releasetime > time()) $str .= "To be";
    else $str .= "Was";
    $str .= " released by ".htmlspecialchars_decode($this->releaser)." on ".
      date("F jS Y", $this->releasetime)." \n";
    $str .= "    \n";
    $str .= "    
\n";
    $total = 0;
    if($this->tracks) {
      foreach($this->tracks as $track) {
				$str .= $track->show($this->releasetime);
				$total += $track->playtime;
      }
    }
    if($this->tracks && sizeof($this->tracks) > 1) {
      $str .= "      Total playtime: ".
	time2str($total)." \n";
    }
    $str .= "    
\n";
    $str .= "    
".
      Markdown(htmlspecialchars_decode($this->description))." \n";
    
    if($this->refs) $str .= $this->refs->show();
    $str .= "  
 \n";
    return $str;
  }
  public function showCompact()
  {
    $str = "";
    $str .= "  \n"; 
    /*
    $str .= "    
".$this->title." (".
      date("Y", $this->releasetime).") \n"; 
    */
    $str .= "    
\n";
    $str .= "    
";		
    if($this->releasetime > time()) $str .= "Coming ".date("F jS Y", $this->releasetime);
    else $str .= "Out now!";
    $str .= " \n";
    $str .= "  
 \n";
    $str .= "\n";
    return $str;
  }
	
  public function addTrack($track)
  {
    $key = $track->number;
    $this->tracks[$key] = $track;
  }
  /*
  public function addReview($review)
  {
    $key = $review->id;
    $this->reviews[$key] = $review;
  }
  */
  public function addRefs($refs)
  {
    $this->refs = $refs;
  }
  public function Disc($title, $releasetime, $description, $cover, $releaser)
  {
    $this->title = $title;
    $this->releasetime = $releasetime;
    $this->description = $description;
    $this->cover = $cover;
    $this->releaser = $releaser;
  }
}
class Discography {
  private $file;
  private $discs = array();
  // Admin config
  public $admin_title = "Discography";
  public $admin_submodules = array("Add Disc" => "add",
				   "Edit Disc" => "edit",
				   "Delete Disc" => "delete");
  public function admin($sub, $action, $vars)
  {
    switch($sub) {
    case "add":
      break;
    case "edit":
      break;
    case "delete":
      break;
    }
  }
  public function run($params)
  {
    global $GLOBALS;
    $str = "\n";
    foreach($params as $param => $value) {
      switch($param) {
      case "latest":
	$id = -1;
	foreach($this->discs as $disc) {
	  if($disc->releasetime > $id) $id = $disc->releasetime;
	}
	if($id != -1) {
	  $disc = $this->discs[$id];
	  return $str .= $disc->showCompact();
	}
	break;
      default:
	$lyrics = $GLOBALS["lyrics"];
	$number = $GLOBALS["track"];
	//foreach($params as $param => $value) {}
			  
	if($lyrics && $number) {
	  if($this->discs) {
	    foreach($this->discs as $disc) {
	      if($disc->releasetime == $lyrics) {
		$str .= $disc->showLyrics($number);
		break;
	      }
	    }
	  }
	} else {
	  if($this->discs) {
	    foreach($this->discs as $disc) {
	      $str .= $disc->show();
	    }
	  }
	}
	break;
      }
    }
    $str .= "
\n";
    return $str;
  }
  public function add($disc) {
    $key = $disc->releasetime;
    $this->discs[$key] = $disc;
  }
  public function getImageSize($cat)
  {
    switch($cat) {
    case "cover":
      return new ImageSize($this->coverwidth, $this->coverheight, true);
    case "compactcover":
      //      return new ImageSize($this->compactcoverwidth, $this->compactcoverheight, true);
      return new ImageSize(150, 150, true);
    default:
      die(404);
    }
  }
	
  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()
  {
    $reviews = loadModule("reviews");
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->load($this->file);
    $discography = $dom->documentElement;
		
    $this->coverwidth = $discography->getAttribute('coverwidth');
    $this->coverheight = $discography->getAttribute('coverheight');
    $discs = $dom->getElementsByTagName('disc');
    foreach($discs as $d) {
      $description = "";
      $dess = $d->getElementsByTagName('description');
      foreach($dess as $des) {
				$description = $des->textContent;
      }
      $disc = new Disc($d->getAttribute('title'),
		       $d->getAttribute('releasetime'),
		       $description,
		       $d->getAttribute('cover'),
		       $d->getAttribute('releaser'));
      $tracks = $d->getElementsByTagName('track');
      foreach($tracks as $t) {
				$lyrics = "";
				$ls = $t->getElementsByTagName('lyrics');
				foreach($ls as $l) {
					$lyrics = $l->textContent;
				}
				
				$track = new Track($t->getAttribute('title'),
													 $t->getAttribute('number'),
													 $t->getAttribute('playtime'),
													 $t->getAttribute('previewurl'),
													 $lyrics);
				
				$disc->addTrack($track);
      }
      $rs = $d->getElementsByTagName('refs');
      foreach($rs as $r) {
        $refs = new Refs($r);
        $disc->addRefs($refs);
        break;
      }
      $this->add($disc);
    }
  }
  public function Discography($file)
  {
    $this->file =  $file;
    if(file_exists($file)) $this->read();
  }
}
function discography_init()
{
  global $DATA_DIR;
  return new Discography($DATA_DIR . "/discography.xml");
}
?>