function rx_gallery () {
	var thumb_active = 1, // selected item
			thumb_slide_act = 1, // thumbs slide position
			thumb_width = false, // thumb outer width
			thumb_list = [], // array of thumb nodes
			thumb_min = 10, // min no of thumbs
			thumb_scroll = 5, // scroll thumbs - thumb_min / 2
			thumb_max = 30, // max no of thumbs

			node_pic = $('.w-picture').children('div'), // big pictures wrap
			node_pic_vis = false, // big first picture
			node_pic_hid = false, // big second picture
			node_pic_vis_p = false,
			node_pic_hid_p = false,
			node_preload = $('.q-hidden'), // preloader
			node_thumb = $('.w-thumbnail');

			pic_id_list = [], // array of thumb image id's;
			pic_id_len = 2, // length of id. 01-99, 2 digits
			pic_count = false, // number of images in gallery
			pic_ext = '.jpg', // picture file extensiton
			pic_width = '673', // picture width

			file_thumb =  _file_thumb; // 'flo_img_thumbnails/', // thumb folder name
			file_pic = _file_pic; //'images/', // picture folder name

			c_select = 'selected', // class for selected item
			c_item = 'item-', // li item class
			c_slide = 'fx-slide',
			c_hidden = 'fx-hidden',
			c_opacity = 'fx-opacity',
			c_first = 'fx-first',

			fx_busy = false;


	this.init = function() {
		var i, j, k, l, n, m;

		// add classes to thumb li
		n = $('.w-thumbnail');
		i = 0;
		j = n.children('li');
		j.each( function() {
			k = (i>thumb_max) ? 'item-delete' : 'item-' + leading_zero(++i);
			$(this).addClass(k);
		});

		// remove exceed thumbnails
		n.children('.item-delete').remove();

		// add missing thumbnails
		if (j.size() < thumb_min) {
			l = thumb_min - j.size() - 1;
			for (i=0; i<thumb_min - j.size(); i++) {
				k = document.createElement('li');
				k.className= c_item + leading_zero(l++) ;
				n.append(k);
			}
		}

		// cache thumbs li to array
		i = 0;
		k = file_thumb.length;

		n.children('li').each(function() {
			thumb_list[++i] = $(this);
			// cache thumb img ids to array
			if (j = $(this).find('img').attr('src')) {
				pic_id_list[i] = file_pic + j.substr(k);

				l = document.createElement('img');
				l.src = pic_id_list[i];
				node_preload.append(l);

				//
			}
		});
		pic_count = pic_id_list.length;

		// set width for ul
		thumb_width = n.children('li').outerWidth(true);
		$('.w-thumbnail').css('width', thumb_list.length * thumb_width );

		// set active item
		thumb_list[thumb_active].addClass(c_select);

		i = document.createElement('p');
		i.setAttribute('class', c_first);
		node_pic.append(i);

		node_pic_vis_p = node_pic.children('p');
		i = document.createElement('img');
		i.src = get_pic_url(thumb_active);
		node_pic_vis_p.append(i);
		node_pic_vis = node_pic_vis_p.children('img');

		i = document.createElement('p');
		i.setAttribute('class', c_hidden);
		node_pic.append(i);

		node_pic_hid_p = node_pic.children('p:last');
		i = document.createElement('img');
		i.src = get_pic_url(thumb_active) ;
		node_pic_hid_p.append(i);
		node_pic_hid = node_pic_hid_p.children('img');

		$('.z-picture a[href$="#navy-prev"]').click(navy_picture_prev);
		$('.z-picture a[href$="#navy-next"]').click(navy_picture_next);
		$('.z-thumbnail a[href$="#navy-prev"]').click(navy_thumb_prev);
		$('.z-thumbnail a[href$="#navy-next"]').click(navy_thumb_next);
		$('.s-thumbnail').click(function(event) { navy_thumb(event); } );

		node_pic.hover(
			function() {
				if (i = get_thumb_url(thumb_active)) {
					j = $(this).children('a');
					j.attr('href', i);
					j.css('cursor', 'pointer');
					j.animate( { opacity:1 }, 'fast' );
				}
			},
			function() {
				i = $(this).children('a');
				i.attr('href', '#')
				i.css('cursor', 'default');
				i.animate( { opacity:0 }, 'fast' );
			}
		);
	}
	//

	function navy_thumb(event) {
		var node = $(event.target),
				n_li = node.parents('li'),
				i, j;

		if (node.is('img') ) {
			event.preventDefault();

			if ( n_li.hasClass(c_select) ) { return; }

			i = get_thumb_id_to_class( n_li.attr('class') );
			fx_transition('opacity', thumb_active, i);
			return false;
		}

		return false;
	}
	//

	function navy_picture_prev() {
		if ( thumb_active - 1 > 0 ) {
			fx_transition('slide-prev', thumb_active, thumb_active - 1);
		}
		return false;
	}
	//

	function navy_picture_next() {
		if ( thumb_active + 1 < pic_count ) {
			fx_transition('slide-next', thumb_active, thumb_active + 1);
		}
		return false;
	}
	//

	function navy_thumb_prev() {
		var i;

		if (thumb_slide_act > 1) {
			i = (pic_count-1) - thumb_min ;
			i = (i > thumb_scroll) ? thumb_scroll : i;
			i = i * thumb_width;

			thumb_slide_act -- ;

			$('.s-thumbnail').animate(
					{ scrollLeft : '-=' + i },
					'normal'
			);

		}
		return false;
	}
	//

	function navy_thumb_next() {
		var i;

		if (pic_count > thumb_min * thumb_slide_act) {
			i = (pic_count-1) - thumb_min ;
			i = (i > thumb_scroll) ? thumb_scroll : i;
			i = i * thumb_width;

			thumb_slide_act ++ ;

			$('.s-thumbnail').animate(
					{ scrollLeft : '+=' + i },
					'normal'
			);

		}
		return false;
	}
	//

	// switch selected item in thumbs
	function switch_active_item(old_id, new_id) {
		thumb_list[old_id].removeClass(c_select);
		thumb_list[new_id].addClass(c_select);
		return true;
	}
	//

	//
	function get_thumb_id_to_class(css_class) {
		return parseFloat(css_class.substr(c_item.length));
	}
	//

	function get_thumb_url(pic_id) {
		var node;
		node = node_thumb.find('.'+c_item + leading_zero(pic_id)).find('a');
		if (node.attr('hash') != '') { return node.attr('hash'); }

		return false;
	}

	// get pic url from thumb_active
	function get_pic_url(pic_id) {
		var node = node_thumb.find('.'+c_item + leading_zero(pic_id)).find('img');
		return file_pic + node.attr('src').substr(file_thumb.length) ;
	}
	//

	// transitions between pictures
	function fx_transition(trans_id, old_id, new_id) {
		function cleanup() {
			node_pic_vis_p.removeClass(c_first);
			node_pic_vis_p.addClass(c_hidden);
			node_pic_vis_p.css('left', '0px');
			node_pic_vis_p.css('opacity', '1');

			node_pic_hid_p.addClass(c_first);
			node_pic_hid_p.removeClass(c_opacity);
			node_pic_hid_p.removeClass(c_slide);
			node_pic_hid_p.css('left', '0px');
			node_pic_hid_p.css('opacity', '1');

			i = node_pic_vis_p;
			node_pic_vis_p = node_pic_hid_p;
			node_pic_hid_p = i;

			i = node_pic_vis;
			node_pic_vis = node_pic_hid;
			node_pic_hid = i;

			switch_active_item( old_id, new_id);
			thumb_active = new_id;

			fx_busy = false;
		}
		//

		var i;


		if (fx_busy) { return false; }
		fx_busy = true;

		switch (trans_id) {
			case 'opacity':
				// alert( get_pic_url(new_id) );
				node_pic_hid.attr('src', get_pic_url(new_id) );

				node_pic_hid_p.addClass(c_opacity);
				node_pic_hid_p.removeClass(c_hidden);

				node_pic_vis_p.animate(
						{ opacity: 0 }, 'normal',
						function() {
							node_pic_hid_p.animate(
									{ opacity: 1 }, 'normal', cleanup
							);
						}
				);


				break;

			case 'slide-next':
				node_pic_hid.attr('src', get_pic_url(new_id) );

				node_pic_hid_p.addClass(c_slide);
				node_pic_hid_p.css('left', pic_width+'px');
				node_pic_hid_p.removeClass(c_hidden);

				node_pic_vis_p.animate( { left: '-='+pic_width }, 'normal' );
				node_pic_hid_p.animate( { left: '-='+pic_width }, 'normal', cleanup );
				break;

			case 'slide-prev':
				node_pic_hid.attr('src', get_pic_url(new_id) );
				node_pic_hid_p.addClass(c_slide);
				node_pic_hid_p.css('left', -pic_width+'px');
				node_pic_hid_p.removeClass(c_hidden);

				node_pic_vis_p.animate( { left: '+='+pic_width }, 'normal' );
				node_pic_hid_p.animate( { left: '+='+pic_width }, 'normal', cleanup );
				break;

			default :

				break;

		}
	}
	//

	// add leading zeroes
	function leading_zero(v) {
		if (parseInt(v)<10) return ('0'+parseInt(v));
		return v;
	}
	//

	function echo(v) {
		$('#debug').html(v);
	}

}

$(document).ready(function() {
	var gallery = new rx_gallery();

	gallery.init();

});

