// galerie
function GalleyBox(element, numx, bigImg) {
	if ($('#gallery-' + numx).length != 0) return false;
	this.bigImg = bigImg;
	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');
			str = str.replace('images/', 'images/large/');
			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++;
		});
		if (images.length > 1) {
			$(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);
				}
			});
		}
	}

	// vytvoreni galleryboxu
	$("body").append('<div id="gallery-'+numx+'" class="gallery-box-all"><div class="gallery-box-black"></div><div class="gallery-box">'+
	'<div class="gallery-box-image"><img src="" /></div>'+
	'<div id="gallery-position"><a href="#" class="gallery-box-left" title="'+box.txtPrev+'"><span>&lt;</span></a>'+
	'<span id="gallery-position-num">1</span> / '+box.srcs.length+
	'<a href="#" class="gallery-box-right" title="'+box.txtNext+'"><span>&gt;</span></a>'+
	'<a href="#" class="gallery-box-close" title="'+box.txtClose+'"><span>X</span></a></div>'+
	'<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);});
	$(box.bigImg).css("cursor","pointer");
	$(box.bigImg).click(function() {return box.bigImage();});

	// 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 +" .gallery-box-image").append(this);
				$(box.ident +" .gallery-box-image img").fadeIn(200);
				$(box.ident +" .gallery-box p").text(box.titles[box.imgShow]);
				$(box.ident +" .gallery-box #gallery-position-num").text(num+1);
			}).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;
			}
		}
	}

	this.bigImage = function () {
		this.show(this.imgShow);
		return false;
	}

	return true;
}


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

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

	init: function (select, table) {
		var pay = this;
		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();
		$(this.table + " td.post.price span").text(this.state[value][0]);
		var newPrice = $(this.table + " td.total.price span").text() - oldPost;
		$(this.table + " td.total.price span").text(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);
		}
	}
}

// hover nahedy
function hoverText (element, text) {
	$(element).each( function () {
		var box = this;
		if ($(box).find(text).length > 0) {
			$(box).hover(
				function() {$(box).find(text).show();$(box).addClass("hover");},
				function() {$(box).find(text).hide();$(box).removeClass("hover");}
			).click ( function () {
				window.location.href = $(box).find("a").attr("href");
			});
		}
	});
}

function clickImage (element, image) {
	$(element).each( function () {
		$(this).click( function () {
			$(image).attr('src', $(this).attr("href"));
			return false;
		});
	});
}

function hoverImage (element, image) {
	$(element).each( function () {
		$(this).hover( function () {
			if ($(image).attr('src') != $(this).attr("href")) {
			$(image).parent().height($(image).height());
			if ($(image).parent().width() < $(image).width()) $(image).parent().width($(image).width());
			$(image).hide();
			$(image).load(function () {
				$(this).fadeIn(200);
				$(image).parent().height($(this).height());
			}).attr('src',  $(this).attr("href"));
			//$(image).attr('src',);
			}
		});
	});
}

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