SSPaging = {};
SSPaging.click = function (el) {
	if($('.commentsPanel').is('.loading')) { return; }
	var toLoad = $(el).attr('href');
	var newContentLoaded = function() { $('.commentsPanel').removeClass('loading'); SSPaging.init(); }
	var showNewContent = function() { $('.commentsPanel .indexWrapper').animate({height:'show',opacity:'show'},'normal',newContentLoaded); }
	var loadContent = function() { $('.commentsPanel .indexWrapper').load(toLoad,'',showNewContent); }
	$('.commentsPanel').addClass('loading');
	$('.commentsPanel .indexWrapper').animate({height:'hide',opacity:'hide'},'normal', loadContent);
}
SSPaging.init = function () {
	$(".commentsPanel .paging a").click(function() { SSPaging.click($(this)); return false; });
}

$(document).ready(function() {
	$('.pages-home #SportSearch').blur(function(el) { if ($('#SportSearch').val() == '') $('#SportSearch').val('podaj sport'); });
	$('.pages-home #SportSearch').focus(function(el) { if ($('#SportSearch').val() == 'podaj sport') $('#SportSearch').val(''); });
	if ($('.pages-home #SportSearch').val() == '') $('#SportSearch').val('podaj sport');
	$('#SportSearchForm').submit(function() { if ($('.pages-home #SportSearch').val() == 'podaj sport') $('#SportSearch').val('') });

	$('.pages-home #SpotSearch').blur(function(el) { if ($('#SpotSearch').val() == '') $('#SpotSearch').val('podaj sport, miasto, dane spotu'); });
	$('.pages-home #SpotSearch').focus(function(el) { if ($('#SpotSearch').val() == 'podaj sport, miasto, dane spotu') $('#SpotSearch').val(''); });
	if ($('.pages-home #SpotSearch').val() == '') $('#SpotSearch').val('podaj sport, miasto, dane spotu');
	$('#SpotSearchForm').submit(function() { if ($('.pages-home #SpotSearch').val() == 'podaj sport, miasto, dane spotu') $('#SpotSearch').val('') });

	/* Selectors */
	$('th.selector input').each(function(){
		if ($(this).is(':checked')) {
			$(this).parents('table').eq(0).find('td.selector input').attr('checked','checked');
		} else {
			$(this).parents('table').eq(0).find('td.selector input').attr('checked','');
		}
	});

	$('th.selector input').click(function(){
		if ($(this).is(':checked')) {
			$(this).parents('table').eq(0).find('td.selector input').attr('checked','checked');
		} else {
			$(this).parents('table').eq(0).find('td.selector input').attr('checked','');
		}
	});

	/* fancybox */
	$("a[class=fancybox]").fancybox({
		'zoomOpacity'			: true,
		'overlayShow'			: false,
		'zoomSpeedIn'			: 500,
		'zoomSpeedOut'			: 500
	});

	/* comments AJAX paging */
	SSPaging.init();

	$('input.locality').Autocomplete({
		source: '/spots/autocompleteCity.xml',
		delay: 500,
		autofill: false,
		helperClass: 'autocompleter',
		selectClass: 'selectAutocompleter',
		minchars: 2
	});
});