var layerToggle = new Class({
	initialize: function(trigger,layerId,layerTitle) {
		this.myTrigger = document.getElement(trigger);
		this.myLayer = $(layerId);
		this.myClose = $(layerId).getElement('.close');
		if (layerTitle) {
			this.myLayer.getElement('.title').setHTML(layerTitle);
		}
		this.myTrigger.addEvent("click", function(event) {
			this.myLayer.setStyle('display','block');
			event = new Event(event).stop();
			
		}.bind(this));
		this.myClose.addEvent("click", function(event) {
			this.myLayer.setStyle('display','none');
			event = new Event(event).stop();
			this.clearForm();
		}.bind(this));
	},
	clearForm: function() {	
		if($type($('shareContentStatus'))){
			if($type($('isLoggedIn')) && $('isLoggedIn').value == false) {
				$('shareContentStatus').setHTML('');
			}
		}
		if($type($('sendEmailForm'))){
			$('sendEmailForm').getElements('input').each(function(myInput){
				if(!myInput.hasClass('submit')){
					myInput.value = '';
				}
			});				
		}
	}
});

var shareContent = new Class ({ 
	initialize: function(formId,targetId,assetId,userId,userEmail) {
		this.myTarget = $(targetId);
		this.assetId = assetId;
		this.userId = userId;
		this.userEmail = userEmail;
		this.myForm = $(formId);
		this.feedConduit = "sendEmail.action";
		this.myInputField = $('emailRecipient'); 
		this.myForm.addEvent("submit", function(event) {
			this.submitEmail(this.myInputField.value);
			event = new Event(event).stop();
		}.bind(this));
	},
	submitEmail: function(emailRecipient) {	
		var myAjax = new Ajax(this.feedConduit + '?accountId=' + this.userId + '&accountEmail=' + this.userEmail  + '&toEmail=' + emailRecipient + '&assetUrl=' + this.assetId, {
			method: 'get',
			evalScripts: false,
			update: this.myTarget,
			onRequest: function(data) {
				this.myTarget.setHTML('<p class="error">Loading...</p>');
			}.bind(this),			
			onFailure: function(data) {
				this.myTarget.setHTML('<p class="error">Could not submit information.</p>');
			}.bind(this)
		}).request();
	}
});
var toggleComment = new Class ({ 
	initialize: function(containerID) {
		this.container = $(containerID);
		this.btnPostComment = this.container.getElement('.postComment');
		this.commentBlock = this.container.getElement('.commentForm');
		
		this.btnClose = this.commentBlock.getElement('.btnClose');
		this.btnSubmit = this.commentBlock.getElement('.btnSubmit');
		this.commentForm = this.commentBlock.getElement('form');
		this.commentTextArea = this.commentBlock.getElement('textarea');
		this.assetId = "http%3A//ea.com/" + $('assetId').value;
	
		this.initElms();
	},
	initElms: function() {
		this.btnPostComment.addEvent("click", function(event) {
			event = new Event(event).stop();
			this.btnPostComment.setStyle("display","none");
			this.commentBlock.setStyle("display","block");
			
			// Media Page only
			if($type($('commentBlock'))) {
				$('commentBlock').setStyle("display","none");
			}
		}.bind(this));
		
		this.btnClose.addEvent("click", function(event) {
			event = new Event(event).stop();
			if($type(this.commentTextArea)){
				this.commentTextArea.value = 'Write your comment here..';
			}
			this.btnPostComment.setStyle("display","block");
			this.commentBlock.setStyle("display","none");
			// Media Page only
			if($type($('commentBlock'))) {
				$('commentBlock').setStyle("display","block");
			}	
		}.bind(this));	
		
		if($type(this.btnSubmit)){
			this.btnSubmit.addEvent("click", function(event) {
				event = new Event(event).stop();
				this.postComment();
			}.bind(this));	
		}
		
		if($type(this.commentTextArea)){
			this.commentTextArea.addEvent("focus", function(event) {
				event = new Event(event).stop();
				this.commentTextArea.value = '';			
			}.bind(this))
		}	
	},
	postComment: function() {
		//console.log(escape(this.commentTextArea.value));
		var url = this.commentForm.action + "&assetId=" + this.assetId + "&comment=" + escape(this.commentTextArea.value);
		
		var myPostCommentAjax = new Ajax(url, {
			method: 'get',
			onRequest: function(data) {
				this.commentTextArea.disabled = "disabled";
			}.bind(this),			
			onComplete: function() {
				//console.log('Update Comments');
				this.updateComments();
				// Media Page only
				if($type($('commentBlock'))) {
					$('commentBlock').setStyle("display","block");	
				}					
			}.bind(this),
			onFailure: function() {
				this.commentBlock.setHTML('<p class="error">Error: Could not submit information. Please ensure you are using a valid persona and not a classic screen name.</p>');
			}.bind(this)
		}).request();		
		
	},
	updateComments: function() {
		var myUpdateCommentsAjax = new Ajax('comment.action?workflow=GetCommentsForAssetIdWorkflow&assetId=' + this.assetId, {
			method: 'get',
			onComplete: function(data) {
				$('content').getElement('.comments').getElement('ul').setHTML(data);
				initFlags();
				this.commentTextArea.disabled = "";
				this.commentTextArea.value = 'Write your comment here..';
				this.btnPostComment.setStyle("display","block");
				this.commentBlock.setStyle("display","none");				
			}.bind(this)
		}).request();		
		var myUpdateCommentsCountAjax = new Ajax('commentCount.action?workflow=GetCommentsForAssetIdWorkflow&assetId='  + this.assetId, {
			method: 'get',
			onComplete: function(data) {
				$('discussionsBlock').getElement('h3').setHTML(data);
			}.bind(this)
		}).request();
		
	}
});

function initFlags(){
	var commentsFlags = $('content').getElement('.comments').getElement('ul').getElements('.flag');
	commentsFlags.each(function(commentsFlag){
		commentsFlag.addEvent('click', function(e) {
			e = new Event(e).stop();
			var url = commentsFlag.href;
			new Ajax(url, {
				method: 'get',
				onComplete: function() {
					commentsFlag.setStyle('display','none');
				}
			}).request();
		});
	});	

}







