$(function() {
	
	/* first page media box */
	$('#mediabox').innerfade({
		animationtype: 'fade',
		speed: 'slow',
		timeout: '5000',
		type: 'sequence',
		containerheight: '350px'
	});
	
	/* submenu uzitki */
	$('#open-submenu-uzitki').click(function() {
		$('#submenu-uzitki').show();
		return false;
	});
	
	$('#submenu-uzitki a').click(function(event) {
		event.stopPropagation();
	});
	
	$(document).click(function() {
		$('#submenu-uzitki').hide();
	});
	
	/* popover forms */
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed:'slow',
		theme:'dark_square',
		slideshow:false,
		show_title: true, 
		autoplay_slideshow: false,
		overlay_gallery: false,
		default_width: 310,
		default_height: 364,
		keyboard_shortcuts: false,
		opacity: 0.40
	});
	
	/* ajax forms */
	
	
	$('.popover-form form').ajaxForm();
	
	$('.popover-form form').live('submit', function() {
		var error = false;
		$('.required', this).each(function() {
			var item_val = $.trim($(this).val());
			if (item_val == '' && !$(this).hasClass('C') ||
				($(this).hasClass('mail') && item_val.search('@') == -1) ||
				($(this).hasClass('C') && $(this).is(':checkbox') && !$(this).is(':checked'))
				) {

				error = true;
				if ($(this).hasClass('C')) {
					$(this).next('label').addClass('error');
				} else {
					$(this).prev('label').addClass('error');
				}
			} else {
				if ($(this).hasClass('C')) {
					$(this).next('label').removeClass('error');
				} else {
					$(this).prev('label').removeClass('error');
				}
			}
		});
		if (!error) {
			var content = $(this).parent();
			var action_url = $(this).attr('action');
			$('p.actions', $(this)).hide();
			/*
			$.post(action_url, $(this).serialize(), function(data) {
				content.replaceWith(data);
			});
			*/
			$(this).ajaxSubmit({
				complete: function(data) {
					content.replaceWith(data.responseText);
				}
			});
			
		}
		return false;
	});
	
	/* gallery */
	$('.gallery').popover_gallery ({
		data_source: window.location.href + '?debug=1',
		thumbnail_size: 'th_',
		sort: 'Sort DESC'
	});
	
});
