layerinfo "type" = "theme";
layerinfo "name" = "Branch Prefers";


function render_layout (Page p, string lay) {
   if ($lay == "3CL") {
      container_open("beta");
      $p->lay_sidebar_secondary();
      container_close();
      container_open("gamma");
      $p->lay_sidebar_primary();
      container_close();
      container_open("alpha");
      $p->print_body();
      container_close();
      return;
   }

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

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

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


}





function colored_ljuser(UserLite u, Color bg, Color fg) : string {
    var Image  uiimg       = userinfoicon($u);
    var string base_url    = get_url($u, "recent");
    var string profile_url = get_url($u, "userinfo");
    return """<a href="$profile_url"><img src="$uiimg.url" alt="[info]" width="$uiimg.width" height="$uiimg.height" /></a><a href="$base_url">$u.username</a>""";
}

function print_entry(Page p, EntryLite e, Color bgcolor, Color fgcolor, bool hide_text) {
    var bool showposter = false;
    if (not $e.poster->equals($e.journal)) { $showposter = true; }

    var Comment c;
    var Entry en;
    if ($e isa Comment) {
        $c = $e as Comment;
    } else {
        $en = $e as Entry;
    }

    var string id = "";
    if (defined $c) {
        $id = " id=\"asset-$e.journal.username-$c.talkid\"";
    } else {
        $id = " id=\"asset-$e.journal.username-$en.itemid\"";
    }

    """
    <div$id class="post-asset asset">
      <div class="asset-inner">
        <div class="asset-header">
          <h2 class="asset-name page-header2">""";

    if (defined $en) {
        if (defined $en.security_icon) {
            """<span class="lj-entry-securityicon">$en.security_icon</span>&nbsp;""";
        }
    }

    var string subject  = "";
    var string datetime = "";

    if ($e.subject != "") {
        $subject  = striphtml($e.subject);
        $datetime = lang_posted_by_date_and_time($e, $showposter, true, true);
    } else {
        $subject  = lang_posted_by_date_and_time($e, $showposter, true,  false);
        $datetime = lang_posted_by_date_and_time($e, $showposter, false, true);
    }

    """<a href="$e.permalink_url">$subject</a>""";
    "</h2>";

    """
          <div class="asset-meta">
            <ul class="asset-meta-list">
              <li class="item"><span><abbr class="datetime">$datetime</abbr></span></li>
            </ul>
          </div>
        </div>
        <div class="asset-content">
          <div class="asset-body">""";
    var bool showuserpic = $*opt_userpic_main;
    if ($p.view == "friends") { 
        $showuserpic = $*opt_userpic_friends;
    }
    if ($p.view == "entry") {
        $showuserpic = $*opt_userpic_entrypage;
    }

    var string userpic = ($showuserpic and defined $e.userpic) ? ($e.userpic + "<br />") : "";
    """<div class="user-icon">$userpic""";

    var string posted = "";
    if ($p.view == "friends") {
        if (not $e.poster->equals($e.journal)) {
            $posted = $posted + colored_ljuser($e.poster, $bgcolor, $fgcolor) + "<br />";
        }
        $posted = $posted + colored_ljuser($e.journal, $bgcolor, $fgcolor);
    } else {
        if (not $e.poster->equals($e.journal)) {
            $posted = $posted + $e.poster;
        }
    }
    print "$posted";

    print "</div>";

    $e->print_text();

    "</div>\n";
    if (defined $en) {
        $en->print_metadata();
    }

    "</div>\n";
    $p->print_reply_container({"target" => "entrycomment", "class" => "quickreply"});
    $e->print_linkbar();

    """<br /><div class="asset-footer"></div>\n</div>\n</div>\n""";
}




set theme_designer = "_branch_";
set theme_designer_type = "ljuser";




function print_module_tags(string title) {

#####   Config   #####

  # Specify your delimiter.  One char only -- extra chars get truncated.
  var string delimiter = ":";

  # Do you want to show the tag use counts?
  var bool show_count = false;

  # Specify the text to show just before the use count, if any
  var string pre_count = "[";

  # Specify the text to show just after the use count, if any
  var string post_count = "]";

##### End Config #####

  var Page p = get_page();
  var string list = "";    
  
  if (size $p->visible_tag_list() > 0) { 
    if ($delimiter->length() > 1) {
      $delimiter = $delimiter->substr(0, 1);
    } 

    var string[] closing_html;
    var string[] prev_tags;
    var int tag_list_pos = 0;
    var string tier_code = "";
    $closing_html[0] = "";
    $prev_tags[0] = "";
    foreach var TagDetail t ($p->visible_tag_list()) {
      var string[] tags;
 
      if ($t.name) {  
        # mt:20050623
        # Split tags into array on delimiter. Oh god, my kingdom
        # for a function.  Stolen shamelessly from lj-user rane500.
        var int array_counter = 0;
        var string buffer = "";
        foreach var string char ($t.name) {
          if($char == $delimiter) {
            $tags[$array_counter] = $buffer;
            $array_counter = $array_counter + 1;
            $buffer = "";
          }
          else {
            $buffer = $buffer + $char;
          } 
        }  
        $tags[$array_counter] = $buffer;
  
        var int pos = 0;
        foreach var string tier($tags) {
          if (size $closing_html <= $pos) {
            # mt:20050623
            # $closing_html keeps track of html that is used to close off open
            # lists.  Its length must be kept >= to that of the current tag.
            $closing_html[$pos] = "";
          }   

          if (size $prev_tags <= $pos) {
            # mt:20050625
            # The current tag has more tiers than the previous tag.  To avoid array
            # ref errors when comparing the current tier to the previous one (which
            # is non-existent, of course) add empty string to $prev_tags.
            $prev_tags[$pos] = "";
          }
    
          # mt:20050623
          # If we're on a tag's last tier, we need to return a link to the tag,
          # otherwise plain text is returned.
          if (size $tags == ($pos + 1)) {
            $tier_code = """<a href="$t.url">$tier</a>""";
            if ($show_count) {
              $tier_code = $tier_code + """&nbsp;${pre_count}${t.use_count}${post_count}""";
            }
          }
          else {
            $tier_code = """$tier""";
          }

          # mt:20050625
          # $prev_tags stuffed with dummy empty string when it has fewer tiers than 
          # current tag.
          if ($prev_tags[$pos] == "") {     
            # mt:20050623
            # The current tag has more tiers than the previous tag, so a new
            # list must be opened.                    
            $list = $list + """<ul class="tagList"><li class="tagItem">$tier_code""";
            $closing_html[$pos] = "</li></ul>";
          }
          elseif ($tags[$pos] != $prev_tags[$pos]) {   
            # mt:20050623
            # The current tag's tier is not the same as the previous tag's tier of  
            # the same level.  This means we may need to close some lists.
            var int i = 0;
            foreach var string html ($closing_html) {
              if ($i > $pos) {
                $list = $list + $closing_html[$i];                                 
                # mt:20050623: As we append the closing code, pop it off the array.
                $closing_html[$i] = "";
              }
              $i++;
            }                    
 
            if ($closing_html[$pos] == "") {          
              # mt:20050623
              # This is the first tier at this level, so open list.
              $list = $list + """<ul class="tagList"><li class="tagItem">$tier_code""";
              $closing_html[$pos] = "</li></ul>";
            }
            else {              
              # mt:20050623
              # There have already been tiers added at this level, so just close the previous
              # list item before adding the new tier.
              $list = $list + """</li><li class="tagItem">$tier_code""";
            }                    
          }
          else {
            # mt:20050623
            # The current tag's tier is exactly the same as the previous tag's tier at
            # this same level.  It has already been included in the list, so do nothing.
          }  
          # mt:20050623: Moving on to next tier in this tag!
          $pos++;
        }
        $prev_tags = $tags;           
      }        
      # mt:20050623: Next tag in the list!
      $tag_list_pos++;
    }  

    # mt:20050623
    # All the tags have been added so close all outstanding lists.
    var int i = 0;
    var string remaining_html = "";
    foreach var string html ($closing_html) { 
      if ($html != "") {
        $remaining_html = $html + $remaining_html;
        $closing_html[$i] = "";
      }
      $i++;
    }
    $list = $list + $remaining_html;
  }
  
  open_module("categories", $title, "");
  """$list""";
  var string tags_url = $p.journal->base_url() + "/tag/"; # TODO: Need Page.view_url{"tags"} which doesn't exist yet.
  print """<div class="widget-footer"><a href="$tags_url" title="$*text_sidebar_tags">$*text_sidebar_tags</a></div>\n""";
  close_module();
}


/* add class to entry page prevnext */

function EntryPage::print_body() {
    var string skiplinks = "";

    var Link forward = $.entry->get_link("nav_next");
    var Link backward = $.entry->get_link("nav_prev");

    if (defined $forward or defined $backward) {
        $skiplinks = """<p class="prevnext">""";
        if (defined $backward) {
            $skiplinks = $skiplinks + """<a href="$backward.url">$*text_entry_prev</a>""";
        }
        if (defined $backward and defined $forward) {
            $skiplinks = $skiplinks + " | ";
        }
        if (defined $forward) {
            $skiplinks = $skiplinks + """<a href="$forward.url">$*text_entry_next</a>""";
        }
        $skiplinks = $skiplinks + """</p>""";
        println safe $skiplinks;
    }
	
	    $this->print_entry($.entry);

    if ($.entry.comments.enabled and $.comment_pages.total_subitems > 0) {

    """<a id="comments"></a>
<div id="comments">
          <div class="comments-inner">
            <h2 class="comments-header page-header2">$*text_comments</h2>
            <div class="comments-body">
              <div class="comments-nav">
                ( """; $this->print_reply_link({"target" => "topcomment", "linktext" => $*text_post_comment}); """ )""";
    $this->print_reply_container({"target" => "topcomment"});
    """</div>""";

    if($.multiform_on) {
        $this->print_multiform_start();
    }

    if(not $.comment_pages.all_subitems_displayed) {
        $.comment_pages->print();
    }

    foreach var Comment c ($.comments) {
        """<div style="margin-left: 0px">""";
        $this->print_comment($c);
        """</div>""";
    }

    if(not $.comment_pages.all_subitems_displayed) {
        $.comment_pages->print();
    }

    if ($.multiform_on) {
        print """<div style="text-align: center">"""; $this->print_multiform_actionline(); print "</div>";
        $this->print_multiform_end();
    }

"""
              <div class="comments-nav">
                ( """; $this->print_reply_link({"target" => "bottomcomment", "linktext" => $*text_post_comment}); """ )""";
    $this->print_reply_container({"target" => "bottomcomment"});

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