







	_positioner = {

		init: function() {

			if($$('.pagePrevious').length > 0) {

				if(_pdir > 0) {
					initLeft = 'left: -3000px;';
					endLeft = 'left: 3000px;';
				}
				else {
					initLeft = 'left: 3000px;';
					endLeft = 'left: -3000px;';
				}

				_positioner.pageActive = $$('.pageActive')[0];
				_positioner.pageActive.hide();
				_positioner.pageActive.setStyle(initLeft);
				_positioner.pageActive.show();
				new Effect.Morph(_positioner.pageActive, {
					style: 'left: 0;',
					duration: 1.3,
					transition: Effect.Transitions.EaseFromTo
				});

				_positioner.pagePrevious = $$('.pagePrevious')[0];
				new Effect.Morph(_positioner.pagePrevious, {
					style: endLeft,
					duration: 1.3,
					delay: 0.3,
					transition: Effect.Transitions.EaseFromTo,
					afterFinish: function() { _positioner.pagePrevious.remove(); }
				});

			}

		}

	};








	_gallery = {

		init: function(which, item) {

			_gallery.category = which;
			_gallery.step = 0;
			_gallery.running = true;
			_gallery.loaded = false;

			_gallery.autostartindex = (item) ? item : false;

			$('preview_'+_gallery.category).fade({
				duration: 0.2,
				to: 0.3
			});

			new Insertion.Before($('preview_'+_gallery.category), '<span id=\"loadingcontainer\"><span id=\"innerloading\"><img src=\"/imgs/loading.gif\" alt=\"\" id=\"loading\" /><br /><span id=\"loadingpercent\">0 %</span></span></span>');

			_gallery.preload();

		},

		preload: function() {

			new Ajax.Request('/', {
				parameters: 'action=imgarray&id='+_gallery.category,
				onSuccess: function(t) {
					e = eval('('+t.responseText+')');
					if(e.state == 1) {

						_gallery.imgs = new Hash(e.array.evalJSON());
						_gallery.imgs.length = parseInt(e.arraylength);
						_gallery.imgs.maxheight = parseInt(e.imgmaxheight);
						_gallery.imgs.firstwidth = parseInt(e.imgfirstwidth);
						_gallery.imgs.totalwidth = parseInt(e.imgtotalwidth);
						_gallery.preloading();

					}
				}
			});

		},

		preloading: function() {

			_gallery.imgs.each(function(a) {

				window['galpreloadimg'+a[0]] = new Image();
				window['galpreloadimg'+a[0]].src = a[1];

			});

			_gallery.loadedimgs = 0;

			_gallery.preloadingprogressInterval = window.setInterval(function() {
				_gallery.preloadingprogress();
			}, 100);

		},

		preloadingprogress: function() {

			_gallery.imgs.each(function(a) {

				if(window['galpreloadimg'+a[0]].complete) {
					window['galpreloadimg'+a[0]] = false;
					_gallery.loadedimgs++;
				}

				if(_gallery.loadedimgs == _gallery.imgs.length) {
					_gallery.finishpreloading();
				}

			});

			_gallery.updatepercentage();

		},

		finishpreloading: function() {

			if(_gallery.preloadingprogressInterval) {
				window.clearInterval(_gallery.preloadingprogressInterval);
			}

			window.setTimeout(function() {

				if($('loadingcontainer')) {

					$('loadingcontainer').remove();

					$$('#innercontainer .page').each(function(a) {
						a.fade({
							duration: 0.8
						});
					});

					window.setTimeout(function() {
						$('preview_'+_gallery.category).appear({
							duration: 0.2
						});
					}, 1000);

					new Insertion.Before('container', '<div id="detailcontainer" style="display: none;"><a href="#" id="detailclose" onclick="_gallery.close(); return false;" style="top: -67px;" title="close">&nbsp;</a><a href="#" id="detailprev" onclick="_gallery.prev(); return false;" style="left: -123px;" title="previous">&nbsp;</a><div id="detailimgs"><div id="detailimgsinner" style="display: none;"></div></div><a href="#" id="detailnext" onclick="_gallery.next(); return false;" style="right: -173px;" title="next">&nbsp;</a><div id="detailtrack"><div id="detailhandle">&nbsp;</div></div></div>');

					imgboxes = '';
					_gallery.imgs.each(function(a,b) {
						imgboxes += '<span class="imgbox"><img src="'+a[1]+'" alt="" /><span class="imgcounter">'+(b+1)+'/'+_gallery.imgs.length+'</span></span>';
					});

					$('detailimgsinner').update(imgboxes);

					$('detailimgsinner').setStyle('left: -2000px; height: '+_gallery.imgs.maxheight+'px; margin-top: -'+Math.round(_gallery.imgs.maxheight/2)+'px;');

					$('detailcontainer').appear();

					window.setTimeout(function() {
						$('detailimgsinner').appear({
							duration: 0.5
						});
					}, 800);

					new Effect.Morph('detailimgsinner', {
						duration: 2,
						transition: Effect.Transitions.EaseFromTo,
						style: 'left: 0px',
						afterFinish: function() {

							_gallery.shownavigator('next');

							new Effect.Morph('detailclose', {
								style: 'top: 0px;',
								duration: 0.3
							});

							if(_gallery.autostartindex) {
								_gallery.slider.setValue(_gallery.autostartindex);
							}

						}
					});

					$('detailimgs').setStyle('left: '+Math.round((document.viewport.getWidth()-_gallery.imgs.firstwidth)/2)+'px;');


					window.setTimeout(function() {

						_gallery.slider = new Control.Slider('detailhandle', 'detailtrack', {
							axis: 'horizontal',
							minimum: 0,
							maximum: (_gallery.imgs.length),
							range: $R(0,(_gallery.imgs.length-1)),
							onSlide: function(u) {

								new_u = Math.round(u);

								if(Prototype.Browser.Gecko) Element.setStyle('detailhandle', { 'cursor': '-moz-grabbing' });
								if(Prototype.Browser.Gecko) Element.setStyle('detailtrack', { 'cursor': '-moz-grabbing' });

								sliderOffset = (document.viewport.getWidth()-_gallery.imgs.firstwidth)/2;
								sliderOffset2 = (document.viewport.getWidth()/1.975) + ($$('.imgbox')[(_gallery.imgs.length-1)].getWidth()/1.975) - sliderOffset;

								scrollToTmp = Math.round(-(u/(_gallery.imgs.length-1))*(_gallery.imgs.totalwidth-sliderOffset2) + sliderOffset);
								_gallery.scrollmanually(scrollToTmp);

							},
							onChange: function(u) {

								nachkomma = (u - Math.floor(u)) * 10;

								new_u = Math.floor(u);

								if(nachkomma > 3) {
									new_u++;
								}

								handleCurrentPos = parseInt($('detailhandle').getStyle('left'));

								if(Prototype.Browser.Gecko) Element.setStyle('detailhandle', { 'cursor': '-moz-grab' });
								if(Prototype.Browser.Gecko) Element.setStyle('detailtrack', { 'cursor': 'default' });

								_gallery.directprevnext(new_u);


								if(u % 1 != 0) {

									_gallery.slider.setValue(new_u);

									handleNewPos = Math.round((379/(_gallery.imgs.length-1))*new_u);

								}

							}
						});

						Event.observe($('detailcontainer'), 'mousewheel', _gallery.mousescroll, false);
						Event.observe($('detailcontainer'), 'DOMMouseScroll', _gallery.mousescroll, false); // Firefox

						_gallery.loaded = true;

					}, 100);


				}

			}, 500);

		},

		updatepercentage: function() {

			percent = Math.round((_gallery.loadedimgs/_gallery.imgs.length)*100);
			$('loadingpercent').update(percent + ' %');

		},

		next: function() {

			if((_gallery.step+1) < _gallery.imgs.length) {
				_gallery.step++;
			}

			_gallery.slider.setValue(_gallery.step);


		},

		prev: function() {

			if((_gallery.step-1) >= 0) {
				_gallery.step--;
			}

			_gallery.slider.setValue(_gallery.step);

		},

		directprevnext: function(step) {

			_gallery.step = step;

			if(_gallery.step >= 0 && _gallery.step < _gallery.imgs.length) {

				if(_gallery.step == 0) {
					_gallery.scrolldetailimgs(Math.round((document.viewport.getWidth()-$$('.imgbox')[_gallery.step].getWidth())/2));
				} else {
					_gallery.scrolldetailimgs(-($$('.imgbox')[_gallery.step].offsetLeft)+Math.round((document.viewport.getWidth()-$$('.imgbox')[_gallery.step].getWidth())/2));
				}

				_location.set(_gallery.step+1);

			}

		},

		scrolldetailimgs: function(to) {

			if(_gallery.step <= 0) {
				_gallery.hidenavigator('prev');
			}
			else {
				_gallery.shownavigator('prev');
			}

			if(_gallery.step >= _gallery.imgs.length-1) {
				_gallery.hidenavigator('next');
			}
			else {
				_gallery.shownavigator('next');
			}

			if(_gallery.scrolldetailimgs_effect) {
				_gallery.scrolldetailimgs_effect.cancel();
			}

			_gallery.scrolldetailimgs_effect = new Effect.Morph('detailimgs', {
				duration: 0.6,
				transition: Effect.Transitions.EaseFromTo,
				style: 'left: '+to+'px'
			});

		},

		scrollmanually: function(to) {

			$('detailimgs').setStyle('left: '+to+'px;');

		},

		hidenavigator: function(which) {

			slctr = (which == 'prev') ? 'left' : 'right';
			amnt = (which == 'prev') ? 123 : 173;

			if($('detail'+which)) {

				new Effect.Morph('detail'+which, {
					style: slctr + ': -'+amnt+'px;',
					duration: 0.3
				});

			}

		},

		shownavigator: function(which) {

			slctr = (which == 'prev') ? 'left' : 'right';

			if($('detail'+which)) {

				new Effect.Morph('detail'+which, {
					style: slctr + ': 0px;',
					duration: 0.3
				});

			}

		},

		reposition: function() {

			if(_gallery.loaded) {
				_gallery.directprevnext(_gallery.step);
			}

		},

		mousescroll: function(e) {

			w = Event.wheel(e)/10;

			if(w > 0) {
				newSliderIndex = _gallery.next();
			}
			else {
				newSliderIndex = _gallery.prev();
			}

		},

		keynav: function(e) {

			if(_gallery.running && (_gallery.step || _gallery.step == 0)) {

				key = (e.keyCode) ? e.keyCode : e.which;

				if(key == Event.KEY_LEFT) { _gallery.prev(); }
				if(key == Event.KEY_RIGHT) { _gallery.next(); }
				if(key == Event.KEY_HOME) { _gallery.slider.setValue(0); }
				if(key == Event.KEY_END) { _gallery.slider.setValue(_gallery.imgs.length-1); }
				if(key == Event.KEY_ESC) { _gallery.close(); }

			}

		},

		close: function() {

			if(_gallery.running) {

				_gallery.running = false;

				Event.stopObserving($('detailcontainer'), 'mousewheel', _gallery.mousescroll, false);
				Event.stopObserving($('detailcontainer'), 'DOMMouseScroll', _gallery.mousescroll, false); // Firefox

				$('detailcontainer').fade({
					duration: 0.5,
					afterFinish: function() {
						$('detailcontainer').remove();
					}
				});

				$$('#innercontainer .page').each(function(a) {
					a.appear({
						duration: 0.5
					});
				});

				_location.set('');

			}

		}

	};














	_location = {

		init: function() {

			_location.initlocation = _location.getcleanlocation();
			_location.checkinterval = window.setInterval(_location.check, 200);
			_location.handlers = new Hash();

		},

		set: function(which) {

			window.location.href = _location.initlocation = _location.getcleanlocation() + '#' + which;

		},

		getcleanlocation: function() {

			return window.location.href.replace(/\#.+/,"");

		},

		getidentifier: function() {

			var locID;
			loc = window.location.href;
			hashPos = loc.indexOf('#');

			if(hashPos != -1) {

				tmpLocID = loc.substring((hashPos+1));
				locID = tmpLocID;

				}

			if(locID == '') { return false; }
			else { return locID; }

		},

		check: function() {

			if(window.location.href != _location.initlocation) {
				_location.initlocation = window.location.href;
				_locationapp();
			}

		}

	};











	_locationapp = function() {

		_lid = _location.getidentifier();

		if(_gallery.running) {

			if(!isNaN(_lid)) {

				_lid = _lid-1;

				if(_lid >= 0 && _lid < _gallery.imgs.length) {

					_gallery.slider.setValue(_lid);

				}

			}

		}

		else {

			activePage = $$('.pageActive')[0];

			if(activePage.hasClassName('pagetype_summary')) {

				activePageCat = activePage.id.replace(/page_/, '');

				if(!isNaN(_lid)) {

					_lid = _lid-1;
					_gallery.init(activePageCat, _lid);

				}

			}

		}

	};











	Object.extend(Event, {
		wheel:function (event){
			var delta = 0;
			if (!event) event = window.event;
			if (event.wheelDelta) {
				delta = event.wheelDelta/120;
				if (window.opera) delta = -delta;
			} else if (event.detail) { delta = -event.detail/3;	}
			return Math.round(delta); //Safari Round
		}
	});









	document.observe('dom:loaded', function() {

		_positioner.init();

		_location.init();

		Event.observe(window, 'resize', function() { if(_gallery.running) { _gallery.reposition(); } });

		Event.observe(window, 'keypress', _gallery.keynav);

	});









