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


function panelMagnifier (src) {
	var maginfier = $('#product-magnifier');
	var preview = $('#product-zoom-small');
	var zoom = $('#product-zoom-big');

	var ratio = zoom.height() / maginfier.height();
	var lensTop = Array(preview.offset().top, preview.offset().left);
	var lensCenter = Array(maginfier.height() * 0.5, maginfier.width() * 0.5);
	var previewBottom = Array(preview.height(), preview.width());

	function moveViewport(e) {
		if (e.pageY > lensTop[0] && e.pageY < (lensTop[0] + previewBottom[0] + lensCenter[0]) &&
			e.pageX > lensTop[1] && e.pageX < (lensTop[1] + previewBottom[1] + lensCenter[1])) { // pro IE6
			var newPosTop = e.pageY - lensTop[0] - lensCenter[0];
			var newPosLeft = e.pageX - lensTop[1] - lensCenter[1];
			if (e.pageY < (lensTop[0] + lensCenter[0])) newPosTop += (lensCenter[0] + lensTop[0] - e.pageY);
			else if (e.pageY > (lensTop[0] + previewBottom[0] - lensCenter[0])) newPosTop -= (e.pageY - lensTop[0] - previewBottom[0] + lensCenter[0]);
			if (e.pageX < (lensTop[1] + lensCenter[1])) newPosLeft += (lensCenter[1] + lensTop[1] - e.pageX);
			else if (e.pageX > (lensTop[1] + previewBottom[1] - lensCenter[1])) newPosLeft -= (e.pageX - lensTop[1] - previewBottom[1] + lensCenter[1]);
			maginfier.css('top',newPosTop+'px').css('left',newPosLeft+'px');
			zoom.css('background-position', Math.round(-newPosLeft*ratio) +'px '+ Math.round(-newPosTop*ratio) +'px');
		}
	}

	var image = new Image();
	$(image).load(function () {
		zoom.css('background-image','url(' + src + ')');
		ratio = $(image).attr('height') / preview.find('img').height();
		maginfier.mousemove(function(e){ moveViewport(e); });
		preview.mousemove(function(e){ moveViewport(e); });
	}).attr('src', src);
}

/**
 * Image changer
 * @author DaVee
 * @version 0.8
 * @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 >= 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.change(i+1)}, changer.timer);
		}).error(function () {
			setTimeout(function(){changer.change(i+1)}, changer.timer);
		}).attr("src", changer.images[i]);
	}

	this.run = function () {
		setTimeout(function(){changer.change(1)}, changer.timer);
	}
}


function panelTech(panel, target) {
	var list = $(panel).find("img");
	if (list.length != 0) {
		$(list).each( function (img) {
			$(this).hover(
				function () {
					$("#target-text").stop(true,true).hide();
					$(target).html( '<div id="target-text">' + $(this).parent().children(".hidden").html() + "</div>" );
					$("#target-text").fadeIn();
				},
				function () {
					$("#target-text").fadeOut( function () {
						$(target).html("");
					});
				}
			);
		});
	}
}


function addLink(parent,target) {
	$(parent).each( function () {
		var link = $(this).find("a");
		$(this).children(target).before('<a href ="'+$(link).attr("href")+'"></a>');
		$(this).children("a").append($(this).children(target)).hover( function() {
			$(link).addClass("hover"); }, function() { $(link).removeClass(); }
		);
	});
}
