$(document).ready(function() {
	$('#galleri').jcarousel({
		// Configuration goes here
		vertical: false,
		scroll: 2,
		auto: 3,
		wrap: 'both',
		animation: 'slow',
		initCallback: galleri_initCallback
	});

	$('#galleri').find('a').click(function() {
		$('#main_image>a').attr('href', $(this).attr('href'));
		var data = $(this).metadata();
		if (data.medium) {
			$('#main_image').find('img').attr('src', data.medium);
		}
		$('#galleri').find('a').removeClass('active');
		$(this).addClass('active');
		return false;
	});

	$('#informace > tbody').hide();
	$('#viceinformaci').removeClass('hidden');
	$('#informace > tbody').find("th, td").removeClass('last');
	$('#informace > thead').find("th:last, td:last").addClass('last');
	$('#vice').toggle(
			function() {
				$('#informace > tbody').show();
				$('#informace > thead').find("th, td").removeClass('last');
				$('#informace > tbody').find("th:last, td:last").addClass('last');
				$(this).text('Skrýt informace');
				var data = $(this).prev('img').metadata();
				if (data.up) {
					$(this).prev('img').attr('src', data.up);
				}
				return false;
			},
			function() {
				$('#informace > tbody').hide();
				$('#informace > tbody').find("th, td").removeClass('last');
				$('#informace > thead').find("th:last, td:last").addClass('last');
				$(this).text('Zobrazit více informací');
				var data = $(this).prev('img').metadata();
				if (data.down) {
					$(this).prev('img').attr('src', data.down);
				}
				return false;
			}
	);

	RegisterForm();

	$('.back').click(function() {
		var vyskyt = document.referrer.indexOf(document.location.host)
		if (vyskyt !== -1) {
			history.go(-1);
			return false;
		}
	});
});

function RegisterForm() {
	$('#carform').validate({
		submitHandler: function(form) {
			$.blockUI({
				css: {
					border: 'none',
					padding: '15px',
					backgroundColor: '#000',
					'-webkit-border-radius': '10px',
					'-moz-border-radius': '10px',
					'filter': 'alpha(opacity = 80)',
					opacity: '0.8',
					color: '#fff'
				},
				overlayCSS: {
					backgroundColor: '#000',
					opacity: '0.0'
				},
				message: '<p class="overlay s"><img src="/vyvoj/autobazar/images/ajax-loader-overlay.gif" alt="Load" /></p><p class="overlay">Odesílá se formulář. Vyčkejte prosím...</p>'
			});
			jQuery(form).ajaxSubmit({
				success: RequestFormSucces
			});
		}
	});
	
	$('#carform').find('input, textarea').focus(function(){
		$(this).addClass('selected');
	});
	
	$('#carform').find('input, textarea').blur(function(){
		$(this).removeClass('selected');
	});
	

}

function RequestFormSucces(response) {
	$('#carFormTarget').html(response);
	RegisterForm();
	$.unblockUI({ fadeOut: 0 });
}

function galleri_initCallback(carousel) {
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.options.animation = 250;
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.options.animation = 250;
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};

