(function($) {
	jQuery.fn.galleryslide = function(options) {

		var opts = jQuery.extend({}, $.fn.galleryslide.defaults, options);
		var animating = false;
		delay = opts.delay;
		autoplay = opts.autoplay;
		loop = opts.loop;
		bubbleElements = opts.bubbleElements;
		slideimages = this;
        slideimages.length=slideimages.length/2;

        if (slideimages.length == 1) {
			$(slideimages[0]).show();
		}
		if (slideimages.length < 2) {
			return false;
		}
		navarea = $(opts.navigation);

		/* generate navigation */
        /*
		for (i = 0; i < slideimages.length; i++) {
			$(opts.navigation).append("<li><a href='#'>" + $(slideimages).find("img").attr("title") + "</a></li>")
		}
        */
		navigators = $(opts.navigation).find("a");
		activeNav = $(navigators[0]).parent().addClass("active");
		activeArea = $(slideimages[0]).addClass("visible");


		$(navigators).click( function() {
            navarea.fadeOut();
			if (animating) {
				return false
			}
			/* remember indexes before change */

			var toShow = $(navigators).index(this);
			var toHide = $(navigators).index($(activeNav).find("a"));
			/* check is there is something to change */

			if (toShow == toHide) {
				return false
			}

			animating = true;

			/* change active navigator */

			$(activeNav).removeClass("active");
			activeNav = $(this).parent().addClass("active");

			/* handlers and private vars */

			var toShowObj = $(slideimages[toShow]);
			var toHideObj = $(slideimages[toHide]);

			var toHideHeight = $(toHideObj).height();
			/* which way, commander? */

			if (toShow < toHide) {
				toHideHeight = 0 - toHideHeight
			}

			/* animation */

			if (bubbleElements != "") {
				bubbles = $(toShowObj).find(bubbleElements).each( function() {
					$(this).css("opacity", "0").css("visibility", "hidden")
				})
			}

			$(toShowObj).css("top", toHideHeight);
			$(toShowObj).css("display", "block");
			cntHeight = 0;
			$(toShowObj).children().each( function() {
				cntHeight = cntHeight + $(this).height()
			})

			//$(slideimages).parent().height(cntHeight+11).parent().height(cntHeight); // ,,God grant me the serenity to accept the things I cannot change`` | EVILSTUFF!

			$(toShowObj).animate({
				opacity: 1,
				top: 0
			}, {
				duration: 500,
				queue: false,
				easing: "swing",
                complete: function() {
                    navarea.fadeIn();
                }
			});
			$(toHideObj).animate({
				opacity: 0
			}, {
				duration: 500,
				queue: false
			}).animate({
				top: -toHideHeight
			}, {
				duration: 800,
				queue: false,
				complete: function() {
					$(toHideObj).removeClass("visible");
					animating = false;
					if (autoplay) {
						clearTimeout(myTimer);
					}

					if (bubbleElements != "") {
						$(bubbles).each(function() {
							$(this).css("visibility", "visible")
							rnd = (parseInt(Math.random(5) * 10) + 1) * 100;

							$(this).animate({
								opacity: 1
							}, {
								duration: rnd,
								queue: true,
								complete: function() {
									$(this).css("zoom", "0")
								}
							})
						})
					}


					if (autoplay) {
						myTimer = setTimeout(autoNext, delay)
					}

				}
			});

			$(toShowObj).addClass("visible");

			return false;

		})

		/* autoplay */

		if (!autoplay) {

			return false

		}

		myTimer = setTimeout(autoNext, delay)

	};

	autoNext = function() {

		act = $(navigators).index($(activeNav).find("a"));

		if ((act + 1) != slideimages.length) {
			$(activeNav).next().find("a").click()
		} else {

			if (loop) {
				$(navigators[0]).click()
			}

		}

		myTimer = setTimeout(autoNext, delay)
	}

	/* defaults */

	jQuery.fn.galleryslide.defaults = {
		navigators : '',  // container in which navigation links should be generated (jQuery object)
		speed : 5,    // nothing yet ;-) TODO -- speed of animation
		autoplay: false,  // should the sliding start automatically? (boolean)
		loop: false,   // should it loop? (boolean)
		delay: 10000,   // delay between changing photos in (msecs)
		bubbleElements: ''  // elements inside that should be animated separately (jQuery selector, i.e 'p,ul')
	};
})(jQuery);
