/* Bloggish S2 alterations */
layerinfo type = "layout";
layerinfo name = "Branch Prefers";


set base_theme = "__none";

/* put a div around previous/next instead of a p */
function RecentPage::print_body() {

    if ($.nav.backward_url != "" or $.nav.forward_url != "") {
        println """<div 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 """</div>""";
    }

    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 ($.nav.backward_url != "" or $.nav.forward_url != "") {
        println """<div 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 """</div>""";
    }
}

/* put a div around the entry header */
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; }

      

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

         "<div class='entry-body'>\n";
		 
		 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) { "<p class='poster'>"+lang_posted_by($e.poster, $e.journal, $this)+"</p>"; }
          "</div>";
      }
		 
         $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";
}

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

/* adding a footer div, also a clearing footer below columns */
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();
    """<title>""" + $this->title() + """</title>\n""";

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

$this->print_control_strip();

container_open("container");

if (viewer_sees_hbox_top()) {
    if ($print_below_banner) {
        print_banner($this);

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

        print_banner($this);
    }
} else {
    print_banner($this);
}

container_open("pagebody");

 render_layout($this, $lay);
 
 "<div id='content-footer'></div>";

container_close();

"<div id='page-footer'><div id='page-footer-inner'></div></div>";

container_close();

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

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

/* add archive link to Calendar */
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_short[$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>""";
	
	"<a href='http://branchandroot.insanejournal.com/calendar'>Archive</a>

    close_module();
}

/* moving nav links to banner */
function print_banner(Page p) {
    container_open("banner");
	
    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);	
	
      "<h1 id='banner-header'>$p.global_title</h1>";
      "<h2 id='banner-description'>$p.global_subtitle</h2>";
    container_close();
}










/* sidebar options add (from expressive)-to go just before propgroup css */
propgroup sidebars = "Sidebars";
propgroup sidebars {
    property string tag_display {
        des = "Display of tags in sidebar";
        values = "cloud|Cloud|list|List";
    }
    set tag_display = "cloud";

    property bool opt_tag_limit {
        des = "Cut off tag display in sidebar after certain limit.";
    }
    set opt_tag_limit = false;

    property int tag_limit {
        des = "Limit of tags to display in Tags sidebar.";
    }
    set tag_limit = 0;

    property string first_item {
        des = "First item in sidebar";
        values = "none|(none)|userprofile|Profile|calendar|Calendar|links|Links|tags|Tags|archive|Page Summary|syndicate|Syndicate|customtext|Custom Text";
    }
    set first_item = "userprofile";

    property string second_item {
        des = "Second item in sidebar";
        values = "none|(none)|userprofile|Profile|calendar|Calendar|links|Links|tags|Tags|archive|Page Summary|syndicate|Syndicate|customtext|Custom Text";
    }
    set second_item = "calendar";

    property string third_item {
        des = "Third item in sidebar";
        values = "none|(none)|userprofile|Profile|calendar|Calendar|links|Links|tags|Tags|archive|Page Summary|syndicate|Syndicate|customtext|Custom Text";
    }
    set third_item = "links";

    property string fourth_item {
        des = "Fourth item in sidebar";
        values = "none|(none)|userprofile|Profile|calendar|Calendar|links|Links|tags|Tags|archive|Page Summary|syndicate|Syndicate|customtext|Custom Text";
    }
    set fourth_item = "tags";

    property string fifth_item {
        des = "Fifth item in sidebar";
        values = "none|(none)|userprofile|Profile|calendar|Calendar|links|Links|tags|Tags|archive|Page Summary|syndicate|Syndicate|customtext|Custom Text";
    }
    set fifth_item = "archive";

    property string sixth_item {
        des = "Sixth item in sidebar";
        values = "none|(none)|userprofile|Profile|calendar|Calendar|links|Links|tags|Tags|archive|Page Summary|syndicate|Syndicate|customtext|Custom Text";
    }
    set sixth_item = "syndicate";

    property string seventh_item {
        des = "Seventh item in sidebar";
        values = "none|(none)|userprofile|Profile|calendar|Calendar|links|Links|tags|Tags|archive|Page Summary|syndicate|Syndicate|customtext|Custom Text";
    }
    set seventh_item = "none";

    property string customtext_title {
        des = "Title to use for custom text sidebar module";
    }
    set customtext_title = "";

    property string customtext_url {
        des = "URL to link to from custom text sidebar module";
    }
    set customtext_url = "";

    property string customtext_body {
        des = "Body of custom sidebar module";
        cols = 40;
        rows = 10;
        string_mode = "html";
    }
    set customtext_body = "";
}

/* and to arrange them */
function prop_init() {
    # make array of modules
    var string[] modules = [];
    if (viewer_sees_vbox()) {
        $modules = [$*first_item, "ads", $*second_item, $*third_item, $*fourth_item, $*fifth_item, $*sixth_item, $*seventh_item];
    } else {
        $modules = [$*first_item, $*second_item, $*third_item, $*fourth_item, $*fifth_item, $*sixth_item, $*seventh_item];
    }

    # Find total of modules that are actually used
    var int total = 0;
    var string[] used_modules = [];
    foreach var string module ($modules) {
        if ($module != "none") {
            $used_modules[$total] = $module;
            $total++;
        }
    }

    # keep track of where we are
    var int count = 0;

    # figure out which module properties we're using
    var string[] properties = [];

    # cycle thru each module
    foreach var string module ($used_modules) {
        # userprofile
        if ($module == "userprofile") {
            $properties = ["userprofile", "Profile", "1", "1"];

        # mini-calendar
        } elseif ($module == "calendar") {
            $properties = ["calendar", "Latest Month"];

        # linklist
        } elseif ($module == "links") {
            $properties = ["links", "Links"];

        # tags
        } elseif ($module == "tags") {
            $properties = ["tags", "Tags"];

        # summary
        } elseif ($module == "archive") {
            $properties = ["archive", "Page Summary"];

        # syndicate
        } elseif ($module == "syndicate") {
            $properties = ["syndicate", "Syndicate"];

        # ads
        } elseif ($module == "ads") {
            $properties = ["ads", ""];

        # custom text
        } elseif ($module == "customtext") {
            $properties = ["customtext",$*customtext_title,$*customtext_body,$*customtext_url];
        }

        # Divide in half to find out which sidebar to allocate module to
        var int maxcount = $total / 2;

        # If the total can't be divided cleanly in half, add 1
        if (($maxcount * 2) != $total) {
            $maxcount++;
        }

        # index is % $maxcount to find position for sidebar
        var int index = $count % $maxcount;
        $count++;

        if ($count <= $maxcount) {
            # if count <= $maxcount, put in primary
            $*sidebar_primary[$index] = $properties;
        } else {
            # if count > $maxcount, put in secondary
            $*sidebar_secondary[$index] = $properties;
        }
    }

    # Put poweredby module last
    $*sidebar_secondary[size($*sidebar_secondary)] = ["poweredby"];


    # 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;
    }
}