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

function ImageChanger (element) {
	this.timer = 3000;
	this.element = element;
	this.pages = $(element).children();

	this.run = function () {
		setTimeout( function () { self.show(0); }, self.timer );
	}
	
	this.show = function (i) {
		var next = (i+1 >= self.pages.length) ? 0 : i+1;
		var image = new Image();
		$(image).load(function () {
			$(self.pages[i]).fadeOut(1000, function () { $(this).hide(); });
			$(self.pages[next]).hide().fadeIn(1000, function () {
				setTimeout( function () { self.show(next); }, self.timer );
			});
		}).error(function () {
			setTimeout( function () { self.show(next); }, self.timer );
		}).attr("src", $(self.pages[next]).find("img").attr("src"));
	}

	var self = this;
	if (this.pages.length > 1) this.run();
}

/* old
function ImageChanger (image) {
	this.timer = 3000;
	this.images = Array();
	this.parent = $(image).parent();
	this.element = image;
	var changer = this;
	
	this.add = function (image) {
		this.images.push(image);
	}

	this.run = function () {
		setTimeout(function(){changer.show(1)}, changer.timer);
	}
	
	this.show = function (i) {
		if (i >= changer.images.length) i = 0;
		var newImage = new Image();
		$(newImage).load(function () {
			$(changer.element).fadeOut( 1000, function () { $(this).remove(); });
			$(changer.element).after(newImage);
			$(newImage).hide().fadeIn(1000);
			changer.element = newImage;
			setTimeout(function(){changer.show(i+1)}, changer.timer);
		}).error(function () {
			setTimeout(function(){changer.show(i+1)}, changer.timer);
		}).attr("src", changer.images[i]);
	}
}
*/

// hover nahedy
var textx = "";
function hoverOut (newProd) {
	var old = $('#product-image img').attr('src');
	var text = $('#product-image #ctxt').text();
	$('#product-thumbs').mouseleave(function() {
		if ($('#product-image .product-new').length != 0) $('#product-image .product-new').remove();
		if (newProd == 1) $('#product-image').prepend('<div class="product-new"></div>');
		$('#product-image #ctxt').text(" loading ... ");
		$('#product-image img').fadeOut('fast',function() {
			$('#product-image img').attr('src',old);
			$('#product-image img').fadeIn(function() {
				$('#product-image #ctxt').text(text);
			});
		});
	});
}
function hoverThumb (element, image, text, newProd) {
	$('#'+element).hover(
		function() {
			textx = text;
			$('#product-image #ctxt').text(" loading ... ");
			$('#product-image img').fadeOut(function() {
				$('#product-image img').attr('src',image);
				if ($('#product-image .product-new').length != 0) $('#product-image .product-new').remove();
				if (newProd == 1) $('#product-image').prepend('<div class="product-new"></div>');
				$('#product-image img').load(function() {
					$('#product-image img').fadeIn(function () {
						$('#product-image #ctxt').text(textx);
					});
				});
			});
		},
		function() {
			$('#product-image img').stop(true,true);
			$('#product-image #ctxt').stop(true,true);
			$('#product-image img').show();
		}
	);
}

// text technika
function hoverTech (image, text) {
	$('#'+image).hover(
		function () {
			var height = $('#'+text).height();
			if ( $('#'+text).parent().height() < height ) $('#'+text).parent().css('height',height+40);
			$('#'+text).show();
		},
		function () {
			$('#'+text).hide();
		}
	);
}

function boxShow(text,parent) {
	if($("#text-box-show").length == 0) {
		$("body").append('<div id="text-box-show"><p></p></div>');
	}
	if ($("#text-box-show").css("display") =='none') {
		$("#text-box-show p").html(text);
		$("#text-box-show").show();
	}
	$(parent).mousemove(function(e) {
		$("#text-box-show").css({'left': e.pageX+8,'top': e.pageY + 2});
	});
	$(parent).mouseout(function(e) {
		$("#text-box-show").unbind();
		$("#text-box-show").hide();
	});
}


// registrace
function potvrzeni_zasilani(element) {
	var form = $(element).parent();
	var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var adresa = document.getElementById("email").value;
	if (adresa == "" || !filter.test(adresa)) {
		alert("Nebyla zadána e-mailová adresa.");
	}
	else {
		var akce = confirm("Odesláním vaší e-mailové adresy udělujete firmě Piccollo spol. s.r.o, oddělení značky Loap souhlas k odběru mailových zpráv o nejnovějších promo akcích, nabídkách a dalších nových službách (na základě zák. č. 480/2004 Sb.).\nPravidelné zasílání nabídek můžete kdykoliv ukončit kliknutím na odhlašovací text, který bude umístněn v dolní části každého zaslaného e-mailu.\n\nOpravdu chcete zaregistrovat Vaší emailovou adresu?");
		if (akce) {
			$.ajax({
				type: "GET",
				url: "/registration.php",
				data: $(form).serialize() + "&regist=send&ajax=true",
				success: function(mess) {
					alert(mess);
				}
			});
		}
	}
	return false;
}

// rozbalovani menu
$(document).ready(function() {
	/*$("#page-katalog .sub-box .back").each(function() {
		if (!$.browser.msie) $(this).hide();
	});*/
	var list = $("#page-katalog .sub-box");
	$("#page-katalog ul>li").each(function() {
		var subbox = $(this).children(".sub-box");
		var submenu = $(this).children(".sub-box").children(".back");
		if (subbox.length != 0) {
			$(this).hover(
				function() {
					if ($.browser.msie) {
				//		$(list).each(function() { $(this).stop(true,true); $(this).hide(); }); // fix skryvani
						$(subbox[0]).slideDown(300); // IE6 fix
					}
					else $(submenu[0]).slideDown(300);
				},
				function() {
					if ($.browser.msie) $(subbox[0]).hide(); // IE6 fix
					else $(submenu[0]).hide();
				}
			);
		}
	});


	var speed = 80;
	var child = "ul";
	var submenu = $("#page-menu ul li").children(child);

	if (submenu.length != 0) {
		$(submenu).hide();
		//$(submenu).children(child).hide();
		$(submenu).each( function () {
			var select = $(this);
			var height = $(this).height();
			$(this).width($(this).width()).css('overflow','hidden'); // fixing IE 7 bug
			$(this).height(0).show();

			$(this).parent().hover(
				function() { $(select).stop(true,false).animate({height: (height+"px")}, (Math.log(height) * speed)); },
				function() { $(select).stop(true,false).animate({height: "0px"}, (Math.log(height) * speed)); }
			);
		});
	}
});

/*** filters ***/
function ContentFiltr (url,kolekce,list,lang) {
	this.lang = lang;
	this.url = url;
	this.kolekce = kolekce;
	this.list = list;
	this.options = Array();
	var filtr = this;

	$("#filterit").click(function() { return filtr.showIt(); });

	this.addOption = function (element) {
		this.options.push(element);
		if ($('#'+element).is("select")) {
			$('#'+element).change(function () {
				filtr.loadOption(element);
			});
		}
		else {
			var inpVal = $('#'+element).val();
			$('#'+element).click(function () {
				$(this).val("");
			});
			$('#'+element).blur(function () {
				var valid = true;
				var num = $(this).val().replace(' ','');
				if (num.length > 0) {
					for (var i=0; i < num.length; i++) {
						var key = num.charAt(i); //cycle through characters
						if (key == " ") {}
						else if (key < "0" || key > "9") {
							valid = false;
							break;
						}
					}
				}
				else valid = false;
				if (!valid) {
					$(this).val(inpVal);
				}
			});
		}
	}

	this.loadOption = function (element) {
		var demand = "&";
		for (i = 0; i < this.options.length; i++) {
			var opt = document.getElementById(this.options[i]);
			if ($(opt).is("select")) {
				var val = opt.options[opt.selectedIndex].value;
				demand += this.options[i] + "=" + val + "&";
				if (element == this.options[i]) break;
			}
			else {
				demand += this.options[i] + "=" + $(opt).val() + "&";
			}
		}
		for (i = filtr.options.length - 1; i > 0; i--) {
			if (element == filtr.options[i]) break;
			else {
				$('#'+filtr.options[i]).remove();
				filtr.options.pop();
			}
		}
		$.ajax({
			type: "GET",
			url: "ajax-filter.php",
			data: "lang=" + filtr.lang + "&kolekce="+ filtr.kolekce + "&list=" + filtr.list + demand,
			success: function(html) {
				if(html != "") {
					$(opt).parent().append(html);
					cut = html;
					while(true) {
						var begin = cut.indexOf('id="');
						if (begin < 1) break;
						var cut = cut.substr(begin + 4);
						filtr.addOption(cut.substr(0,cut.indexOf('"')));
					}
				}
			}
		});
	}

	this.showIt = function () {
		var vals = Array();
		var demand = "&";
		for (i = 0; i < this.options.length; i++) {
			var opt = document.getElementById(this.options[i]);
			if ($(opt).is("select")) vals[i] = opt.options[opt.selectedIndex].value;
			else vals[i] = $(opt).val();
			if (vals[i] != "") demand += this.options[i] + "=" +vals[i] + "&";
		}
		window.location.href= url + demand;
		return false;
	}
}

/*** homepage - promo ***/
function promoInit (homepage) {
	var show = true;
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf("promovisit");
	if (!homepage && ind != -1) show = false;
	if (show) document.cookie = "promovisit=true";
	else $("#promo-flash").hide();
}

function promohide() {
	$("#promo-flash").slideUp("normal");
}
