/*
    NAME:    TagListing
    AUTHOR:  Jay Allen, Endevver Consulting, http://endevver.com
    URL:     https://trac.endevver.com/movabletype/wiki/code/taglisting
    DATE:    June 6, 2007
    VERSION: 1.0
    LICENSE: This code is licensed under the Artistic License
    
    SUMMARY:
    
        This library was developed to provide an easy method to output
        Movable Type entry tags using Javascript to prevent spiders from
        crawling the tag search links and kicking off CGI processes.
        
        In addition, it provides for customizable output through use of
        the DisplayTemplate library, which is a prerequisite.  See 
        https://trac.endevver.com/movabletype/wiki/code/displaytemplate
        for more information on that library.

        For details on usage, please see 
        https://trac.endevver.com/movabletype/wiki/code/taglisting

*/
function TagListing() {
    this.template = '<a href="[baseTagSearchURL]?blog_id=[blogid]&tag=[tagquery]" rel="tag">[tagname]</a>'
    this.glue = ", "
    this.strict = 1
    this.nocase = undefined
    this.limit = 0
    this.tags = new Array()


    if (window.baseTagSearchURL)   this.baseTagSearchURL = window.baseTagSearchURL
    if (window.tagDisplayTemplate !== undefined) this.template = window.tagDisplayTemplate
    if (window.tagDisplayGlue !== undefined)     this.glue = window.tagDisplayGlue
}

TagListing.prototype.addTag = function(tag) { this.tags[this.tags.length] = tag; }

TagListing.prototype.format = function(tag) {
    if (tag.baseTagSearchURL == undefined)
        tag.baseTagSearchURL = this.baseTagSearchURL
    dt = new DisplayTemplate(this.template, tag)
    dt.strict = this.strict
    dt.nocase = this.nocase
    return dt.eval()
}

TagListing.prototype.compile = function () {

    if (this.baseTagSearchURL == undefined)
        throw new Error("TagListing property 'baseTagSearchURL' not defined")

    var tagarray = new Array();
    var count = this.limit || this.tags.length
    if (count > this.tags.length) count = this.tags.length
    for (i=0; i<count; i++) {
        tagarray[tagarray.length] = this.format(this.tags[i]);
    }
    glue = this.glue || ""
    var tagstring = tagarray.join(glue);
    return tagstring
}
TagListing.prototype.write = function () { document.write(this.compile()) }
 