/**
 * 
 * @param {Object} local_conf
 */
CommentsList = function(local_conf) {
	//
    var conf = {};

	conf.boxId = null;
	
	conf.commentBlockClassName = null;
	conf.commentAuthorFieldName = null;
	conf.commentTextFieldName = null;
	conf.commentIdFieldName = null;
	conf.spamIdFieldName = null;
	conf.postContentClassName = null;
	conf.resetBlockIdFieldName = null;
		
	conf.commentAuthorEmptyValue = '';
	conf.commentTextEmptyValue = '';
	conf.spamTextEmptyValue = '';

	conf.commentAuthorMaxChars = 20;
	conf.commentTextMaxChars = 3000;
	conf.spamTextMaxChars = 3000;

	conf.listAjaxUrl = null;
	conf.commentAjaxUrl = null;
	conf.spamAjaxUrl = null;

	conf.commentSuccessHtml = '';
	conf.commentErrorHtml = '';
	conf.spamSuccessHtml = '';
	conf.spamErrorHtml = '';

	conf.commentAuthorInvalidInfo = '';
	conf.commentTextInvalidInfo = '';
	conf.spamReasonInvalidInfo = '';
	conf.spamTextInvalidInfo = '';
	
	conf.slideSpeed = 250;
	conf.infoDelay = 2000;
	
	conf.userIsLogged = false;//czy jestesmy zalogowani
		
	conf.commentsReferers = null;
	//
	var boxJQ = jQuery([]);

	var commentBlockJQ = jQuery([]);
	var commentFormJQ = jQuery([]);
	var commentSuccessJQ = jQuery([]);
	var commentErrorJQ = jQuery([]);	

	var spamBlockJQ = jQuery([]);
	var spamFormJQ = jQuery([]);
	var spamSuccessJQ = jQuery([]);
	var spamErrorJQ = jQuery([]);
	var spamErrorDataJQ = jQuery([]);
	
	var resetBlockJQ = jQuery([]);
	
	var resetCommentForm = true;
	
	//
	var tools = {};	
	
	tools.maxChars = function(elementJQ, maxChars) {
		elementJQ
			.keypress(function(event) {  
				var key = event.which;  

				if(key >= 33 || key == 13) {  
					if(jQuery(this).val().length >= maxChars) {  
						event.preventDefault();
						return false;
					}  
				}  
			});  
	}

	/**
	 * 
	 */
	var init = function(local_conf) {
		conf = jQuery.extend(conf, local_conf);
			//
	
		//
		boxJQ = jQuery('#' + conf.boxId);

		boxJQ.find('.' + conf.commentBlockClassName).not('.firstPost').hide();
		
		commentBlockJQ = boxJQ.find('.' + conf.commentBlockClassName);
		commentFormJQ = commentBlockJQ.find('form').not('#createThreadForm');
		
		commentSuccessJQ = jQuery(conf.commentSuccessHtml ? '<div id="postAddForm" class="postMessage messageSuccess"><div class="modBegin"><span></span></div>'+conf.commentSuccessHtml+'<div class="modEnd"><span></span></div></div>' : [])
			.insertBefore(commentBlockJQ)
			.hide();
		
		commentErrorJQ = jQuery(conf.commentErrorHtml ? '<div id="postAddForm" class="postMessage messageError"><div class="modBegin"><span></span></div>'+conf.commentErrorHtml+'<div class="modEnd"><span></span></div></div>' : [])
			.insertBefore(commentBlockJQ)
			.hide();

		spamBlockJQ = boxJQ.find('.postSpamForm').hide();		
		spamFormJQ = spamBlockJQ.find('form');

		spamSuccessJQ = jQuery(conf.spamSuccessHtml ? '<div class="postMessage messageSuccess"><div class="modBegin"><span></span></div>'+conf.spamSuccessHtml+'<div class="modEnd"><span></span></div></div>' : [])
			.insertBefore(spamBlockJQ)
			.hide();

		spamErrorJQ = jQuery(conf.spamErrorHtml ? '<div class="postMessage messageError"><div class="modBegin"><span></span></div>'+conf.spamErrorHtml+'<div class="modEnd"><span></span></div></div>' : [])
			.insertBefore(spamBlockJQ)
			.hide();

		spamErrorDataJQ = jQuery(conf.spamErrorDataHtml ? '<div class="postMessage messageError"><div class="modBegin"><span></span></div>'+conf.spamErrorDataHtml+'<div class="modEnd"><span></span></div></div>' : [])
			.insertBefore(spamBlockJQ)
			.hide();
		
		resetBlockJQ = boxJQ.find('#'+conf.resetBlockIdFieldName).hide();
					
		if(Inpl.User.isLoggedIn()){			
			var author = Inpl.User.getNick();
			conf.commentAuthorEmptyValue = author;
			commentFormJQ.find('.author input[type="text"]').val(author);
			
			spamFormJQ.find('.imageCodeImg').remove();							
			jQuery('<input>').attr({type:'hidden',name:'userSess',id:'userSess',value:Inpl.User.getNick()}).appendTo(spamFormJQ);			
		}

		commentFormJQ.find('.author input[type="text"]')
			.val(conf.commentAuthorEmptyValue)
			.focus(function() {
				if(jQuery(this).val() === conf.commentAuthorEmptyValue) {
					jQuery(this).val('');
//					jQuery(this).get(0).select();
				}
			})
			.keydown(function() {
				if(jQuery(this).val() === conf.commentAuthorEmptyValue) {
					jQuery(this).val('');
				}
			})
			.blur(function() {
				if(jQuery(this).val() === '') {
					jQuery(this).val(conf.commentAuthorEmptyValue);
				}
			});

		commentFormJQ.find('.text textarea')
			.val(conf.commentTextEmptyValue)
			.focus(function() {
				if(jQuery(this).val() === conf.commentTextEmptyValue) {
					jQuery(this).val('');
//					jQuery(this).get(0).select();
				}
			})
			.keydown(function() {
				if(jQuery(this).val() === conf.commentTextEmptyValue) {
					jQuery(this).val('');
				}
			})
			.blur(function() {
				if(jQuery(this).val() === '') {
					jQuery(this).val(conf.commentTextEmptyValue);
				}
			});

		spamFormJQ.find('.message textarea')
			.val(conf.spamTextEmptyValue)
			.focus(function() {				
				if(jQuery(this).val() === conf.spamTextEmptyValue) {
					jQuery(this).val('');
//					jQuery(this).get(0).select();
				}
			})
			.keydown(function() {
				if(jQuery(this).val() === conf.spamTextEmptyValue) {
					jQuery(this).val('');
				}
			})
			.blur(function() {
				if(jQuery(this).val() === '') {
					jQuery(this).val(conf.spamTextEmptyValue);
				}
			});

		//
		tools.maxChars(commentFormJQ.find('.author input[type="text"]'), conf.commentAuthorMaxChars);
		tools.maxChars(commentFormJQ.find('.text textarea'), conf.commentTextMaxChars);
		tools.maxChars(spamFormJQ.find('.message textarea'), conf.spamTextMaxChars);
		
		//
		commentFormJQ.find('.author input[type="text"], .text textarea')
			.after('<div class="invalidInfo"></div>');

		spamFormJQ.find('.reason label').eq(0).add('.message textarea').add('.imageCodeImg #imageCodeImgRefresh')
			.after('<div class="invalidInfo"></div>');
		/**
			przyciski akcji
		*/
		
		if(conf.userIsLogged) {
		
			boxJQ.find('.addPost a').click(function() {
				/*comment(jQuery(this).parents('.addPost').eq(0));*/
				comment(jQuery('.addPost').eq(0).find('a').parents('.addPost').eq(0));
				return false;
			});
		
			boxJQ.find('.list .functions .reply').click(function() {
				reply(jQuery(this).parents('.'+conf.postContentClassName).eq(0), jQuery(this).parents('li').eq(0));
				return false;
			});
			
			boxJQ.find('.list .functions .quoteReply').click(function() {
				reply(jQuery(this).parents('.addPost').eq(0), jQuery(this).parents('li').eq(0), true);
				/*reply(jQuery(this).parents('.'+conf.postContentClassName).eq(0), jQuery(this).parents('li').eq(0), true);*/
				return false;
			});
	
			boxJQ.find('.list .functions .spam').click(function() {			
				spam(jQuery(this).parents('.'+conf.postContentClassName).eq(0), jQuery(this).parents('li').eq(0));
				return false;
			});
					
			try {
				if(location.pathname){			   
					jQuery(document).find('.list .functions .reply a[href="'+location.pathname+location.hash+'"]').click();
					jQuery(document).find('.list .functions .quoteReply a[href="'+location.pathname+location.hash+'"]').click();
					jQuery(document).find('.list .functions .spam a[href="'+location.pathname+location.hash+'"]').click();
					
					if(location.hash.indexOf('postAddForm')>-1||location.pathname.indexOf(',postAddForm')>-1){						
						resetCommentForm = false;					
						jQuery('.addPost a').eq(0).click();					
					}
					
				}
			}catch(e){}
						
		} else {
			
			prependLoginFormReferer(boxJQ.find('.list .functions .reply'));
			prependLoginFormReferer(boxJQ.find('.list .functions .quoteReply'));			
			prependLoginFormReferer(boxJQ.find('.list .functions .spam'));			
			
			if( jQuery('.addPost a').is('a') ) {
				jQuery('.addPost a').setFloatbox({
	            ajaxUrl:'/ajax/logowanie?referer='+conf.commentsReferers['addPost'].refererUrl+'&crc='+conf.commentsReferers['addPost'].crc,
	            wrapperClass: 'floatbox-loginForm',
	            closeButtonHtml: '<div class="close-floatbox-button close-floatbox"><span>zamknij</span></div>',
	            after: function (htmlJQ) {
	                setTimeout(function() {
	                        jQuery('#login').focus();
	                    },
	                    1000);
	
	                jQuery('#floatbox-box')
	                    .find('a, input, textarea').not('[type="hidden"]')
	                        .each(function(i) {
	                            jQuery(this).attr('tabindex', i + 1);
	                        });
	            }});
			}	
		    return false;
		}
				
		//
		commentFormJQ.submit(function() {
			commentFormSubmit(jQuery(this));
			return false;
		});

		commentFormJQ.find('input[type="reset"]').click(function() {
			commentFormDiscard();
			return false;
		});

		spamFormJQ.submit(function() {
			spamFormSubmit(jQuery(this));
			return false;
		});

		spamFormJQ.find('input[type="reset"]').click(function() {
			spamFormDiscard();
			return false;
		});

		//	
		spamFormJQ.find('.message').hide();	
		spamFormJQ.find('.reason input[type="radio"]').click(function() {
			if(jQuery(this).is(':checked')) {
				switch(jQuery(this).val()) {
					case '3':
						spamFormJQ
							.find('.message')
								.show()
								.css('display','block');
						break;
					case '1':
					case '2':
					default:
						spamFormJQ.find('.message').hide();
				}
			}
		});		
		
		
		//
		jQuery.ajaxSetup({
			timeout: 10000});
	};
	
	var prependLoginFormReferer = function(replyButtons){
		for(a in replyButtons){
					
			if(replyButtons[a].tagName && replyButtons[a].tagName.toLowerCase()=='li'){	
				indx = jQuery(replyButtons[a]).parents('li').eq(0).attr('id').substring(1);				
				jQuery(replyButtons[a]).setFloatbox({
		            ajaxUrl: '/ajax/logowanie?referer='+conf.commentsReferers[indx].refererUrl+'&crc='+conf.commentsReferers[indx].crc,
		            wrapperClass: 'floatbox-loginForm',
		            closeButtonHtml: '<div class="close-floatbox-button close-floatbox"><span>zamknij</span></div>',
		            after: function (htmlJQ) {
		                setTimeout(function() {
		                        jQuery('#login').focus();
		                    },
		                    1000);
		
		                jQuery('#floatbox-box')
		                    .find('a, input, textarea').not('[type="hidden"]')
		                        .each(function(i) {
		                            jQuery(this).attr('tabindex', i + 1);
		                        });
		            }});
			}
		}
	}
	
	/**
	 * 
	 * @param {Object} beforeJQ
	 */
	var comment = function(beforeJQ) {
	
		if(commentBlockJQ.prev().get(0) !== beforeJQ.get(0)) {
			commentBlockJQ.animate({
				'height': 'hide', 'opacity': 'hide'},
				{duration: conf.slideSpeed});
		}
		
		spamBlockJQ.animate({
			'height': 'hide', 'opacity': 'hide'},
			{duration: conf.slideSpeed});
		spamSuccessJQ.hide();
		spamErrorJQ.hide();
		spamFormJQ.show();
		
		commentBlockJQ.queue(function() {
			
			jQuery('.messageSuccess').hide();
			jQuery('.messageError').hide();
			commentSuccessJQ.hide();
			commentErrorJQ.hide();
			
			resetBlockJQ.show();
			commentFormJQ.show();
	
			if( resetCommentForm ) {
				commentFormReset();
			}
			
			/*if(commentBlockJQ.prev().get(0) !== beforeJQ.get(0)) {*/				
				/*
					
				*/
				commentFormJQ
					.find('input[name="'+conf.commentIdFieldName+'"][type="hidden"]')
						.val('');
				
				commentFormJQ
					.find('a[rel="floatbox[login_href]"]')												
						.attr('href','/logowanie?referer='+conf.commentsReferers['addPost'].refererUrl+'&crc='+conf.commentsReferers['addPost'].crc);
				
				jQuery(this).insertAfter(beforeJQ);
				
				commentErrorJQ.insertAfter(jQuery(this));
				commentSuccessJQ.insertAfter(jQuery(this));
			/*}*/

			jQuery(this).dequeue();
		});

		commentBlockJQ.animate({
			'height': 'show', 'opacity': 'show'},
			{duration: conf.slideSpeed});

		commentBlockJQ.queue(function() {			
			commentFormJQ.find('.author input[name="'+conf.commentAuthorFieldName+'"][type="text"], .text textarea[name="'+conf.commentTextFieldName+'"]')
				.eq(0).focus().get(0).select();

			jQuery(this).dequeue();
		});
		
		jQuery(window).scrollTo( '#'+conf.boxId );
		
	};

	/**
	 * 
	 * @param {Object} blockJQ
	 */
	var reply = function(beforeJQ, blockJQ, isQuote) {
		isQuote = typeof(isQuote) === 'boolean' ? isQuote : false;

		spamBlockJQ.animate({
			'height': 'hide', 'opacity': 'hide'},
			{duration: conf.slideSpeed});
		spamSuccessJQ.hide();
		spamErrorJQ.hide();
		spamFormJQ.show();
			
		if(commentBlockJQ.prev().get(0) !== beforeJQ.get(0)) {
			commentBlockJQ.animate({
				'height': 'hide', 'opacity': 'hide'},
				{duration: conf.slideSpeed});
		}
			
		commentBlockJQ.queue(function() {
			
			jQuery('.messageSuccess').hide();
			jQuery('.messageError').hide();
			commentSuccessJQ.hide();
			commentErrorJQ.hide();
			
			resetBlockJQ.show();
			commentFormJQ.show();
	
			if(commentBlockJQ.prev().get(0) !== beforeJQ.get(0)) {
				commentFormReset();
				commentFormJQ
					.find('input[name="'+conf.commentIdFieldName+'"][type="hidden"]')
						.val(blockJQ.attr('id').substring(1));

				commentFormJQ.find('a[rel="floatbox[login_href]"]')
								.attr('href','/logowanie?referer='+conf.commentsReferers[blockJQ.attr('id').substring(1)].refererUrl+'&crc='+conf.commentsReferers[blockJQ.attr('id').substring(1)].crc);
				
				jQuery(this).insertAfter(beforeJQ);
				
				commentErrorJQ.insertAfter(jQuery(this));
				commentSuccessJQ.insertAfter(jQuery(this));
				
			}

			if(isQuote) {
				quote(blockJQ);
			}

			jQuery(this).dequeue();
		});

		commentBlockJQ.animate({
			'height': 'show', 'opacity': 'show'},
			{duration: conf.slideSpeed});

		commentBlockJQ.queue(function() {
			commentFormJQ.find('.author input[name="'+conf.commentAuthorFieldName+'"][type="text"], .text textarea[name="'+conf.commentTextFieldName+'"]')
				.eq(0)
					.focus()
					.each(function() {
						if(!isQuote) {
							jQuery(this).get(0).select();
						}
					});				
			jQuery(this).dequeue();
		});
		jQuery(window).scrollTo( '#'+conf.boxId );
	};
	
	/**
	 * 
	 * @param {Object} blockJQ
	 */
	var quote = function(blockJQ) {
	
		var block = blockJQ.find('.'+conf.postContentClassName+' .postText');			
				
		var txt = '';	
		
		jQuery(block).contents().filter(function(){
			return !jQuery(this).hasClass('quo');
		}).each(function(){
			if( this.nodeType == 3 ) {
				txt += this.nodeValue;
			} 
		});
				
		var cite = jQuery(block).find('.quo').get(0);
		
		if( cite ) {
			cite = jQuery(cite).html();			
		} else {
			cite = '';
		}		
			
		var users = /<span\sclass="?user"?>([\w~\s]+)<\/span>/gi;					
		cite = cite.replace(users, "[quote user=$1]");
					
		var quo = /<span\sclass="?quo"?>/gi;					
		cite = cite.replace(quo, "");
		
		var spans = /<\/span>/gi;					
		cite = cite.replace(spans, "[/quote]");
		
		if( cite != '' ) {
			cite = cite+"[/quote]";
		}								
				
		var quoteTextNew = "[quote user="+blockJQ.find('.author .user cite').children().text()+"]"+txt+cite+"[/quote]";
		
		commentFormJQ
			.find('.text textarea')
				.val(quoteTextNew + "\n");
	};

	/**
	 * 
	 * @param {Object} formJQ
	 */
	var commentFormSubmit = function(formJQ) {
		formJQ.ajaxSubmit({
			url: conf.commentAjaxUrl,
			dataType: 'json',
			beforeSubmit: function(formData, commentFormJQ, options) {
				var valid = true;
				
				var commentAuthorJQ = commentFormJQ.find('.author input[name="'+conf.commentAuthorFieldName+'"][type="text"]');			
				if(commentAuthorJQ.is('.author input[name="'+conf.commentAuthorFieldName+'"][type="text"]') && commentAuthorJQ.val().length > 0  && commentAuthorJQ.val().length <= conf.commentAuthorMaxChars ) {
					var commentAuthorValue = commentAuthorJQ.val();					
					if(commentAuthorValue === '' || commentAuthorValue === conf.commentAuthorEmptyValue) {
						commentFormJQ.find('.author')
							.addClass('invalid')
							.find('.invalidInfo')
								.text(conf.commentAuthorInvalidInfo);
	
						valid = false;
					}
					else {
						commentFormJQ.find('.author')
							.removeClass('invalid')
							.find('.invalidInfo').text('');
					}
				} else if(commentAuthorJQ.is('.author input[name="'+conf.commentAuthorFieldName+'"][type="text"]') && commentAuthorJQ.val().length > conf.commentAuthorMaxChars) {
					commentFormJQ.find('.author')
						.addClass('invalid')
						.find('.invalidInfo')
							.text('Podpis przekracza dozwoloną długość.');

					valid = false;
				}

				var commentTextJQ = commentFormJQ.find('.text textarea[name="'+conf.commentTextFieldName+'"]');				
				if( commentTextJQ.is('.text textarea[name="'+conf.commentTextFieldName+'"]') && commentTextJQ.val().length > 0 && commentTextJQ.val().length <= conf.commentTextMaxChars) {
					var commentTextValue = commentTextJQ.val();
								
					if(commentTextValue === '' || commentTextValue === conf.commentTextEmptyValue) {
						commentFormJQ.find('li.text')
							.addClass('invalid')
							.find('.invalidInfo')
								.text(conf.commentTextInvalidInfo);
	
						valid = false;
					}
					else {
						commentFormJQ.find('li.text')
							.removeClass('invalid')
							.find('.invalidInfo').text('');
					}
				} else if( commentTextJQ.is('.text textarea[name="'+conf.commentTextFieldName+'"]') && commentTextJQ.val().length > conf.commentTextMaxChars) {
					commentFormJQ.find('li.text')
						.addClass('invalid')
						.find('.invalidInfo')
							.text('Treść przekracza dozwoloną długość.');

					valid = false;
				}
				
				if(valid === true) {
					commentBlockJQ.animate({
						'opacity': .5},
						{duration: conf.slideSpeed});

					return true;
				}

				return false;
			},
			success: function(json) {
				commentBlockJQ.animate({'opacity': 1});
				commentBlockJQ.hide();
				commentFormJQ.hide();
				
				switch(json.status) {
					case 'ok':
						commentFormReset();
						commentSuccessJQ.show();

						setTimeout(function() {
								commentSuccessJQ.animate({
									'height': 'hide', 'opacity': 'hide'},
									{duration: conf.slideSpeed});
							},
							conf.infoDelay);
							
						break;

					case 'error':
						if(json.code=='not logged' && conf.redirectPageUrl){
							location.href = conf.redirectPageUrl;
							break;
						}
					default:
						commentErrorJQ.show();

						setTimeout(function() {
								commentBlockJQ.animate({
									'height': 'hide', 'opacity': 'hide'},
									{duration: conf.slideSpeed});
							},
							conf.infoDelay);

						break;
				}
			},
			error: function(e) {
				commentBlockJQ.animate({'opacity': 1});
				commentBlockJQ.hide();
				commentFormJQ.hide();

				commentErrorJQ.show();

				setTimeout(function() {
						commentBlockJQ.animate({
							'height': 'hide', 'opacity': 'hide'},
							{duration: conf.slideSpeed});
					},
					conf.infoDelay);
			}});
	}

	/**
	 * 
	 */
	var commentFormDiscard = function() {
		commentBlockJQ.animate({
			'height': 'hide', 'opacity': 'hide'},
			{duration: conf.slideSpeed});

		commentBlockJQ.queue(function() {
			commentFormReset();
			jQuery(this).dequeue();
		});
	}

	/**
	 * 
	 */
	var commentFormReset = function() {
		commentFormJQ
			.resetForm()
			.find('.invalid')
				.removeClass('invalid')
				.end()
			.find('.invalidInfo')
				.text('')
				.end()
			.find('.author input[type="text"]')
				.val(conf.commentAuthorEmptyValue)
				.end()
			.find('.text textarea')
				.val(conf.commentTextEmptyValue);
	}

	/**
	 * 
	 * @param {Object} blockJQ
	 */
	var spam = function(beforeJQ, blockJQ) {
			
		commentBlockJQ.animate({
			'height': 'hide', 'opacity': 'hide'},
			{duration: conf.slideSpeed});
		commentSuccessJQ.hide();
		commentErrorJQ.hide();
		spamErrorDataJQ.hide();
		commentFormJQ.show();
		
		if(spamBlockJQ.prev().get(0) !== beforeJQ.get(0)) {
			spamBlockJQ.animate({
				'height': 'hide', 'opacity': 'hide'},
				{duration: conf.slideSpeed});
		}

		spamBlockJQ.queue(function() {
			jQuery('.messageSuccess').hide();
			jQuery('.messageError').hide();
			spamSuccessJQ.hide();
			spamErrorJQ.hide();
			spamErrorDataJQ.hide();
			spamFormJQ.show();

			if(spamBlockJQ.prev().get(0) !== beforeJQ.get(0)) {
				spamFormReset();				
				spamFormJQ
					.find('input[name="'+conf.spamIdFieldName+'"][type="hidden"]')
						.val(blockJQ.attr('id').substring(1));

				jQuery(this).insertAfter(beforeJQ);
			
				spamErrorDataJQ.insertAfter(jQuery(this));
				spamErrorJQ.insertAfter(jQuery(this));
				spamSuccessJQ.insertAfter(jQuery(this));
			}

			jQuery(this).dequeue();
		});

		spamBlockJQ.animate({
			'height': 'show', 'opacity': 'show'},
			{duration: conf.slideSpeed});

		spamBlockJQ.queue(function() {
			spamBlockJQ.find('.reason input[name="reason"], .message textarea[name="message"]')
				.eq(0).focus().get(0).select();

			jQuery(this).dequeue();
		});
	};

	/**
	 * 
	 * @param {Object} formJQ
	 */
	var spamFormSubmit = function(formJQ) {
		
		formJQ.ajaxSubmit({
			dataType: 'json',
			url: conf.spamAjaxUrl,
			beforeSubmit: function(formData, spamFormJQ, options) {
				var valid = true;
				
				var spamReasonJQ = spamFormJQ.find('.reason input[type="radio"]:checked');
				
				if(spamReasonJQ.length === 0) {
					spamFormJQ.find('.reason')
						.addClass('invalid')
						.find('.invalidInfo').text(conf.spamReasonInvalidInfo);
					valid = false;
					return false;
				}
				else {
					spamFormJQ.find('.reason')
						.removeClass('invalid')
						.find('.invalidInfo').text('');
				}
				
				var spamTextJQ = spamFormJQ.find('.message textarea[name="message"]');
				if(spamReasonJQ.length > 0 && spamTextJQ.length > 0) {
					var spamTextValue = spamTextJQ.val();
					if(spamReasonJQ.val() === '3' && (spamTextValue === '' || spamTextValue === conf.spamTextEmptyValue)) {
						spamFormJQ.find('.message')
							.addClass('invalid')
							.find('.invalidInfo')
							.text(conf.spamTextInvalidInfo);
						valid = false;
					
						return false;
					}
					else {
						spamFormJQ.find('.message')
							.removeClass('invalid')
							.find('.invalidInfo').text('');
					}
				}
				
				if(jQuery('#imageCodeImg-input').val()==""){
					var text = spamFormJQ.find('.imageCodeImg')
							.addClass('invalid')
							.find('.invalidInfo')
							.text("Przepisz kod z obrazka");
							valid = false;
						return false;
				}
				
				if(valid === true) {
					spamBlockJQ.animate({
						'opacity': 0.5},
						{duration: conf.slideSpeed});
					return true;				
				}
				
				return false;
			},
			success: function(json) {
				spamBlockJQ.animate({'opacity': 1});
				spamBlockJQ.hide();
				spamFormJQ.hide();

				switch(json.status) {
					case 'ok':
						spamFormReset();
						spamSuccessJQ.show();
		
						setTimeout(function() {
							spamSuccessJQ.animate({
									'height': 'hide', 'opacity': 'hide'},
									{duration: conf.slideSpeed});
							},
							conf.infoDelay);

						break;

					case 'error':
						if(json.code=='0'){
								spamErrorDataJQ.show();
								setTimeout(
									function() {
										spamErrorDataJQ.animate({
											'height': 'hide', 'opacity': 'hide'},
											{duration: conf.slideSpeed});
									},
									conf.infoDelay);
									break;
							}
					default:
						spamErrorJQ.show();

						setTimeout(function() {
								spamBlockJQ.animate({
									'height': 'hide', 'opacity': 'hide'},
									{duration: conf.slideSpeed});
							},
							conf.infoDelay);

						break;
				}
				
				
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				spamBlockJQ.animate({'opacity': 1});
				spamBlockJQ.hide();
				spamFormJQ.hide();

				spamErrorJQ.show();

				setTimeout(function() {
						spamBlockJQ.animate({
							'height': 'hide', 'opacity': 'hide'},
							{duration: conf.slideSpeed});
					},
					conf.infoDelay);
			}});
	}

	/**
	 * 
	 */
	var spamFormDiscard = function() {
		spamBlockJQ.animate({
			'height': 'hide', 'opacity': 'hide'},
			{duration: conf.slideSpeed});

		spamBlockJQ.queue(function() {
			spamFormReset();
			jQuery(this).dequeue();
		});
	}

	/**
	 * 
	 */
	var spamFormReset = function() {		
		spamFormJQ
			.resetForm()
			.find('.invalid')
				.removeClass('invalid')
				.end()
			.find('.invalidInfo')
				.text('')
				.end()
			.find('.message textarea')
				.val(conf.spamTextEmptyValue)
				.end()
			.find('#imageCodeImg-input')
				.val('')
				.end()
			.find('#imageCodeImgRefresh').click();
	}

	//
	jQuery(document).ready(
    	function() {
        	init(local_conf);
    	}
    );
}
