
	var timeout = false;
	var fallbackstart = 0;
	var complete = 0;	
	var vmax = 15;	
	var sizes = Array();					
	var ready = false;

	/* encoding links */
	
	function encodeHEX(n) {
		window.location="\x6D\x61\x69"+"\x6C\x74\x6F\x3A"+n+"\x40"+"\x62\x6c\x6f\x6b\x65\x73"+'\x2E'+'\x63\x68';
	}

	/* global onload script */
	
	function alignFooter() {
		if ($('left').getSize().y > $('background').getSize().y) {
			$('background').setStyle('height', ($('left').getSize().y + 50)+"px");
		}
		alignLine();
		window.addEvent('domready', function() {
		   initAnim();
		});
	}

	/* resize right line onload and onresize */

	function alignLine() {
		$('background2').setStyle('height', ($('background').getSize().y)+"px");
	}

	/* init animation */

	function initAnim() {
		$('cntinner')._cursorpos = Array();			

		/* make draggable */
		$('popup').makeDraggable();			
		/* close button */
		$('closebut').addEvent('click', function(){
		    closepop();
		});	
		/* popup link events */
		$(document.body).getElements('.thumb').addEvents({
		    'click': function(){
		        openimg(this.alt);
		    }
		});
		/* container move events */
		$('cnt').addEvents({
			'mousedown': function(event) {
				$('cntinner').pausing = true;
			}.bind(this),
			'mouseup': function(event) {
				$('cntinner').pausing = false;
			}.bind(this),										
			'mousemove': function(event) {
				$('cntinner')._cursorpos = Array(event.page.x - $('cnt').getPosition().x,event.page.y - $('cnt').getPosition().y);
				if (!timeout) {timeout = setInterval(function(){
					animate ($('cntinner'));
				},20);}
			}.bind(this),
			'mouseleave': function(event) {
				clearInterval(timeout);
				timeout = false;
			}.bind(this),
		});			
	}

	/* show the popup, run the animation */

	function openimg(the_path) {		
		/* reading index from url */
		var ind = parseInt(the_path.split('/').getLast());
		/* setting the style of the background */
		$('container').setStyle('height', window.getScrollSize().y+"px");
		$('container').setStyle('width', window.getScrollSize().x+"px");
		$('container').setStyle('display', 'block' );
		sizes = Array();
		ready = false;
		/* ajax-call: the image list */
		var req = new Request.HTML(
			{
			url:the_path, 
			method: "get", 
			onComplete: function(tree,code,html) {
				/* html complete */
				$('cntinner').set('html',html);
				$('cntinner').gallerywidth = 0;
				$('cntinner').xoffset = 1;
				$('cntinner').pausing = false;
				$('cntinner').setStyle('width', "10000px");
				$('cntinner').setStyle('visibility', "hidden");
				$('popup').setStyle('visibility', 'visible' );
				complete = 0;
				/* looping thru the images */
				var arr = $('cntinner').getElements('img');
				arr.each(function(elm){
					elm.addEvents({
						/* counting the loaded ones */
						'load': function(event) {
							$('cntinner').gallerywidth += elm.getParent().getSize().x;
							sizes[complete] = $('cntinner').gallerywidth;
							/* Starting with offset if not 0 */
							if (ind==complete) fallbackstart = $('cntinner').xoffset = ($('cnt').getSize().x-$('cntinner').gallerywidth>0)?(0):($('cnt').getSize().x-$('cntinner').gallerywidth);
							complete++;

							/* If all loaded: Go! */
							if (complete==arr.length) {
								$('cntinner').setStyle('width', $('cntinner').gallerywidth + "px");
								$('cntinner').setStyle('visibility', "visible");
								$('cntinner').setStyle('margin-left', fallbackstart);
								$('debug').set('html', (ind+1) + " / " + (complete))			
								ready = true;
							}
						}.bind(this)
					});							
				},this);
			},
			onFailure: function() {
				$('popup').set('text', 'The request failed.');
			}
		});
		req.send();
	}
	
	/* closing action: hiding layers, clearing timeout */
	
	function closepop() {
		$('cntinner').getChildren().destroy();
		$('container').setStyle('display', 'none' );
		$('popup').setStyle('visibility', 'hidden' );	
		clearInterval(timeout);
		timeout = false;					
		$('debug').set('html',"")					
	}

	/* do the motion */

	function animate (obj) {
		if (ready) {
			if (!obj.pausing) {
				var xmax = parseInt(obj.getParent().getSize().x);
				var vcur = Math.round(((vmax*2) / xmax * obj._cursorpos[0]) - vmax);
				if (obj.xoffset<=0 && obj.xoffset>=xmax-obj.gallerywidth) obj.xoffset -= vcur;
				if (obj.xoffset>0) obj.xoffset=0;
				if (obj.xoffset<xmax-obj.gallerywidth) obj.xoffset=xmax-obj.gallerywidth;
				obj.setStyle('margin-left', obj.xoffset);
			}
			for (var k=0; k < sizes.length; k++) {
				 if(0-sizes[k]+(xmax/2)<=obj.xoffset) break;

			}
			$('debug').set('html', (k+1) + " / " + (complete))			
		}
	}
	
