function invisionApi()
{
	this.boardUrl = 'http://boards.baltimoreravens.com'; // The board url (NEVER include the ending slash)
	this.cdnUrl = 'http://content.boards.baltimoreravens.com'; // The CDN url, example: 'http://cdnurl.com' (NEVER include the ending slash)
	this.imagesFolder = 'ravens'; // The style_images folder of the skin
	this.memberData = null;
	this.articleData = null;
	this.articleComments = null;
	this.systemSettings = null;
	this.guid = null;
	this.commentsStart = 0;
	this.reputation = {};
	this.facebook_enabled = true;
	this.twitter_enabled = true;
}
	
	invisionApi.prototype.getLoginBarTemplate = function() {
		var str='';
		
		if ( !this.memberData.pp_thumb_photo )
		{
			this.memberData.pp_thumb_photo = this.memberData.photo;
		}

		if ( !this.memberData.name )
		{
			this.memberData.name = this.memberData.members_display_name;
		}


		if (  this.memberData != 0 ) {
			document.getElementById("headerSocialBar").className = 'loggedIn';
			str+='<div id="socialBarDropWrap"><div id="socialBarDropContainer">\n\t\t\t<p class="socialBarDropTitle"><span>THE</span>FLOCK</p>\n\t\t\t<ul class="socialDropList">';

			// Add link list
			str+='<li><a href="' + this.boardUrl + '/index.php?'+ this.memberData.profile +'">My Profile</a></li><li><a href="' + this.boardUrl + '/index.php?app=core&module=usercp">My Settings</a></li><li><a href="' + this.boardUrl + '/index.php?app=members&module=messaging">Messenger</a></li><li><a href="' + this.boardUrl + '/index.php?app=members&module=profile&section=friends&do=list">Manage Friends</a></li><li><a href="' + this.boardUrl + '/index.php?app=core&module=usercp&tab=members&area=ignoredusers">Managed Ignored Users</a></li><li><a href="' + this.boardUrl + '/index.php?app=gallery&module=user&section=user&user='+this.memberData.member_id+'">My Gallery</a></li><li><a href="' + this.boardUrl + '/index.php?app=core&module=help">Help</a></li>';
			
			if ( this.memberData.validation == 1) {
					str+='<li><a href="' + this.boardUrl + '/index.php?app=core&module=global&section=register&do=reval">Resend Validation</a></li>';
			}

			str+='</ul><a id="socialDropLogout" href="' + this.boardUrl + '/index.php?app=core&amp;module=global&amp;section=login&amp;do=logout&amp;k='+ this.memberData.form_hash +'&amp;return=' + location.href+'">Logout</a></div><div id="socialBarWelcome"><a href="' + this.boardUrl + '/index.php?'+ this.memberData.profile +'">'+this.memberData.name+'</a></div></div><a id="socialBarLogout" href="' + this.boardUrl + '/index.php?app=core&amp;module=global&amp;section=login&amp;do=logout&amp;k='+ this.memberData.form_hash +'&amp;return=' + location.href+'">Logout</a><img src="'+this.memberData.pp_thumb_photo+'" id="socialBarIcon" width="31" height="31" />\n';
		}
		else
		{
			//GUEST
			str+='<div id="socialBarDropWrap"><div id="socialBarDropContainer"><a href="#mainFooter" class="socialDropLink">Looking for your PSL login?</a><br /><br /><p class="socialBarDropTitle">Log In To The Flock</p><p>Enter your member name and password to sign in.</p><form action="'+this.boardUrl+'/index.php?app=core&amp;module=global&amp;section=login&amp;do=process&amp;auth_key=880ea6a14ea49e853634fbdc5015a024" method="post"><fieldset><input type="hidden" name="return" id="return" value="'+ escape(location.href) +'" /><label for="username">Username or Email*</label><input type="text" name="ips_username" id="username" /><label for="password">Password*</label><input type="password" name="ips_password" id="password" /><br />Remember me?&nbsp;<input type="checkbox" checked="checked" name="rememberMe" value="1" id="remember" style="width: 20px;"/><br /><button type="submit">Submit</button></fieldset></form><p><a href="'+this.boardUrl+'/interface/facebook/index.php?reg=1"><img src="' + (this.cdnUrl != null ? this.cdnUrl : this.boardUrl) + '/public/style_images/ravens/facebook_login.png" alt="" /></a></p><p><a href="'+this.boardUrl+'/interface/twitter/index.php?reg=1"><img src="'+(this.cdnUrl != null ? this.cdnUrl : this.boardUrl)+'/public/style_images/ravens/twitter_login.png" alt="" /></a></p><a href="'+this.boardUrl+'/index.php?app=core&amp;module=global&amp;section=lostpass" class="socialDropLink">Forget your password?</a><a href="'+this.boardUrl+'/index.php?app=core&amp;module=global&amp;section=register" id="socialDropLogout" class="socialDropLink">Want to sign up?</a></div><div id="socialBarWelcome"><img src="http://www.baltimoreravens.com/media/navigation/social_bar_myravens.jpg" /></div><a id="socialBarLogin" href="'+this.boardUrl+'/index.php?app=core&amp;module=global&amp;section=login">Login</a></div>&nbsp;|&nbsp;<a id="socialBarJoin" href="'+this.boardUrl+'/index.php?app=core&amp;module=global&amp;section=register">Join</a><img src="' + (this.cdnUrl != null ? this.cdnUrl : this.boardUrl) + '/public/style_images/ravens/profile/default_thumb.png" width="31" height="31" id="socialBarIcon" />\n';
		}
		
		return str;
	};
	    invisionApi.prototype.initialize = function(boardUrl, imagesFolder) {
    	imagesFolder = imagesFolder ? imagesFolder : 'master';
    	
        this.boardUrl = boardUrl;
        this.imagesFolder = imagesFolder;
	};

    invisionApi.prototype.getMemberData = function( returnCallBack ) {
    	var thisObj = this;
    	
    	/* Check cookies */
		var ca = document.cookie.split(';');
		
		for(var i=0;i < ca.length;i++)
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length); // Trim leading spaces
			cookieArray = c.split('=');
			if ( cookieArray[0] == 'member_json' )
			{
				thisObj.memberData = eval('(' +unescape(cookieArray[1])+ ')');
				thisObj.memberData.name = decodeURI(this.memberData.members_display_name ? this.memberData.members_display_name : thisObj.memberData.name);
				
				returnCallBack(thisObj);
			}
		}
		
		/* Fallback if we don't have the cookie for some reasons... */
		if ( thisObj.memberData == null )
    	{
    		//thisObj.memberData = { form_hash: "880ea6a14ea49e853634fbdc5015a024" };
    		thisObj.systemSettings = { memberFormHash: "880ea6a14ea49e853634fbdc5015a024", facebook_enabled : true, twitter_enabled : true };
    		
	        //jQuery.getJSON(this.boardUrl + "/index.php?app=core&module=commentsApiLogin&jsoncallback=?", function(json) {
	        //    if ( json.status != 'ok' ) {
	        //        return false;
	        //    }
				
	        //    if ( json.memberData && typeof json.memberData === 'object' && json.memberData.id > 0 ) {
	        //        thisObj.memberData = json.memberData;
	        //    } else {
	         //       thisObj.memberData = 0;
	         //   }
	            
	        //    if ( json.systemSettings && typeof json.systemSettings === 'object' ) {
	        //        thisObj.systemSettings = json.systemSettings;
	         //   }
	            thisObj.memberData = 0;
	            returnCallBack(thisObj);
	        // } );
    	}
    };

    invisionApi.prototype.fetchCommentSystemDataByGuid = function ( guid, returnCallBack, commentsLimit ) {
    	commentsLimit = ( parseInt(commentsLimit) > 0 ) ? parseInt(commentsLimit) : 10;
        var thisObj = this;
        
        /* Force caching */
        jQuery.ajaxSetup({ cache: true });
        
        if ( this.cdnUrl == null )
        {
        	var getUrl = this.boardUrl + "/index.php?app=forums&module=commentsApiSystem&guid=" + escape(escape(guid)) + "&limit=" + commentsLimit + "&jsoncallback=?";
        }
        else
		{
			var getUrl = this.cdnUrl + "/cdn_js/serve.php/" + encodeURIComponent(encodeURIComponent(guid)) + ".js&limit=" + commentsLimit + "&jsoncallback=?";
		} 
        
        jQuery.getJSON( getUrl, function (json) {
            if (json.status != 'ok' || typeof json.articleData !== 'object') {
                return false;
            }

            thisObj.articleData = json.articleData;

            if (typeof json.articleComments === 'object' && json.articleComments.length > 0) {
                thisObj.articleComments = json.articleComments;
            } else {
                thisObj.articleComments = null;
            }
			
            returnCallBack(thisObj);
        });
    };
	
    invisionApi.prototype.fetchCommentSystemDataByUrl = function( url, returnCallBack, commentsLimit ) {
    	commentsLimit = ( parseInt(commentsLimit) > 0 ) ? parseInt(commentsLimit) : 10;
        var thisObj = this;
        
        jQuery.ajaxSetup({ cache: true });
        jQuery.getJSON(this.boardUrl + "/index.php?app=forums&module=commentsApiSystem&do=fetchCommentsByUrl&url=" + escape(escape(url)) + "&limit=" + commentsLimit + "&jsoncallback=?", function(json) {
            if ( json.status != 'ok' || typeof json.articleData !== 'object' ) {
                return false;
            }
			
            thisObj.articleData = json.articleData;

            if ( typeof json.articleComments === 'object' && json.articleComments.length > 0 )
            {
                thisObj.articleComments = json.articleComments;
            } else {
                thisObj.articleComments = null;
            }
			
            returnCallBack(thisObj);
         } );
    };

    invisionApi.prototype.writeLoginBar = function() {
        var loginBar = document.getElementById("headerSocialBar");
        if ( !loginBar ) {
            document.writeln("\n<div id=\"headerSocialBar\"></div>");
            loginBar = document.getElementById("headerSocialBar");
        }

        if ( this.memberData == null )
        {
            this.getMemberData( function(thisObj) {thisObj.writeLoginBar();});
            return;
        }

        loginBar.innerHTML = this.getLoginBarTemplate();
    };

    invisionApi.prototype.getNewCommentsByGuid = function (guid, commentsLimit) {
    	this.fetchCommentSystemDataByGuid(guid, function (thisObj) { thisObj.writeCommentsByGuid(guid, commentsLimit); }, commentsLimit);
    };

    invisionApi.prototype.writeCommentsByGuid = function( guid, commentsLimit ) {
    	commentsLimit = ( parseInt(commentsLimit) > 0 ) ? parseInt(commentsLimit) : 10;
    	
        var thisObj = this;
        thisObj.guid = guid;

        // Check for commentSystem container, create one if none exist.
        var commentBar = document.getElementById("commentSystem");
        if ( !commentBar ) {
            document.writeln("\n<div id=\"commentSystem\"></div>");
            commentBar = document.getElementById("commentSystem");
        }

        // Check if we have the articleData, if not get it
        if ( this.articleData == null ) {
            this.fetchCommentSystemDataByGuid( guid, function(thisObj){thisObj.writeCommentsByGuid(guid, commentsLimit);}, commentsLimit );
            return;
        }

        var str = ''; //'<div id="respond">';

        if ( this.memberData != null && this.memberData != 0 )
        {
            str+='<p id="commentSystem-Guidelines"><strong><a href="'+this.boardUrl+'/index.php?app=forums&module=extras&section=boardrules" target="top">Golden Rules</a>:</strong> <strong>1.</strong> These comments are to be about Ravens football and football only. <strong>2.</strong> Absolutely no profanity or obscene, vulgar or violent photos, period. <strong>3.</strong> Zero tolerance for personal attacks on anyone including mods. <strong>4.</strong> Don\'t write in all CAPS! It\'s "YELLING" and unnecessary. <strong>5.</strong> No selling tickets, merch or anything else. No spamming or illegal activity. <strong>6.</strong> No political or religious discussions - see rule #1. <strong>7.</strong> No "nonsense" posts that don\'t contribute to the football discussion. Ignorance of these rules is no excuse for breaking them.</p>';

            str+='<h3>Leave a Reply</h3>';
            str+='<div id="commentSystem-reply">';
            str+='<form id="commentSystem-replyForm" action="'+this.boardUrl+'" method="post" enctype="multipart/form-data">';
            str+='<fieldset>';
            str+='<input type="hidden" id="app" name="app" value="forums" />';
            str+='<input type="hidden" id="module" name="module" value="post" />';
            str+='<input type="hidden" id="section" name="section" value="post" />';
            str+='<input type="hidden" id="do" name="do" value="remotePost" />';
            str+='<input type="hidden" id="p" name="p" value="0" />';
            str+='<input type="hidden" id="t" name="t" value="'+thisObj.articleData.tid+'" />';
            str+='<input type="hidden" id="f" name="f" value="'+thisObj.articleData.fid+'" />';
            str+='<input type="hidden" id="auth_key" name="auth_key" value="'+this.memberData.form_hash+'" />';
            str+='<input type="hidden" id="enabletrack" name="enabletrack" value="0" />';
            str+='<input type="hidden" id="preview" name="preview" value="0" />';
            str+='<input type="hidden" id="commentsLimit" name="commentsLimit" value="'+commentsLimit+'" />';
            str+='</fieldset>';
            str+='<fieldset class="submit">';
            str+='<textarea name="Post" id="Post" rows="5" cols="60"></textarea>';
            str+='<button type="submit" id="submitBtn">Submit</button>';
            str+='</fieldset>';
            str+='</form>';
            str+='</div>';
        }

        if ( this.memberData == null )
        {
            this.getMemberData( function(thisObj){thisObj.writeCommentsByGuid( guid );} );
            return;
        } else if ( this.memberData == 0 ) {
            str+='<p id="commentSystem-userBar">You must be <a href="'+this.boardUrl+'/index.php?app=core&amp;module=global&amp;section=login&amp;referer='+location.href+'"><b>logged in</b></a> to post a comment.</p>';
        } else {
            str+='<p id="commentSystem-userBar">You are logged in as <a href="'+this.boardUrl+'/index.php?'+this.memberData.profile+'"><b>'+this.memberData.name+'</b></a>.</p>';
        }

        //str+='</div>';
        str+='<br />';
        str+='<table width="656" height="31" cellspacing="0" cellpadding="0" border="0" bgcolor="#e8e8e8"><tbody><tr>';
        str+='<td width="144"><span class="commentstitle">'+this.articleData.posts+' Comments</span></td>';
        str+='<td>Sort By: <span class="commentFilter newest selected" style="font-weight: bold;">Newest First</span> | <span class="commentFilter oldest" style="text-decoration: underline; font-weight: bold; cursor: pointer;">Oldest First</span> | <span class="commentFilter recommended" style="text-decoration: underline; font-weight: bold; cursor: pointer;">Most Recommended</span></td>';
        str+='</tr></tbody></table>';

        // Top Pagination
        if ( this.articleComments != null && this.articleComments.length > 0 && this.articleData.posts > commentsLimit )
        {
            totalPages = Math.ceil( this.articleData.posts / commentsLimit );
            currentPage = this.commentsStart > 0 ? (this.commentsStart / commentsLimit) + 1 : 1;

            str+='<div id="commentSystem-topPagination"><span class="total">Page ' + currentPage + ' of ' + totalPages + '</span><ul>';

            if ( currentPage > 3 ) { str+='<li class="first"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', 0);">First</a></li>'; }
            if ( currentPage > 1 ) { str+='<li class="prev"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart - commentsLimit) + ');">Previous</a></li>'; }
            if ( currentPage - 2 > 0 ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart - (commentsLimit*2)) + ');">' + (currentPage - 2) + '</a></li>'; }
            if ( currentPage - 1 > 0 ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart - commentsLimit) + ');">' + (currentPage - 1) + '</a></li>'; }
            str+='<li class="active">' + currentPage + '</li>';
            if ( currentPage + 1 <= totalPages ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart + commentsLimit) + ');">' + (currentPage + 1) + '</a></li>'; }
            if ( currentPage + 2 <= totalPages ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart + (commentsLimit*2)) + ');">' + (currentPage + 2) + '</a></li>'; }
            if ( currentPage < totalPages ) { str+='<li class="next"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart + commentsLimit) + ');">Next</a></li>'; }
            if ( currentPage + 3 <= totalPages ) { str+='<li class="last"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + ((totalPages -1) * commentsLimit) + ');">Last</a></li>'; }
            str+='</ul></div>';
        } else {
            str+='<div id="commentSystem-pagination no_pages">Page 1 of 1</div>';
        }

        str+='<div id="commentList">';
        str+='<ul id="commentsList_'+guid+'">';

        if ( this.articleComments != null && this.articleComments.length > 0 )
        {
            jQuery.each(this.articleComments,function(id, data) {
                var repStyle = data.rep_points > 0 ? 'positive' : (data.rep_points < 0 ? 'negative' : 'zero' );

                str+='<li id="li-comment-'+data.pid+'" class="li-comment">';

                // Reputation
                str+='<div id="rep_post_'+data.pid+'" class="li-comment-rep">';
                str+='<span title="Reputation" class="reputation ' + repStyle + ' rep_show">'+data.rep_points+'</span>';
                
                if ( thisObj.memberData.member_id > 0 && data.userCanRate )
				{
                    str+='<a title="Vote this post up" class="rep_up" href="'+thisObj.boardUrl+'/index.php?app=core&amp;module=global&amp;section=reputation&amp;do=add_rating&amp;app_rate=forums&amp;type=pid&amp;type_id='+data.pid+'&amp;rating=1&amp;secure_key='+thisObj.memberData.form_hash+'&amp;post_return='+data.pid+'"><img src="'+(thisObj.cdnUrl != null ? thisObj.cdnUrl : thisObj.boardUrl)+'/public/style_images/'+thisObj.imagesFolder+'/add.png" alt="Vote this post up" /></a>';
                    str+='<a title="Vote this post down" class="rep_down" href="'+thisObj.boardUrl+'/index.php?app=core&amp;module=global&amp;section=reputation&amp;do=add_rating&amp;app_rate=forums&amp;type=pid&amp;type_id='+data.pid+'&amp;rating=-1&amp;secure_key='+thisObj.memberData.form_hash+'&amp;post_return='+data.pid+'"><img src="'+(thisObj.cdnUrl != null ? thisObj.cdnUrl : thisObj.boardUrl)+'/public/style_images/'+thisObj.imagesFolder+'/delete.png" alt="Vote this post down" /></a>';
                }
                str+='</div>';
				
                // User information
                str+='<div class="li-comment-head">';
                str+='<strong><a href="'+thisObj.boardUrl+'/index.php?showuser='+data.memberData.member_id+'">'+data.memberData.members_display_name+'<img width="48" height="48" class="avatar avatar-48 photo" src="'+(data.memberData.pp_thumb_photo ? data.memberData.pp_thumb_photo : (thisObj.cdnUrl != null ? thisObj.cdnUrl : thisObj.boardUrl)+'/public/style_images/'+thisObj.imagesFolder+'/profile/default_thumb.png')+'" alt="" /></a></strong>';
                str+='<br />Posted<br/><a href="'+thisObj.boardUrl+'/index.php?showtopic='+data.topic_id+'&amp;view=findpost&amp;p='+data.pid+'">'+data.formattedPostDate+'</a>';
                str+='</div>';

                // Article  comment
                str+='<div id="comment-'+data.pid+'" class="li-comment-body">'+data.post+'</div>';

                str+='</li>';
            } );
        }
        str+='</ul></div>';

        // Bottom Pagination
        if ( this.articleComments != null && this.articleComments.length > 0 && this.articleData.posts > commentsLimit )
        {
            totalPages = Math.ceil( this.articleData.posts / commentsLimit );
            currentPage = this.commentsStart > 0 ? (this.commentsStart / commentsLimit) + 1 : 1;

            str+='<div id="commentSystem-bottomPagination"><span class="total">Page ' + currentPage + ' of ' + totalPages + '</span><ul>';

            if ( currentPage > 3 ) { str+='<li class="first"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', 0);">First</a></li>'; }
            if ( currentPage > 1 ) { str+='<li class="prev"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart - commentsLimit) + ');">Previous</a></li>'; }
            if ( currentPage - 2 > 0 ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart - (commentsLimit*2)) + ');">' + (currentPage - 2) + '</a></li>'; }
            if ( currentPage - 1 > 0 ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart - commentsLimit) + ');">' + (currentPage - 1) + '</a></li>'; }
            str+='<li class="active">' + currentPage + '</li>';
            if ( currentPage + 1 <= totalPages ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart + commentsLimit) + ');">' + (currentPage + 1) + '</a></li>'; }
            if ( currentPage + 2 <= totalPages ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart + (commentsLimit*2)) + ');">' + (currentPage + 2) + '</a></li>'; }
            if ( currentPage < totalPages ) { str+='<li class="next"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart + commentsLimit) + ');">Next</a></li>'; }
            if ( currentPage + 3 <= totalPages ) { str+='<li class="last"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\'newest\', ' + commentsLimit + ', ' + ((totalPages -1) * commentsLimit) + ');">Last</a></li>'; }
            str+='</ul></div>';
        }

        commentBar.innerHTML = str;
        
        if ( thisObj.memberData != 0 )
       	{
	        jQuery.each(thisObj.articleComments,function(id, data) {
	            if ( data.userCanRate )
				{
	                thisObj.registerReputation( 'rep_post_' + data.pid, { app: 'forums', type: 'pid', typeid: data.pid }, parseInt(data.rep_points) );
	            }
	        } );
       	}
    };
	
    invisionApi.prototype.writeCommentsByUrl = function( guid, commentsLimit ) {
    	commentsLimit = ( parseInt(commentsLimit) > 0 ) ? parseInt(commentsLimit) : 10;
        var thisObj = this;
        thisObj.guid = guid;

        // Check for commentSystem container, create one if none exist.
        var commentBar = document.getElementById("commentSystem");
        if ( !commentBar ) {
            document.writeln("\n<div id=\"commentSystem\"></div>");
            commentBar = document.getElementById("commentSystem");
        }

        // Check if we have the articleData, if not get it
        if ( this.articleData == null ) {
            this.fetchCommentSystemDataByUrl( guid, function(thisObj){thisObj.writeCommentsByUrl(guid, commentsLimit);}, commentsLimit );
            return;
        }

        var str = ''; //'<div id="respond">';

        // Add guidelines, if they were given

        if ( this.memberData != null && this.memberData != 0 )
        {
            str+='<p id="commentSystem-Guidelines"><strong><a href="'+this.boardUrl+'/index.php?app=forums&module=extras&section=boardrules" target="top">Golden Rules</a>:</strong> <strong>1.</strong> These comments are to be about Ravens football and football only. <strong>2.</strong> Absolutely no profanity or obscene, vulgar or violent photos, period. <strong>3.</strong> Zero tolerance for personal attacks on anyone including mods. <strong>4.</strong> Don\'t write in all CAPS! It\'s "YELLING" and unnecessary. <strong>5.</strong> No selling tickets, merch or anything else. No spamming or illegal activity. <strong>6.</strong> No political or religious discussions - see rule #1. <strong>7.</strong> No "nonsense" posts that don\'t contribute to the football discussion. Ignorance of these rules is no excuse for breaking them.</p>';

            str+='<h3>Leave a Reply</h3>';
            str+='<div id="commentSystem-reply">';
            str+='<form id="commentSystem-replyForm" action="'+this.boardUrl+'" method="post" enctype="multipart/form-data">';
            str+='<fieldset>';
            str+='<input type="hidden" id="app" name="app" value="forums" />';
            str+='<input type="hidden" id="module" name="module" value="post" />';
            str+='<input type="hidden" id="section" name="section" value="post" />';
            str+='<input type="hidden" id="do" name="do" value="remotePost" />';
            str+='<input type="hidden" id="p" name="p" value="0" />';
            str+='<input type="hidden" id="t" name="t" value="'+thisObj.articleData.tid+'" />';
            str+='<input type="hidden" id="f" name="f" value="'+thisObj.articleData.fid+'" />';
            str+='<input type="hidden" id="auth_key" name="auth_key" value="'+this.memberData.formHash+'" />';
            str+='<input type="hidden" id="enabletrack" name="enabletrack" value="0" />';
            str+='<input type="hidden" id="preview" name="preview" value="0" />';
            str+='</fieldset>';
            str+='<fieldset class="submit">';
            str+='<textarea name="Post" id="Post" rows="5" cols="60"></textarea>';
            str+='<button type="submit" id="submitBtn">Submit</button>';
            str+='</fieldset>';
            str+='</form>';
            str+='</div>';
        }

        if ( this.memberData == null )
        {
            this.getMemberData( function(thisObj){thisObj.writeCommentsByUrl( guid );} );
            return;
        } else if ( this.memberData == 0 ) {
            str+='<p id="commentSystem-userBar">You must be <a href="'+this.boardUrl+'/index.php?app=core&amp;module=global&amp;section=login&amp;referer='+location.href+'"><b>logged in</b></a> to post a comment.</p>';
        } else {
            str+='<p id="commentSystem-userBar">You are logged in as <a href="'+this.memberData.profile.href+'"><b>'+this.memberData.name+'</b></a>.</p>';
        }

        //str+='</div>';
        str+='<br />';
        str+='<table width="656" height="31" cellspacing="0" cellpadding="0" border="0" bgcolor="#e8e8e8"><tbody><tr>';
        str+='<td width="144"><span class="commentstitle">'+this.articleData.posts+' Comments</span></td>';
        str+='<td>Sort By: <span class="commentFilter newest selected" style="font-weight: bold;">Newest First</span> | <span class="commentFilter oldest" style="text-decoration: underline; font-weight: bold; cursor: pointer;">Oldest First</span> | <span class="commentFilter recommended" style="text-decoration: underline; font-weight: bold; cursor: pointer;">Most Recommended</span></td>';
        str+='</tr></tbody></table>';

        // Top Pagination
        if ( this.articleComments != null && this.articleComments.length > 0 && this.articleData.posts > commentsLimit )
        {
            totalPages = Math.ceil( this.articleData.posts / commentsLimit );
            currentPage = this.commentsStart > 0 ? (this.commentsStart / commentsLimit) + 1 : 1;

            str+='<div id="commentSystem-topPagination"><span class="total">Page ' + currentPage + ' of ' + totalPages + '</span><ul>';

            if ( currentPage > 3 ) { str+='<li class="first"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', 0);">First</a></li>'; }
            if ( currentPage > 1 ) { str+='<li class="prev"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart - commentsLimit) + ');">Previous</a></li>'; }
            if ( currentPage - 2 > 0 ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart - (commentsLimit*2)) + ');">' + (currentPage - 2) + '</a></li>'; }
            if ( currentPage - 1 > 0 ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart - commentsLimit) + ');">' + (currentPage - 1) + '</a></li>'; }
            str+='<li class="active">' + currentPage + '</li>';
            if ( currentPage + 1 <= totalPages ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart + commentsLimit) + ');">' + (currentPage + 1) + '</a></li>'; }
            if ( currentPage + 2 <= totalPages ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart + (commentsLimit*2)) + ');">' + (currentPage + 2) + '</a></li>'; }
            if ( currentPage < totalPages ) { str+='<li class="next"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart + commentsLimit) + ');">Next</a></li>'; }
            if ( currentPage + 3 <= totalPages ) { str+='<li class="last"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + ((totalPages -1) * commentsLimit) + ');">Last</a></li>'; }
            str+='</ul></div>';
        } else {
            str+='<div id="commentSystem-pagination no_pages">Page 1 of 1</div>';
        }

        str+='<div id="commentList">';
        str+='<ul id="commentsList_'+guid+'">';

        if ( this.articleComments != null && this.articleComments.length > 0 )
        {
            jQuery.each(this.articleComments,function(id, data) {
                var repStyle = data.rep_points > 0 ? 'positive' : (data.rep_points < 0 ? 'negative' : 'zero' );

                str+='<li id="li-comment-'+data.pid+'" class="li-comment">';

                // Reputation
                str+='<div id="rep_post_'+data.pid+'" class="li-comment-rep">';
                str+='<span title="Reputation" class="reputation ' + repStyle + ' rep_show">'+data.rep_points+'</span>';
                if ( thisObj.memberData != 0 && data.userCanRate ) {
                    str+='<a title="Vote this post up" class="rep_up" href="'+thisObj.boardUrl+'/index.php?app=core&amp;module=global&amp;section=reputation&amp;do=add_rating&amp;app_rate=forums&amp;type=pid&amp;type_id='+data.pid+'&amp;rating=1&amp;secure_key='+thisObj.memberData.form_hash+'&amp;post_return='+data.pid+'"><img src="'+(thisObj.cdnUrl != null ? thisObj.cdnUrl : thisObj.boardUrl)+'/public/style_images/'+thisObj.imagesFolder+'/add.png" alt="Vote this post up" /></a>';
                    str+='<a title="Vote this post down" class="rep_down" href="'+thisObj.boardUrl+'/index.php?app=core&amp;module=global&amp;section=reputation&amp;do=add_rating&amp;app_rate=forums&amp;type=pid&amp;type_id='+data.pid+'&amp;rating=-1&amp;secure_key='+thisObj.memberData.form_hash+'&amp;post_return='+data.pid+'"><img src="'+(thisObj.cdnUrl != null ? thisObj.cdnUrl : thisObj.boardUrl)+'/public/style_images/'+thisObj.imagesFolder+'/delete.png" alt="Vote this post down" /></a>';
                }
                str+='</div>';

                // User information
                str+='<div class="li-comment-head">';
                str+='<strong><a href="'+thisObj.boardUrl+'/index.php?showuser='+data.memberData.member_id+'">'+data.memberData.members_display_name+'<img width="48" height="48" class="avatar avatar-48 photo" src="'+(data.memberData.pp_thumb_photo ? data.memberData.pp_thumb_photo : (thisObj.cdnUrl != null ? thisObj.cdnUrl : thisObj.boardUrl)+'/public/style_images/'+thisObj.imagesFolder+'/profile/default_thumb.png')+'" alt="" /></a></strong>';
                str+='<br />Posted<br/><a href="'+thisObj.boardUrl+'/index.php?showtopic='+data.topic_id+'&amp;view=findpost&amp;p='+data.pid+'">'+data.formattedPostDate+'</a>';
                str+='</div>';

                // Article  comment
                str+='<div id="comment-'+data.pid+'"  class="li-comment-body">'+data.post+'</div>';

                str+='</li>';
            } );
        }
        str+='</ul></div>';

        // Bottom Pagination
        if ( this.articleComments != null && this.articleComments.length > 0 && this.articleData.posts > commentsLimit )
        {
            totalPages = Math.ceil( this.articleData.posts / commentsLimit );
            currentPage = this.commentsStart > 0 ? (this.commentsStart / commentsLimit) + 1 : 1;

            str+='<div id="commentSystem-bottomPagination"><span class="total">Page ' + currentPage + ' of ' + totalPages + '</span><ul>';

            if ( currentPage > 3 ) { str+='<li class="first"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', 0);">First</a></li>'; }
            if ( currentPage > 1 ) { str+='<li class="prev"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart - commentsLimit) + ');">Previous</a></li>'; }
            if ( currentPage - 2 > 0 ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart - (commentsLimit*2)) + ');">' + (currentPage - 2) + '</a></li>'; }
            if ( currentPage - 1 > 0 ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart - commentsLimit) + ');">' + (currentPage - 1) + '</a></li>'; }
            str+='<li class="active">' + currentPage + '</li>';
            if ( currentPage + 1 <= totalPages ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart + commentsLimit) + ');">' + (currentPage + 1) + '</a></li>'; }
            if ( currentPage + 2 <= totalPages ) { str+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart + (commentsLimit*2)) + ');">' + (currentPage + 2) + '</a></li>'; }
            if ( currentPage < totalPages ) { str+='<li class="next"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + (this.commentsStart + commentsLimit) + ');">Next</a></li>'; }
            if ( currentPage + 3 <= totalPages ) { str+='<li class="last"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\'newest\', ' + commentsLimit + ', ' + ((totalPages -1) * commentsLimit) + ');">Last</a></li>'; }
            str+='</ul></div>';
        }

        commentBar.innerHTML = str;
        
        /* Register reputation */
        if ( thisObj.memberData != 0 )
       	{
	        jQuery.each(thisObj.articleComments,function(id, data) {
	            if ( data.userCanRate )
				{
	                thisObj.registerReputation( 'rep_post_' + data.pid, { app: 'forums', type: 'pid', typeid: data.pid }, parseInt(data.rep_points) );
	            }
	        } );
       	}
    };

    invisionApi.prototype.updateCommentsByFilterUrl = function( changeFilter, limit, offset ) {
    	limit = (parseInt(limit) > 0) ? parseInt(limit) : 10;
    	
        var thisObj = this;
        var comments = document.getElementById('commentsList_'+thisObj.guid);

        var sortKey;
        var sortBy;

        if ( changeFilter == 'recommended' ) {
            sortKey = 'rep_points';
            sortBy = 'desc';

        } else if ( changeFilter == 'oldest' ) {
            sortKey = 'post_date';
            sortBy = 'asc';
        } else {
            sortKey = 'post_date';
            sortBy = 'desc';
        }

        setOffset = '';
        if ( parseInt(offset) >= 0 ) {
            setOffSet =  '&offSet=' + parseInt(offset);
            thisObj.commentsStart = parseInt(offset);
        }

        setLimit = '&limit=' + limit;

        jQuery.getJSON(thisObj.boardUrl + "/index.php?app=forums&module=commentsApiSystem&do=fetchCommentsByUrl&url=" + escape(escape(thisObj.guid)) + "&sortKey="+sortKey+"&sortBy="+sortBy+setLimit+setOffSet+"&jsoncallback=?", function(json) {
            if ( json.status != 'ok' || typeof json.articleData !== 'object' ) {
                return false;
            }

            thisObj.articleData = json.articleData;

            if ( typeof json.articleComments === 'object' && json.articleComments.length > 0 )
            {
                thisObj.articleComments = json.articleComments;
            }

            jQuery('.commentstitle').html(thisObj.articleData.posts + ' Comments');

            // Top Pagination
            if ( thisObj.articleComments != null && thisObj.articleComments.length > 0 && thisObj.articleData.posts > limit )
            {
                totalPages = Math.ceil( thisObj.articleData.posts / limit );
                currentPage = thisObj.commentsStart > 0 ? (thisObj.commentsStart / limit) + 1 : 1;

                var filterHtml = '<span class="total">Page ' + currentPage + ' of ' + totalPages + '</span><ul>';
                if ( currentPage > 3 ) { filterHtml+='<li class="first"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\''+changeFilter+'\', ' + limit + ', 0);">First</a></li>'; }
                if ( currentPage > 1 ) { filterHtml+='<li class="prev"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\''+changeFilter+'\', ' + limit + ', ' + (thisObj.commentsStart - limit) + ');">Previous</a></li>'; }
                if ( currentPage - 2 > 0 ) { filterHtml+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\''+changeFilter+'\', ' + limit + ', ' + (thisObj.commentsStart - (limit*2)) + ');">' + (currentPage - 2) + '</a></li>'; }
                if ( currentPage - 1 > 0 ) { filterHtml+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\''+changeFilter+'\', ' + limit + ', ' + (thisObj.commentsStart - limit) + ');">' + (currentPage - 1) + '</a></li>'; }
                filterHtml+='<li class="active">' + currentPage + '</li>';
                if ( currentPage + 1 <= totalPages ) { filterHtml+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\''+changeFilter+'\', ' + limit + ', ' + (thisObj.commentsStart + limit) + ');">' + (currentPage + 1) + '</a></li>'; }
                if ( currentPage + 2 <= totalPages ) { filterHtml+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\''+changeFilter+'\', ' + limit + ', ' + (thisObj.commentsStart + (limit*2)) + ');">' + (currentPage + 2) + '</a></li>'; }
                if ( currentPage < totalPages ) { filterHtml+='<li class="next"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\''+changeFilter+'\', ' + limit + ', ' + (thisObj.commentsStart + limit) + ');">Next</a></li>'; }
                if ( currentPage + 3 <= totalPages ) { filterHtml+='<li class="last"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilterUrl(\''+changeFilter+'\', ' + limit + ', ' + ((totalPages -1) * limit) + ');">Last</a></li>'; }
                filterHtml+='</ul>';
                jQuery('#commentSystem-topPagination').html(filterHtml);
                jQuery('#commentSystem-bottomPagination').html(filterHtml);
            }

            var str ='';
            if ( thisObj.articleComments != null && thisObj.articleComments.length > 0 )
            {
                jQuery.each(thisObj.articleComments,function(id, data) {
                    var repStyle = data.rep_points > 0 ? 'positive' : (data.rep_points < 0 ? 'negative' : 'zero' );

                    str+='<li id="li-comment-'+data.pid+'" class="li-comment">';

                     // Reputation
                    str+='<div id="rep_post_'+data.pid+'" class="li-comment-rep">';
                    str+='<span title="Reputation" class="reputation ' + repStyle + ' rep_show">'+data.rep_points+'</span>';
                    if ( thisObj.memberData != 0 && data.userCanRate ) {
                        str+='<a title="Vote this comment up" class="rep_up" href="'+thisObj.boardUrl+'/index.php?app=core&amp;module=global&amp;section=reputation&amp;do=add_rating&amp;app_rate=forums&amp;type=pid&amp;type_id='+data.pid+'&amp;rating=1&amp;secure_key='+thisObj.memberData.form_hash+'&amp;post_return='+data.pid+'"><img src="'+(thisObj.cdnUrl != null ? thisObj.cdnUrl : thisObj.boardUrl)+'/public/style_images/'+thisObj.imagesFolder+'/add.png" alt="+" /></a>';
                        str+='<a title="Vote this comment down" class="rep_down" href="'+thisObj.boardUrl+'/index.php?app=core&amp;module=global&amp;section=reputation&amp;do=add_rating&amp;app_rate=forums&amp;type=pid&amp;type_id='+data.pid+'&amp;rating=-1&amp;secure_key='+thisObj.memberData.form_hash+'&amp;post_return='+data.pid+'"><img src="'+(thisObj.cdnUrl != null ? thisObj.cdnUrl : thisObj.boardUrl)+'/public/style_images/'+thisObj.imagesFolder+'/delete.png" alt="-" /></a>';
                    }
                    str+='</div>';

                    // User information
                    str+='<div class="li-comment-head">';
                    str+='<strong><a href="'+thisObj.boardUrl+'/index.php?showuser='+data.memberData.member_id+'">'+data.memberData.members_display_name+'<img width="48" height="48" class="avatar avatar-48 photo" src="'+(data.memberData.pp_thumb_photo ? data.memberData.pp_thumb_photo : (thisObj.cdnUrl != null ? thisObj.cdnUrl : thisObj.boardUrl)+'/public/style_images/'+thisObj.imagesFolder+'/profile/default_thumb.png')+'" alt="" /></a></strong>';
                    str+='<br />Posted<br/><a href="'+thisObj.boardUrl+'/index.php?showtopic='+data.topic_id+'&amp;view=findpost&amp;p='+data.pid+'">'+data.formattedPostDate+'</a>';
                    str+='</div>';

                    // Article  comment
                    str+='<div id="comment-'+data.pid+'"  class="li-comment-body">'+data.post+'</div>';

                    str+='</li>';
                } );
                comments.innerHTML = str;

                jQuery.each(thisObj.articleComments,function(id, data) {
                    if ( thisObj.memberData != 0 && data.userCanRate ) {
                        thisObj.registerReputation( 'rep_post_' + data.pid, { app: 'forums', type: 'pid', typeid: data.pid }, parseInt(data.rep_points) );
                    }
                } );

                jQuery('.commentFilter').each( function() {
                    if ( jQuery(this).hasClass(changeFilter) ) {
                        jQuery(this).attr('style','font-weight: bold;');
                        jQuery(this).addClass('selected');
                    } else {
                        jQuery(this).attr('style', 'text-decoration: underline; font-weight: bold; cursor: pointer;');
                        jQuery(this).removeClass('selected');
                    }
                });
            }
        } );
    };

    invisionApi.prototype.updateCommentsByFilter = function( changeFilter, limit, offset ) {
    	limit = (parseInt(limit) > 0) ? parseInt(limit) : 10;
    	
        var thisObj = this;
        var comments = document.getElementById('commentsList_'+thisObj.guid);

        var sortKey;
        var sortBy;

        if ( changeFilter == 'recommended' ) {
            sortKey = 'rep_points';
            sortBy = 'desc';

        } else if ( changeFilter == 'oldest' ) {
            sortKey = 'post_date';
            sortBy = 'asc';
        } else {
            sortKey = 'post_date';
            sortBy = 'desc';
        }

        setOffset = '';
        if ( parseInt(offset) >= 0 ) {
            setOffSet =  '&offSet=' + parseInt(offset);
            thisObj.commentsStart = parseInt(offset);
        }

        setLimit = '&limit=' + limit;

        jQuery.getJSON(thisObj.boardUrl + "/index.php?app=forums&module=commentsApiSystem&guid=" + escape(escape(thisObj.guid)) + "&sortKey="+sortKey+"&sortBy="+sortBy+setLimit+setOffSet+"&jsoncallback=?", function(json) {
            if ( json.status != 'ok' || typeof json.articleData !== 'object' ) {
                return false;
            }

            thisObj.articleData = json.articleData;

            if ( typeof json.articleComments === 'object' && json.articleComments.length > 0 )
            {
                thisObj.articleComments = json.articleComments;
            }

            jQuery('.commentstitle').html(thisObj.articleData.posts + ' Comments');

            // Top Pagination
            if ( thisObj.articleComments != null && thisObj.articleComments.length > 0 && thisObj.articleData.posts > limit )
            {
                totalPages = Math.ceil( thisObj.articleData.posts / limit );
                currentPage = thisObj.commentsStart > 0 ? (thisObj.commentsStart / limit) + 1 : 1;

                var filterHtml = '<span class="total">Page ' + currentPage + ' of ' + totalPages + '</span><ul>';
                if ( currentPage > 3 ) { filterHtml+='<li class="first"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\''+changeFilter+'\', ' + limit + ', 0);">First</a></li>'; }
                if ( currentPage > 1 ) { filterHtml+='<li class="prev"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\''+changeFilter+'\', ' + limit + ', ' + (thisObj.commentsStart - limit) + ');">Previous</a></li>'; }
                if ( currentPage - 2 > 0 ) { filterHtml+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\''+changeFilter+'\', ' + limit + ', ' + (thisObj.commentsStart - (limit*2)) + ');">' + (currentPage - 2) + '</a></li>'; }
                if ( currentPage - 1 > 0 ) { filterHtml+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\''+changeFilter+'\', ' + limit + ', ' + (thisObj.commentsStart - limit) + ');">' + (currentPage - 1) + '</a></li>'; }
                filterHtml+='<li class="active">' + currentPage + '</li>';
                if ( currentPage + 1 <= totalPages ) { filterHtml+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\''+changeFilter+'\', ' + limit + ', ' + (thisObj.commentsStart + limit) + ');">' + (currentPage + 1) + '</a></li>'; }
                if ( currentPage + 2 <= totalPages ) { filterHtml+='<li><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\''+changeFilter+'\', ' + limit + ', ' + (thisObj.commentsStart + (limit*2)) + ');">' + (currentPage + 2) + '</a></li>'; }
                if ( currentPage < totalPages ) { filterHtml+='<li class="next"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\''+changeFilter+'\', ' + limit + ', ' + (thisObj.commentsStart + limit) + ');">Next</a></li>'; }
                if ( currentPage + 3 <= totalPages ) { filterHtml+='<li class="last"><a href="#commentSystem" onClick="javascript:invisionApi.updateCommentsByFilter(\''+changeFilter+'\', ' + limit + ', ' + ((totalPages -1) * limit) + ');">Last</a></li>'; }
                filterHtml+='</ul>';
                jQuery('#commentSystem-topPagination').html(filterHtml);
                jQuery('#commentSystem-bottomPagination').html(filterHtml);
            }

            var str ='';
            if ( thisObj.articleComments != null && thisObj.articleComments.length > 0 )
            {
                jQuery.each(thisObj.articleComments,function(id, data) {
                    var repStyle = data.rep_points > 0 ? 'positive' : (data.rep_points < 0 ? 'negative' : 'zero' );

                    str+='<li id="li-comment-'+data.pid+'" class="li-comment">';

                     // Reputation
                    str+='<div id="rep_post_'+data.pid+'" class="li-comment-rep">';
                    str+='<span title="Reputation" class="reputation ' + repStyle + ' rep_show">'+data.rep_points+'</span>';
                    if ( thisObj.memberData != 0 && data.userCanRate ) {
                        str+='<a title="Vote this post up" class="rep_up" href="'+thisObj.boardUrl+'/index.php?app=core&amp;module=global&amp;section=reputation&amp;do=add_rating&amp;app_rate=forums&amp;type=pid&amp;type_id='+data.pid+'&amp;rating=1&amp;secure_key='+thisObj.memberData.form_hash+'&amp;post_return='+data.pid+'"><img src="'+(thisObj.cdnUrl != null ? thisObj.cdnUrl : thisObj.boardUrl)+'/public/style_images/'+thisObj.imagesFolder+'/add.png" alt="+" /></a>';
                        str+='<a title="Vote this post down" class="rep_down" href="'+thisObj.boardUrl+'/index.php?app=core&amp;module=global&amp;section=reputation&amp;do=add_rating&amp;app_rate=forums&amp;type=pid&amp;type_id='+data.pid+'&amp;rating=-1&amp;secure_key='+thisObj.memberData.form_hash+'&amp;post_return='+data.pid+'"><img src="'+(thisObj.cdnUrl != null ? thisObj.cdnUrl : thisObj.boardUrl)+'/public/style_images/'+thisObj.imagesFolder+'/delete.png" alt="-" /></a>';
                    }
                    str+='</div>';

                    // User information
                    str+='<div class="li-comment-head">';
                    str+='<strong><a href="'+thisObj.boardUrl+'/index.php?showuser='+data.memberData.member_id+'">'+data.memberData.members_display_name+'<img width="48" height="48" class="avatar avatar-48 photo" src="'+(data.memberData.pp_thumb_photo ? data.memberData.pp_thumb_photo : (thisObj.cdnUrl != null ? thisObj.cdnUrl : thisObj.boardUrl)+'/public/style_images/'+thisObj.imagesFolder+'/profile/default_thumb.png')+'" alt="" /></a></strong>';
                    str+='<br />Posted<br/><a href="'+thisObj.boardUrl+'/index.php?showtopic='+data.topic_id+'&amp;view=findpost&amp;p='+data.pid+'">'+data.formattedPostDate+'</a>';
                    str+='</div>';

                    // Article  comment
                    str+='<div id="comment-'+data.pid+'"  class="li-comment-body">'+data.post+'</div>';

                    str+='</li>';
                } );
                comments.innerHTML = str;

                jQuery.each(thisObj.articleComments,function(id, data) {
                    if ( thisObj.memberData != 0 && data.userCanRate ) {
                        thisObj.registerReputation( 'rep_post_' + data.pid, { app: 'forums', type: 'pid', typeid: data.pid }, parseInt(data.rep_points) );
                    }
                } );

                jQuery('.commentFilter').each( function() {
                    if ( jQuery(this).hasClass(changeFilter) ) {
                        jQuery(this).attr('style','font-weight: bold;');
                        jQuery(this).addClass('selected');
                    } else {
                        jQuery(this).attr('style', 'text-decoration: underline; font-weight: bold; cursor: pointer;');
                        jQuery(this).removeClass('selected');
                    }
                });
            }
        } );
    };

    invisionApi.prototype.submitPost = function( jqueryFormObj ) {
    	var commentsLimit = 10;
        var thisObj = this;

        var formData = '';
        jQuery(':input', jqueryFormObj).each(function() {
        	if ( this.name == 'commentsLimit' )
        	{
        		commentsLimit = parseInt(this.value);
        	}
        	else
        	{
        		formData = formData + "&" + this.name + "=" + (this.name == 'Post' ? escape(escape(this.value)) : escape(this.value));
        	}
        } );

		jQuery.getJSON(thisObj.boardUrl + "/index.php?app=forums&module=post&section=post&do=remotePost&jsoncallback=?", formData, function(json, textStatus) {
            thisObj.updateCommentsByFilter('newest',commentsLimit,0);
		} );
    };
	
    invisionApi.prototype.registerReputation = function( id, url, rating ) {
        thisObj = this;
		
        if( !jQuery( '#' + id ) ){ return; }
		
        // Find rep up

        // Following only good for jQuery 1.3
        var selector = '#' + id;
        var rep_up = jQuery(selector).find('.rep_up:first');
        var rep_down = jQuery(selector).find('.rep_down:first');
        var sendUrl = thisObj.boardUrl + '/index.php?app=core&module=ajax&section=reputation&do=add_rating&returnType=json&app_rate=' + url.app + '&type=' + url.type + '&type_id=' + url.typeid + '&secure_key=' + thisObj.memberData.form_hash;
        
        if (rep_up) {
            rep_up.bind( 'click', {obj: thisObj, pid: id}, function(event){
                event.data.obj.repRate(1, event.data.pid);
                return false;
            } );
        }

        if (rep_down) {
        	rep_down.bind( 'click', {obj: thisObj, pid: id}, function(event){
                event.data.obj.repRate(-1, event.data.pid);
                return false;
            } );
        }

		thisObj.reputation[ id ] = { obj: jQuery(selector), url: url, sendUrl: sendUrl, currentRating: rating || 0 };
    };

    invisionApi.prototype.repRate = function( type, id ) {

        thisObj = this;
        var value = ( type == 1 ) ? 1 : -1;

        if ( !thisObj.reputation[ id ] ) {
            return;
        } else {
            var rep = thisObj.reputation[ id ];
        }

        // Send ping
        jQuery.getJSON( rep.sendUrl + '&rating=' + value + '&jsoncallback=?', function(json,textStatus) {
            if( json.status == 'ok' )
			{
				try {
                    // It worked! Hide the rep buttons
                    rep.obj.find('.rep_up:first').hide();
                    rep.obj.find('.rep_down:first').hide();
                } catch(err) { }

                // Update the figure
                var rep_display = rep.obj.find('.rep_show:first');
                if ( rep_display ) {
                    rep_display.removeClass('positive negative zero');

                    var newValue = rep.currentRating + value;
					
                    if( newValue > 0 ) {
                    	rep_display.addClass('positive');
                    } else if( newValue < 0 ) {
                    	rep_display.addClass('negative');
                    } else {
                    	rep_display.addClass('zero');
                    }

                    rep_display.html( newValue );
                }
            } else {
                alert( json.message ? json.message : json.data );
            }
        } );
     };

var invisionApi = new invisionApi();

jQuery(document).ready(function() {
	// CSS
	jQuery(document.createElement('link')).attr({media: 'screen', href: (invisionApi.cdnUrl != null ? invisionApi.cdnUrl : invisionApi.boardUrl) + '/public/style_css/articleComments.css', type: 'text/css', rel: 'stylesheet'}).appendTo(document.getElementsByTagName('head')[0]);
	
	// Comment filters
    jQuery('.commentFilter').live( 'click', function(e) {
    	if ( jQuery(e.target).hasClass('selected') ) {
            return;
        }
		
        var filterClass = jQuery(e.target).attr("class").replace(/commentFilter /, '');
        invisionApi.updateCommentsByFilter(filterClass, 0, 0);
    });
	
	// Submit button
    jQuery('#submitBtn').live( 'click', function() {
        if ( jQuery("#commentSystem-replyForm #Post").val().length < 4 ) {
            if ( jQuery("#commentSystem-replyError").length == 0 ) {
                jQuery("#commentSystem-reply").append("<div id='commentSystem-replyError'>Your comment must consist of at least 4 characters. Thank you.</div>");
            }
            jQuery("#commentSystem-replyError").show().fadeOut(3000);
            return false;
        }
        invisionApi.submitPost(jQuery("#commentSystem-replyForm"));
        
        /* Clean comment and show thanks */
        jQuery("#commentSystem-replyForm #Post")[0].value='';
        
        if ( jQuery("#commentSystem-replySubmitted").length == 0 ) {
            jQuery("#commentSystem-reply").append("<div id='commentSystem-replySubmitted'>Thank you for your comment!</div>");
        }
        jQuery("#commentSystem-replySubmitted").show().fadeOut(4000);
        return false;
    });

    jQuery('#ipsCommentBox-form').submit(function(){
        return false;
    });
});
