if (1 == intelli.config.html_reviews)
{
	$("textarea.ckeditor_textarea").each(function()
	{
		if(!CKEDITOR.instances[$(this).attr("id")])
		{
			intelli.ckeditor($(this).attr("id"), {toolbar: 'Basic'});
		}
	});	
}
var rate_it=function()
{
	$('#submit_reviews').css("display",($('#submit_reviews').css('display')=='none' ? "block" : 'none'));
}
var last_star=0;
var star_it=function(id)
{
	if(last_star!=0)$('#r'+last_star+'-unit').toggleClass('rate');
	$('#r'+id+'-unit').toggleClass('rate');
	$('#rating_field').val(id);
	last_star = id;
}
$(function()
{
	$("div.rev_rat").each(function(i, v)
	{
		var prf = $(this).attr('id').split('_')[2];

		var comment_rating = new commentRating({
			el: $(this).attr('id'),
			max: intelli.config.reviews_rating_max,
			text: intelli.lang.reviews_rate_this,
			cls: 'revrat_' + prf + '_'
		});
		
		comment_rating.init();
	});

	var rewiews_textcounter = new intelli.textcounter({
		textarea_el: 'review_form'
		,counter_el: 'review_counter'
		,max: intelli.config.reviews_max_chars
		,min: intelli.config.reviews_min_chars
	}).init();

	$("#review").validate(
	{
		rules:
		{
			author: "required"
			,email:
			{
				required: true
				,email: true
			}
			,security_code: "required"
			,review:
			{
				required: true
				,minlength: intelli.config.reviews_min_chars
				,maxlength: intelli.config.reviews_max_chars
			}
		}
		,submitHandler: function(form)
		{
			var el = $("#add");
			//var form = $(this);

			el.attr("disabled", "disabled");
			el.val("Loading...");
			el.css("background", "url('templates/common/img/ajax-loader.gif') left top no-repeat");
			el.css("padding-left", "15px");

			var body = $("#review_form").val();
			var listing_id = $("input[name='listing_id']").val();
			var security_code = $("input[name='security_code']").val();
			var title = $("input[name='title']").val();
			var rate = $("input[name='rate']").val();
			var author = $("input[name='author']").val();

			var params = new Object();

			params['action'] = 'add';
			params['title'] = title;
			params['body'] = body;
			params['captcha'] = security_code;
			params['listing_id'] = listing_id;
			params['author'] = author;
			params['rate'] = new Object();

			$("input[name^='revrat_']").each(function(i, v)
			{
				var id = $(this).attr('name').split('_')[1];
				
				params['rate[' + id + ']'] = $(this).val();
			});

			$.post("controller.php?plugin=reviews", params, function(data)
			{
				var data = eval('(' + data + ')');
				var type = data.error ? 'error' : 'notification';
				
				el.attr("disabled", "");
				el.val("Rate it");
				el.css("background", "");
				el.css("padding-left", "");

				if(!data.error)
				{
					if(1 == intelli.config.reviews_approval)
					{
						var html = new Array();

						html = ['<div class="posted">', intelli.lang.review_author, '&nbsp;',
						'<strong>', data.review.author, '</strong>&nbsp;/&nbsp;', data.review.date, '</div>',
						'<div class="review">', data.review.body, '</div>'].join('');

						$("#reviews_container").append(html);
					}

					$("input[name='title'][type='text']").val('');
					$("input[name='rate'][type='text']").val('1');
					$("input[name='security_code'][type='text']").val('');
					$("#captcha_image_1").click();
					$("#review_form").val('');

					rewiews_textcounter.init();
				}

				intelli.notifBox(
				{
					id: 'error',
					type: type,
					msg: data.msg
				});
			});
		}
	});
});


