$(document).ready(function(){
	
	$('a.photo').colorbox({
		maxWidth : '90%',
		maxHeight : '90%',
		minWidth : 630,
		minHeight : 660
	});
	
	$('a.modal').colorbox({
		width : 640,
		height : 480,
		iframe : true
	});
	
	$('a.out').click(function(){
		if ($(this).hasClass('photo') !== true) {
			window.open($(this).attr('href'));
			return false;
		}
	});
	
	$('.file').click(function(){
		$('a:first', $(this)).click();
		return false;
	});
	
	$('#add_okna').click(function(){
		$('tr.row_okna:first').clone().appendTo('table.rows_okna').find('td.number').html(
			$('table.rows_okna tr.row').length + '.'
		);
		return false;
	});
	
	$('#add_vrata').click(function(){
		$('tr.row_vrata:first').clone().appendTo('table.rows_vrata').find('td.number').html(
			$('table.rows_vrata tr.row').length + '.'
		);
		return false;
	});
	
	$('.delete_row').live('click', function(){
		if (confirm($('#JS_LABEL_DELETE_SURE').val())) {
			ROWS_NUM = $('tr.row_' + $(this).attr('rel'), $(this).parent().parent().parent()).length;
			if (ROWS_NUM < 2) return false;
			$(this).parent().parent().remove();
			rnmb = 1;
			$.each(   $('tr.row_' + $(this).attr('rel') + ' td.number')   ,function(k, v){
				v.innerHTML = rnmb + '.';
				rnmb++;
			});
		}
	});	
	
	var slideBlocked = false;
	
	slideshowSet = function(n) {
		slideBlocked = true;
		var activeSlide = $('#slideshow img.active');
		var nextSlide = $('#slideshow img[rel=' + n + ']');
		activeSlide.addClass('last-active');
		$('#SSHOW_CURRENT').val(n);
		nextSlide.css({ opacity : 0.0 }).addClass('active').animate(
			{ opacity : 1.0 }, 1000, function(){
			activeSlide.removeClass('active last-active');
			slideBlocked = false;
		});
	}
	
	if (parseInt($('#SSHOW_TOTAL').val()) > 1) {
		var slideInterval = setInterval(function(){
			var currentSlide = parseInt($('#SSHOW_CURRENT').val());
			var nextSlide = (1 + currentSlide);
			if ($('#slideshow img[rel=' + nextSlide + ']').length > 0) {
				slideshowSet(nextSlide);
			} else {
				slideshowSet(1);
			}
		}, $('#SSHOW_SECS').val());
	}
	
	$(function () { $('input').keypress(function (e) { var code = null; code = (e.keyCode ? e.keyCode : e.which); return (code == 13) ? false : true; }); });
	
});
