// JavaScript Document
var wallIsOpen = null;
var isAjax = false;

var wall = function() {
	this.url = 'member/ajax/ajax_add_wall.cfm';
	this.id = 0;
}

wall.prototype.openWall = function(obj, id) {
	if (wallIsOpen == null)
		wallIsOpen = true;
	else if (wallIsOpen)
		wallIsOpen = false;
	else
		wallIsOpen = true;
		
	if (!wallIsOpen) {
		get('walllink').innerHTML = 'Post a Message';
		$('#wall').hide('slow');
		
		this.id = 0;
		
		return false;
	}
		
	this.id = id;
	
	get('walllink').innerHTML = 'Cancel Post';
	get('wall').innerHTML = get('commentBox').innerHTML;
	$('#wall').show('slow');
	
	return false;
}

wall.prototype.setWallComment = function(obj, e) {
	if (isAjax) { return; }
	
	if (!e)
		var e = window.event;
	
	var x = posMouseX(e) - 135;
	var y = posMouseY(e) - 150;

	if (get('comment').value.length == 0) {
		jWin.setTitle('Error');
		jWin.setSize(250,100); 
		jWin.setContentInnerHTML('<div style="text-align:center">You cannot leave the comment box empty.<br /><br /><input class="formBtnSmall" type="button" value="Close" onclick="jWin.hide()" /></div>');
		jWin.showClick(x,y);
		fade('jWin');
		return;
	}
	
	if (get('comment').value.length > 250) {
		jWin.setTitle('Error');
		jWin.setSize(250,100); 
		jWin.setContentInnerHTML('<div style="text-align:center">Your comment is too long.<br />Please reduce it to 250 characters or less.<br /><br /><input class="formBtnSmall" type="button" value="Close" onclick="jWin.hide()" /></div>');
		jWin.showClick(x,y);
		fade('jWin');
		return;
	}
	
	if (get('private').checked)
		var p = 1;
	else
		var p = 0;
	
	isAjax = true;
	
	$.ajax({
		type:'POST',
		url:this.url,
		data:'id='+this.id+'&text='+encodeURIComponent(get('comment').value)+'&p='+p,
		dataType:'json',
		success: function(msg) {
			if(msg.SUCCESS) {
				y = y - 50;
				jWin.setTitle('Success');
				jWin.setSize(250,100); 
				jWin.setContentInnerHTML(msg.MSG);
				jWin.showClick(x,y);
				fade('jWin');
				var div = document.createElement('div');
				div.innerHTML = msg.HTML;
				div.className = 'wallComment';
				div.id = 'wall'+msg.UID;
				div.style.display = 'none';
				
				if (get('nowall'))
					get('nowall').style.display = 'none';
				
				get('commentlist').insertBefore(div, get('commentlist').firstChild);
				$(div).show('slow');
				get('walllink').innerHTML = 'Post a Message';
				$('#wall').hide('slow');
				wallIsOpen = false;
				isAjax = false;
			}
			else {
				jWin.setTitle('Error');
				jWin.setSize(250,100); 
				jWin.setContentInnerHTML(msg.MSG);
				jWin.showClick(x,y);
				fade('jWin');
				isAjax = false;
			}
		}
	});
}

wall.prototype.deleteWall = function(obj, e, id, receiver_id) {
	if (!e)
		var e = window.event;
	
	var x = posMouseX(e) - 135;
	var y = posMouseY(e) - 150;
	
	jWin.setTitle('Delete Message');
	jWin.setSize(250,100); 
	jWin.setContentInnerHTML('<div style="text-align:center">Are you sure you want to delete this message?<br /><br /><input class="formBtnSmall" type="button" value="Yes" onclick="wa.confirmDelete('+x+','+y+','+id+','+receiver_id+', \''+obj+'\')" />&nbsp;<input class="formBtnSmall" type="button" value="No" onclick="jWin.hide()" /></div>');
	jWin.showClick(x,y);
	fade('jWin');
	return false;
}

wall.prototype.confirmDelete = function(x, y, id, receiver_id, obj) {
	if (isAjax) { return; }
	
	isAjax = true;
	
	$.ajax({
		type:'POST',
		url:'member/ajax/ajax_delete_wall.cfm',
		data:'receiver_id='+receiver_id +'&id='+id,
		dataType:'json',
		success: function(msg) {
			if(msg.SUCCESS) {
				var x = location.href.split('#');
				location.href = x[0] + '#wallComments';
				location.reload();
				/*
				y = y - 50;
				jWin.setTitle('Success');
				jWin.setSize(250,100); 
				jWin.setContentInnerHTML(msg.MSG);
				jWin.showClick(x,y);
				fade('jWin');
				$('#wall'+obj).hide('slow');
				get('wall'+obj).innerHTML = '';
				*/
				isAjax = false;
			}
			else {
				jWin.setTitle('Error');
				jWin.setSize(250,100); 
				jWin.setContentInnerHTML(msg.MSG);
				jWin.showClick(x,y);
				fade('jWin');
				isAjax = false;
			}
		}
	});
}

var wa = new wall();