jQuery(function() {
	var $ = jQuery;
	
	settings = {
          tr: { radius: 5 },
          tl: false,
          br: false,
          bl: false,
          antiAlias: true,
          autoPad: true
    };
	
	$('.block h3').wrap('<div class="rounded"></div>');
	$('.block .rounded').each(function() {
		(new curvyCorners(settings, this)).applyCornersToAll();
	});
	
	if($('#webcams').size()) {
		setInterval(function() {
			var image = $('#lightbox-image');
			if (image.size()) {
				var src = image.attr('src');
				if (src) {
                    if (src.indexOf('&dummy=') == -1) {
                        src += '&dummy=';
                    }
					src = src.replace(/&dummy=.*/, '&dummy=' + ((new Date()).getTime()/1000));
					image.removeAttr('src');
					image.attr('src', src);
				}
			}
		}, 5000);	
	}

	$('#files .tag .files').hide().parent().find('h2').css({ cursor: 'pointer' }).click(function() {
	    $('#files .files').hide('fast');
	    var files = $('.files', $(this).parent());
	    if (files.is(':visible')) {
	        files.hide('fast');
	    } else {
	        files.show('fast');
	    }
	})


    $('#menu a').append('<div class="l"></div>').append('<div class="r"></div>').hover(
    	function() {
    		$('.l, .r', this).show();
    	},
    	function() {
    		$('.l, .r', this).hide();
    	}
    );
    
    var la = $('.gallery a');
    if (la.size()) {
	    la.lightBox({
			imageLoading: '/images/indicator_big.gif',
			imageBtnClose: '/images/cross.png',
			imageBtnPrev: '/images/arrow_left.png',
			imageBtnNext: '/images/arrow_right.png',
			txtImage: 'Obrázek',
			txtOf: 'z'
		});
	}

	// h1 page position
	setTimeout(function() {
        var image = $('.page .header h1').prev('.image_header');
    	image.next('h1').css({ position: 'absolute', top: (image.height() - 26) + 'px' });
    }, 500);
    
    $('#search input').css('opacity', 0.6)
    	.blur(function() {
			$(this).css('opacity', 0.6);
			if ($(this).val() == '') {
				$(this).val('Hledat...');
			}
    		
   		})
   		.focus(function() {
    		if ($(this).val() == 'Hledat...') {
				$(this).val('');
			}
    		$(this).css('opacity', 1);
   		})
   		.focus().blur();
   		
   	$('#search-results #filter').change(function() {
   		$(this).parents('form').submit();
   	});
   	
   	$('#phone-results .result').each(function(i) {
	    $(this).addClass((i%2)?'odd':'even');
	});
	
	$('#phone .type label img').css({ cursor: 'pointer' }).click(function() {
		$(this).parents('.type input').click();
	});
	
	$('#phone .type input').click(function() {
		$('#phone form').submit();
	});
	
	if($('#application').size()) {
		
		var recalculate_full_price = function() {
			var price = $('#application .person').size() * $('#application #price').val();
			$('#application #price_full').val(price);
		}
		
		var add = $('<img src="/images/user_add.png" alt="+"/>');
		var remove = $('<img src="/images/user_delete.png" alt="-"/>');
		
		add.click(function() {
			var p = $('#application #persons').clone();
			p.val('');
			$('#application .person:last').after(p);
			recalculate_full_price();
		});
		
		remove.click(function() {
			if ($('#application .person').size() > 1) {
				$('#application .person:last').remove();
				recalculate_full_price();
			}
		});
		
		// required fields
		var check = function() {
			$('#application label').removeClass('required');
			var labels = [];
			if($('#corporation:checked').size()) {
				labels = ['persons', 'phone', 'email'];
			}
			if($('#corporation_1:checked').size()) {
				labels = ['persons', 'phone', 'email', 'ic', 'dic', 'contact_person'];
			}
			
			$(labels).each(function() {
				$('#application label[for='+this+']').addClass('required');
			});
		}
		
		$('#corporation_1, #corporation, label[for=corporation], label[for=corporation_1]').click(function() { setTimeout(check, 100) });
		check();
		
		$('#application #persons').before(add);
		$('#application #persons').before(remove);
		
	}
});