/**
 * @author jwhitcraft
 */
// #############################################################################
// initlialize image / image string caches
var imgStringCache = new Array();
var imgCache = new Array();
imgCache = {
    "I"  : '<img src="/images/talkback/tree/tree_i.gif" alt="" />',
    "L"  : '<img src="/images/talkback/tree/tree_ltr.gif" alt="" />',
    "T"  : '<img src="/images/talkback/tree/tree_t.gif" alt="" />'
};

var userAvatar = new Ext.Template(
'                            <td width="70">',
'                                <img src="/shared/image.php?img={user_avatar}&amp;w=65&amp;h=65" width="65" height="65" style="padding-right: 5px;" />',
'                            </td>'
);

var postTemplate = new Ext.Template(
'<table cellpadding="6" cellspacing="1" border="0" width="100%" align="center" class="tborder tb_table">',
'       <tbody>',
'          <tr>',
'              <td class="thead" width="100%">',
'                  <a name="post{id}"></a>',
'                  <span class="floatRight"><a href="{post_link}#post{id}" title="{headline}">#{post_num}</a>&nbsp;<a href="{report_link}" title="Report Post"><img src="/images/talkback/report_post.gif" width="16" height="16" style="vertical-align: middle;" /></a></span>',
'                  {date} {time}',
'              </td>',
'          </tr>',
'      </tbody>',
'      <tbody>',
'            <tr>',
'                <td class="alt1">',
'                    <table cellpadding="2" cellspacing="0" border="0" width="100%" class="user_info">',
'                        <tr>',
'                            {user_avatar_html}',
'                            <td valign="middle">',
'                                <h2 style="margin: 0;">{post_name}</h2>',
'                               {user_title}',
'                            </td>',
'                        </tr>',
'                    </table>',
'                    <img src="/images/talkback/post.gif" width="16" height="16" style="vertical-align: -3px;" />&nbsp;{headline}',
'                    <hr size="1" style="color: #FFFFFF;" />',
'                    {body}',
'                    <div class="floatRight">',
'                       <br />',
'                       <a href="{quotelink}{id}" title="Reply to this Post"><img src="/images/talkback/quote_reply.jpg" width="100" height="20" alt="Reply to this Post" /></a>',
'                    </div>',
'                </td>',
'            </tr>',
'      </tbody>',
'   </table>',
'   <br />');

// #############################################################################
// function to show the next or previous post
function showPrevNextPost(nextOrPrev)
{
    info = pn[curpostid].split(',');
    showPost(info[nextOrPrev]);
}

// #############################################################################
// function to navigate to a new post
function navToPost(postid, noreload) {
    if (postid != 0 && !noreload) {
        //window.location = "topic.php?post=" + postid + "#poststop";
    }
}

// #############################################################################
// function to show a post, either via JS, or to click a link...
function showPost(postid, noreload)
{
    if (typeof pd[postid] != 'undefined')
    {
        try
        {
            Ext.fly("link" + curpostid).setStyle("fontWeight","normal");
            Ext.fly("link" + postid).setStyle("fontWeight","bold");

            // get the post
            var post = pd[postid];

            if(post.user_avatar != "" && post.user_avatar != null) {
                post.user_avatar_html = userAvatar.apply(post);
            }

			post.quotelink = quotelink;

            postTemplate.overwrite("tb_posts",post);

            FIRSTPOSTID = postid;
            LASTPOSTID = postid;
        }
        catch(e)
        {
            navToPost(postid, noreload);
        }
    }
    else
    {
        navToPost(postid, noreload);
    }

    // set the current postid to the clicked link postid
    curpostid = postid;
    return false;
}


// #############################################################################
// function to write a span end
function writeLink(postid, isnew, attachment, userid, imgString, title, datestring, timestring, link)
{
    // get the bgclass for the row and then write it
    if (postid == curpostid)
    {
        bgclass = 'alt2';
    }
    else
    {
        bgclass = 'alt1';
    }
    document.write('<div class="' + bgclass + '" id="div' + postid + '">');

    // check to see if we have already cached the result of this image string
    if (!imgStringCache[imgString])
    {
        // not cached - we need to build the image string and stick it into the cache
        imgStringCache[imgString] = "";

        // split the imgString and write out the appropriate images...
        imgArray = imgString.split(',');

        for (i in imgArray)
        {
            if(typeof imgArray[i] != "function") {
                curType = imgArray[i];
                if (isNaN(curType))
                {
                    // pull the appropriate image from the imgCache
                    imgStringCache[imgString] += imgCache[curType];
                }
                else
                {
                    // write a blank image with the appropriate width
                    imgStringCache[imgString] += '<img src="http://www.indycar.com/shared/images/blank.gif" width="' + (curType * 20) + '" height="20" alt="" />';
                }
            }
        }
    }

    // now write out the cached image string
    document.write(imgStringCache[imgString]);

    // see if we should display the 'new' posticon and then write it
    if (isnew == 1)
    {
        statusicon = 'new';
    }
    else
    {
        statusicon = 'old';
    }
    document.write('<img src="/images/talkback/post_' + statusicon + '.gif" alt="" /> ');

    if (datestring == 'more') {
        document.write('<a href="' + link + '#post' + postid + '" id="link' + postid + '"><i>More replies below current depth...</i></a></div>\n');
    } else {

        document.write(userid.bold() + " ");


        // set the title link
        if (postid == curpostid)
        {
            titlestyle = ' style="font-weight:bold;"';
        }
        else
        {
            titlestyle = '';
        }

        document.write('<a href="' + link + '#post' + postid + '" onclick="return showPost(' + postid + ')" id="link' + postid + '"' + titlestyle + '>' + title + '</a> ');

        document.write(datestring + ', <span class="time">' + timestring + '</span>');

        // complete the string
        document.write('</div>\n');

    }
}

// #############################################################################
// function to write a span end
function writeLinkStory(postid, isnew, attachment, userid, imgString, title, datestring, timestring, link)
{
    // get the bgclass for the row and then write it
    bgclass = 'alt1';
    document.write('<div class="' + bgclass + '" id="div' + postid + '">');

    // check to see if we have already cached the result of this image string
    if (!imgStringCache[imgString])
    {
        // not cached - we need to build the image string and stick it into the cache
        imgStringCache[imgString] = "";

        // split the imgString and write out the appropriate images...
        imgArray = imgString.split(',');

        for (i in imgArray)
        {
            if(typeof imgArray[i] != "function") {
                curType = imgArray[i];
                if (isNaN(curType))
                {
                    // pull the appropriate image from the imgCache
                    imgStringCache[imgString] += imgCache[curType];
                }
                else
                {
                    // write a blank image with the appropriate width
                    imgStringCache[imgString] += '<img src="http://www.indycar.com/shared/images/blank.gif" width="' + (curType * 20) + '" height="20" alt="" />';
                }
            }
        }
    }

    // now write out the cached image string
    document.write(imgStringCache[imgString]);

    // see if we should display the 'new' posticon and then write it
    if (isnew == 1)
    {
        statusicon = 'new';
    }
    else
    {
        statusicon = 'old';
    }
    document.write('<img src="/images/talkback/post_' + statusicon + '.gif" alt="" /> ');

    if (datestring == 'more') {
        document.write('<a href="' + link + '#post' + postid + '" id="link' + postid + '"><i>More replies below current depth...</i></a></div>\n');
    } else {

        document.write(userid.bold() + " ");


        // set the title link
        titlestyle = '';

        document.write('<a href="' + link + '#post' + postid + '" id="link' + postid + '"' + titlestyle + '>' + title + '</a> ');

        document.write(datestring + ', <span class="time">' + timestring + '</span>');

        // complete the string
        document.write('</div>\n');

    }
}