
/**
 * unpolite.js
 *
 * add functionality to the unpolite links so that people can submit their complaint from within the page
 */
 
var unpolite = Class.create({
	
	/*
	 * CONSTRUCTOR
	 */
	initialize: function ( wrapperId , linkClass , commentPrefix , nodeId , lang, number1, number2, hash ) {
		// delivered variables
		this.root 					= $( wrapperId );
		this.linkClass 				= linkClass;
		this.commentPrefixLength 	= commentPrefix.length;
		this.nodeId 				= nodeId;
		this.lang 					= lang;
		this.number1				= number1;
		this.number2				= number2;
		this.hash					= hash;
	
		// language definitions
		this.bo 			= new Array();
		this.bo['nl'] 		= "Beledigend? Ongepast?";
		this.bo['fr'] 		= "Offensant? Inapproprié?";  // IE 6 error with é
		
		this.p1 			= new Array();
		this.p1['nl'] 		= "Geef hier aan waarom je de bovenstaande reactie in strijd vindt met onze huisregels en waarom deze reactie dus niet had mogen verschijnen. Onze moderator zal opnieuw beoordelen of de reactie terecht is geplaatst.";
		this.p1['fr'] 		= "Dites nous pourquoi vous trouvez que cette réaction ne respecte pas nos règles du jeu et ne devrait pas être affichée.  Notre modérateur réexaminera la réaction afin de déterminer si elle est justifiée.";
		
		this.ne 			= new Array();
		this.ne['nl'] 		= "Gelieve uw naam in te vullen.";
		this.ne['fr'] 		= "Veuillez indiquez votre nom";
		
		this.n 				= new Array();
		this.n['nl'] 		= "Uw Naam:";
		this.n['fr'] 		= "Votre nom:";
		
		this.ee 			= new Array();
		this.ee['nl'] 		= "Gelieve uw e-mail adres in te vullen.";
		this.ee['fr'] 		= "Veuillez indiquer votre adresse e-mail.";
		
		this.e 				= new Array();
		this.e['nl'] 		= "Uw email:";
		this.e['fr'] 		= "Votre e-mail:";
		
		this.ce 			= new Array();
		this.ce['nl'] 		= "Gelieve uw commentaar in te vullen.";
		this.ce['fr'] 		= "Veuillez indiquer un commentaire.";
		
		this.c 				= new Array();
		this.c['nl'] 		= "Uw commentaar:";
		this.c['fr'] 		= "Votre commentaire:";
		
		this.sr 			= new Array();
		this.sr['nl'] 		= "Spelregels";
		this.sr['fr'] 		= "Règles du jeu";
		
		this.srLink 		= new Array();
		this.srLink['nl'] 	= "/service/disclaimer#spelregels";
		this.srLink['fr'] 	= "/service/mentionslegales#spelregels";
		
		this.v 				= new Array();
		this.v['nl'] 		= "Verzenden";
		this.v['fr'] 		= "Envoyer";
		
		this.a 				= new Array();
		this.a['nl'] 		= "Annuleren";
		this.a['fr'] 		= "Annuler";
		
		this.aba			= new Array();
		this.aba['nl']   	= "Gelieve een som in te vullen.";
		this.aba['fr']   	= "Gelieve een som in te vullen.";
		
		this.capt			= new Array();
		this.capt['nl']		= "Spam protection:"; 
		this.capt['fr']		= "Spam protection:";
		
		this.sum			= new Array();
		this.sum['nl']		= " is de som van " + this.number1 + " + " + this.number2;
		this.sum['fr']		= " est la somme de " + this.number1 + " + " + this.number2;
				
	
		// new internal variables
		this.counter = 0;
	
	
		// initialize
		if(this.root != undefined) {
			this.initLinks();
		}
	},


	/*
	 * INITIALIZE LINKS
	 */
	initLinks: function() {
		var unpoliteLinks = this.root.getElementsByClassName( this.linkClass );
		
		for( var i = 0 ; i < unpoliteLinks.length ; i++) {
			Event.observe( unpoliteLinks[i] , 'click' , this.showReact.bindAsEventListener( this , unpoliteLinks[i].up( 'div' ) , unpoliteLinks[i].up( 'li' ) ) , false );
		}
	},
	
	/*
	 * BUILD THE REACT BOX
	 */
	buildReact: function( ref , root , reportUrl ) {
		
		this.counter++;
		
		var upWrap = new Element( 'div' , { 'id': 'reactForm' + this.counter , 'class': 'art_unpolite clearfix' , 'style':'display:none;' } );
		
		var upTitle = $(document.createElement('h3'));
		upTitle.appendChild(document.createTextNode(this.bo[this.lang]));
			upWrap.appendChild(upTitle);
		
		var upTxt = $(document.createElement('p'));
		upTxt.appendChild(document.createTextNode(this.p1[this.lang]));
			upWrap.appendChild(upTxt);
		
		var upForm = new Element ( 'form' , { 'name': 'comment_f' , 'method': 'POST' , 'action': reportUrl , 'class': 'chck_f style_f clearfix' } );
		
		var upNode = new Element ( 'input' , { 'type': 'hidden' , 'value': this.nodeId , 'name': 'notification.nodeId' } );
			upForm.appendChild(upNode);
		
		var upCheck = new Element ( 'input' , { 'type': 'hidden' , 'name': 'notification.submit' } );
			upForm.appendChild(upCheck);


		var upCheck2 = new Element ( 'input' , { 'type':'hidden' , 'name': 'notification' , 'value': 'true' } );
			upForm.appendChild(upCheck2);
			
		var upNameDiv = new Element ( 'div' );
		var upNameErr = new Element ( 'h4' , { 'class': 'alert' , 'style': 'display:none;' } );
		upNameErr.appendChild(document.createTextNode(this.ne[this.lang]));
			upNameDiv.appendChild(upNameErr);
		
		var upNameLabel = new Element ( 'label' , { 'for': 'comment_n' } );
		upNameLabel.appendChild(document.createTextNode(this.n[this.lang]));
			upNameDiv.appendChild(upNameLabel);
		
		var upName = new Element ( 'input' , { 'type': 'text' , 'class': 'var_f' , 'id': 'comment_n' , 'name': 'notification.name' } );
		addInputChange(upName);
			upNameDiv.appendChild(upName);
			
		upForm.appendChild(upNameDiv);
		
		var upEmailDiv = new Element ( 'div' );

		var upEmailErr = new Element ( 'h4' , { 'class': 'alert' , 'style': 'display:none;' } );
		upEmailErr.appendChild( document.createTextNode( this.ee[this.lang] ) );
			upEmailDiv.appendChild( upEmailErr );
		
		var upEmailLabel = new Element ( 'label' , { 'for': 'comment_e' } );
		upEmailLabel.appendChild(document.createTextNode(this.e[this.lang]));
			upEmailDiv.appendChild(upEmailLabel);
		

		var upEmail = new Element ( 'input' , { 'type': 'text' , 'class': 'mail_f' , 'id': 'comment_e' , 'name': 'notification.email' } );
		addInputChange(upEmail);
			upEmailDiv.appendChild(upEmail);
			
		upForm.appendChild(upEmailDiv);
		
		var upTextDiv = new Element ( 'div' );
		
		var upTextErr = new Element ( 'h4' , { 'class': 'alert' , 'style': 'display:none;'} );
		upTextErr.appendChild(document.createTextNode(this.ce[this.lang]));
			upTextDiv.appendChild(upTextErr);

		var upTextLabel = new Element ( 'label' , { 'for': 'comment_b' , 'style': 'display:none;'} );
		upTextLabel.appendChild(document.createTextNode(this.c[this.lang]));
			upTextDiv.appendChild(upTextLabel);

		var upText = new Element ( 'textarea' , { 'class': 'var_f' , 'id': 'comment_b', 'name': 'notification.body' } );
		addInputChange(upText);
			upTextDiv.appendChild(upText);
			
		upForm.appendChild(upTextDiv);
		
		
		//new code
		
		var upHash = new Element ( 'input' , { 'type': 'hidden' , 'name': 'hash', 'value' : this.hash } );
		
		upForm.appendChild(upHash);
		
		var upCaptchDiv = new Element ( 'div' , { 'class': 'clearfix' } );
		
		var upCaptchaErr = new Element ( 'h4' , { 'class': 'alert' , 'style': 'display:none;'} );
		upCaptchaErr.appendChild(document.createTextNode(this.aba[this.lang]));
			upCaptchDiv.appendChild(upCaptchaErr);		

		var upCaptchLabel = new Element ( 'label' , { 'for': 'captcha', 'class' : 'req' } );
		upCaptchLabel.appendChild(document.createTextNode(this.capt[this.lang]));
			upCaptchDiv.appendChild(upCaptchLabel);
			
		var upCaptchInput = new Element ( 'input' , { 'type': 'text' , 'name': 'captcha', 'class' : 'num_f', 'id' : 'captcha' } );
		addInputChange(upCaptchInput);
			upCaptchDiv.appendChild(upCaptchInput);			
			
		var upClDiv = new Element ( 'div' , { 'class' : 'spamprot' } );
						
		var upCaptchMsg = new Element ( 'span' );
		upCaptchMsg.appendChild(document.createTextNode(this.sum[this.lang]));
			upClDiv.appendChild(upCaptchMsg);
			
		upCaptchDiv.appendChild(upClDiv);

		upForm.appendChild(upCaptchDiv);
		
		//end new code

		var upLink = new Element ( 'a' , { 'href': this.srLink[this.lang], 'class': 'fleft bluelnk' } );
		upLink.appendChild(document.createTextNode(this.sr[this.lang]));
			upForm.appendChild(upLink);

		var upBtnA = new Element ( 'button' , { 'type': 'button' , 'class': 'blackbtn clearfix' } );
		Event.observe(upBtnA, 'click', this.hideReact.bindAsEventListener(this, upWrap), false);
		
		var upBtnASpan = new Element ( 'span' );
		upBtnASpan.innerHTML = this.a[this.lang];
			upBtnA.appendChild(upBtnASpan);
		
		var upBtn = new Element('button', { 'class': 'blackbtn clearfix'} );
		upBtn.observe('click', function(event) {
			var fe = Form.getElements(upForm);
			var ab = true;
			fe.each(function(fec) {
				fev = fec.value.strip();
				if (!(fieldChck(fec, fev))) {ab = false; alertIt('error', fec);}					
			});
			if (!(ab)) {
				Event.stop(event);
			}
			else {
				upForm.submit();
			}
		});
		
		var upBtnSpan = new Element ( 'span' );
		upBtnSpan.innerHTML = this.v[this.lang];
		upBtn.appendChild(upBtnSpan);
			
		
		upForm.appendChild(upBtn);
		upForm.appendChild(upBtnA);
		
		upWrap.appendChild(upForm);

		root.appendChild(upWrap);
		
		
		return(upWrap);
	},
	

	/*
	 * SHOW THE REACTION BOX
	 */
	showReact: function(e, i, j) {
		Event.stop(e);
		var theForm = $A(arguments)[2].down('.art_unpolite');
		if(theForm == undefined) {
			var reportUrl = $A(arguments)[2].getElementsByClassName('art_report')[0].readAttribute('href');
			theForm = this.buildReact($A(arguments)[1], $A(arguments)[2], reportUrl);
		}
		
		if (openItem) {
			Effect.BlindUp($(openItem));
		}		
		
		if(!theForm.visible()) {
			$(theForm).setStyle({
				height: ''
			});				
			Effect.BlindDown(theForm);
			openItem = $(theForm).id;
		} else {
			Effect.BlindUp(theForm);
			openItem = null;
		}
	},
	
	/*
	 * HIDE THE REACTION BOX
	 */
	hideReact: function(e, i) {
		Event.stop(e);
		Effect.BlindUp($A(arguments)[1]);
		openItem = null;		
	},
	
	/*
	 * REMOVE ALL ELEMENTS FROM WRAPPER
	 */
	removeAllChildren: function (element) {
		element = $(element);
		if ( element.hasChildNodes()) {
		    while ( element.childNodes.length >= 1 ) {
		        element.removeChild( element.firstChild );       
			} 
		}
	}
	
});