function komentaras()
{

	var obj = $('komentaru-forma');

	if( obj )
	{
		Event.observe( obj, 'submit', function(e)
		{
			Event.stop(e);

			obj.request({
				onComplete: function(r)
				{
					if(r.responseText == 'ok')
					{
						history.go(0);
					}
					else
					{
						alert(r.responseText);
					}
				}
			});
		});

	}
}

Event.observe( window, 'load', komentaras );
Event.observe( window, 'load', function(){
	if( $('komentaru-forma-user') )
	{
		$('komentaru-forma-user').enable();
	}
});

function komentarai_user(obj, user_id)
{
	$(obj).request({
		onCreate:
			function()
			{
				$(obj).disable();
			},
		onComplete:
			function(r)
			{
				$(obj).enable();

				var t = r.responseText;
				if( t == 'ok' )
				{
					komentarai_user_view( user_id );
					alert( 'Jūsų komentaras buvo įrašytas.' );
					location.hash = '';
					location.hash = 'comm_view_hash';
					$(obj).reset();
					$('comm_field').focus();
				}
				else
				{
					alert(t);
					$('comm_field').focus();
				}
			}
	});
}

function komentarai_user_view( user_id )
{
	new Ajax.Request( 'index.php?get_user_comm=1&user_id='+user_id, {
		onCreate: function()
		{
			$('comm_view').innerHTML = '<div class="comm_wait">Palaukite...</div>';
		},
		
		onComplete: function(r)
		{
			$('comm_view').innerHTML = '';
			var t = r.responseText;
			
			if( !t.isJSON() )
			{
//				alert(t);
				$('comm_view').innerHTML = '<div class="comm_error">Nesuprantamas serverio atsakymas. <a href="javascript:void(0);" onclick="komentarai_user_view('+user_id+');return false;">Bandyti dar kartą.</a></div>';
			}
			else
			{
				t = t.evalJSON();
				if( t.length > 0 )
				{
					t.each(function(v){

						// the template (our formatting expression)
						var myTemplate = new Template($('comm_tpl').innerHTML);

						// our data to be formatted by the template
						var show = v;

						// let's format our data
						$('comm_view').innerHTML = $('comm_view').innerHTML + myTemplate.evaluate(show);
					});
				}
				else
				{
					$('comm_view').innerHTML = '<div class="comm_empty">Komentarų nėra.</div>';
				}

			}
		}
	});
}

function komentarai_user_del( comm_id, user_id )
{
	if( confirm("Ar tikrai norite trinti komentarą ID: "+comm_id+"?") )
	{
		new Ajax.Request( 'index.php?del_user_comm=1&id='+comm_id, {
			onComplete: function( r )
			{
				var t = r.responseText;

				if( t == 'ok' )
				{
					komentarai_user_view( user_id );
					alert('Komentaras ištrintas.');
					location.hash = '';
					location.hash = 'comm_view_hash';
				}
				else
				{
					alert(t);
				}
			}
		});
	}
}


function kom_karma( id, value )
{
	new Ajax.Request( 'index.php', {
		method: 'post',
		parameters: 'kom_karma=1&id='+id+'&value='+value,

		onComplete: function(r)
		{
			var json = r.responseText;

			if( !json.empty() )
			{
				if( json.isJSON() )
				{
					json = json.evalJSON();

					if( json.status != 'ok' )
					{
						alert( json );
					}
					else
					{
						

						try
						{
							

						var obj_y = document.getElementById('karma_vote_'+id+'_y');
						var obj_n = document.getElementById('karma_vote_'+id+'_n');

						if( json.opacity == 'both_50' )
						{
							if( obj_y )
							{
								obj_y.className = 'opacity_50';
							}

							obj_n.className = 'opacity_50';
						}
						else if( json.opacity == 'y_50' )
						{
							if( obj_y )
							{
								obj_y.className = '';
							}

							obj_n.className = 'opacity_50';
						}
						else if( json.opacity == 'n_50' )
						{
							if( obj_y )
							{
								obj_y.className = 'opacity_50';
							}

							obj_n.className = '';
						}

						// span update
						if( json.span )
						{
							var span_dom = document.getElementById( 'karma_value_'+id );
							span_dom.innerHTML = json.span;
						}
							
						}
						catch (e)
						{
							alert(e);
						}

					
					}
				}
				else
				{
					alert( 'json error: '+json );
				}
			}
		}
	});
}
