// galerie
function GalleyBox(element, numx, block) {
	if ($('#gallery-' + numx).length != 0) return false;
	this.gallery = element;
	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();
	this.blocker = block;

	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>');

	$('<a class="gallery-prev" href="#"></a>').prependTo(element);
	$('<a class="gallery-next" href="#"></a>').appendTo(element);

	// pridani akci
	$(element +" a.gallery-next").click(function() {return box.next();});
	$(element +" a.gallery-prev").click(function() {return box.prev();});
	$(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);});
	$(box.ident +" a.gallery-box-left").click(function() {return box.scroll(true);});
	$(box.ident +" a.gallery-box-right").click(function() {return box.scroll(false);});

	// zobrateni
	this.show = function (num) {
		var box = this;
		box.imgShow = num;
		box.select(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;
			}
		}
	}

	this.select = function(imageIndex) {
		$(this.gallery +' a.gallery-prev').removeClass('disabled');
		$(this.gallery +' a.gallery-next').removeClass('disabled');
		this.imgShow = imageIndex;

		this.position(imageIndex);
		if (imageIndex <= 0) $(this.gallery +' a.gallery-prev').addClass('disabled');
		if (imageIndex >= (this.imgCount-this.blocker)) $(this.gallery +' a.gallery-next').addClass('disabled');
		return false;
	}

	this.position = function (image) {
		var box = this;
		var i = 0;
		var place = 0;
		if ($('.gallery-view').width() < $(this.gallery +' .gallery-content').width()) {
			$(this.gallery +' .gallery-content a').each(function() {
				if (i < image && i < (box.imgCount - box.blocker)) {
					place += $(this).width();
					i++;
				}
			});
			$(this.gallery +' .gallery-content').animate({left: -place+'px'});
		}
	}

	this.next = function() {
		this.select((this.imgShow)+1);
		return false;
	}

	this.prev = function() {
		this.select((this.imgShow)-1);
		return false;
	}
	
	this.select(0);
	return true;
}


function GalleryBoxSlim (element, mark) {
	if ($('#gallery-' + mark).length != 0) return false;
	// localization
	var clientLang = (navigator.userLanguage || navigator.language).substr(0,2).toLowerCase();
	this.text = {
		'en' : {prev: 'previous', next: 'next', close: 'close', loading: 'loading ...', error: 'Image not found.'},
		'cs' : {prev: 'p\u0159edchozí', next: 'dal\u0161í', close: 'zav\u0159ít', loading: 'na\u010dítá se ...', error: 'Obrázek nelze na\u010díst.'}
	};
	// variables
	this.ident = '#gallery-' + mark;
	this.locale = this.text[clientLang] !== undefined ? this.text[clientLang] : this.text['en'];
	this.imgShow = 0;
	this.imgCount = 0;
	this.srcs = Array();
	this.titles = Array();

	// image load
	this.load = function (element) {
		var images = $(element).find("a");
		if (images.length != 0) {
			var box = this;
			$(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++;
			});
		}
	}

	// show galllery
	this.show = function (num) {
		var box = this;
		box.imgShow = num;
	//	box.select(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;
	}

	// create link on element to show gallery
	this.createLink = 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;
			}
		}
	}

	var box = this;
	this.load(element);

	// html gallery code insetion
	$("body").append('<div id="gallery-'+mark+'" class="gallery-box-all"><div class="gallery-box-black"></div><div class="gallery-box">'+
	'<a href="#" class="gallery-box-close" title="'+box.locale["close"]+'"><span>'+box.locale["close"]+'</span></a>'+
	'<a href="#" class="gallery-box-left" title="'+box.locale["prev"]+'"><span>'+box.locale["prev"]+'</span></a>'+
	'<div class="gallery-box-image"><img src="" /></div>'+
	'<a href="#" class="gallery-box-right" title="'+box.locale["next"]+'"><span>'+box.locale["next"]+'</span></a>'+
	'<p></p><div class="gallery-box-cleaner"></div></div></div>');

	// action association
	if (box.imgCount > 1) {
		$(box.ident +" a.gallery-box-left").click(function() {return box.scroll(true);});
		$(box.ident +" a.gallery-box-right").click(function() {return box.scroll(false);});
		$(document).keydown( function(event) {
			if ($(box.ident + ":visible").length != 0) {
				if (event.keyCode == "37") box.scroll(true);
				else if (event.keyCode == "39") 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);});
	return true;
}


// komentare
function windowComment () {
	$("#window-discussion").draggable({ cursor: 'move' });
	$("#window-discussion h3").css('cursor', 'move');
}

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('h4').text());
		$("#window-discussion input[name$='b_parent']").val(reply);
		$("#window-discussion .error").hide().html("");
		$('#window-discussion').css('top',($(parent).offset().top - 200)+'px');
		$('#window-discussion').slideDown('normal');
	});
	return false;
}

function hideComment () {
	$('#window-discussion').slideUp('normal');
	return false;
}

function panelMagnifier () {
	var maginfier = $('#viewport');
	var preview = $('#preview');
	var zoom = $('#zoom');

	var ratio = zoom.height() / maginfier.height();
	var viewportTop = preview.offset().top;
	var viewportHalf = (maginfier.height() * 0.5);
	var previewBottom = preview.height() - viewportHalf -2;

	function moveViewport(e) {
		if (e.pageY > viewportTop && e.pageY < (viewportTop+previewBottom)) { // pro IE6
			var newPosition = e.pageY - viewportTop - viewportHalf;
			if (e.pageY < (viewportTop + viewportHalf)) newPosition += (viewportHalf + viewportTop - e.pageY);
			maginfier.css('top',newPosition+'px');
			zoom.css('background-position', 'center '+ Math.round(-1*newPosition*ratio) +'px');
		}
	}

	maginfier.mousemove(function(e){moveViewport(e);});
	preview.mousemove(function(e){moveViewport(e);});
}


function activeBoxLink (box) {
	$(box).each( function () {
		var link = $(this).children("a").attr("href");
		$(this).click( function () {window.location.href = link} );
		// IE no hover support
		$(this).hover( function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); });
	});
}
