/**
 * Funkce dostane na vstup element, na ktery bylo kliknuto. Musim nalezt
 * nadrazeny prvek (td element) a nej najit potomka radiobutton a oznacit jej
 * Akci vyvola odkaz, ktery je na stejne urovni jako radio button
 */
function changeImage(clickElem, mainImg)
{
	var maxX = 220;
	var maxY = 500;

	if ($(mainImg).length) {
		$(mainImg).attr('href',$(clickElem).attr('href'));
// sestavim obrazek
		var image = new Image;
		image.onload = function()
		{
			if (image.width > maxX) {
				image.height = Math.ceil(image.height * maxX / image.width);
				image.width = maxX;
			}
			if (image.height > maxY) {
				image.width = Math.round(image.width * maxY / image.height);
				image.height = maxY;
			}
			var img = $("img",$(mainImg));
			$(img).attr('src', image.src);
			$(img).attr('width', image.width);
			$(img).attr('height', image.height);
			$(img).attr('jqimg',image.src);
		}
		image.src = $(clickElem).attr('href');

	}
}

function getID(classElem){
	var pom = $(classElem).attr('class');
	pom = pom.match(/id_[a-z\d]*[^\s]/);
	return pom;
}

$(document).ready(function()
{
	var mainImg = $(".main_img");
	if ($(mainImg).length != 0) {
		$(mainImg).click(function() {
			var image = new Image;
			image.onload = function() {
				js_fce_okno_auto($(mainImg).attr('title'), $(mainImg).attr('href'), image.width, image.height);
			}
			image.src = $(mainImg).attr('href');
			return false;
		});
	}

	$('.param_img').each(function() {
		$(this).click(function() {
			$("#" + getID($(this))).attr('checked',"checked");
			changeImage(this,mainImg);
			return false;
		});
	});

	$('.param_img2').each(function() {
		$(this).click(function() {
			changeImage(this,mainImg);
			return false;
		});
	});
	
	$('.param_img_check').each(function() {
		$(this).click(function() {
			var elem = $('a',($(this).parent()).parent());
			if ($(elem).length) {
				changeImage(elem,mainImg);
			}
		});
	});

	$('.param_img3').each(function() {
		$(this).click(function() {
			$("#" + getID($(this))).attr('checked',"checked");
		});
	});
});
