/* ******************************************** */
/*                                              */
/*                    SETTINGS                  */
/*                                              */
/* ******************************************** */



jQuery().ready(function() {	

	
	jQuery('.write_comment_addlink').click(function() {
		jQuery('.write_comment_field').val('');
		jQuery('.write_comments_active_buttons').show();
		jQuery('.write_comments_deactivated_buttons').hide();
		jQuery('.write_comment_save_indicator').hide();
		jQuery('#write_comment_addlink').hide();
		jQuery('#write_comment_wrapper').slideDown(500);
	});	
		
	jQuery('#write_comment_cancle_btn').click(function() {
		jQuery('#write_comment_addlink').show();
		jQuery('#write_comment_wrapper').slideUp(500);
	});		
	
	jQuery('#write_comment_save_btn').click(function() {
		
		text = jQuery('.write_comment_field').val();
		
		tmp_text = text.replace(/\t/g,"");
		tmp_text = tmp_text.replace(/\t/g,"");
		tmp_text = tmp_text.replace(/\s+/g,"");
		tmp_text = tmp_text.replace(/\<1br \/>/gi, "");

		if(tmp_text.length > 0) {
			jQuery.ajax({
				type: "POST",
				dataType: 'jsonp',
				data: { comment : text },
				url: 'ajax_make_comment',
				success: function(response){
					text = response.comment;
					author_date = response.info;
					
					
					if(text.length > 0 && text != "NONE") {		
						jQuery('.write_comment_error').hide();		
						jQuery('.write_comment_save_indicator').show();				
						jQuery('.write_comments_active_buttons').hide();
						jQuery('.write_comments_deactivated_buttons').show();
						
						task = window.setTimeout(function() {
							jQuery('#write_comment_addlink').show();
							jQuery('#write_comment_wrapper').slideUp(500, function() {
								
								jQuery('<li />').append(jQuery('<div />').html(text ).append(jQuery('<span />').addClass('info grey').html('Written by ' + author_date + ' UTC' + '<br /><br />'))).prependTo('#comments_list').hide().slideDown(500);
								
								if(jQuery('#comments_list').children().length > 5) {
									jQuery('#comments_list > :last').slideUp(500, function() {
										jQuery(this).remove();
									});
								}
								
								jQuery('.no_comment_there').slideUp(500);
							});
							
							comments_count = parseInt(jQuery('.all_comments_count').html()) + 1;
							jQuery('.all_comments_count').html(comments_count);
							if(comments_count > 5) {
								jQuery('.view_all_comments_link').fadeIn(500);
							}
							
						}, 500)
					} else {						
						jQuery('.write_comment_error').show().html('Please enter a review without HTML Tags');	
					}
				}
			});
						
			
		} else {
			jQuery('.write_comment_error').show().html('Please enter a review');	
		}
		
		
		
		
		
	});		
	
	
	
});
