// Widok aktualnie dodanych postów
function getView(viewFile)
{
	$('#panel-content').load(viewFile);
}

// Wy¶wietlanie okienka
function hideInt()
{
	$('#user_main').hide();
}

// Ukrywanie okienka
function showInt(info)
{
	if (info)
	{
		$('#user_int').html('<p class="center">' + info + '</p>');
	}
	
	$('#user_main').show();
}

$(function()
{
	getView(viewFile);
	setInterval('getView(\''+viewFile+'\')', refresh);

	// Dodawanie wpisu
	$('#shout_post').click(function() 
	{

		var shout_text = $('#shout_text').val();
		var shout_id = $('#shout_id').val();

		var dataString = 'shout_text='+ shout_text + '&shout_id=' + shout_id;
		
		$.ajax({
			type: 'POST',
			url: addShout,
			data: dataString,
			success: function(response)
			{
				if (response) 
				{
					showInt(response);
				}
			}
		});
		
		$('#shout_text').val('');
		$('#shout_info').html("<b>0</b>/700");
		
		getView(viewFile);
		
		return false;
	});
	
	// Zarz±dzanie wpisem
	$('.shout_admin').live('click', function()
	{
		var id = this.id;
		
		showInt('<p class="center"><a href="javascript:void(0)" class="edit">Eytuj</a> | <a href="javascript:void(0)" class="delete">Usuń</a></p>');
		
		// Edycja wpisu
		$('.edit').click(function()
		{
			$.ajax({
				type: 'POST',
				url: editShout,
				data: 'id=' + id + '&action=getShout',
				success: function(response) {
				
					showInt('<textarea name="shout_save" id="shout_save">'+response +'</textarea><p id="save" class="pointer">Zapisz</p>');
					
					$('#save').click(function()
					{
						var data = $('#shout_save').val();
						$.ajax({
							type: 'POST',
							url: editShout,
							data: 'id=' + id + '&action=updateShout&message='+data,
							success: function(response) {
							
								if (response == '1')
								{
									hideInt();									
									getView(viewFile);
								}
								else
								{
									showInt('Wystąpił błąd przy edycji wpisu');
								}
							}
						});
					});
				}
			});
		});
		
		
		
		// Usuwanie wpisu
		$('.delete').click(function()
		{		
			$.ajax({
				type: 'POST',
				url: delShout,
				data: 'id=' + id,
				success: function(response) {
					if (response == '1')
					{
						getView(viewFile);
						hideInt();					}
					else
					{
						showInt('Wystąpił błąd przy usuwanie wpisu');
					}
				}
			});
		});
	});
	
	// Edycja wpisu
		$('.shout_edit').live('click', function()
		{
			var id = this.id;
			
			$.ajax({
				type: 'POST',
				url: editShout,
				data: 'id=' + id + '&action=getShout',
				success: function(response) {
				
					showInt('<textarea name="shout_save" id="shout_save">'+response +'</textarea><p id="save" class="pointer">Zapisz</p>');
					
					$('#save').click(function()
					{
						var data = $('#shout_save').val();
						$.ajax({
							type: 'POST',
							url: editShout,
							data: 'id=' + id + '&action=updateShout&message='+data,
							success: function(response) {
							
								if (response == '1')
								{
									hideInt();
									getView(viewFile);
								}
								else
								{
									showInt('Wystąpił błąd przy edycji wpisu');
								}
							}
						});
					});
				}
			});
		});
	
	// Informacje o IP
	$('.shout_info').live('click', function()
	{
		var ip = $(this).attr('title');
		showInt('IP: '+ ip);
	});
	
	// Zamykanie okienka powiadomień
	$('#shout_discard').click(function()
	{
		hideInt();	});
	$('#shout_text').keyup(function()
	{
		var Len = $('#shout_text').attr('value').length;
		$('#shout_info').html("<b>"+Len+"</b>/700");
	});
	$('#shout_text').click(function()
	{
		var Len = $('#shout_text').attr('value').length;
		$('#shout_info').html("<b>"+Len+"</b>/700");
	});
	$('#shout_open_form').toggle(function() {
		$('#shoutbox_form').animate({opacity:0}).slideDown(700).animate({opacity:1});
	}, function() {
		$('#shoutbox_form').animate({opacity:0}).slideUp(700);
	});
});

