$(document).ready(function() {

// gallery
$(function() {
	$('.gallery a, .lightbox').lightbox({
		fitToScreen: true,
		strings : {
			help: ' \u2190 / P - previous image\u00a0\u00a0\u00a0\u00a0\u2192 / N - next image\u00a0\u00a0\u00a0\u00a0ESC / X - close image gallery',
			prevLinkTitle: 'předchozí obrázek',
			nextLinkTitle: 'následující obrázek',
			prevLinkText:  '&laquo; Předchozí',
			nextLinkText:  'Další &raquo;',
			closeTitle: 'zavřít',
			image: 'Obrázek ',
			of: ' z '
		}
	});
});


// forum form toggling
$('#toggle').hide();
$('#toggle-show').show().click(function() {
	$('#toggle').slideDown(200);
	$(this).hide();
	$('#toggle-hide').show().click(function() {
		$('#toggle').slideUp(200);
		$(this).hide();
		$('#toggle-show').show();
	});
});

$('.react').live('click', function() {
	var id = $(this).attr('id');
	id = id.substring(2);
	$('.react-cell').html('');
	$('.react-hide').text('Reagovat');
	$('.react-hide').attr('class', 'react');

	var html = $('#react-form').html();
	$('td#'+id).html(html);
	$('td#'+id+' #frmreactForm-forum_id').attr('value', id);

	$(this).text('Skrýt');
	$(this).attr('class','react-hide');

	$('.react-hide').live('click', function() {
		$('td#'+id).html('');
		$(this).text('Reagovat');
		$(this).attr('class', 'react');
	});
});

// edit dialog
var id;
$('.edit').live('click', function(event) {
	event.preventDefault();
	id = $(this).children('a').attr('rel');
	$('#dialog-edit').dialog('open');
});

$('#dialog-edit').dialog({
	autoOpen: false,
	title: 'Editovat příspěvek',
	modal: true,
	resizable: false,
	width: 800,
	open: function() {
		html = $(this).html();
		tmp = $(this);
		tmp.html('<p>Načítám data...</p>');
		$.getJSON('', {'do': 'editClicked', 'id': id}, function (data) {
			tmp.html(html);
			// filling the form values
			for(var name in data) {
				$('.ui-dialog :input[type!=hidden]').addClass('text ui-widget-content ui-corner-all');
				$('#dialog-edit [name=' + name + ']').val(data[name]);
			}
		});
	}
});

// history
/*
function hashQuery(hash) {
	$.get(hash);
}

var hashChecker = function() {
	lastHash = location.hash;
	return setInterval(function() {
		if(lastHash !== location.hash) {
			lastHash = location.hash;
			hashQuery(location.hash);
		}
	}, 50);
}
hashChecker();
*/

// AJAX links
$("a.ajax").live("click", function (event) {
    event.preventDefault();
	/*if (!window.location.href.match('^http://[^/#]+/#.*')) {
		window.location.replace('http://' + window.location.hostname + '#' + window.location.pathname)
	}
	window.location.hash = $(this).attr('href');*/
    $.get(this.href);
});

// AJAX forms
$("form.ajax").live("submit", function () {
    $(this).ajaxSubmit();
    return false;
});

$("form.ajax :submit").live("click", function () {
    $(this).ajaxSubmit();
    return false;
});

// AJAX-loader
$('#ajax-loader').ajaxStart(function () {
        $(this).fadeIn(100);
    })
    .ajaxStop(function () {
        $(this).fadeOut(100);
    });

});
