// galerie
function GalleyBox(element, numx) {
	this.ident = '#gallery-' + numx;
	this.txtPrev= 'Previous';
	this.txtNext= 'Next';
	this.txtClose= 'Close';
	this.imgShow= 0;
	this.imgCount= 0;
	this.srcs= Array();
	this.titles= Array();

	var box = this;
	var images = $(element).find("a");

	// galerie
	if (images.length != 0) {
		$(images).each( function (img) {
			var str = $(this).attr('href');
			box.srcs.push(str);
			box.titles.push($(this).children("img").attr('title'));
			var x = box.imgCount + 0;
			$(this).click( function () { box.show(x); return false; } );
			box.imgCount++;
		});
	}

	// vytvoreni galleryboxu
	$("body").append('<div id="gallery-'+numx+'" class="gallery-box-all"><div class="gallery-box-black"></div><div class="gallery-box">'+
	'<a href="#" class="gallery-box-close" title="'+box.txtClose+'"><span>'+box.txtClose+'</span></a>'+
	'<a href="#" class="gallery-box-left" title="'+box.txtPrev+'"><span>'+box.txtPrev+'</span></a>'+
	'<div class="gallery-box-image"><img src="" /></div>'+
	'<a href="#" class="gallery-box-right" title="'+box.txtNext+'"><span>'+box.txtNext+'</span></a>'+
	'<p></p><div class="gallery-box-cleaner"></div></div></div>');

	// pridani akci
	$(box.ident +" a.gallery-box-left").click(function() { return box.scroll(true); });
	$(box.ident +" a.gallery-box-right").click(function() { return box.scroll(false); });
	$(box.ident +" a.gallery-box-close").click(function() { $(box.ident).fadeOut(500); return false; });
	$(box.ident +" .gallery-box-black").click(function() { $(box.ident).fadeOut(500); });

	// zobrateni
	this.show = function (num) {
		var box = this;
		box.imgShow = num;
		if ($(box.ident).css("display") =='none') {
			$(box.ident +" .gallery-box").css('top',$(window).scrollTop());
			$(box.ident).fadeIn(500);
			if ($(document.body).height() > $(window).height()) $(box.ident +" .gallery-box-black").height($(document.body).height()+300);
			else $(box.ident +" .gallery-box-black").height('100%');
			if ($(document.body).width() > $(window).width()) $(box.ident +" .gallery-box-black").width($(document.body).width());
			else $(box.ident +" .gallery-box-black").width('100%');
		}

		$(box.ident +" .gallery-box p").text('loading ...');

		var imgNew = new Image();
		$(box.ident +" .gallery-box-image img").fadeOut(200, function() {
			$(box.ident +" .gallery-box-image").empty();
			$(imgNew).load(function () {
				var height = $(imgNew).attr('height');
				var width = $(imgNew).attr('width');
				var part = Math.round(width / 3);
				$(box.ident +" .gallery-box").height(height+47).width(width);
				$(box.ident +" .gallery-box-image").height(height).width(width);
				$(box.ident +" a.gallery-box-left").height(height).width(part);
				$(box.ident +" a.gallery-box-right").height(height).width(part);
				$(box.ident +" .gallery-box-image").append(this);
				$(box.ident +" .gallery-box-image img").fadeIn(200);
				$(box.ident +" .gallery-box p").text(box.titles[box.imgShow]);
			}).attr('src', box.srcs[box.imgShow]);
		});

		if (this.imgShow == 0) $(box.ident +" .gallery-box-left").hide();
		else $(box.ident +" .gallery-box-left").show();
		if (this.imgShow == this.imgCount-1) $(box.ident +" .gallery-box-right").hide();
		else $(box.ident +" .gallery-box-right").show();
	}

	// listovani
	this.scroll = function (left) {
		if (left) {
			if (this.imgShow > 0) this.show(this.imgShow-1);
		}
		else {
			if (this.imgShow < this.imgCount-1) this.show(this.imgShow+1);
		}
		return false;
	}

	this.appendImg = function (element) {
		var box = this;
		var img = $("#"+element+" img").attr('src');
		img = img.substring( img.lastIndexOf('/') + 1 );
		for(var i = 0; i < this.srcs.length; i++) {
			if (this.srcs[i].indexOf(img) > 0) {
				$("#"+element).click( function () { box.show(i); return false; } );
				$("#"+element).css('cursor','pointer');
				break;
			}
		}
	}
	return true;
}


function showComment (element,reply) {
	var parent = $(element).parent();
	$('#window-discussion').slideUp('normal',function () {
		if (reply == '0') $("#window-discussion input[name$='b_title']").val('');
		else $("#window-discussion input[name$='b_title']").val('RE: '+$(parent).children('h3').text());
		$("#window-discussion input[name$='b_parent']").val(reply);
		$("#window-discussion .error").hide().html("");
		$('#window-discussion').css('top',$(parent).offset().top+'px');
		$('#window-discussion').slideDown('normal');
	});
	return false;
}
function hideComment () {
	$('#window-discussion').slideUp('normal');
	return false;
}
