var gbComment = new Class({
	Implements: Options,
	
    options: {
		handlerdiv			: 'commentform',
		handlerbtn			: ['sendform', 'sendformguest'],
		//handlerbtn			: 'sendformguest',
		listcomment			: 'listcomment',
		textarea			: 'msg',
		badword				: 'sex|xxx|porn|viagra|teen',
		votevalue			: {
								elid: 'resetvote',
								points: 0,
								votes: 0,
								comment: 0,
								myslogan: false
							}
	},
	
	initialize: function(options){
		this.setOptions(options);
		if ($(this.options.listcomment)){
			for (i=0; i<this.options.handlerbtn.length; i++){
				if ($(this.options.handlerbtn[i])){
					this.bound = {};
					this.bound.onClickEvent = this.onClickEvent.bindWithEvent(this, $(this.options.handlerbtn[i]));
					$(this.options.handlerbtn[i]).addEvent('click', this.bound.onClickEvent);
				}
			}
		}
	},
	
	onClickEvent: function (event, el){
		//if ($(this.options.textarea).value.length > 0){
			this.setRequest();
		//}
		return false;
	},
	
	pushIn: function (sethtml){
		$(this.options.textarea).value = '';
		this.options.votevalue.comment ++;
		if ($('uservote').value.length > 0 && $('uservote').value != '' && $('uservote').value > 0 && this.options.votevalue.myslogan != 'guest'){
			this.options.votevalue.points += Number($('uservote').value);
			this.options.votevalue.votes ++;
			$('voterow').setStyle('display', 'none');
		}
		$(this.options.listcomment).innerHTML += sethtml;
		this.resetVote ();
	},
	
	resetVote: function (){
		average = this.options.votevalue.points/this.options.votevalue.votes;
		if (isNaN(average)){
			average = 0;
		}
		average = average.toFixed(2);
		var txt = '<img src="img/star'+Math.round(average)+'.png" alt="'+average+'" title="'+average+'" /><br />'+this.options.votevalue.points+' Points<br />'+average+' Average<br />'+this.options.votevalue.comment+' Comments';
		if (this.options.votevalue.myslogan && this.options.votevalue.myslogan != 'guest'){
			txt = '<img src="img/star'+Math.round(average)+'.png" alt="'+average+'" title="'+average+'" /><br />'+this.options.votevalue.points+' Points<br />'+this.options.votevalue.votes+' Votes<br />'+average+' Average<br />'+this.options.votevalue.comment+' Comments';
		}
		$(this.options.votevalue.elid).innerHTML = txt;
		
		$(this.options.handlerdiv).setStyle('display', 'none');
	},
	
	checkBadword: function (){
		wordAr = this.options.badword.split('|');
		inputAr = [$(this.options.textarea), $('email')];
		//input = input.replace(/ /g, '');
		for(i=0; i < wordAr.length; i++ ) {
			for (n = 0; n < inputAr.length; n++){
				if (inputAr[n]){
					strpos = inputAr[n].value.indexOf(wordAr[i]);
					if (strpos >= 0 ){
						return ['Badword: '+wordAr[i], strpos, wordAr[i].length, inputAr[n]];
					}else if (n==1 && !this.checkEmail(inputAr[n].value)){
						return ['Wrong E-Mail: '+inputAr[n].value, 0, inputAr[n].value.length, inputAr[n]];
					}
				}
            }
		}
		return true;
	},
	
	showBadword: function (badword){
		alert (badword[0]);
		badword[3].focus();
		if (Browser.Engine.trident){
			range = badword[3].createTextRange();
			range.move("character", badword[1]);
			range.moveEnd("character", badword[2]);
			range.select();
		}else {
			badword[3].selectionStart = badword[1];
			badword[3].selectionEnd = badword[1]+badword[2];
		}
	},
	
	checkEmail: function (email) {
		var a = false;
		var res = false;
		if(typeof(RegExp) == 'function') {
			var b = new RegExp('abc');
			if(b.test('abc') == true){
				a = true;
			}
		}
		
		if(a == true){
			reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
			'(\\@)([a-zA-Z0-9\\-\\.]+)'+
			'(\\.)([a-zA-Z]{2,4})$');
			res = (reg.test(email));
		}else{
			res = (email.search('@') >= 1 && email.lastIndexOf('.') > email.search('@') && email.lastIndexOf('.') >= email.length-5);
		}
		return(res);
	},
	
	setRequest: function (){
		badword = this.checkBadword();
		if (badword != true){
			this.showBadword(badword);
			return false;
		}
		/*
		var txt = '<span><a href="en/slogans.html&amp;bid=1"><strong>aaa</strong></a> (06.08.2009 17:40:39)<br />'+$(this.options.textarea).value+'</span><div class="trenn">&nbsp;</div>';
		this.pushIn(txt);
		*/
		var guest = '';
		if (this.options.votevalue.myslogan == 'guest'){
			guest = '&email='+$('email').value;
		}
		
		//return false;
		var myRequest = new Request({
			method: 'post',
			url: 'inc/ajax_comment.php',
			onSuccess: function (txt,xml){
				this.pushIn(txt);
				//alert (txt);
			}.bind(this),
			onFailure: function (txt){
				//alert (txt);
				alert ('ERROR');
			}
		});
		myRequest.send('msg='+$(this.options.textarea).value+'&uservote='+$('uservote').value+'&bid='+$('bid').value+guest);
	}
	
});
gbComment.implement(new Options);

window.addEvent('domready', function(){
	if (typeof gbCommentOpt == 'undefined'){
		gbCommentOpt = {}; 
	}
	gbComment = new gbComment(gbCommentOpt);
});
