/**
 * Gallery box Classic version
 * @author DaVee
 * @version 0.9
 * @license under WTFPL 2.0
 */
function GalleryBox (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;
		var imgNew = new Image();
		box.imgShow = num;
		if ($(box.ident).css("display") == "none") {
			if ($.browser.msie && jQuery.browser.version < 8) $(box.ident +" .gallery-box").css("margin-top", 0);
			$(box.ident +" .gallery-box").css("top", $(window).scrollTop());
			$(box.ident).fadeIn(500);
			box.center();
		}

		$(box.ident +" .gallery-box p").text(box.locale["loading"]);
		$(box.ident +" .gallery-box-image img").fadeOut(200, function() {
			$(box.ident +" .gallery-box-image img").remove();
			$(imgNew).load(function () {
				var height = $(imgNew).attr('height');
				var width = $(imgNew).attr('width');
				$(box.ident +" .gallery-box").width(width);
				$(box.ident +" .gallery-box-image").height(height).width(width).append(this);
				box.center();
				$(box.ident +" .gallery-box-image img").fadeIn(200);
				$(box.ident +" .gallery-box p").text(box.titles[box.imgShow]);
			}).error(function () {
				$(imgNew).stop(true,true);
				$(box.ident +" .gallery-box-image").append(new Image());
				$(box.ident +" .gallery-box p").text(box.locale["error"]);
			}).attr('src', box.srcs[box.imgShow]);
		});

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

	// place into center of screen
	this.center = function () {
		var top = Math.round(($(window).height() - $(box.ident +" .gallery-box").height()) / 2);
		// IE 6 + 7 fix
		if ($.browser.msie && jQuery.browser.version < 8) {
			var oldTop = parseFloat(($(box.ident +" .gallery-box").css("margin-top")).replace('px', ''));
			var baseTop = parseFloat(($(box.ident +" .gallery-box").css("top")).replace('px', ''));
			if (jQuery.browser.version < 7) top = Math.round(top / 2);
			$(box.ident +" .gallery-box").css("top", top > 0 ? (baseTop + top - oldTop)+"px" : (baseTop + 0 - oldTop)+"px");
			$(box.ident +" .gallery-box").css("margin-top", top > 0 ? top+"px" : 0+"px");
			// IE 6 fix
			if (jQuery.browser.version < 7) {
				$(box.ident +" .gallery-box").css('left', Math.round(($(window).width() - $(box.ident +" .gallery-box").width()) / 2) + "px");
				$(box.ident +" .gallery-box-black")
					.height($(window).height() > $(document).height() ? $(window).height() : $(document).height())
					.width($(window).width() > $(document).width() ? $(window).width() : $(document).width());
				if (($(box.ident +" .gallery-box").offset().top + $(box.ident +" .gallery-box").height()) > $(box.ident +" .gallery-box-black").height()) {
					$(box.ident +" .gallery-box-black").height($(box.ident +" .gallery-box").offset().top + $(box.ident +" .gallery-box").height());
				}
				if ($(box.ident +" .gallery-box").width() > $(box.ident +" .gallery-box-black").width()) {
					$(box.ident +" .gallery-box-black").width($(box.ident +" .gallery-box").width());
				}
			}
		}
		else {
			$(box.ident +" .gallery-box").css("margin-top", top > 0 ? top+"px" : 0);
		}
	}

	// 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">'+
	'<div class="gallery-box-image">'+
	'<a class="gallery-box-right" title="'+box.locale["next"]+'"><span>'+box.locale["next"]+'</span></a>'+
	'<a class="gallery-box-left" title="'+box.locale["prev"]+'"><span>'+box.locale["prev"]+'</span></a>'+
	'<img src="" /></div>'+
	'<a class="gallery-box-close" title="'+box.locale["close"]+'"><span>'+box.locale["close"]+'</span></a>'+
	'<p></p><div class="gallery-box-clear"></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;
}

// rozbalovani menu a animace
$(document).ready(function() {
	// menu
	var submenu = $("#menu ul li").children("ul");
	if (submenu.length != 0) {
		$(submenu).each( function () {
			var select = $(this);
			$(this).parent().hover(
				function() {
					$(select).stop(true,true);
					$(select).slideDown(300);
				},
				function() {
					$(select).slideUp(300);
				}
			);
		});
	}
	// animace
	if (!$.browser.msie || $.browser.version > 9) {
		animationBear();
		animationBaby();
	}
});

function animationBear () {
	$('#ani-bear')
		.delay(200)
		.animate({rotate: 0.2, translateX: -15}, 3000, 'swing')
		.delay(200)
		.animate({rotate: -0.2, translateX: 15}, 3000, 'swing', function () {animationBear(); })
}

function animationBaby () {
	$('#ani-baby')
		.delay(100)
		.animate({rotate: 0.8}, 3500, 'swing')
		.delay(100)
		.animate({rotate: -0.5}, 3500, 'swing', function () {animationBaby(); })
}
