function sb_comments_captcha_resizer(){
		$('img.sb_captcha_img').each( function() {
			var captchaWidth = $(this).outerWidth();
			var containerWidth = $(this).parents('.editable').width();
			if ( captchaWidth > containerWidth ) {
				if ( $(this).hasClass('sb_width_handled') ) {
					return;
				}
				$(this).addClass ( 'sb_width_handled' );
				$(this).width ( containerWidth ).siblings('.sb_captcha_enlarge_note').show();
				$(this).mouseover( function(e){
					$('#sb_captcha_magnifier').remove();
					var off = $(this).offset();
					$('body').append ( '<div id="sb_captcha_magnifier" style="position: absolute; z-index: 99999; top: ' + off.top + 'px; left: ' + off.left + 'px;"><img src="' + $(this).attr('src') + '" /></div>');
					$('#sb_captcha_magnifier').mouseout( function() {
						$(this).remove();
					});
				});
			}
		});
};

var sbCommentsDateFormats = {};
var sbTZDiff = null;
$(document).ready(function(){
	$('.sb_submit_link').click(function(e){
		var formId = this.id.replace(/^sb_submit(.*)$/ig,"#sb_comment_form$1");
		var data = {};
		var parentId = $(this).parents('div.sb_comments').attr('id');
		var fcName = $(formId+' input[name="fcname"]').val();
		sbCommentsAddFormLoader(parentId,fcName);
		var theUrl = $(formId).attr('action');
		var self = $(this);
		$('#'+parentId+' p.sb_error').html('');
		$(formId+' :input').each(function(){
			data[$(this).attr('name')] = $(this).val();
		});
		$.post($(formId).attr('action'),data,function(response){
			$(formId).find('.sb_comments_preloader_overlay').remove();
			if (response.status) {
				if (response.status == 'ok') {
					$.getJSON(theUrl,function(data){
						$('#'+parentId+' form :input').val('');
						$('#'+parentId+' form input[name="fcname"]').val(fcName);
						var theHtml = data.data;
						if (typeof(sbUrlRewriter) != 'undefined') {
							theHtml = sbUrlRewriter.rewrite(theHtml);
						}
						$('#'+parentId+' .sb_comments_content_div').html(theHtml+'<div class="sb_widget_comment_success_message">'+sbCommentsSuccessMessage+'</div>');
						sbCommentsAttachPaginationLinks (parentId,encodeURIComponent(fcName));
						window.location.hash=$('#'+parentId+' .sb_comments_content_div').find('a[name]:first').attr('name');
						$(formId.replace(/^#sb_comment_form-(.*)$/ig,"#sb_showform_link_$1")).show();
						$(formId).hide();
					});
				} else if(response.errors && response.errors.fields) {
					$('#'+parentId+' form :input').each(function(){
						if (typeof(response.errors.fields[$(this).attr('name')]) != 'undefined') {
							$(this).prev('p.sb_error').html(response.errors.fields[$(this).attr('name')]);
						}
					});
				}
				var captchaImg = self.parents('form').find('.sb_captcha_img');
				var mathExpr = self.parents('form').find('.sb_math_str');
				var jsField = self.parents('form').find('input[name="protect_field"]');
				if (response.captcha && captchaImg.length > 0) {
					captchaImg.attr('src',response.captcha);
				} else if (mathExpr.length>0) {
					mathExpr.text (response.mathprotect);
				} else if (jsField.length>0) {
					jsField.val(response.jsprotect);
				} else {
					self.parents('form').find('input[name="protect_field"]').val('');
				}
			}
		},'json');
		return false;
	});
	
	$('.sb_comment_form .sb_reload_captcha').click(function(){
		var self = $(this);
		var fcName =$(this).parents('form').find('input[name="fcname"]').val();
		var theRegEx = new RegExp('^([^;]*);'+encodeURIComponent(fcName)+':([^;&]*);?(&.*)?$','ig');
		var theUrl = '';
		if ($(this).parents('form').attr('action').match(theRegEx)) {
			theUrl = $(this).parents('form').attr('action').replace(theRegEx, "$1;"+encodeURIComponent(fcName)+":\$2%26getnewprotectionstring=true%26r="+Math.random()+"$3");
		} else {
			theUrl = $(this).parents('form').attr('action').replace(/^([^&]*)(&rewriteurls.*$)?/ig,'$1;'+encodeURIComponent(fcName)+":getnewprotectionstring=true%26r="+Math.random()+"$2");
		}
		$.getJSON(theUrl,function(response){
			if (self.siblings('img').length > 0) {
				self.siblings('img').attr('src',response.captcha);
			}
		});
		return false;
	});
});

function sbCommentsAttachPaginationLinks (randName,fcName) {
	$('#'+randName+' .sb_comments_content_div a').unbind('click');
	$('#'+randName+' .sb_comments_content_div a').click(function(){
		if ($(this).attr('name')) {
			return;
		}
		sbCommentsAddLoader(randName);
		var theRegEx = new RegExp('^([^;]*);'+fcName+':([^;&]*);?(&.*)?$','ig');
		var theUrl = '';
		if ($(this).attr('href').match(theRegEx)) {
			theUrl = $(this).attr('href').replace(theRegEx, "$1;"+fcName+":\$2%26exec=true%26r="+Math.random()+"$3");
		} else {
			theUrl = $(this).attr('href').replace(/^([^&]*)(&rewriteurls.*$)?/ig,'$1;'+fcName+":exec=true%26r="+Math.random()+"$2");
		}
		$.getJSON(theUrl,function(response){
			var theHtml = response.data;
			if (typeof(sbUrlRewriter) != 'undefined') {
				theHtml = sbUrlRewriter.rewrite(theHtml);
			}
			$('#'+randName+' .sb_comments_content_div').html(theHtml);
			sbCommentsAttachPaginationLinks (randName,fcName);
			window.location.hash=$('#'+randName+' .sb_comments_content_div').find('a[name]:first').attr('name');
		});
		return false;
	});
	changeDate (randName);
}

function sbCommentsAddLoader (randName) {
	var theWidth = $('#'+randName+' .sb_comments_content_div').width();
	var theHeight = $('#'+randName+' .sb_comments_content_div').height();
	var theTop = $('#'+randName+' .sb_comments_content_div').position().top;
	var theLeft = $('#'+randName+' .sb_comments_content_div').position().left;
	$('#'+randName+' .sb_comments_content_div').append('<div class="sb_comments_preloader_overlay" style="width:'+theWidth+'px;height:'+theHeight+'px;top:'+theTop+'px;left:'+theLeft+'px;"><div class="sb_comments_preloader"></div></div>');
}

function sbCommentsAddFormLoader(randName) {
	var theWidth = $('#'+randName+' form').width()+20;
	var theHeight = $('#'+randName+' form').height()+20;
	$('#'+randName+' form').append('<div class="sb_comments_preloader_overlay" style="width:'+theWidth+'px;height:'+theHeight+'px;top:0px;left:0px;"><div class="sb_comments_preloader"></div></div>');
}

function changeDate (randName,dateFormat,serverTimeStamp) {
	if (dateFormat) {
		sbCommentsDateFormats[randName] = dateFormat;
	} else if(sbCommentsDateFormats[randName]) {
		dateFormat = sbCommentsDateFormats[randName];
	} else {
		dateFormat = 'Y-m-d H:i:s';
	}
	
	if (sbTZDiff === null && serverTimeStamp) {
		var currentDate = new Date();
		var tzDiff = roundToNum(Math.ceil(currentDate.getTime()/1000 - serverTimeStamp)/60,15)*60;
		sbTZDiff = tzDiff;
	} else {
		var tzDiff = sbTZDiff===null ? 0 : sbTZDiff ;
	}
	$('#'+randName+' span.sb_comments_date').each (function (){
		$(this).html(date(dateFormat,parseInt($(this).parent().find('span.sb_comments_timestamp').text())+tzDiff));
	});
}

function roundToNum(value,precision) {
	if (Math.abs(value)<precision/2) {
		return 0;
	}
	var modulo  = value % precision;
	if (modulo === 0) {
		return value;
	} else {
		var fullPart = Math.floor(value/precision);
		if (modulo>(precision/2)) {
			return (fullPart+1)*precision;
		} else {
			return fullPart*precision;
		}
	}
}