// galerie
function GalleyBox(element, numx) {
	if ($('#gallery-' + numx).length != 0) return false;
	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 addLink(parent,target) {
	$(parent).each( function () {
		var link = $(this).find("a");
		$(this).children(target).before('<a href ="'+$(link).attr("href")+'"></a>');
		$(this).children("a").append($(this).children(target)).hover( function() {
			$(link).addClass("hover");}, function() {$(link).removeClass();}
		);
	});
}

function animateAll() {
	$(document).unbind('ready');
	$("#page-navigation #slider").hide();
	$("#page-content").hide();
	$("#home-news .home-box").hide();
	$("#page-navigation #slider").show("slide", {direction: "left"}, 1000);
	$("#page-content").show("slide", {direction: "up"}, 1000);
	animateNews($("#home-news .home-box"), 0);
}
function animatePage() {
	$("#page-content").hide();
	$("#page-content").show("slide", {direction: "left"}, 1000);
}
function animateNews(news , i) {
	if (i < news.length) $(news[i]).fadeIn(700, function() {animateNews(news, i+1);});
}

/*** HOMEPAGE ***/
function imageChanger (image) {
	this.images = Array();
	this.parent = $(image).parent();
	this.element = image;
	var changer = this;
	this.add = function (image) {
		this.images.push(image);
	},
	this.start = function (i) {
		if (i >= this.images.length) i = 0;
		$(changer.parent).css("background-image","url('"+$(changer.element).attr("src")+"')");
		$(changer.element).hide();
		var img = new Image();
		$(img).load(function () {
			$(changer.element).attr("src", changer.images[i]);
			$(changer.element).fadeIn(1000);
			setTimeout("home.start("+(i+1)+")", 5000);
		}).attr("src", changer.images[i]);
	}
}

/***  MENU  ***/
$(document).ready( function () {
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		$("#page-navigation ul>li").each( function () {
			var child = $(this).children("ul");
			if ($(child).size() > 0) {
				$(this).hover( function () {$(child).show();}, function () {$(child).hide();});
			}
		});
	}
});

