// galerie
function GalleyBox(element) {
	this.ident = '#gallery-' + element;
	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+" img");

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

	// vytvoreni galleryboxu
	$("body").append('<div id="gallery-'+element+'" 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;
}

// rozbalovani menu
$(document).ready(function() {
	$("#page-katalog .sub-box .back").each(function() {
		if (!$.browser.msie) $(this).hide();
	});
	var list = $("#page-katalog .sub-box");
	$("#page-katalog>ul>li").each(function() {
		var subbox = $(this).children(".sub-box");
		var submenu = $(this).children(".sub-box").children(".back");
		if (subbox.length != 0) {
			$(this).hover(
				function() {
					if ($.browser.msie) {
						$(list).each(function() { $(this).stop(true,true); $(this).hide(); }); // fix skryvani
						$(subbox[0]).slideDown("normal"); // IE6 fix
					}
					else $(submenu[0]).slideDown("normal");
				},
				function() {
					if ($.browser.msie) $(subbox[0]).hide(); // IE6 fix
					else $(submenu[0]).hide();
				}
			);
		}
	});

	var subUls = $("#page-katalog .sub-box ul ul");
	if (subUls.length != 0) {
		$(subUls).each(function() {
			$(this).hide();
			var ul = this;
			var parent = $(this).parent("li");
			$(parent).hover(
				function() {
					$(ul).slideDown("normal");
				},
				function() {
					$(ul).stop(true,true);
					$(ul).slideUp("normal");
				}
			);
		});
	}
});

// hover nahedy
var textx = "";
function hoverOut() {
	var old = $('#product-image img').attr('src');
	var text = $('#product-image #ctxt').text();
	$('#product-thumbs').mouseleave(function() {
		$('#product-image #ctxt').text(" loading ... ");
		$('#product-image img').fadeOut('fast',function() {
			$('#product-image img').attr('src',old);
			$('#product-image img').fadeIn(function() {
				$('#product-image #ctxt').text(text);
			});
		});
	});
}
function hoverThumb(element,image,text) {
	$('#'+element).hover(
		function() {
			textx = text;
			$('#product-image #ctxt').text(" loading ... ");
			$('#product-image img').fadeOut(function() {
				$('#product-image img').attr('src',image);
				$('#product-image img').load(function() {
					$('#product-image img').fadeIn(function () {
						$('#product-image #ctxt').text(textx);
					});
				});
			});
		},
		function() {
			$('#product-image img').stop(true,true);
			$('#product-image #ctxt').stop(true,true);
			$('#product-image img').show();
		}
	);
}

// text technika
function hoverTech (image, text) {
	$('#'+image).hover(
		function () {
			var height = $('#'+text).height();
			if ( $('#'+text).parent().height() < height ) $('#'+text).parent().css('height',height+40);
			$('#'+text).show();
		},
		function () {
			$('#'+text).hide();
		}
	);
}



function boxShow(text,parent) {
	if($("#text-box-show").length == 0) {
		$("body").append('<div id="text-box-show"><p></p></div>');
	}
	if ($("#text-box-show").css("display") =='none') {
		$("#text-box-show p").html(text);
		$("#text-box-show").show();
	}
	$(parent).mousemove(function(e) {
		$("#text-box-show").css({'left': e.pageX+8,'top': e.pageY + 2});
	});
	$(parent).mouseout(function(e) {
		$("#text-box-show").unbind();
		$("#text-box-show").hide();
	});
}
