function updateBanners() {
	// sidebar-right-banner
	$.get(
		$('div#sidebar-right-banner').attr('url'),
		function(data) {
			$('div#sidebar-right-banner').replaceWith(data);
		}
	);
	// sidebar-left-banner
	$.get(
		$('div#sidebar-left-banner').attr('url'),
		function(data) {
			$('div#sidebar-left-banner').replaceWith(data);
		}
	);
}

function showThread(url) {
	
	Boxy.load(url, {
		'title': " &nbsp; ",
		'closeText': "X",
		'unloadOnHide': true,
		'afterShow': function() {
			//JT_init();
			scrollDownMessagePopup();
			// focus
			$('textarea#message_content').trigger('focus');
			// elastic
			$('textarea.elastic').elastic();
			// auto scroll
			$('textarea#message_content').bind('keyup change cut paste', function() {
				scrollDownMessagePopup();
			});
			
			// aktualizuje user bar
			updateUserBar();
			// aktualizuje inbox
			updateInbox();
		}
	});
}

function updateInbox() {
	
	$.get(
		$('div#messages').attr('url'),
		function(data) {
			$('td#content').html("");
			$('td#content').html(data);
		}
	);
}

function updateParty() {
	
	$.get(
		$('div#party').attr('url'),
		function(data) {
			$('td#content').html("");
			$('td#content').html(data);
		}
	);
}

function updateUserBar() {
	
	$.get(
		$('div#user-bar').attr('url'),
		function(data) {
			$('div#user-bar').replaceWith(data);
		}
	);
}

function updateParty() {
	
	$.get(
		$('div#party').attr('url'),
		function(data) {
			$('td#content').html("");
			$('td#content').html(data);
		}
	);
}

$().ready(function(){
	
	$('#fancybox-wrap').live('contextmenu', function() {
	    //alert('Kopiiowanie jest zabronione'); //do stuff
	    return false;
	});
	
	$('a.ajax-link').live('click', function() {
		
		$link = $(this);
		$.get(
			$link.attr('href'),
			function(data) {
				$('td#content').html("");
				$('td#content').html(data);
				// update banners content
				updateBanners();
			}
		);
		
		return false;
	});
	
	scrollDownMessagePopup = function() {
		$('div.message-popup').scrollTop($('div.message-popup').prop('scrollHeight') - $('div.message-popup').height());
	}
	$('td.view-thread, a.view-thread').live('click', function() {
		
		$(this).parents('tr:first').removeClass('new-message');
		
		showThread($(this).attr('href'));

		return false;
	});
	
	/*
	$('td.view-thread a.jTip').unbind('click').bind('click', function(event){
		event.stopPropagation();
	});
	*/
	
	$('a.who-rated, a.who-liked, a.remove-user').live('click', function() {

		Boxy.load($(this).attr('href'), {
			'title': " &nbsp; ",
			'closeText': "X",
			'unloadOnHide': true,
			'afterShow': function() {
				//JT_init();
			}
		});

		return false;
	});
	
	$('div.message-popup a.send').live('click', function() {

		var $form = $(this).parents('form:first');
		
		$.post(
			$form.attr('action'),
			$form.serialize(),
			function (response) {
				$('table#message-list').replaceWith(response);
				scrollDownMessagePopup();
				// focus
				$('textarea#message_content').trigger('focus');
				// elastic
				$('textarea.elastic').elastic();
				// auto scroll
				$('textarea#message_content').bind('keyup change cut paste', function() {
					scrollDownMessagePopup();
				});
				// aktualizuje inbox
				updateInbox();
			}
		);
		
		return false;
	});
	
	$('div.party-popup a.send').live('click', function() {

		var $form = $(this).parents('form:first');
		
		$.ajax({
			url: $form.attr('action'),
			data: $form.serialize(),
			dataType: 'json',
			type: 'POST',
			success: function (response) {
				
				if (response.success) {
					$form.find('a.close').trigger('click');
					
					// aktualizuje widok imprezy
					updateParty();
				}
			}
		});
		
		return false;
	});
	
	$('a.set-status').bind('click', function() {

		Boxy.load($(this).attr('href'), {
			'title': " &nbsp; ",
			'closeText': "X",
			'afterShow': function() {
				//
			}
		});

		return false;
	});
	
	$('div.status-popup input[type="submit"]').live('click', function() {

		$form = $(this).parents('form:first');
		
		$.post(
			$form.attr('action'),
			$form.serialize(),
			function(data) {
				data = $.trim(data);
				if (data == "1") {
					$('span#status').html($form.find('input[name="comet[status]"]').val());
					$('div.title-bar').find('a.close').trigger('click');
				} else {
					$('div.status-popup')
						.replaceWith(data);
					$('div.status-popup')
						.addClass('boxy-content');
					
				}
			}
		);

		return false;
	});
	
	// elastic
	$('textarea.elastic').elastic();
	$('textarea.elastic').trigger('update');
	
});

