function removeInputHint(input)
{
	input = $(input);
	if (input.hasClass('titleHint'))
	{
		input
			.removeClass('titleHint')
			.css({
				'font-style': 'normal',
				'color': '#000'
			})
			.val('');
	}
}

function addInputHint(input)
{
	input = $(input);
	if (input.val() == '')
	{
		input
			.addClass('titleHint')
			.css({
				'font-style': 'italic',
				'color': '#999'
			})
			.val(input.attr('title'));
	}
}

jQuery.fn.removeInputHints = function()
{
	$('input:text[title]', this).each(function(){
		removeInputHint(this);
	});
	return this;
}

jQuery.fn.addInputHints = function()
{
	$('input:text[title]', this).each(function(){
		addInputHint(this);
	});
	return this;
}

var loadingImage = '<img class="loading" style="border: 0px; margin: auto;" src="/images/loading.gif" alt="" />';

$(document).ready(function(){

	// Lightbox
	$('a.gallery').fancybox({
		'imageScale': true,
		'zoomOpacity': true,
		'zoomSpeedIn': 500,
		'zoomSpeedOut': 500,
		'overlayShow': true,
		'overlayOpacity': 0
	});

	// Input hint
	var titles = $('input:text[title]');
	titles.focus(function(){
		removeInputHint($(this));
	});
	titles.blur(function(){
		addInputHint($(this));
	});
	$('form:has(input:text[title])').submit(function(){
		$.each($('input:text[title]', this), function(){
			var title = $(this);
			if (title.hasClass('titleHint'))
				title.val('');
		});
		return true;
	});
	titles.blur();

	// Comment submit
	$('#comment_submit').click(function(){
		$('.comment_field').attr('name', function(){ return this.id });
		$('#comment_form').submit();
	});

	// Page list navigation
	$('.page_list.nav a').click(function(){
		var nav = $(this).parent();
		var parent = nav.parent();
		if ($(this).hasClass('back'))
			var page = Math.max($('.page', parent).text() - 1, 1);
		else
			var page = $('.page', parent).text() - 0 + 1;
		var el = nav.clone(true);
		nav.replaceWith(loadingImage);
		$.ajax({
			type: 'POST',
			url: '/req/',
			data: {
				go: 'page_list',
				action: 'show',
				parent: $('.parent', parent).text(),
				news: $('.news', parent).text(),
				thumbnails: $('.thumbnails', parent).text(),
				page: page
			},
			success: function(data){
				$('#page_list').replaceWith(data);
				$('.nav.back', el).show();
				$('.nav.forward', el).show();
				if (page == 1)
					$('.nav.back', el).hide();
				if (($('#page_list .last_page').text() - 0) == 1)
					$('.nav.forward', el).hide();
			},
			complete: function(){
				$('img.loading', parent).replaceWith(el);
			}
		});
	});

	// Event calendar switch
	$('.calendar a.day').click(function(){
		var event_panel = $('div.event_panel', $(this).closest('div.events'));
		if (event_panel.length)
		{
			$('div.day', event_panel).hide();
			$('div.day_' + $(this).text(), event_panel).show();
		}
		return false;
	});
});
