function SimpleGallery (presenter, list, show) {
	this.pres = presenter;
	this.parent = '#'+$(presenter).parent().attr("id");
	this.frame = '#'+$(presenter).find("img").parent().attr("id");
	this.imgShow = show;
	this.imgCount = 0;
	this.srcs = Array();
	this.titles = Array();
	this.list = list;

	var box = this;
	var images = $(list).find("a");

	// galerie
	if (images.length != 0) {
		$(images).each( function (link) {
			var img = $(this).children("img");
			var src = $(img).attr('src');
			box.srcs.push(src.replace("/min_", "/"));
			box.titles.push($(img).attr('title'));
			var x = box.imgCount + 0;
			$(this).click( function () {box.show(x);return false;} );
			box.imgCount++;
		});
		$(box.pres +" .prev").click( function () {return box.scroll(true);} );
		$(box.pres +" .next").click( function () {return box.scroll(false);} );
		$(box.pres).width($(box.frame + " img").width());
		$(box.frame).height($(box.frame + " img").height());
		$(box.list).hide();
		$(box.list).height($("#gallery-content").height());
		$(box.list).show();
	}

	this.rescale = function () {
		$(this.list).hide();
		$(this.list).height($("#gallery-content").height());
		$(this.list).show();
	}

	// zobrateni
	this.show = function (num) {
		var box = this;
		if (box.imgShow != num) {
			box.imgShow = num;
			$(box.pres +" p").text('loading ...');
			var imgNew = new Image();
			$(box.pres +" img").fadeOut(300, function() {
				$(box.frame).empty();
				$(imgNew).load(function () {
					var height = $(imgNew).attr('height');
					var width = $(imgNew).attr('width');

					$(box.parent).width(width + 100);
					$(box.pres).width(width);
					$(box.frame).height(height);
					$(box.frame).append(this);
					$(box.frame +" img").hide().fadeIn(300);
					$(box.pres +" p").text(box.titles[box.imgShow]);
					box.rescale();
					box.position(num);
				}).attr('src', box.srcs[box.imgShow]);
			});

			if (box.imgShow == 0) $(box.pres +" .prev").hide();
			else $(box.pres +" .prev").show();
			if (box.imgShow == box.imgCount-1) $(box.pres +" .next").hide();
			else $(box.pres +" .next").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.position = function (image) {
		var box = this;
		var i = 0;
		var place = 0;
		var imglist = $(box.list).height();
		var scrollist = $(box.list +' #gallery-scroll').height();
		
		if (imglist < scrollist) {
			$(box.list).find('a').each(function() {
				if (i < (image-1) && i < box.imgCount) {
					place += $(this).height();
					i++;
				}
			});
			if (scrollist - imglist - place < 0) place = scrollist - imglist;
			if (place < 0) place = 0;
			$(box.list +' #gallery-scroll').animate({top: - place + 'px'});
		}
		else {
			$(box.list +' #gallery-scroll').animate({top: 0 + 'px'});
		}
	}
}


function SimpleHover (frame, list) {
	var box = this;
	var imgOld = $(frame + " img").attr('src');
	var images = $(list).find("a");

	if (images.length != 0) {
		$(images).each( function () {
			var image =  $(this).children("img").attr('src');
			image = image.replace("/min_", "/")
			$(this).hover(
				function() {
					$(frame).height($(frame + " img").height());
					$(frame + ' img').fadeOut( function() {
						$(frame + ' img').attr('src',image);
						$(frame + ' img').load( function() {
							$(frame).height($(frame + " img").attr('height'));
							$(frame + ' img').fadeIn();
						});
					});
				},
				function() {
					$(frame + ' img').stop(true,true).show();
				}
			);
			$(list).mouseleave ( function () {
			//	$(frame + ' img').stop(true,true);
				$(frame + ' img').fadeOut( function() {
					$(frame + ' img').attr('src',imgOld);
					$(frame + ' img').load( function() {
						$(frame).height($(frame + " img").attr('height'));
						$(frame + ' img').fadeIn();
					});
				});
			});
			$(this).click( function () {return false;} );
		});
	}
}


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

/*** HOMEPAGE ***/
function backgroundChanger (image) {
	this.images = Array();
	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;
		var img = new Image();
		$(changer.element).hide();
		$(img).load(function () {
			$(changer.element).css("background-image","url('"+changer.images[i]+"')");
			$(changer.element).height($(window).height());
			$(changer.element).fadeIn(1000, function () {
				$("body").css("background-image","url('"+changer.images[i]+"')");
			});
		}).attr("src", changer.images[i]);
		setTimeout("images.start("+(i+1)+")", 8000);
	}
}

/*** menu ***/
function runScripts () {
   	// menu
	if ($("#page-left .select").length != 0) runAnimate("#page-left .select");
	else if ($("#page-right-fix .select").length != 0) runAnimate("#page-right-fix .select");

	if ($("#page-preview .rec-list").length != 0) {
		$("#page-preview .rec-list").each ( function () {
			if ($(this).find("h2 a").length != 0) {
				var link = $(this).find("h2 a").attr("href");
				$(this).click( function () {window.location.href = link} );
				// IE no hover support
				$(this).hover( function () {$(this).addClass("hover");}, function() {$(this).removeClass("hover");});
			}
		});
	}
	if ($(".panel-conserts a").length != 0) {
		var panel = $(".panel-conserts");
		$(panel).click( function () {window.location.href = $(panel).find("a").attr("href")} );
		$(panel).hover( function () {$(panel).addClass("hover");}, function() {$(panel).removeClass("hover");} );
	}

	// registration
	/*$("#form-registration a").click( function () {
		if ($("#form-registration form").css("display") == "block") $("#form-registration form").slideUp(300);
		else $("#form-registration form").slideDown(300);
		return false;
	});*/
}


var aniTimer;
function animate (element) {
	if ($(element).hasClass('js')) $(element).removeClass('js');
//	else $(element).addClass('js');
//	if ($(element).length != 0) aniTimer = setTimeout("animate('"+element+"')",500);

}
function runAnimate (element) {
	if ($(element).hasClass('js')) $(element).removeClass('js');
//	if (aniTimer) clearTimeout(aniTimer);
//	aniTimer = setTimeout("animate('"+element+"')",500);
}


/*** forum ***/
function emoticon (text) {
	var area = $("#b_text");
	text = ' ' + text + ' ';
	if($(area).val() == "Text příspěvku") $(area).val("");

	var caretPos = getCaretPosition(document.getElementById('b_text'));
	var txt = $(area).val();
	$(area).val(txt.substr(0, caretPos) + text + txt.substr(caretPos));
	$(area).focus();
}

function getCaretPosition (ctrl) {
	var CaretPos = 0;	// IE Support
	if (document.selection) {
		ctrl.focus ();
		var Sel = document.selection.createRange ();
		Sel.moveStart ('character', -ctrl.value.length);
		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;
	return (CaretPos);
}

function floatPanel (element) {
	$(element).css('right', 10 - ($(document).width() - $(window).width() - $(this).scrollLeft()) + 'px');
	$(window).scroll(function () {
		$(element).css('right', 10 - ($(document).width() - $(window).width() - $(this).scrollLeft()) + 'px');
	});
/*	var base = $(element).position().top;
	$(window).scroll(function () {
		var move = base + $(this).scrollTop();
		$(element).css('top', move + 'px');
	});*/
}

$(document).bind("ready", function() {
	runScripts();
	if ($(window).width() < $(document).width()) floatPanel("#page-right-fix");
});

// vyber statu
var PayType = {
	table: "",
	currency: "",
	state: new Array(),

	init: function (select, table, currency) {
		var pay = this;
		this.currency = currency;
		this.table = table;
		$(select).change( function () {
			pay.change($(this).val());
		});
	},

	addState: function (id,cost,delivery) {
		this.state[id] = new Array(cost, delivery);
	},
	change: function (value) {
		var oldPost = $(this.table + " td.post.price span").text().replace(this.currency,'');
		$(this.table + " td.post.price span").text(this.currency + " " + this.state[value][0]);
		var newPrice = $(this.table + " td.total.price span").text().replace(this.currency,'') - oldPost;
		$(this.table + " td.total.price span").text(this.currency + " " + (newPrice + this.state[value][0]));

		if (this.state[value][1] == 1) $("#pay_deliver").parent().show();
		else {
			 $("#pay_deliver").parent().hide();
			 $("#pay_transfer").attr('checked', true);
		}
	}
}

function runBasket (basket) {
	var form = $(basket).find("form");
	$(basket).find("input:text").each ( function () {
		$(this).change( function () {$("#shop-recount").click();});
	});
}

// registrace
function registrationSend (element, defName, uri) {
	var form = $(element).parent();
	var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var name = document.getElementById("reg-name").value;
	var adresa = document.getElementById("reg-mail").value;
	if (name == "" || name == defName) {
		alert("Nebylo zadáno jméno.");
	}
	else if (adresa == "" || !filter.test(adresa)) {
		alert("Nebyla zadána e-mailová adresa.");
	}
	else {
		/*var akce = confirm("Opravdu chcete zaregistrovat Vaší emailovou adresu?");
		if (akce) {*/
			$.ajax({
				type: "GET",
				url: uri+"registration.php",
				data: $(form).serialize() + "&regist=send&ajax=true",
				success: function(mess) {
					alert(mess);
				}
			});
		//}
	}
	return false;
}
