public function render($echo = false, $domDocument = null)
    {
        $isChild = (bool)$domDocument;
        if (!$isChild) {
            $domDocument                = new \DOMDocument("1.0", "utf-8");
            $domDocument->xmlStandalone = true;
        }
        $maniaLink = $domDocument->createElement("manialink");
        if (!$isChild) {
            $domDocument->appendChild($maniaLink);
        }

        if ($this->maniaLinkId) {
            $maniaLink->setAttribute("id", $this->maniaLinkId);
        }
        if ($this->version > 0) {
            $maniaLink->setAttribute("version", $this->version);
        }
        if ($this->name) {
            $maniaLink->setAttribute("name", $this->name);
        }
        if ($this->background) {
            $maniaLink->setAttribute("background", $this->background);
        }
        if (!$this->navigable3d) {
            $maniaLink->setAttribute("navigable3d", "0");
        }
        if ($this->timeout) {
            $timeoutXml = $domDocument->createElement("timeout", $this->timeout);
            $maniaLink->appendChild($timeoutXml);
        }
        if ($this->dico) {
            $dicoXml = $this->dico->render($domDocument);
            $maniaLink->appendChild($dicoXml);
        }
        if ($this->layer) {
            $maniaLink->setAttribute("layer", $this->layer);
        }
        
        $scriptFeatures = array();
        foreach ($this->children as $child) {
            $childXml = $child->render($domDocument);
            $maniaLink->appendChild($childXml);
            if ($child instanceof ScriptFeatureable) {
                $scriptFeatures = array_merge($scriptFeatures, $child->getScriptFeatures());
            }
        }

        if ($this->stylesheet) {
            $stylesheetXml = $this->stylesheet->render($domDocument);
            $maniaLink->appendChild($stylesheetXml);
        }

        if ($scriptFeatures) {
            $this->createScript()
                 ->loadFeatures($scriptFeatures);
        }
        if ($this->script) {
            if ($this->script->needsRendering()) {
                $scriptXml = $this->script->render($domDocument);
                $maniaLink->appendChild($scriptXml);
            }
            $this->script->resetGenericScriptLabels();
        }

        if ($isChild) {
            return $maniaLink;
        }
        if ($echo) {
            header("Content-Type: application/xml; charset=utf-8;");
            echo $domDocument->saveXML();
        }
        return $domDocument;
    }