/**
 * Gallery box Classic version MODED
 * @author DaVee
 * @version 0.9
 * @license under WTFPL 2.0
 */
function GalleryBlock (element, mark, block) {
	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.gallery = element;
	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();
	this.blocker = block;

	// 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;
		box.select(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;
	}

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

	// 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;
			}
		}
	}

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

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

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

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

	// action association
	if (box.imgCount > 1) {
	$(element +" a.gallery-next").click(function() {return box.next();});
	$(element +" a.gallery-prev").click(function() {return box.prev();});
		$(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);});

	this.select(0);
	return true;
}

/**
 * Gallery box Classic version
 * @author DaVee
 * @version 0.92
 * @license under WTFPL 2.0
 */
function GalleryBox (element, mark) {
	if (mark === undefined) mark = 'box';
	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();

	// load links from element
	this.load = function (element) {
		var box = this;
		$(element).each( function () {
			var images = $(this).find("a");
			if (images.length != 0) {		
				$(images).each( function () {
					box.loadImage(this);
				});
			}
			else box.loadImage(element);
		});
	}
	
	// add image
	this.loadImage = function (link) {
		if ($(link).attr('href') !== undefined) {
			var str = $(link).attr("href");
			this.srcs.push(str);
			this.titles.push($(link).children("img").attr("title"));
			var x = this.imgCount + 0;
			$(link).click( function () {box.show(x);return false;} );
			this.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;
}

// registrace
function potvrzeni (element, empty) {
	var form = $(element).parent().parent();
	var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var adresa = document.getElementById("inp-mail").value;
	if (adresa == "" || !filter.test(adresa)) {
		alert(empty);
	}
	else {
		$.ajax({
			type: "GET",
			url: $(form).attr("action"),
			data: $(form).serialize() + "&regist=send&ajax=true",
			success: function(mess) {
				alert(mess);
			}
		});
	}
	return false;
}

function ajaxLoad (link, element) {
	var retState = true;
	var urlRequest = $(link).attr('href') + '&ajax=true';
	$.ajax({
		type: "GET",
		dataType: "xml",
		async: false,
		url: urlRequest,
		success: function (xhtml) {
			var title = $(xhtml).find('title').text();
			if (title != "") document.title = title;
			$(element).html($(xhtml).find('xhtml').text());
			retState = false;
		}
	});
	return retState;
}

/**
 * Promotion banner
 * @author DaVee
 * @version 0.8
 * @license under WTFPL 2.0
 */
function PromoBanner () {
	this.banner = $("#banner-promo-content");
	this.image = $("#banner-promo-img");
	this.promo = $("#banner-promo");
	this.background = $("#banner-promo-background");
	this.close = $("#banner-promo-close");
	$(this.promo).hide();

	this.centerTop = function (height) {
		var top = Math.round((($(window).height() - height) / 2) - ($(window).height() / 20));
		return (top > 0 ? top+"px" : 0);	
	}
	
	this.show = function () {
		if ($.browser.msie && jQuery.browser.version < 7) {
			$(this.background).height($(document).height() > $(window).height() ? $(document).height() : $(window).height())
					.width($(document).width() > $(window).width() ? $(document).width() : $(window).width());
		}
		var box = this;
		var img = $(box.image).find("img");
		var image = new Image();
		
		$(this.promo).fadeIn(500);
		$(this.banner).css("top", this.centerTop($(this.banner).height()));

		$(image).load(function () {
			var width = $(this).attr('width');
			var height = $(this).attr('height');
			$(box.banner).animate({ width: width+"px", top: box.centerTop(height)}, 1000);
			$(box.image).animate({ height: height+"px", width: width+"px"}, 1000, function () {
				$(img).fadeIn(500);
				$(box.close).slideDown(500);
			});
		}).attr("src", $(img).attr("src"));	
	}
	
	var box = this;
	$(this.image).find("img").hide();
	$(this.background).click( function () {$(box.promo).fadeOut(1000);});
	$(this.close).hide().find("a").click( function () {$(box.promo).fadeOut(1000); return false;});
	$(this.banner).width("200px");
	$(this.image).height("200px");
	setTimeout( function () {box.show();}, 800);
}

/**
 * Image changer
 * @author DaVee
 * @version 0.5
 * @license under WTFPL 2.0
 */
function ImageChanger (image) {
	this.timer = 5000;
	this.images = Array();
	this.parent = $(image).parent();
	this.element = image;
	var changer = this;

	this.add = function (image) {
		this.images.push(image);
	},
	this.change = function (i) {
		if (i >= this.images.length) i = 0;
		$(changer.parent).css("background-image","url('"+$(changer.element).attr("src")+"')");
		$(changer.parent).height(($(changer.element).height()));
		$(changer.element).hide();
		var img = new Image();
		$(img).load(function () {
			$(changer.element).attr("src", changer.images[i]);
			$(changer.element).fadeIn(1000);
			setTimeout( function(){changer.change(i+1)}, changer.timer);
		}).attr("src", changer.images[i]);
	}
	
	this.run = function () {
		setTimeout( function(){changer.change(1)}, changer.timer);
	}
}

function foodTable () {
	var move = $("#food-table").height() - $("#food-sections ul").height() - 10;
	var top = ($("#food-sections").offset()).top;
	 $(window).scroll(function () {
		var scroll = $(window).scrollTop();
		if (scroll >= top && (scroll - top) < move) {
			$("#food-sections ul").css('top', (scroll - top) + "px");
		}
	 });
	// IE 6 + 7 fix
	if ($.browser.msie && jQuery.browser.version < 8) {
		$("#food-sections").css("height", ($("#food-table").height() - 30) + "px");
	}
}

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