// Required language variables: 39,155,175,182,183,184,185,187,784,787,829,830,831,832,833,834,835,854,856,891,1025,1026,1032,1034,1071
SocialEngineAPI.dsComments = new Class({
	
	// Class
	Implements: [Options],
	
	// Properties
	Base: {},
	page: 1,
	total: 0,
	changed: false,
	isEditing: false,
	
	options: {
		// Controls ajax request options
		'ajaxURL' : 'misc_js.php',
		'ajaxMethod' : 'post',
		'ajaxSecure' : false,
		
		// Can the viewer comment
		'canComment' : false,
		'commentHTML' : false,
		'commentCode' : false,
		
		// Controls the height of the comment box
		'originalHeight' : 70,
		'dswidth': 468,
		'dsheight': 100,
		'editDswidth': 468,
		'editDsheight': 100,
		
		// The type of comment ex. 'media'
		'type' : false,
		
		// The column that identifies the type ex. 'media_id'
		'typeIdentifier' : false,
		
		// The actual ID of the object commented on
		'typeID' : false,
		
		// Pagination Info
		'paginate' : false,
		'cpp' : false,
		
		// Comment links
		'commentLinks' : {'reply' : false, 'walltowall' : false},
	
		// Some other stuff to identify the object
		'object_owner' : false,
		'object_owner_id' : false,
		'typeTab' : false,
		'typeCol' : false,
		'typeTabParent' : false,
		'typeColParent' : false,
		'typeChild' : false
	},

	// Methods
	initialize: function(options){
		this.setOptions(options);
		if( this.options.initialTotal ) this.total = this.options.initialTotal;
		var bind = this;
		window.addEvent('domready', function(){
			bind.showPostComment();
			//bind.options.originalHeight = textarea_autogrow('comment_body');
			bind.getComments(1);
		});
	},

	showPostComment: function(options){
		var innerHTML = "";
		// POST COMMENT
		
		innerHTML += 
			"<h2>" +
			this.Base.Language.Translate(854) +
			" (<span id='" + this.options.type + '_' + this.options.typeID + '_totalcomments' + "'>" + this.total + "</span>)" +
			"</h2>"; 
		
		if( this.options.canComment ){
			innerHTML += 
			"<form name='dscomment' action='misc_js.php' method='post' target='ajaxframe'>" +
			//"<textarea name='comment_body' id='comment_body' style='width:"+(this.options.dswidth)+"px; height:"+(this.options.dsheight)+"px;'>" + this.Base.Language.Translate(829) + "</textarea>";
			"<textarea class='text' name='comment_body' id='comment_body' style='width:"+(this.options.dswidth)+"px; height:"+(this.options.dsheight)+"px;'>" + this.Base.Language.Translate(829) + "</textarea>";
		
		if( this.options.commentHTML ) innerHTML += 
			"<p>" +
			this.Base.Language.TranslateFormatted(1034, [this.options.commentHTML]) +
			"</p>";
		
		if( this.options.commentCode ) innerHTML += 
			"<div style='float: left; margin-top: 5px;'>" +
			"<a href='javascript:void(0);' onClick=\"this.blur();$('secure_image').src=$('secure_image').src+'?'+(new Date()).getTime();\">" +
			"<img src='./images/secure.php' id='secure_image' border='0' height='20' width='67' class='signup_code'>" + 
			"</a>" +
			" <input type='text' name='comment_secure' id='comment_secure' class='text' size='6' maxlength='10'> " +
			"<img src='./images/icons/tip.gif' border='0' class='Tips1' style='vertical-align: middle;' title='" + this.Base.Language.Translate(856) + "'>" +
			"</div>";
		
			innerHTML += 
			"<div style='text-align: right; margin: 5px ;'>" +
			"<a href='javascript:document.dscomment.submit();' class='stbut'><div class='left_stbut'></div><div id='comment_submit' class='mid_stbut'>" + this.Base.Language.Translate(833) + "</div><div class='right_stbut'></div></a><div class='clear'></div>" +
			//"<a href='javascript:document.dscomment.submit();' class='stbut' style='float:left; width:136px; '><div class='left_stbut'></div><div id='comment_submit' class='mid_stbut' style='width:110px;'>" + this.Base.Language.Translate(833) + "</div><div class='right_stbut'></div></a><br />" +
			// "<input type='submit' id='comment_submit' class='button' value='" + this.Base.Language.Translate(833) + "'>" +
			"<input type='hidden' name='task' value='comment_post'>" +
			"<input type='hidden' name='type' value='" + this.options.type + "'>" +
			"<input type='hidden' name='iden' value='" + this.options.typeIdentifier + "'>" +
			"<input type='hidden' name='value' value='" + this.options.typeID + "'>";
		
		if( this.options.object_owner && this.options.object_owner_id ){
			innerHTML += '<input type="hidden" name="object_owner" value="' + this.options.object_owner + '">' +
			'<input type="hidden" name="object_owner_id" value="' + this.options.object_owner_id + '">';
		} 
		else {
			innerHTML += '<input type="hidden" name="user" value="' + this.Base.Owner.user_info.user_username + '">';
		}
		
		if( this.options.typeTab ) innerHTML += "<input type='hidden' name='tab' value='" + this.options.typeTab + "'>";
		if( this.options.typeCol ) innerHTML += "<input type='hidden' name='col' value='" + this.options.typeCol + "'>";
		if( this.options.typeTabParent ) innerHTML += '<input type="hidden" name="tab_parent" value="' + this.options.typeTabParent + '">';
		if( this.options.typeColParent ) innerHTML += '<input type="hidden" name="col_parent" value="' + this.options.typeColParent + '">';
		if( this.options.typeChild ) innerHTML += '<input type="hidden" name="child" value="1">';
		
		innerHTML += 
			"</div>" +
			"<div id='comment_error' style='color: #FF0000; display: none;'></div>" +
			"</div>" +
			"</form>";
		}
	
		// DELETE COMMENT
	    innerHTML += 
			'<div style="display: none;" id="confirmcommentdelete">' +
			'<div style="margin-top: 10px;">' +
			this.Base.Language.Translate(1026) +
			'</div>' +
			'<br />' +
			'<form action="misc_js.php" method="post" target="ajaxframe" id="dscommform">' +
			'<a href="javascript:$('+"'dscommform'"+').submit();" onclick="parent.TB_remove();" class="stbut" ><div class="left_stbut"></div><div  class="mid_stbut">'+this.Base.Language.Translate(175)+'</div><div class="right_stbut"></div></a>' +
			'<a href="javascript:void(0);" onclick="parent.TB_remove();" class="stbut" style="margin-left:5px;"><div class="left_stbut"></div><div  class="mid_stbut">'+this.Base.Language.Translate(39)+'</div><div class="right_stbut"></div></a>' +
			'<input type="hidden" name="task" value="comment_delete">' +
			'<input type="hidden" name="comment_id" id="del_comment_id" value="0">' +
			'<input type="hidden" name="type" value="' + this.options.type + '">' +
			'<input type="hidden" name="iden" value="' + this.options.typeIdentifier + '">' +
			'<input type="hidden" name="value" value="' + this.options.typeID + '">';
		
		if( this.options.object_owner && this.options.object_owner_id ){
			innerHTML += '<input type="hidden" name="object_owner" value="' + this.options.object_owner + '">' +
			'<input type="hidden" name="object_owner_id" value="' + this.options.object_owner_id + '">';
		} 
		else {
			innerHTML += '<input type="hidden" name="user" value="' + this.Base.Owner.user_info.user_username + '">';
		}
	    
	    if( this.options.typeTab ) innerHTML += '<input type="hidden" name="tab" value="' + this.options.typeTab + '">';
	    if( this.options.typeCol ) innerHTML += '<input type="hidden" name="col" value="' + this.options.typeCol + '">';
	    if( this.options.typeTabParent ) innerHTML += '<input type="hidden" name="tab_parent" value="' + this.options.typeTabParent + '">';
	    if( this.options.typeColParent ) innerHTML += '<input type="hidden" name="col_parent" value="' + this.options.typeColParent + '">';
	    if( this.options.typeChild ) innerHTML += '<input type="hidden" name="child" value="1">';
	   
	    innerHTML += 
	    		'</form>' +
	  		'</div>';
		
		var postCommentContainerElement = $(this.options.type + '_' + this.options.typeID + '_postcomment');
		postCommentContainerElement.innerHTML = innerHTML;
	
		// Add events
		var bind = this;
		if( this.options.canComment ){
			postCommentContainerElement.getElement('form').addEvent('submit', function()    { bind.checkText();       });
			postCommentContainerElement.getElement('textarea').addEvent('focus', function() { bind.removeText(this);  });
			postCommentContainerElement.getElement('textarea').addEvent('blur', function()  { bind.addText(this);     });
		}
	},

getComments: function(direction){
	if( direction=='next' )
		this.page++;
	else if( direction=='previous' )
		this.page--;
	else if( $type(direction) )
		this.page = direction;
	if( this.options.paginate ) {
		window.scroll(0,0);
	} 
	else {
		this.options.cpp = this.total;
	}
	if( this.options.object_owner && this.options.object_owner_id ){
		var object_owner = this.options.object_owner;
		var object_owner_id = this.options.object_owner_id;
		var user = '';
	} 
	else {
		var object_owner = '';
		var object_owner_id = '';
		var user = this.Base.Owner.user_info.user_username;      
	}
	// AJAX
	var bind = this;
	var request = new Request.JSON({
		'url' : this.options.ajaxURL,
		'method' : this.options.ajaxMethod,
		'secure' : this.options.ajaxSecure,
		'data' : {
		'task'  : 'comment_get',
		'user'  : user,
		'object_owner' : object_owner,
		'object_owner_id' : object_owner_id,
		'type'  : this.options.type,
		'iden'  : this.options.typeIdentifier,
		'dswidth'  : this.options.dswidth,
		'dsheight'  : this.options.dsheight,
		'value' : this.options.typeID,
		'cpp'   : this.options.cpp,
		'p'     : this.page
	},
	'onComplete' : function(responseObject, responseText){
		bind.updateComments(responseObject);
	}
	});
	request.send();
	},

// THIS FUNCTION UPDATES THE COMMENTS
updateComments: function(responseObject){
	if( $type(responseObject)!="object" ){
		/*alert('There was an error processing the request.');*/
		return false;
	}
    
	// Prepare
	this.total  = parseInt(responseObject.total_comments) || 0;
	this.page   = responseObject.p;
    
	var maxpage = responseObject.maxpage;
	var p_start = responseObject.p_start;
	var p_end   = responseObject.p_end;
    
	var totalCommentElement     = $(this.options.type + '_' + this.options.typeID + '_totalcomments');
	var commentContainerElement = $(this.options.type + '_' + this.options.typeID + '_comments');
	var comments = $H(responseObject.comments);
    
	// UPDATE TOTAL COMMENTS AND PAGE VARS
	totalCommentElement.innerHTML = this.total;

	// CREATE DIV TO HOLD COMMENT BODY FOR CLEANING
	var commentBodyDiv = document.createElement('div');
    
	// EMPTY CONTAINER 
	commentContainerElement.empty();
    
	// LOOP OVER COMMENTS
	var bind = this;
	if(bind.Base.Core.settings.setting_url){ 
		var querySeparator = '?'; 
	} 
	else{
		 var querySeparator = '&'; 
	}
	comments.each(function(commentObject, commentID){
		var newComment = new Element('div', {
			'id' : 'comment_' + commentID
		});
      
		// BUILD COMMENT
		var newCommentInnerHTML = "<div class='TopLine'><table width='100%'><tr>";
      
		// AUTHOR PHOTO
		if( commentObject.comment_authoruser_id && commentObject.comment_authoruser_exists ){ 
			newCommentInnerHTML += "<td class='thumb' style='width: 120px; padding-right: 10px;'><a href='" + commentObject.comment_authoruser_url + "'><img src='" + commentObject.comment_authoruser_photo + "' width='" + commentObject.comment_authoruser_photo_width + "'></a></td>";
		}
		else{ 
			newCommentInnerHTML += "<td class='thumb' style='width: 120px; padding-right: 10px;'><img src='./images/nophoto.gif' width='100' border='0'></td>";
			//newCommentInnerHTML += "<div style='overflow: hidden;'>";
		}
		newCommentInnerHTML += "<td>";
		// AUTHOR NAME/LINK
		if( !commentObject.comment_authoruser_id ) { 
			newCommentInnerHTML += "<h3>" + bind.Base.Language.Translate(835) + "</h3>";
		}
		else if( !commentObject.comment_authoruser_exists ) { 
			newCommentInnerHTML += "<h3>" + bind.Base.Language.Translate(1071) + "</h3>";
		}
		else { 
			newCommentInnerHTML += "<h3><a href='" + commentObject.comment_authoruser_url + "'>" + commentObject.comment_authoruser_displayname + "</a></h3>";
			// COMMENT DATE
			newCommentInnerHTML += "<p class='SmallText'>" + commentObject.comment_date + "</p>";
			// COMMENT BODY
			newComment.setProperty('html', commentObject.comment_body);
			newCommentInnerHTML += "<p><span class='profile_comment_body' id='profile_comment_body_" + commentID + "'>" + commentObject.comment_body + "&nbsp;</span></p>";
		}
      
		// COMMENT LINKS
		var links = new Array();
		if( bind.Base.Viewer.user_exists && commentObject.comment_authoruser_id && commentObject.comment_authoruser_exists ){
		// REPLY LINK (VISIBLE ONLY TO OWNER)
		if(bind.options.commentLinks.reply && bind.Base.Viewer.user_info.user_id == bind.Base.Owner.user_info.user_id && bind.Base.Viewer.user_info.user_id != commentObject.comment_authoruser_id) { 
			links.push("<a href='"+commentObject.comment_authoruser_url+querySeparator+"v=comments'>" + bind.Base.Language.Translate(787) + "</a>"); 
		}
		// WALL-TO-WALL (VISIBLE ONLY IF USER CAN SEE AUTHOR'S PROFILE)
		if(bind.options.commentLinks.walltowall && commentObject.comment_authoruser_id != bind.Base.Owner.user_info.user_id && commentObject.comment_authoruser_private == false) { 
			links.push("<a href=\"javascript:TB_show('" + bind.Base.Language.Translate(1032) + "', 'profile_comments.php?user=" + bind.Base.Owner.user_info.user_username +"&user2=" + commentObject.comment_authoruser_username + "&TB_iframe=true&height=450&width=550', '', './images/trans.gif');\">" + bind.Base.Language.Translate(891) + "</a>");
		}
		// MESSAGE (VISIBLE IF USER IS NOT AUTHOR)
		if( commentObject.comment_authoruser_id!=bind.Base.Viewer.user_info.user_id ){
			links.push("<a href=\"javascript:TB_show('" + bind.Base.Language.Translate(784) + "', 'user_messages_new.php?to_user=" + commentObject.comment_authoruser_displayname + "&to_id=" + commentObject.comment_authoruser_username + "&TB_iframe=true&height=400&width=450', '', './images/trans.gif');\">" + bind.Base.Language.Translate(834) + "</a>");
		}
		// EDIT (VISIBLE IF USER IS AUTHOR)
		if( commentObject.comment_authoruser_id==bind.Base.Viewer.user_info.user_id ){
			links.push("<a class=\"commentEditLink\" href=\"javascript:void(0);\" id='comment_edit_link_" + commentID + "'>" + bind.Base.Language.Translate(187) + "</a>");
		}
	}
	// DELETE (VISIBLE IF USER IS AUTHOR OR USER IS OWNER)
	if((commentObject.comment_authoruser_exists && commentObject.comment_authoruser_id == bind.Base.Viewer.user_info.user_id) || (bind.Base.Viewer.user_exists && bind.Base.Viewer.user_info.user_id == bind.Base.Owner.user_info.user_id)){
		links.push("<a class=\"commentDeleteLink\" href=\"javascript:void(0);\" id='comment_delete_link_" + commentID + "'>" + bind.Base.Language.Translate(155) + "</a>");
	}
	newCommentInnerHTML += links.join('&nbsp;|&nbsp;');
	newCommentInnerHTML += "</td></tr></table></div>";
	// ADD NEW INNERHTML
	newComment.setProperty('html', newCommentInnerHTML);
	newComment.inject(commentContainerElement);
	// ADD EVENTS
	if( newComment.getElement('.commentEditLink') ) newComment.getElement('.commentEditLink').addEvent('click', function(){
		bind.editComment(commentID);
	});
	if( newComment.getElement('.commentDeleteLink') ) newComment.getElement('.commentDeleteLink').addEvent('click', function(){
		bind.confirmDelete(commentID);
	});
});

// CREATE PAGINATION DIV
if(this.options.paginate && this.total > this.options.cpp) { 
	var commentPaginationTop = new Element('div', {'styles': {'text-align' : 'center'}});
	//var commentPaginationBottom = new Element('div', {'styles': {'text-align' : 'center'}});
	
	var commentPaginationBottom = new Element('div', {'class': 'TopLine TextCenter'});
	
	if(this.page > 1) {
		var paginationHTMLTop = "<a href='javascript:void(0);' id='comment_last_page_top'>&#171; " + bind.Base.Language.Translate(182) + "</a>";
		var paginationHTMLBottom = "<a href='javascript:void(0);' id='comment_last_page_bottom'>&#171; " + bind.Base.Language.Translate(182) + "</a>";
	} 
	else {
		var paginationHTMLTop = "<font class='disabled'>&#171; " + bind.Base.Language.Translate(182) + "</font>";
		var paginationHTMLBottom = "<font class='disabled'>&#171; " + bind.Base.Language.Translate(182) + "</font>";
	}
	if(p_start == p_end) {
		paginationHTMLTop += "&nbsp;|&nbsp; " + this.Base.Language.TranslateFormatted(184, [p_start, this.total]) + "&nbsp;|&nbsp;";
		paginationHTMLBottom += "&nbsp;|&nbsp; " + this.Base.Language.TranslateFormatted(184, [p_start, this.total]) + "&nbsp;|&nbsp;";
	} 
	else {
		paginationHTMLTop += "&nbsp;|&nbsp; " + this.Base.Language.TranslateFormatted(185, [p_start, p_end, this.total]) + "&nbsp;|&nbsp;";
		paginationHTMLBottom += "&nbsp;|&nbsp; " + this.Base.Language.TranslateFormatted(185, [p_start, p_end, this.total]) + "&nbsp;|&nbsp;";
	}
	if(this.page != maxpage) {
		paginationHTMLTop += "<a href='javascript:void(0);' id='comment_next_page_top'>" + bind.Base.Language.Translate(183) + " &#187;</a>";
		paginationHTMLBottom += "<a href='javascript:void(0);' id='comment_next_page_bottom'>" + bind.Base.Language.Translate(183) + " &#187;</a>";
	} 
	else {
		paginationHTMLTop += "<font class='disabled'>" + bind.Base.Language.Translate(183) + " &#187;</font>";
		paginationHTMLBottom += "<font class='disabled'>" + bind.Base.Language.Translate(183) + " &#187;</font>";
	}
	//commentPaginationTop.setProperty('html', paginationHTMLTop);
	commentPaginationBottom.setProperty('html', paginationHTMLBottom);
	//commentPaginationTop.inject(commentContainerElement, 'top');
	commentPaginationBottom.inject(commentContainerElement);
	
	// ADD EVENTS
	if( commentPaginationTop.getElement('a[id=comment_last_page_top]') ) commentPaginationTop.getElement('a[id=comment_last_page_top]').addEvent('click', function(){
		bind.getComments('previous');
	});
	if( commentPaginationBottom.getElement('a[id=comment_last_page_bottom]') ) commentPaginationBottom.getElement('a[id=comment_last_page_bottom]').addEvent('click', function(){
		bind.getComments('previous');
	});
	if( commentPaginationTop.getElement('a[id=comment_next_page_top]') ) commentPaginationTop.getElement('a[id=comment_next_page_top]').addEvent('click', function(){
		bind.getComments('next');
	});
	if( commentPaginationBottom.getElement('a[id=comment_next_page_bottom]') ) commentPaginationBottom.getElement('a[id=comment_next_page_bottom]').addEvent('click', function(){
		bind.getComments('next');
	});
	}
},
// Adds a comment
addComment: function(is_error, comment_body, comment_date){
	if( !this.options.canComment )
	return false;
	if( is_error ){
		$('comment_error').style.display = 'block';
		if( !comment_body.trim() ){
			this.addText($('comment_body'));
			$('comment_error').innerHTML = this.Base.Language.Translate(831);
		}
		else{
			$('comment_error').innerHTML = this.Base.Language.Translate(832);
		}
		// $('comment_submit').value = this.Base.Language.Translate(833);
		$('comment_submit').innerHTML = this.Base.Language.Translate(833);
		//$('comment_submit').disabled = false;
	}
	else{
		$('comment_error').style.display = 'none';
		$('comment_error').innerHTML = '';
		$('comment_body').value = '';
		//$('comment_body').style.height = this.options.originalHeight + 'px';
		this.addText($('comment_body'));

		//$('comment_submit').value = this.Base.Language.Translate(833);
		//$('comment_submit').disabled = false;
		$('comment_submit').innerHTML = this.Base.Language.Translate(833);
		if( $('comment_secure') ){
			$('comment_secure').value = '';
			$('secure_image').src = $('secure_image').src + '?' + (new Date()).getTime();
		}
		// INPUT COMMENTS
		this.page = 1;
		this.total++;
  	    this.getComments();
	}
},

editComment: function(commentID){
	var bind = this;
	if( this.isEditing ) return false;
	this.isEditing = true;

	//var commentContainerElement = $(this.options.type + '_' + this.options.typeID + '_comments');
	//var commentElement = commentContainerElement.getElement('profile_comment_body_' + commentID);
	var commentElement = $('profile_comment_body_' + commentID);
    
	var height = commentElement.offsetHeight + 10;
	var commentText = commentElement.innerHTML.replace(/<br>/gi, '\r\n').replace(/>/gi, '&gt;');
    
	var innerHTML = '';
	innerHTML += "	<form action='misc_js.php' method='post' target='ajaxframe' name='editCommentForm'>";
	innerHTML += "		<textarea class='text' name='comment_edit' id='comment_edit_" + commentID + "' style='height: " + (this.options.editDsheight) +" px; width: "+(this.options.editDswidth)+"px;'>" + commentText + "</textarea>";
	innerHTML += "		<input type='hidden' name='task' value='comment_edit'>";
	innerHTML += "		<input type='hidden' name='comment_id' value='" + commentID + "'>";
	innerHTML += "		<input type='hidden' name='type' value='" + this.options.type + "'>";
	innerHTML += "		<input type='hidden' name='iden' value='" + this.options.typeIdentifier + "'>";
	innerHTML += "		<input type='hidden' name='value' value='" + this.options.typeID + "'>";
	
	if( this.options.typeTab ) innerHTML += '		<input type="hidden" name="tab" value="' + this.options.typeTab + '">';
	if( this.options.typeCol ) innerHTML += '		<input type="hidden" name="col" value="' + this.options.typeCol + '">';
	if( this.options.typeTabParent ) innerHTML += '	<input type="hidden" name="tab_parent" value="' + this.options.typeTabParent + '">';
	if( this.options.typeColParent ) innerHTML += '	<input type="hidden" name="col_parent" value="' + this.options.typeColParent + '">';
	if( this.options.typeChild ) innerHTML += '		<input type="hidden" name="child" value="1">';
	
	innerHTML += "	</form>";

	// Inject
	commentElement.innerHTML = innerHTML;
	//textarea_autogrow('comment_edit_' + commentID);
	$('comment_edit_' + commentID).focus();
	// Add events
	$('comment_edit_' + commentID).addEvent('blur', function(){
		document.editCommentForm.submit();
		bind.isEditing = false;
	});
},
confirmDelete: function(commentID){
	$('del_comment_id').value = commentID;
	TB_show(this.Base.Language.Translate(1025), '#TB_inline?height=100&width=300&inlineId=confirmcommentdelete', '', '../images/trans.gif');
},
// UI Methods
removeText: function(commentBody){
	if( !this.changed ){
		commentBody.value = '';
		commentBody.style.color = '#000000';
		this.changed = true;
	}
},
addText: function(commentBody){
	if( !commentBody.value.trim() ){
		commentBody.value = this.Base.Language.Translate(829);
		commentBody.style.color = '#888888';
		this.changed = false;
	}
},
checkText: function(commentBody){
	if( !this.changed )
	$('comment_body').value='';
	//$('comment_submit').value = this.Base.Language.Translate(830);
	//$('comment_submit').disabled = true;
	$('comment_submit').innerHTML = this.Base.Language.Translate(833);
	}
});

