$C = function(first){
	$C.get(first);
	$C.loop = self.setInterval("$C.get()",30000);
};
$C.results = {};
$C.setMonkey = function(){
	$('#havelogged').css({'display':'block'});
	$('#haventlogged').css({'display':'none'});
	$('#hlu').html('<label>Posting As:</label> ' + window.displayName);
};
$C.pause = function(){
	if($C.pause.toggle){
		$('.pause_button').text('Pause');
		$C(false);
		$C.pause.toggle = false;
	}else{
		$('.pause_button').text('Un-Pause');
		clearInterval($C.loop);
		$C.pause.toggle = true;
	}
};
$C.pause.toggle = false;
$C.get = function(first){
	$C.loading();
	var so = {
		"itemId": window.itemid,
		"projectId": 1,
		"folder": 'wgte'
	};
	$.ajax({
		url: '/modules/livecomments/scripts/showprocess.php',
		type: 'post',
		data: so,
		success: function(data){
			$C.results = data;
			$C.display();
			$C.loading('');
		},
		error: function(){
			$C.loading('');
		},
		dataType: 'json'
	});
};
$C.loading = function(){
	if(typeof arguments[0] == 'undefined'){
		$('#loading_box').html('');
		var img = $('<img/>');
			$(img).attr({
				'border': '0',
				'alt': 'Loading...',
				'src': '/wgte/images/search/ajax-loader.gif'
			});
		$('#loading_box').append(img);
			$(img).attr('width', '16');
	}else{
		$('#loading_box').html(arguments[0]);
	}
}
$C.save = function(anon){
	$C.loading();
	var so = {
		"userId": (typeof window.userid != 'undefined')?window.userid:'0',
		"body": anon?$('#comment_content_anon').val():$('#comment_content').val(),
		"itemId": window.itemid,
		"name": $('#comment_displayname').val()
		};

	$.ajax({
		url: '/modules/livecomments/scripts/saveprocess.php',
		type: 'post',
		data: so,
		success: function(data){
			$C.results = data;
			$C.display();
			$('#comment_content').val('');
			$('#comment_content_anon').val('');
			$C.loading('');
		},
		error: function(){
			$C.loading('<span style="color:red">Comments could not load properly.</span>');
		},
		dataType: 'json'
		});
	};
	$C.del = function(c){
		$C.loading();
		var so = {
			"itemId": window.itemid,
			"commentId": c
			};

		$.ajax({
			url: '/modules/livecomments/scripts/deleteprocess.php',
			type: 'post',
			data: so,
			success: function(data){
				$C.results = data;
				$C.display();
				$('#comment_content').val('');
				$('#comment_content_anon').val('');
				$C.loading('');
			},
			error: function(){
				$C.loading('<span style="color:red">Comments could not load properly.</span>');
			},
			datatype: 'json'
		});
	};
	$C.display = function(){
		var z = $('#comments_display');
		z.html('');
		if ($C.results && $C.results != null && $C.results.length != 0 && $C.results != 'null') {
			for (var n = 0; n < $C.results.length; n++) {
				var a = $('<div/>');
				z.append(a);
				a.addClass('live_comment_container');
				
				//Delete Button
				if (typeof window.adminlogged != 'undefined' && window.adminlogged) {
					var b = $('<img/>');
					b.attr({
						'src': '/modules/review/closedelete.gif',
						'title': 'Remove Comment',
						'alt': 'Remove Comment',
						'onclick': '$C.del(\'' + $C.results[n].id + '\');'
					});
					b.addClass('comment_delete');
					a.append(b);
					b.hover(function(){
						$(this).parent().addClass('statusInactive');
					},function(){
						$(this).parent().removeClass('statusInactive');
					})
				}
				
				
				
				var d = $('<div/>');
				a.append(d);
				d.addClass('live_info');
				
				//Display Name
				var b = $('<div/>');
				d.append(b);
				b.addClass('live_user');
				
				if ($C.results[n].userId != 0) {
					var c = $('<a/>');
					c.attr('href', 'http://www.wgte.org/wgte/profile.asp?userid=' + $C.results[n].userId);
				}
				else {
					var c = $('<span/>');
				}
				b.append(c);
				c.html($C.results[n].displayName);
				
				//Comment Time
				var b = $('<div/>');
				d.append(b);
				b.addClass('live_time');
				b.html($C.results[n].date);
				
				//Body
				var b = $('<div/>');
				d.append(b);
				b.addClass('live_body');
				b.html($C.results[n].body);
				
				//Image
				var b = $('<div/>');
				a.append(b);
				b.addClass('live_photo');
				
				if ($C.results[n].userImage != null && $C.results[n].userImage != '') {
					var c = $('<img/>');
					c.attr({
						'src': $C.results[n].userImage,
						'alt': $C.results[n].displayName,
						'title': $C.results[n].displayName
					});
					b.append(c);
				}
				
				var b = $('<div/>');
				a.append(b);
				b.addClass('clear');
			}
		}
	};
