layerinfo type = "layout";
layerinfo name = "Bloggish";
layerinfo redist_uniq = "sixhtml/layout";
layerinfo previews = "sixhtml/layout.jpg";
layerinfo des = "SixApart's standard HTML structure for blogs, to be styled with CSS.";

propgroup presentation {
    # This property is now set in a theme layer, but default is still Powell Street
    property string base_theme {
        des = "Base Theme";
        noui = 1;
    }
    set base_theme = "powell-street";
    # The colors for the default theme
    set theme_bgcolor = "#e7e7ec";
    set theme_fgcolor = "#333333";
    set theme_bordercolor = "#bbbbbb";
    set theme_linkcolor = "#598a92";

    property string layout_type {
        des = "Layout Type";
        values = "1C|One column|2CL|Two column (Sidebar on Left)|2CR|Two column (Sidebar on Right)|3C|Three column";
    }
    set layout_type = "2CR";

    property string{} theme_stylesheet {
        noui = 1;
    }
    set theme_stylesheet = {
        # Bloggish themes
        "april" => "http://stat.livejournal.com/sixhtml/themes/classic/theme-april.css",
        "baby" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-baby.css",
        "beckett" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-beckett.css",
        "bluecrush" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-bluecrush.css",
        "bonjour" => "http://stat.livejournal.com/sixhtml/themes/classic/theme-bonjour.css",
        "bryant" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-bryant.css",
        "cityblend" => "http://stat.livejournal.com/sixhtml/themes/shea/theme-cityblend.css",
        "classy" => "http://stat.livejournal.com/sixhtml/themes/classic/theme-classy.css",
        "earth" => "http://stat.livejournal.com/sixhtml/themes/classic/theme-earth.css",
        "folio" => "http://stat.livejournal.com/sixhtml/themes/shea/theme-folio.css",
        "forestgreen" => "http://stat.livejournal.com/sixhtml/themes/shea/theme-forestgreen.css",
        "green" => "http://stat.livejournal.com/sixhtml/themes/classic/theme-green.css",
        "knitting" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-knitting.css",
        "masala" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-masala.css",
        "minimalist" => "http://stat.livejournal.com/sixhtml/themes/classic/theme-minimalist.css",
        "porpoise" => "http://stat.livejournal.com/sixhtml/themes/shea/theme-porpoise.css",
        "powell-street" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-powell-street.css",
        "purplecrush" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-purplecrush.css",
        "stevenson" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-stevenson.css",
        "stitch" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-stitch.css",
        "subtle" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-subtle.css",
        "sunburned" => "http://stat.livejournal.com/sixhtml/themes/shea/theme-sunburned.css",
        "tickle" => "http://stat.livejournal.com/sixhtml/themes/shea/theme-tickle.css",
        "travel" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-travel.css",
        "wedding" => "http://stat.livejournal.com/sixhtml/themes/lilia/theme-wedding.css",
        # Style Contest themes
        "bamboo_dream" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/bamboo_dream/bamboodream.css",
        "cartoon" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/cartoon/style.css",
        "clean_cut" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/clean_cut/stylesheet.css",
        "cleanstyle_black_and_white" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/cleanstyle_black_and_white/theme-clean_style_bw.css",
        "cleanstyle_blue_and_yellow" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/cleanstyle_blue_and_yellow/theme-clean_style_by.css",
        "espresso_at_midnight" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/espresso_at_midnight/style.css",
        "feminism" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/feminism/feminism.css",
        "fleur" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/fleur/fleur.css",
        "frankenblog" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/frankenblog/frankenblog.css",
        "freshly_baked_bread" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/freshly_baked_bread/theme-fbbread.css",
        "garden_state" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/garden_state/gardenstate.css",
        "gothic" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/gothic/styles.css",
        "hot_springs" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/hot_springs/style.css",
        "kinda_canoe" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/kinda_canoe/boat6.css",
        "kubrick" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/kubrick/theme-kubrick.css",
        "magic_paper" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/magic_paper/magic-book.css",
        "make_my_way" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/make_my_way/makemyway.css",
        "makeup" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/makeup/styles-site.css",
        "mango" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/mango/theme-mango.css",
        "miniml" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/miniml/miniml.css",
        "modernist_citron" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/modernist_citron/modernist-citron.css",
        "modernist_lounge" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/modernist_lounge/modernist-lounge.css",
        "my_news" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/my_news/styles-site.css",
        "newsline" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/newsline/newsline.css",
        "note_taking" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/note_taking/theme-notes.css",
        "painterly" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/painterly/painterly.css",
        "scarf" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/scarf/scarf.css",
        "shadow_dancers" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/shadow_dancers/style.css",
        "shlyapa" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/shlyapa/shlyapa.css",
        "summer_love" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/summer_love/theme-summerlove.css",
        "swimmingly" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/swimmingly/theme-swimmingly.css",
        "the_great_outdoors" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/the_great_outdoors/greatoutdoors.css",
        "the_late_show" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/the_late_show/thelateshow.css",
        "the_road_to_october" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/the_road_to_october/roadtooctober.css",
        "top_secret" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/top_secret/style.css",
        "utopia" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/utopia/stylesheet.css",
        "zen_and_the_art_of_movable_type" => "http://stat.livejournal.com/sixhtml/themes/stylecontest/zen_and_the_art_of_movable_type/theme-zataomt.css",
    };

    property use page_recent_items;
    property use page_friends_items;
    property use page_day_sortorder;
    property use page_year_sortorder;
    property use view_entry_disabled;
    property use use_shared_pic;

    property bool opt_datehead {
        des = "Show Heading for New Day";
    }
    set opt_datehead = true;

    property bool opt_showtime {
        des = "Show Posting Time on Entries";
        note = "This is recommended if you regularly post more than one entry per day.";
    }
    set opt_showtime = true;

    property bool opt_userpic_main {
        des = "Show Userpics on Entries";
    }
    property bool opt_userpic_friends {
        des = "Show Userpics on Entries on your Friends Page";
    }
    property bool opt_userpic_entrypage {
        des = "Show Userpic on the Entry page";
    }
    property bool opt_userpic_comments {
        des = "Show Userpics on Comments";
    }
    set opt_userpic_main = true;
    set opt_userpic_friends = true;
    set opt_userpic_entrypage = true;
    set opt_userpic_comments = true;

    property string[][] sidebar_primary {
        des = "Primary sidebar members";
        noui = 1;
    }
    property string[][] sidebar_secondary {
        des = "Secondary sidebar members";
        noui = 1;
    }
    set sidebar_primary = []; # Set in prop_init()
    set sidebar_secondary = []; # Set in prop_init()

    set external_stylesheet = true;
    property use linklist_support;
    property use custom_control_strip_colors;
}

propgroup colors {
    property use control_strip_bgcolor;
    property use control_strip_fgcolor;
    property use control_strip_bordercolor;
    property use control_strip_linkcolor;
}

propgroup text {
    property use text_post_comment;
    property use text_read_comments;
    property use text_post_comment_friends;
    property use text_read_comments_friends;
    property use text_view_recent;
    property use text_view_archive;
    property use text_view_friends;
    property use text_view_userinfo;
    property use lang_fmt_date_med;
    property use lang_fmt_time_short;

    # i18n props. Not in the interface.
    property string text_comments {
        des = "Text for the heading above the comments on the entry page";
        noui = 1;
    }
    set text_comments = "Comments";
}

propgroup customcss {
    property bool use_6a_base {
        des = "Use layout's \"Base Weblog\" stylesheet";
        note = "Disable this only if you want to re-style this layout completely from scratch using a custom stylesheet and if you selected \"(None)\" as your theme.";
    }
    set use_6a_base = true;

    property string linked_stylesheet {
        des = "Custom external stylesheet URL";
        note = "If you selected \"(None)\" as your theme and have a custom external stylesheet that you'd like to use instead, or if you'd just like to use a custom external stylesheet in addition to your theme's stylesheet, enter its URL here.";
    }
    set linked_stylesheet = "";

    property string custom_css {
        des = "Custom stylesheet";
        note = "If you selected \"(None)\" as your theme and have custom CSS that you'd like to use instead, or if you'd just like to add custom CSS in addition to your theme's stylesheet, enter it here.";
        cols = 80;
        rows = 20;
        string_mode = "css";
    }
    set custom_css = "";
}

function prop_init() {
    # Did the user specify custom sidebars?  If so, we don't want to change them.
    var bool custom_primary = (size $*sidebar_primary > 0) ? true : false;
    var bool custom_secondary = (size $*sidebar_secondary > 0) ? true : false;

    # These themes don't support the 3C layout
    var string[] no_3C_themes = [
        "clean_cut",
        "frankenblog",
        "freshly_baked_bread",
        "gothic",
        "kinda_canoe",
        "miniml",
        "note_taking",
        "painterly",
        "shlyapa",
        "summer_love",
        "the_road_to_october",
    ];

    # Don't let users use 3C if they're using one of the themes that doesn't support it
    if ($*layout_type == "3C") {
        foreach var string theme ($no_3C_themes) {
            if ($theme == $*base_theme) {
                $*layout_type = "2CL";
            }
        }
    }

    # Users who see eboxes cannot use the 3 column layout because the ebox won't always fit
    if (viewer_sees_ebox() and $*layout_type == "3C") {
        $*layout_type = "2CL";
    }

    if (viewer_sees_vbox()) {
        # Users who see ads cannot use the 1 column layout
        if ($*layout_type == "1C") {
            $*layout_type = "2CR";
        }

        if (not $custom_primary) {
            if ($*layout_type == "3C") {
                $*sidebar_primary = [
                    [ "userprofile", "", "0", "1" ],
                    [ "viewlinks", "" ],
                    [ "calendar" ],
                    [ "links", "Links" ],
                    [ "syndicate", "Syndicate" ],
                    [ "poweredby" ],
                ];
            } else {
                $*sidebar_primary = [
                    [ "userprofile", "", "0", "1" ],
                    [ "viewlinks", "" ],
                    [ "calendar" ],
                    [ "ads", "" ],
                    [ "links", "Links" ],
                    [ "tags", "Tags" ],
                    [ "pagesummary", "Page Summary" ],
                    [ "syndicate", "Syndicate" ],
                    [ "poweredby" ],
                ];
            }
        }

        if (not $custom_secondary) {
            $*sidebar_secondary = [
                [ "ads", "" ],
                [ "tags", "Tags" ],
                [ "pagesummary", "Page Summary" ],
            ];
        }
    } else {
        if (not $custom_primary) {
            if ($*layout_type == "3C") {
                $*sidebar_primary = [
                    [ "userprofile", "", "0", "1" ],
                    [ "viewlinks", "" ],
                    [ "calendar" ],
                    [ "links", "Links" ],
                    [ "syndicate", "Syndicate" ],
                    [ "poweredby" ],
                ];
            } else {
                $*sidebar_primary = [
                    [ "userprofile", "", "0", "1" ],
                    [ "viewlinks", "" ],
                    [ "calendar" ],
                    [ "links", "Links" ],
                    [ "tags", "Tags" ],
                    [ "pagesummary", "Page Summary" ],
                    [ "syndicate", "Syndicate" ],
                    [ "poweredby" ],
                ];
            }
        }

        if (not $custom_secondary) {
            $*sidebar_secondary = [
                [ "tags", "Tags" ],
                [ "pagesummary", "Page Summary" ],
            ];
        }
    }

    # Set the control strip colors
    if ($*control_strip_bgcolor.as_string == "") {
        $*control_strip_bgcolor = $*theme_bgcolor;
    }
    if ($*control_strip_fgcolor.as_string == "") {
        $*control_strip_fgcolor = $*theme_fgcolor;
    }
    if ($*control_strip_bordercolor.as_string == "") {
        $*control_strip_bordercolor = $*theme_bordercolor;
    }
    if ($*control_strip_linkcolor.as_string == "") {
        $*control_strip_linkcolor = $*theme_linkcolor;
    }
}

function print_custom_stylesheet() {}

function print_stylesheet () {

print_custom_control_strip_css();
"""
/* user css */
""";
    print_custom_stylesheet();
}

function container_open (string name) {
    print safe "<div id='$name'><div id='$name-inner' class='pkg'>\n";
}

function container_close () {
    "</div></div>\n";
}

function print_banner(Page p) {
    container_open("banner");
      print safe "<h1 id='banner-header'>$p.global_title</h1>";
      print safe "<h2 id='banner-description'>$p.global_subtitle</h2>";
    container_close();
}


function open_module(string intname, string title, string headlink_url, bool nocontent) {
    print safe """<div class="module-$intname module">""";
    if ($title != "") {
        """<h2 class="module-header">""";
        if ($headlink_url != "") { print safe """<a href="$headlink_url">"""; }
        print safe $title;
        if ($headlink_url != "") { """</a>"""; }
        "</h2>\n";
    }
    if (not $nocontent) {
        println """<div class="module-content">""";
    }
}

function close_module(bool nocontent) {
    println "</div>";
    if (not $nocontent) { println "</div>"; }
}

function open_module(string intname, string title, string headlink_url) {
    open_module($intname, $title, $headlink_url, false);
}
function close_module() {
    close_module(false);
}

function print_module_list(string[] list) {
    println """<ul class="module-list">""";
    foreach var string s ($list) {
        print safe """<li class="module-list-item">$s</li>""";
    }
    println """</ul>""";
}

function print_module_userprofile(string title, bool show_profile, bool show_userpic) {
    var Page p = get_page();
    if ($show_userpic and not $show_profile) {
        if (defined $p.journal.default_pic) {
            open_module("photo", $title, $p.journal.userpic_listing_url);
            println ""+$p.journal.default_pic;
            close_module();
        }
        return;
    }

    if (not $show_profile and not $show_userpic) {
        open_module("about", $title, $p.view_url{"userinfo"}, true);
        close_module(true);
        return;
    }

    open_module("about", $title, $p.view_url{"userinfo"});
    println "<div>"+$p.journal.default_pic+"</div>";
    println "<p>"+$p.journal.name+"</p>"; # TODO: Display more stuff such as website link
    close_module();

}

function print_module_viewlinks(string title) {
    var Page p = get_page();
    open_module("viewlinks", $title, "");
    var string[] links = [];
    foreach var string k ($p.views_order) {
        $links[size $links] = """<a href="$p.view_url{$k}">"""+lang_viewname($k)+"""</a>""";
    }
    print_module_list($links);
    close_module();
}

function print_module_poweredby(string title) {
    var Page p = get_page();
    open_module("powered", $title, "");
    println """Powered by <a href="$*SITEROOT/">$*SITENAME</a>""";
    close_module();
}
function print_module_pagesummary(string title) {
    var Page p = get_page();

    var string[] links = [];
    if ($p isa RecentPage) {
        var RecentPage cp = $p as RecentPage;
        var string subject;
        foreach var Entry e ($cp.entries) {
            $subject = ($e.subject != "" ? striphtml($e.subject) : "<em>$*text_nosubject</em>");
            $links[size $links] = """<a href="$e.permalink_url">$subject</a>""";
        }
    }

    if (size($links) < 1) { return; }
    open_module("pagesummary", $title, "");
    print_module_list($links);
    close_module();
}
function print_module_tags(string title) {
    var Page p = get_page();
    var TagDetail[] tags = $p->visible_tag_list();
    if (size($tags) < 1) { return; }
    open_module("categories", $title, ""); # TODO: Link to the TagsPage?

    var string[] links = [];
    foreach var TagDetail tag ($tags) {
        $links[size $links] = """<a href="$tag.url">$tag.name</a>""";
    }
    print_module_list($links);
    close_module();
}
function print_module_calendar() {
    var Page p = get_page();
    var YearMonth mon = $p->get_latest_month();
    open_module("calendar", $mon->month_format(), "");

    println """<table summary="Monthly calendar with links to each day's posts">""";

    println "<tr>";
    foreach var int d (weekdays()) {
        "<th>"+$*lang_dayname_shorter[$d]+"</th>\n";
    }
    println "</tr>";

    foreach var YearWeek week ($mon.weeks) {
        println "<tr>";
        foreach var int i (1 .. $week.pre_empty) {
            print "<td>&nbsp;</td>";
        }

        foreach var YearDay day ($week.days) {
            print "<td>";
            if ($day.num_entries > 0) { print """<a href="$day.url">"""; }
            print $day.day;
            if ($day.num_entries > 0) { print """</a>"""; }
            print "</td>";
        }

        foreach var int i (1 .. $week.post_empty) {
            print "<td>&nbsp;</td>";
        }
        println "</tr>";
    }

    println """</table>""";

    close_module();
}
function print_module_syndicate(string title) {
    var Page p = get_page();
    if ((size $p.data_links_order) < 1) { return; }
    open_module("syndicate", $title, "");

    foreach var string k ($p.data_links_order) {
        print " $p.data_link{$k} ";
    }

    close_module();
}
function print_module_customtext(string title, string text, string titlelink_url) {
    var Page p = get_page();
    open_module("customtext", $title, $titlelink_url);
    print safe $text;
    close_module();
}
function print_module_links(string title) {
    var Page p = get_page();
    var UserLink[] links = $p.linklist;
    if (size($links) < 1 or not $*linklist_support) { return; }

    var bool box_open = false;
    if (not $links[0].is_heading) {
        open_module("typelist", $title, "");
        $box_open = true;
    }

    var string[] items = [];
    foreach var UserLink link ($links) {
        if ($link.is_heading) {
            if ($box_open) {
                print_module_list($items);
                close_module();
                $items = [];
            }
            open_module("typelist", $link.title, "");
            $box_open = true;
        }
        if (not $link.is_heading and $link.title != "") {
            $items[size $items] = """<a href="$link.url">$link.title</a>""";
        }
    }
    if ($box_open) {
        print_module_list($items);
        close_module();
    }
}
function print_module_ads(string title) {
    var Page p = get_page();
    open_module("ads", $title, "");
    $p->print_vbox();
    close_module();
}

function handle_sidebar_array(string[][] list) {
    foreach var string[] item ($list) {
        var string module = $item[0];
        var string title = $item[1];

        if ($module == "userprofile") {
            var bool show_profile = (int($item[2]) != 0 ? true : false);
            var bool show_userpic = (int($item[3]) != 0 ? true : false);
            print_module_userprofile($title, $show_profile, $show_userpic);
        }
        elseif ($module == "viewlinks") {
            print_module_viewlinks($title);
        }
        elseif ($module == "poweredby") {
            print_module_poweredby($title);
        }
        elseif ($module == "pagesummary") {
            print_module_pagesummary($title);
        }
        elseif ($module == "tags") {
            print_module_tags($title);
        }
        elseif ($module == "calendar") {
            print_module_calendar();
        }
        elseif ($module == "syndicate") {
            print_module_syndicate($title);
        }
        elseif ($module == "customtext") {
            var string text = $item[2];
            var string titlelink_url = $item[3];
            print_module_customtext($title, $text, $titlelink_url);
        }
        elseif ($module == "links") {
            print_module_links($title);
        }
        elseif ($module == "ads") {
            print_module_ads($title);
        }
    }
}

function Page::lay_sidebar_primary() {
    handle_sidebar_array($*sidebar_primary);
}
function Page::lay_sidebar_secondary() {
    handle_sidebar_array($*sidebar_secondary);
}

function render_layout (Page p, string lay) {
   if ($lay == "1C") {
      container_open("alpha");
      $p->print_body();
      container_close();
      return;
   }

   if ($lay == "2CL") {
      container_open("alpha");
      $p->lay_sidebar_primary();
      container_close();
      container_open("beta");
      $p->print_body();
      container_close();
      return;
   }

   if ($lay == "2CR") {
      container_open("alpha");
      $p->print_body();
      container_close();
      container_open("beta");
      $p->lay_sidebar_primary();
      container_close();
      return;
   }

   if ($lay == "3C") {
      container_open("alpha");
      $p->lay_sidebar_primary();
      container_close();
      container_open("beta");
      $p->print_body();
      container_close();
      container_open("gamma");
      $p->lay_sidebar_secondary();
      container_close();
      return;
   }


}

function get_layout () : string {
   var Page p = get_page();
   var string lay = $*layout_type;
   if ($lay == "1C" or $lay == "2CR" or $lay == "2CL" or $lay == "3C") {
      return $lay;
   }
   return "2CR";
}

function body_class (string lay) : string {
   var string{} map = {
      "1C" => "layout-one-column",
      "2CL" => "layout-two-column-left",
      "2CR" => "layout-two-column-right",
      "3C" => "layout-three-column",
   };
   return $map{$lay};
}

function Page::print_stylesheets() {
    if ($*use_6a_base) {
        println safe """<link rel="stylesheet" href="http://stat.livejournal.com/sixhtml/themes/common/base-weblog.css" type="text/css" />\n""";
    }

    if ($*base_theme != "__none") {
        println safe """<link rel="stylesheet" href="$*theme_stylesheet{$*base_theme}" type="text/css" />\n""";
    }
    if ($*linked_stylesheet != "") {
        println safe """<link rel="stylesheet" href="$*linked_stylesheet" type="text/css" />\n""";
    }
    # Some themes are too thin for eboxes, so we need extra CSS to stretch them
    if (viewer_sees_ebox()) {
        var string[] eboxextras = [
            "classy",
            "earth",
            "the_road_to_october",
        ];

        foreach var string theme ($eboxextras) {
            if ($*base_theme == $theme) {
                println safe """<link rel="stylesheet" href="$*STATDIR/sixhtml/eboxextras/${theme}.css" type="text/css" />\n""";
            }
        }
    }
    if ($*use_6a_base) {
        println safe """<link rel="stylesheet" href="$*STATDIR/sixhtml/ljextras.css" type="text/css" />\n""";
    }
    if ($*custom_css != "") {
        println """<style type="text/css">""";
        start_css();
        println safe $*custom_css;
        end_css();
        println """</style>""";
    }

    if ($*external_stylesheet) {
        println safe """<link rel="stylesheet" href="$.stylesheet_url" type="text/css" />\n""";
    }
    else {
        println """<style type="text/css">""";
        start_css();
        print_stylesheet();
        end_css();
        println """</style>""";
    }
}

function Page::print () {

 var string lay = get_layout();
 var string body_layout_class = body_class($lay);

 var string[] wide_themes = ["bonjour", "green", "porpoise"]; # These are the wide themes, which look better when the hbox_top is below the banner
 var bool print_below_banner = false;
 foreach var string theme ($wide_themes) {
    if ($theme == $*base_theme) {
        $print_below_banner = true;
    }
 }

   """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="sixapart-standard">
<head>
   """;

    $this->print_head();
    $this->print_stylesheets();
    print safe """<title>""" + $this->title() + """</title>\n""";

"""
</head>
<body class='$body_layout_class lj-view-$.view'>""";

$this->print_control_strip();

if (viewer_sees_hbox_top()) {
    if ($print_below_banner) {
        container_open("container");
        print_banner($this);

        container_open("ad-leaderboard");
        $this->print_hbox_top();
        container_close();
    } else {
        container_open("ad-leaderboard");
        $this->print_hbox_top();
        container_close();

        container_open("container");
        print_banner($this);
    }
} else {
    container_open("container");
    print_banner($this);
}

container_open("pagebody");

 render_layout($this, $lay);

container_close();
container_close();

if (viewer_sees_hbox_bottom()) {
    container_open("ad-5linkunit");
    $this->print_hbox_bottom();
    container_close();
}

"</body></html>";
}



function RecentPage::print_body() {

    if ($.nav.backward_url != "" or $.nav.forward_url != "") {
        println """<p class="content-nav">""";
        if ($.nav.backward_url != "") {
            println """<a href="$.nav.backward_url">"""+get_plural_phrase($.nav.backward_count, "text_skiplinks_back")+"</a>";
        }
        if ($.nav.backward_url != "" and $.nav.forward_url != "") {
            print " | ";
        }
        if ($.nav.forward_url != "") {
            println """<a href="$.nav.forward_url">"""+get_plural_phrase($.nav.forward_count, "text_skiplinks_forward")+"</a>";
        }
        println """</p>""";
    }

    foreach var Entry e ($.entries) {
        if ($*opt_datehead and $e.new_day) {
            print """<h2 class="date-header">"""+$e.time->date_format("med")+"""</h2>""";
        }

        # Print the entry
        $this->print_entry($e);
        if ($e->viewer_sees_ebox()) {
            """<div align="center" style="margin-bottom: 20px;">""";
            $e->print_ebox();
            "</div>";
        }
    }

    if ($.nav.backward_url != "" or $.nav.forward_url != "") {
        println """<p class="content-nav">""";
        if ($.nav.backward_url != "") {
            println """<a href="$.nav.backward_url">"""+get_plural_phrase($.nav.backward_count, "text_skiplinks_back")+"</a>";
        }
        if ($.nav.backward_url != "" and $.nav.forward_url != "") {
            print " | ";
        }
        if ($.nav.forward_url != "") {
            println """<a href="$.nav.forward_url">"""+get_plural_phrase($.nav.forward_count, "text_skiplinks_forward")+"</a>";
        }
        println """</p>""";
    }
}

function EntryPage::print_body() {

    $this->print_entry($.entry);

"""
    <a id="comments"></a>
    <div class="comments">
        <h3 class="comments-header">$*text_comments</h3>
        <div class="comments-content">
            <!-- comment list -->
""";

    if(not $.comment_pages.all_subitems_displayed) {
        $.comment_pages->print();
    }
    foreach var Comment c ($.comments) {
        $this->print_comment($c);
    }
    if(not $.comment_pages.all_subitems_displayed) {
        $.comment_pages->print();
    }

"""
        </div>
    </div>
""";

}

function DayPage::print_body() {
    if ($.has_entries) {
        "<div class=\"day\" id=\"dayyymmmmmdddd\">\n<h2>";
        print $.date->date_format("long");
        "</h2>\n";

        foreach var Entry e ($.entries) {
            $this->print_entry($e);
            if ($e->viewer_sees_ebox()) {
                """<div align="center" style="margin-bottom: 20px;">""";
                $e->print_ebox();
                "</div>";
            }
        }

        "</div>";
    } else {
        "<p>$*text_noentries_day</p>";
    }

    "<div class=\"skiplinks\">\n";
    "<a href=\"$.prev_url\">$*text_day_prev</a> | ";
    "<a href=\"$.next_url\">$*text_day_next</a>\n</div>";

}

function lang_posted_by(UserLite poster, UserLite journal, Page p) : string {
    var string posted = (defined $poster ? ""+$poster : "<i>$*text_poster_anonymous</i>");
    if (not $poster->equals($journal) and not ($p.view == "recent" and $p.journal_type == "C")) {
        $posted = $posted + " in " + $journal;
    }
    return "$posted";
}

function lang_posted_by(UserLite poster, UserLite journal) : string {
    return lang_posted_by($poster, $journal, new Page);
}

function lang_posted_date_and_time(EntryLite e, DateTime dt, bool showdate, bool showtime) : string {
    var string posted = "Posted";
    $posted = $posted + ($showdate ? " on " : " at ");

    if ($showdate and $showtime) {
        if ($e.depth > 0) { # If $e is a comment
            return $posted + $e->time_display("med", "");
        } else {
            return $posted + $dt->date_format("med") + " at " + $dt->time_format();
        }
    }
    if ($showdate and not $showtime) {
        return $posted + $dt->date_format("med");
    }
    if (not $showdate and $showtime) {
        return $posted + $dt->time_format();
    }
    return "";
}

function lang_posted_by_date_and_time(UserLite poster, DateTime dt, bool showposter, bool showdate, bool showtime) : string {
    return lang_posted_date_and_time(new EntryLite, $dt, $showdate, $showtime);
}

function lang_posted_by_date_and_time(EntryLite e, UserLite poster, UserLite journal, DateTime dt, bool showposter, bool showdate, bool showtime) : string {
    return lang_posted_date_and_time($e, $dt, $showdate, $showtime);
}

function Comment::print_linkbar() {
    var Link link;
    var string sep = "<span class='separator'>|</span>";
    var string url = "";
    var string text = "";
    foreach var string link_key ($.link_keyseq) {
        $link = $this->get_link($link_key);
        $url = $link.url;
        $text = $link.caption;
        if (defined $link) {
            """ $sep <a href="$url">$text</a>""";
        }
    }
}

function EntryPage::print_comment(Comment e) {
    if (not $e.full) {
        # Collapsed mode

        "<div class='comment collapsed-comment'"+($e.dom_id ? " id='$e.dom_id'" : "")+">\n";
        "<div class='comment-footer'>";
        var string subject = $e.subject;
        if ($subject == "") { $subject = $*text_nosubject; }
        "<a href='$e.permalink_url' class='collapsed-comment-link'>$subject</a> <span class='separator'>-</span> ";
        print safe lang_posted_by($e.poster, $e.poster); " <span class='separator'>-</span> ";
        print lang_posted_date_and_time($e, $e.time, true, true);
        "</div>";

        if (size($e.replies) > 0) {
            println "<div class='comment-replies'>";

            foreach var Comment c ($e.replies) {
                $this->print_comment($c);
            }

            println "</div>";
        }

        "</div>";

        return;
    }

    "<div class='comment'"+($e.dom_id ? " id='$e.dom_id'" : "")+">\n";

    "<div class='comment-userpic'>";
    if ($*opt_userpic_comments and defined $e.userpic) { "$e.userpic<br />"; }
    print safe "<p class='poster'>"+lang_posted_by($e.poster, $e.poster)+"</p>";
    "</div>";

    if ($e.subject != "") {
        "<h3 class='comment-header'><strong>$e.subject</strong></h3>\n";
    }

    "<div class='comment-content'>";

    var string ip = $e.metadata{"poster_ip"};
    if ($ip != "") {
        var string text = $*text_comment_ipaddr;
        "<div class='metadata'>";
        "<strong>$text</strong> $ip";
        "</div>";
    }

    if (defined $e.subject_icon) {
        """<div class="comment-subject-icon">$e.subject_icon</div>""";
    }

    $e->print_text();
    "</div>";
    "<div class='comment-footer'>";

    var string sep = "<span class='separator'>|</span>";

    print lang_posted_date_and_time($e, $e.time, true, true)+" $sep ";

    "<a class='permalink' href='$e.permalink_url'>$*text_permalink</a>";

    if ($e.parent_url) { print " $sep <a href='$e.parent_url'>$*text_comment_parent</a>"; }
    if ($e.thread_url) { print " $sep <a href='$e.thread_url'>$*text_comment_thread</a>"; }
    if ($e.reply_url)  { print " $sep <a href='$e.reply_url'>$*text_comment_reply</a>"; }

    $e->print_linkbar();
    "</div>";

    if (size($e.replies) > 0) {
        println "<div class='comment-replies'>";

        foreach var Comment c ($e.replies) {
            $this->print_comment($c);
        }

        println "</div>";
    }

    "</div>";
}

function Entry::print_linkbar() {
    var Link link;
    var string sep = "<span class='separator'>|</span>";
    var string url = "";
    var string text = "";
    foreach var string link_key ($.link_keyseq) {
        $link = $this->get_link($link_key);
        $url = $link.url;
        $text = $link.caption;
        if (defined $link) {
            """ $sep <a href="$url">$text</a>""";
        }
    }
}

function Page::print_entry(Entry e) {
    "<div class='entry'" + ($e.dom_id ? " id='$e.dom_id'" : "") + ">\n";

      var bool showtime = $*opt_showtime;
      var bool showdate = not $*opt_datehead;
      var bool showposter = false;
      var bool showuserpic = $*opt_userpic_main;
      if ($.view == "friends") { $showtime = true; $showposter = true; $showuserpic = $*opt_userpic_friends; }
      if ($.view == "day") { $showdate = false; }
      if ($.view == "entry" or $.view == "reply") { $showdate = true; }
      if ($.view == "entry") { $showuserpic = $*opt_userpic_entrypage; }
      if (not $e.poster->equals($e.journal)) { $showposter = true; }

      if (($showuserpic and defined $e.userpic) or $showposter) {
          "<div class='entry-userpic'>";
          if ($showuserpic and defined $e.userpic) { "$e.userpic"; }
          if (($showuserpic and defined $e.userpic) and $showposter) { "<br />"; }
          if ($showposter) { print safe "<p class='poster'>"+lang_posted_by($e.poster, $e.journal, $this)+"</p>"; }
          "</div>";
      }

      "<h3 class='entry-header'>";
      if (defined $e.security_icon) {
          print "<span class='lj-entry-securityicon'>$e.security_icon</span> ";
      }
      "$e.subject</h3>\n";
      "<div class='entry-content'>\n";

         "<div class='entry-body'>\n";
         $e->print_metadata();
         $e->print_text();
         "</div>\n";

         var string sep = "<span class='separator'>|</span>";

         "<p class='entry-footer'>";
         if ($showdate or $showtime) {
             print lang_posted_date_and_time($e, $e.time, $showdate, $showtime)+" $sep ";
         }
         "<a class='permalink' href='$e.permalink_url'>$*text_permalink</a>";


         if ($e.comments.show_postlink) { " $sep "; $e.comments->print_postlink(); }
         if ($e.comments.show_readlink)   { " $sep "; $e.comments->print_readlink(); }

         $e->print_linkbar();
         "</p>";

       "</div>\n";
     "</div>\n";
}

function print_theme_preview() {
    "<img src=\"$*STATDIR/sixhtml/previews/" + $*base_theme + ".jpg\" alt=\"$*base_theme preview\" />";
}
layerinfo type = "layout";
layerinfo name = "Style Contest";
layerinfo redist_uniq = "stylecontest/layout";
layerinfo previews = "stylecontest/layout.jpg";
layerinfo des = "Themes from the Bloggish style contest.";

set base_theme = "the_late_show";
# The colors for the default theme
set theme_bgcolor = "#111111";
set theme_fgcolor = "#eadbc0";
set theme_bordercolor = "#d43402";
set theme_linkcolor = "#eadbc0";

set layout_type = "2CL";