(function($){

        jqGabryGaleriaAkcio = 0;

	jqGabryGaleriaImages = new Array();

	jqGabryGaleriaThumbs = new Array();

        jqGabryGaleriaAktiv = 0;

        jqGabryGaleriaAutoPlay = 0;

        $.fn.jqGabryGaleriaAktivFadeOut = function() {
		this.css({opacity:"0.7"}).
			stop().
			animate({opacity:"1"},500
		);
	};

        $.fn.jqGabryGaleriaAktivFadeIn = function() {
		this.css({opacity:"1"}).
			stop().
			animate({opacity:".7"},500);
	};

        jqGabryGaleriaPlayTimeout = function() {
		jqGabryGaleriaIrany = +1;
		if ( jqGabryGaleriaAktiv == jqGabryGaleriaImages.length-1 ) {
		        jqGabryGaleriaShow(0);
		} else {
		        jqGabryGaleriaNext();
		}
	};

        jqGabryGaleriaPlay = function() {
		clearTimeout(jqGabryGaleriaAutoPlay);
		jqGabryGaleriaAutoPlay = setTimeout( jqGabryGaleriaPlayTimeout, 5000 );
	};


        jqGabryGaleriaPrev = function() {
		jqGabryGaleriaIrany = -1;
		jqGabryGaleriaShow( jqGabryGaleriaAktiv-1 );
	};

        jqGabryGaleriaNext = function() {
		jqGabryGaleriaIrany = +1;
		jqGabryGaleriaShow( jqGabryGaleriaAktiv+1 );
	};

        jqGabryGaleriaShow = function(th) {

		if ( jqGabryGaleriaAkcio == 1 ) {
			setTimeout( "jqGabryGaleriaShow("+th+")",500);
			return false;
		}
		jqGabryGaleriaAkcio = 1;

	        var irany = 0;
		if (jqGabryGaleriaAktiv == th+1) {
			irany--;
		}
		if (jqGabryGaleriaAktiv == th-1) {
			irany++;
		}

		if ( jqGabryGaleriaAktiv >= 0 ) {
			jqGabryGaleriaThumbs[jqGabryGaleriaAktiv].jqGabryGaleriaAktivFadeIn();
		}

		jqGabryGaleriaAktiv = th;

		jqGabryGaleriaThumbs[jqGabryGaleriaAktiv].jqGabryGaleriaAktivFadeOut();

		if ( jqGabryGaleriaAktiv == 0 ) {
			$('.gSShowImage .left').hide(500);
		} else {
			$('.gSShowImage .left').show(500);
		}

		if ( jqGabryGaleriaAktiv == jqGabryGaleriaImages.length-1 ) {
			$('.gSShowImage .right').hide(500);
		} else {
			$('.gSShowImage .right').show(500);
		}

		var src = jqGabryGaleriaImages[th][0];

		var image = new Image();
		image.oldImage = $(".bigpic");
		// image.viewPic = this.th.viewPic;
		image.onload = function(){

			this.onload = null;
			var x = $(image);

			$(this.oldImage)[0].style.position = "absolute";
			var regiX2 = Math.floor(($(".gSShowImage")[0].offsetWidth-$(this.oldImage)[0].offsetWidth)/2);
			var regiY = Math.floor(($(".gSShowImage")[0].offsetHeight-$(this.oldImage)[0].offsetHeight)/2);
			var regiX = regiX2 - 100*irany;
			$(this.oldImage)[0].style.left = regiX2+"px";
			$(this.oldImage)[0].style.top = regiY+"px";


			$(image)[0].style.position = "absolute";
			var ujX = Math.floor(($(".gSShowImage")[0].offsetWidth-image.width)/2);
			var ujX2 = ujX + 100*irany;
			var ujY = Math.floor(($(".gSShowImage")[0].offsetHeight-image.height)/2);
			$(image)[0].style.left = ujX2+"px";
			$(image)[0].style.top = ujY+"px";

			$(image).css({opacity:"0"})
				.animate({opacity:"1","left":ujX},500 )
				.click( function() { 

					faPad0( jqGabryGaleriaThumbs[jqGabryGaleriaAktiv][0], faPad1, faPad2);
					return false;

//					$('#dialog').html('<img src="'+this.src.replace("s.j","b.j")+'">').jqmShow(); } );
					$('#dialog').html('<img src="'+this.src.replace("s.j","b.j")+'">');
				} );

			$('.gSShow .text').animate({ top: $('.gSShowImage').attr("offsetHeight") });
			$(".gSShowImage").append(image);

			$(this.oldImage).css({opacity:"1"})
				.animate({opacity:"0",left:regiX},500,
					function(t){
						$(this).remove();
						image.className = "bigpic";
						image.title = image.alt = jqGabryGaleriaImages[jqGabryGaleriaAktiv][1];
						if (jqGabryGaleriaImages[jqGabryGaleriaAktiv][1] == "") {
							$('.gSShow .text').hide();
						} else {
							$('.gSShow .text').show();
						}
						$('.gSShow .title').text(jqGabryGaleriaImages[jqGabryGaleriaAktiv][1]);
						$('.gSShow .description').text(jqGabryGaleriaImages[jqGabryGaleriaAktiv][2]);
						$('.gSShow .text')[0].style.width = "360px";
						$('.gSShow .text').animate( { top: $('.gSShowImage').attr("offsetHeight")-$('.gSShow .text').attr("offsetHeight") },
							function () {
								// var x = new Date();
								// $('#debuig').append("<br /> start - "+x);
								if (jqGabryGaleriaAutoPlay) jqGabryGaleriaPlay();
								jqGabryGaleriaAkcio = 0;
							})
						});

		}; // end : image.onload 
		$(image).attr("src",src);
		return false;
	},

        $.fn.jqGabryGaleria = function(options){

		return this.each(function(i){
// i - sorszám
			var el = this;
// alert($(this).innerHTML)

// nagy képhez:
//  előző és következőp gombok

// kis képek:
			el.viewPic = $(this).find('.gSShowImage:first');
//alert(el.viewPic)
			// el.viewPic.html("");

			el.viewThumbs = $(this).find('.gSShowThumbs:first');
			// el.viewThumbs.style.height = "200px";
//			el.viewThumbs.css("class","gSShowThumbsJS");
			el.viewThumbs.addClass("gSShowThumbsJS");


//			el.viewThumbs.style.overflowX = "hidden";
//			el.viewThumbs.style.overflowY = "auto";

			el.viewPic.append("<a class='left'></a>");
			el.viewPic.append("<a class='right'></a>");
			el.viewPic.append("<div class='text'><div class='title'></div><div class='description'></div></div>")

			$('.gSShow .text').css("opacity",".5").attr("width", $('.gSShow').attr("offsetWidth") );

			$('body').append('<div class="jqmWindow" id="dialog"></div>');

// $('#dialog').jqm({overlay: 80});

			$(".gSShowImage a").css({opacity:"0.2"})
						.mouseover(
							function(){
								$(this)
//									.stop()
									.animate({opacity:".7"},200);
							})
						.mouseout(
							function(){
								$(this)
//									.stop()
									.animate({opacity:".2"},200);
							})
						.click(
							function(){
								if (this.className == "left") {
									jqGabryGaleriaPrev();
								}
								if (this.className == "right") {
									jqGabryGaleriaNext();
								}
								return false;
							});


			el.thumbs = $(this).find('.gSShowThumbs').find('a');
			el.thumbs.each(
				function(i) {
					this.th = $(this);
					this.thumb = el;
					this.kep = i;

					jqGabryGaleriaThumbs[i] = this.th;

					var src = this.th.attr("href").replace("b.jpg","s.jpg");
					jqGabryGaleriaImages[i] = new Array(src, this.th.find('img')[0].title, this.th.find('img')[0].alt);

					this.ggIndex = i;

					this.th
						.css({opacity:"0.7"})
						.mouseover(
							function(){
								var $f = $(this);
								if (jqGabryGaleriaAktiv == this.ggIndex) return;
								$f.jqGabryGaleriaAktivFadeOut();
							})
						.mouseout(
							function(){
								var $f = $(this);
								if (jqGabryGaleriaAktiv == this.ggIndex) return;
								$f.jqGabryGaleriaAktivFadeIn();
							})
						.click(
							function() {
								jqGabryGaleriaShow( this.ggIndex );
								return false;
							}
						);
				}

			);

			jqGabryGaleriaShow(0);

			jqGabryGaleriaPlay();

		});

	}
})(jQuery);