/* AJAX call for news article */
var loadPost = new Class ({
	initialize: function(containerId,targetId) {
		this.container = $(containerId);
		this.myTarget = $(targetId);
		this.articlesArray = this.container.getElements('.body ul li a.article');
		this.articleLinks();
	},
	articleLinks: function() {
		this.articlesArray.each(function(navItem, index) {
			navItem.addEvent("click", function(e) {
				var param = navItem.href.split('?')[1];
				this.getData(param);
				e.stop();
			}.bind(this));
		}.bind(this));
	},
	getData: function(param) {	
		new Request.HTML({
			url: 'blogConduit.action?' + param,
			evalScripts: true,
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
				this.myTarget.set("html",responseHTML)
				eval(responseJavaScript);
				console.log(responseJavaScript);
			}.bind(this)			
		}).send();
	}
});


var commenting = new Class ({
	initialize: function(containerId,triggerId,flag) {

		this.container 			= $(containerId);
		this.myTrigger 			= $(triggerId);
		this.wrapper 			= this.container.getElement('.wrapper');
		this.myClose 			= this.container.getElement('.close');
		this.mySubmit 			= this.container.getElement('.submit');
		this.myTextarea			= this.container.getElement('textarea');
		this.myAlert			= this.container.getElement('.alert');
		this.myForm				= this.container.getElement('form');
		this.myAccount			= this.container.getElement('#accountId');
		this.myFlag				= document.getElements(flag);
		this.returnTo 			= encodeURIComponent(document.location);
		
		if ($('gus_login_link')) {
			this.myLoginLink		= $('gus_login_link').get('href') + "&surl=" + this.returnTo;
		} else {
			this.myLoginLink		= "https://profile.integration.ea.com/login.do?selectprofile=true&registrationSource=easportsactive&locale=en_US&surl=" + this.returnTo;
		}
		
		this.loginNotice 		= 'You must <a href="' + this.myLoginLink + '">login before you can comment</a>.';
		
		this.myTextareaDefault 	= this.myTextarea.value;
		
		this.myFx1 				= new Fx.Tween(this.container, {duration:500} );
		this.myFx2 				= new Fx.Tween(this.wrapper, {duration:500} );
		this.myFx3 				= new Fx.Tween(this.myTrigger, {duration:250} );
		
		this.wrapper.setStyle('opacity','0');
		this.myTrigger.addEvent("click", function(e) {
			this.myFx3.start('opacity','0').chain(function(){
				this.myFx1.start('height','269px').chain(function(){
					this.myFx2.start('opacity','1');
					//if ((!this.myAccount.value) || (this.myAccount.value == "") || (this.myAccount.value == "undefined")) {
					//	this.sendAlert(this.loginNotice);
					//}
				}.bind(this));
			}.bind(this));
			e.stop();
		}.bind(this));
		
		this.myClose.addEvent("click", function(e) {
			this.closePane();
			e.stop();
		}.bind(this));
		
		this.myTextarea.addEvent("focus", function(e) {
			if (this.myTextarea.value == this.myTextareaDefault) this.myTextarea.value = "";
		}.bind(this));

		this.myTextarea.addEvent("blur", function(e) {
			if (this.myTextarea.value == "") this.myTextarea.value = this.myTextareaDefault;	
		}.bind(this));
		
		this.mySubmit.addEvent("click", function(e) {
			e.stop();
			this.submitComments();
		}.bind(this));
		
		
		this.myFlag.each(function(item) {
			var a = item.getElement('a');
			a.addEvent("click", function(e) {
				e.stop();
				this.flagComments(item);
			}.bind(this));			
		}.bind(this));
		
		
	},
	submitComments: function() {
		if ((!this.myAccount.value) || (this.myAccount.value == "") || (this.myAccount.value == "undefined")) {
			this.sendAlert(this.loginNotice);
		} else if ((this.myTextarea.value == this.myTextareaDefault) || (this.myTextarea.value == "")) {
			this.sendAlert("You must enter a comment before you submit.",3000);
		} else {
			this.sendAlert("Submitting comments...",3000);
			this.myForm.set('send', {
				method: 'get',
				onSuccess: function(response) { 
					this.sendAlert("Your comments have been submitted.",1000);
					this.refreshPage.delay(1000, this); //FIX ME
				}.bind(this),
				onFailure: function(response) { 
					this.sendAlert("Sorry, there has been a problem submitting your comments.",30000);
				}.bind(this)
			});
			this.myForm.send();
		}
	},
	sendAlert: function(alertText,timeout) {
		if (!timeout) timeout = 1000000; 
		var myFx1 = new Fx.Tween(this.myAlert,{duration:350});
		this.myAlert.setStyle('opacity','0');
		this.myAlert.innerHTML = alertText;
		myFx1.start('opacity','1').chain(function(){
			 (function(){
				myFx1.start('opacity','0');
			}).delay(timeout);
		});
	},
	closePane: function() {
		this.myFx2.start('opacity','0').chain(function(){
			this.myFx1.start('height','0px').chain(function(){
				this.myFx3.start('opacity','1');
			}.bind(this));
		}.bind(this));
	},
	flagComments: function(item) {
		var a = item.getElement('a');
		new Request({
			url: a.href,
			onRequest: function(){
				item.innerHTML = "Flagging comment"
			},
			onSuccess: function(){
				item.innerHTML = "Comment flagged"
			},
			onFailure: function(){
				item.innerHTML = "There has been a problem"
			}
		}).send();
	},
	refreshPage: function() {
		document.location.href = document.location.href;
	}
});

