var _randomPicTime = 10000;
var _fadeSpeed = 800;
var _playtime = 5000 + _fadeSpeed;
var _redirect = false;


 var blank = new Image();
 blank.src = 'images/blank.gif';


(function($) {
	$.fn.extend({
		setSrc:		function(src, callback) {
						if (!$.browser.opera)
							this.attr('loadsrc', src).load(function(){ callback(this); });
						else
							this.attr('src', src);
			
						return this;
					},
		
		loadSrc: 	function() {
						var src = this.attr('loadsrc');
						this.removeAttr('loadsrc').attr('src', src);
					},
					
		fadeInX:	function(speed, callback) {
						
						if ($.browser.msie)
						{
							if (callback)
								callback();
							var t = this.show();
							t.width(); 
							return t;
						}
						return this.fadeIn(speed, function() { 

							if (callback !== undefined)
								callback(); 
						});
					},
					
		fadeOutX:	function(speed, callback) {
			
						if ($.browser.msie)
						{
							if (callback)
								callback();
							return this.hide();
						}
			
						return this.fadeOut(speed, function() { 
							if (callback !== undefined)
								callback(); 
						});
					}
	});
})(jQuery);


$(document).ready(function(){

	var _scale = 1;
	var _scaleNav = 1;
	var _buttonsize = 12;	
	
	//----------------------------------------------------------------------------------------------------
	
	
	var main = {
		container:	$('#center').css('margin-left', 'auto'),
		fader: 		$('#fader'),
		visible:	false,
		width:		900,
		height:		600,
		
		show:		function(key) {
			
						var k = key.split('/');
							
						id = k[0];
						index = k.length > 1 ? k[1] : 0;
						
						if (id && id != content.showId && content.data[id] === undefined)
							content.create(id);
						
						if (control.playing && (id != content.showId || !index))
							control.stop();
							
						blend.show(content.isPage(id) || content.isAlbumDesc(id, index));
						randomPicture.show(!content.isAlbum(id));
						menu.expand(!content.isAlbum(id) || content.isAlbumDesc(id, index));
						menu.set(id);
						content.show(id, index);
						
						if (!main.visible)
						{
							if ($.browser.msie)
								main.fader.show();
							else
								main.fader.fadeInX(_fadeSpeed * 2);
						}
						main.visible = true;
					},
		
		resize:		function() {
						
						this.width = Math.round(_scale * 3);
						this.height = Math.round(_scale * 2);
		
						this.container
							.width(this.width)
							.height(this.height)
							.css('font-size', _scaleNav + '%');
					}
	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	var blend = {
		
		element:	$('<div id="blend" style="display: none;"><div></div></div>').prependTo(main.container),
		visible:	false,
		
		show:		function(v) {
						if (v == this.visible) return;
						(this.visible = v) ? this.element.fadeInX(_fadeSpeed) : this.element.fadeOutX(_fadeSpeed);
			  		}
	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	var randomPicture = {
		
		container:	$('<div id="randomContainer"></div>').hide().prependTo(main.container),
		visible:	false,
		picture:	[],
		counter: 	0,
		loading:	null,
		showPic:	null,
		timer:		null,
		loadTimer:	null,
				
		init:		function() {
	
						for(var i = 0; i < startpics.length; i++)
						{
							this.picture[i] = {
								img: $('<img class="randomPic" style="display: none;">')
										.setSrc(root + 'files/gallery/pic_' + startpics[i] + '.jpg', randomPicture.picLoaded)
										.appendTo(this.container),
								loaded: $.browser.opera
							};
						}
	
						if (!$.browser.opera)
							this.loadNext();
						else
						{
							this.counter = this.picture.length;
							this.change();
						}
					},
					
		picLoaded:	function(element) {
			
						randomPicture.picture[randomPicture.loading].loaded = true;
		
						randomPicture.counter++;
						
						if (randomPicture.counter < randomPicture.picture.length && randomPicture.visible && randomPicture.loadTimer === null)
							randomPicture.loadTimer = setTimeout(function () { randomPicture.loadNext();}, Math.round(_randomPicTime * 0.2));
							
						if (randomPicture.counter == 1)
						{
							randomPicture.showPic = randomPicture.loading;
							randomPicture.picture[randomPicture.showPic].img.show();
						}
						
						randomPicture.loading = null;
					},
					
		loadNext:	function() {
	
						if (this.counter == this.picture.length || (!this.visible && this.counter))
							return;
							
						this.loadTimer = null;
			
						do {
							this.loading = parseInt(Math.random() * this.picture.length);
						} while(this.picture[this.loading].loaded);
						
						this.picture[this.loading].img.loadSrc();
					},
					
		change:		function() {
	
						if (!this.picture.length || !this.counter) return;
							
						var old = this.showPic;
					
						do {
							this.showPic = parseInt(Math.random() * this.picture.length);
						} while (!this.picture[this.showPic].loaded || (this.counter > 1 && this.showPic == old));
					
						if (this.showPic != old)
						{
							if (old !== null) this.picture[old].img.fadeOut(_fadeSpeed * 2);
							this.visible ? this.picture[this.showPic].img.fadeIn(_fadeSpeed * 2) : this.picture[this.showPic].img.show();
						}
						
						if (this.counter < this.picture.length && !this.loading && this.loadTimer === null)
							 this.loadTimer = setTimeout(function () { randomPicture.loadNext();}, Math.round(_randomPicTime * 0.6));
					},
					
		show:		function(v)
					{
						if (v == this.visible) return;
	
						if (v)
						{
							this.change();
							this.container.fadeInX(_fadeSpeed);
							
							if (this.timer === null)
								this.timer = setInterval(function () { randomPicture.change();}, _randomPicTime);
						}
						else
						{
							this.container.fadeOutX(_fadeSpeed);
							
							clearInterval(this.timer);
							this.timer = null;
							
							if (this.loadTimer !== null)
							{
								clearTimeout(this.loadTimer);
								this.loadTimer = null;
							}
						}
	
						this.visible = v;
					},
					
		resize:		function() {
						this.container
							.width(main.width)
							.height(main.height)
					}
					
	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	var menu = {
		
		menu:		$('#menu').css('left', '15%'),
		control:	$('#control', this.menu),
		selection:	$('#selection', this.menu),
		logoText:	$('#logo2', this.menu),
		albumTitle:	$('#albumTitle', this.menu),
		links:		$('a', this.menu),
		link:		{},
		expanded:		true,
		scale:		1,
		visible:	true,
		
		
		
		init:		function() {
			
						this.links
							.click(function() { return href(this); })
							.each(function() {
								if (this.id.substr(0,2) == 'l_')
									menu.link[this.id.substr(2)] = $(this);
							});
					},
					
		expand:		function(e)	{
			
						if (e == this.expanded) return;
				
						if (e)
						{
							this.menu.animate({
								left: '15%'
							}, _fadeSpeed);
							
							control.playButton.fadeOutX(_fadeSpeed);
							this.control.fadeOutX(_fadeSpeed);
							this.selection.fadeInX(_fadeSpeed);
							this.albumTitle.fadeOutX(_fadeSpeed);
							this.logoText.fadeInX(_fadeSpeed);
						}
						else
						{
							if (main.visible)
							{
								this.menu.animate({
									left: '101.5%'
								}, _fadeSpeed);
								
								this.control.fadeInX(_fadeSpeed);
								this.selection.fadeOutX(_fadeSpeed);
								this.logoText.fadeOutX(_fadeSpeed);
								this.albumTitle.fadeInX(_fadeSpeed);
							}
							else
							{
								this.menu.css('left', '101.5%');
								this.control.show();
								this.selection.hide();
								this.logoText.hide();
								this.albumTitle.show();
							}
						}
						
						this.expanded = e; 
					},
					
		set:		function(id) {
						this.links.removeClass('selected');
						if (id)
						{
							this.albumTitle.html(this.link[id].html());
							document.title = 'patrik budenz | photography | ' + this.link[id].html();
							this.link[id].addClass('selected');
						}
						else
							document.title = 'patrik budenz | photography';
					},
					
		show:		function() {
						if (this.visible) return;
						this.menu.fadeInX(_fadeSpeed)
						this.visible = true;
					},
					
		hide:		function() {
						if (!this.visible) return;
						this.menu.fadeOutX(_fadeSpeed)
						this.visible = false;
					},
					
		resize:		function() {
			
						this.menu
							.height(parseInt(75 * _scaleNav / 100))
							.width(parseInt(2000 * _scaleNav / 100));
							
						// Workaround für Firefox2 Resize-Problem
						if ($.browser.mozilla && parseFloat($.browser.version) < 1.9 && this.expanded && _scale != this.scale)
						{
							this.scale = _scale;
							this.selection.hide();
							setTimeout(function() { menu.selection.show() } , 1);
						}
					}
	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	var control = {

		control:	$('#control'),
		nextButton:	$('#e_next', this.control).click(function() { control.stop();}),
		prevButton:	$('#e_prev', this.control).click(function() { control.stop();}),
		backButton:	$('#e_back', this.control).click(function() { control.stop();}),
		indexButton:$('#e_index', this.control).show().click(function() { control.stop(); content.openIndex(); }),
		playButton:	$('#e_play', this.control).show().click(function() { control.play(); }),
		stopButton:	$('#e_stop', this.control).click(function() { control.stop(); }),
		counter:	$('#e_counter', this.control),
		picIndex:	$('#e_pictureindex', this.control),
		picCount:	$('#e_picturecount', this.control),
		progress:	$('#progress', this.control),
		bar:		$('#bar', this.control),
		playing:	false,
		playTimer:	null,
		hideMenu:	0,
		blocker:	$('<div class="blocker"></div>')
						.appendTo('body')
						.click(function() { control.listener() })
						.mousemove(function() { control.listener() }),
						
		
		init:		function() {
			
						$(jQuery.browser.mozilla ? window : "body").keydown(function(e) {
							switch(e.keyCode)
							{
								case 37: if (content.isAlbum()) control.prevButton.click(); return false; break;
								case 39: if (content.isAlbum()) control.nextButton.click(); return false; break;
								case 33: case 36: if (content.isAlbum()) { control.stop(); $.historyLoad(content.showId + '/1');} return false; break;
								case 34: case 35: if (content.isAlbum()) { control.stop(); $.historyLoad(content.showId + '/' + gallery[content.showId][2].length); } return false; break;
								case 13: if (content.isAlbum()) { if (control.playing) control.stop(); else control.play(); } return false; break;
								case 27: if (content.isAlbum()) control.backButton.click(); return false; break;
							}
							return true;
						});
					},
					
		set:		function(id, index, max) {
			
						this.picIndex.html((index < 10 ? '0' : '') + index);
						this.picCount.html((max < 10 ? '0' : '') + max);
						
						index = parseInt(index);
						if (index == 1)
							this.prevButton.addClass('disableButton').attr('href', root + id + '/1');
						else
							this.prevButton.removeClass('disableButton').attr('href', root + id + '/' + (index - 1));
							
						if (index == max)
							this.nextButton.addClass('disableButton').attr('href', root + id + '/' + index);
						else
							this.nextButton.removeClass('disableButton').attr('href', root + id + '/' + (index + 1));
							
						this.backButton.attr('href', root + id);
					},
					
		listener:	function() {
						control.blocker.hide();
						menu.show();
						control.hideMenu = 0;
					},
										
		play:		function(time) {
		
						if (this.playing) return;
						
						this.stopButton.show();
						this.playButton.hide();
						content.closeIndex();
						
						this.progress.show();
						this.hideMenu = false;
						if (content.showIndex)
						{
							this.bar
								.stop()
								.css('width', '0%')
								.animate({
									width: '100%'
								}, (time === undefined ? _playtime : time));
							this.playTimer = setTimeout(function () { control.playNext(); }, (time === undefined ? _playtime : time));
							
						}
						else
							this.playNext();
							
						this.playing = true;
					},
	
		playNext:	function () {
			
						this.nextButton.each(function() { href(this);} );
						
						this.hideMenu++;
						if (this.hideMenu >= 2)
						{
							control.blocker.show();
							menu.hide();
						}


							this.bar
								.stop()
								.css('width', '0%')
								.animate({
									width: '100%'
								}, _playtime);
							this.playTimer = setTimeout(function () { 
								if (content.showIndex < gallery[content.showId][2].length)
									control.playNext(); 
								else
									control.stop();
							}, _playtime);
					},
	
		stop:		function() {

						if (!this.playing) return;
						
						if (control.hideMenu >= 2)
						{
							control.blocker.hide();
							menu.show();
							control.hideMenu = 0;
						}
						
						this.playButton.show();
						this.stopButton.hide();
						this.progress.hide();
						this.bar.stop().css('width', '0%');
						
						if (this.playTimer)
							clearTimeout(this.playTimer);
					
						this.playTimer = null;
						this.playing = false;
					},
	
		resize:		function () {
			
						this.nextButton.children().attr('src', root + 'img/next' + _buttonsize + '.gif');
						this.prevButton.children().attr('src', root + 'img/prev' + _buttonsize + '.gif');
						this.playButton.attr('src', root + 'img/play' + _buttonsize + '.gif');
						this.stopButton.attr('src', root + 'img/stop' + _buttonsize + '.gif');
						this.indexButton.attr('src', root + 'img/index' + _buttonsize + '.gif');
						this.backButton.children().attr('src', root + 'img/back' + _buttonsize + '.gif');
						this.counter.css('line-height', _buttonsize + 'px').css('font-size', _buttonsize + 'px').height(_buttonsize);
					}
	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	var content = {

		showId:		null,
		showIndex:	null,
		data:		{},
		
		create:		function(id) {
						
						if (this.data[id] !== undefined)
							return;
							
						if (page[id] !== undefined)
							this.data[id] = new oPage(id, page[id]);
						else if (gallery[id] !== undefined)
							this.data[id] = new oAlbum(id, gallery[id]);
					},
					
		show:		function(id, index) {
			
						if (this.showId && id != this.showId)
							this.data[this.showId].hide(this.showIndex);

						if (id)
						{
							this.data[id].show(index);
							setTimeout(function() { $(window).resize(); }, 10);
						}
							
						this.showIndex = index;	
						this.showId = id;
					},
					
		openIndex:	function() {
						this.data[this.showId].index.open();
					},
					
		closeIndex:	function() {
						this.data[this.showId].index.close();
					},
					
		resize:		function() {

						if (!this.isHome())
							this.data[this.showId].resize(this.showIndex);
					},
					
		isPage:		function(id) {
						if (id === undefined) id = this.showId;
						return (id && this.data[id] && this.data[id].type == 1);
					},
					
		isAlbum:	function(id) {
						if (id === undefined) id = this.showId;
						return (id && this.data[id] && this.data[id].type == 2);
					},
					
		isHome:		function(id) {
						if (id === undefined) id = this.showId;
						return (!id);
					},
					
		isAlbumDesc: function(id, index) {
						if (id === undefined) id = this.showId;
						if (index === undefined) index = this.showIndex;
						return (this.isAlbum(id) && !index);
					},
					
		isPicture:	function(id, index) {
						if (id === undefined) id = this.showId;
						if (index === undefined) index = this.showIndex;
						return (this.isAlbum(id) && index);
					}
	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	function oPage(id, page)
	{
		var _t = this;
		this.id = id;
		this.type = 1;
		this.visible = false;
		this.element = $('<div id="' + id + '" class="page"></div>');
		
		this.content = $('<div class="content"></div>');
		this.wrapper = $('<div class="contentWrapper">' + page[1] + '</div>').appendTo(this.content);
							
		this.content.appendTo(this.element);
		this.element.insertBefore(menu.menu);
		
		if (!$.browser.msie || parseInt($.browser.version) > 6)
			this.content
				.css('overflow', 'hidden')
				.customScroller({_fadeSpeed: 4, vertical:1, horizontal:0});

		this.element.hide();
		
		this.show = function() {
			if (this.visible) return;

			if ($.browser.msie)
				$('.title, strong', _t.wrapper).css('background', 'rgb(239, 239, 234)');

			this.element.fadeInX(_fadeSpeed, function() {
				if ($.browser.msie)
					$('.title, strong', _t.wrapper).css('background', 'none');
			});
			this.visible = true;
		};
		
		this.hide = function() {
			if (!this.visible) return;
			
			if ($.browser.msie)
				$('.title, strong', _t.wrapper).css('background', 'rgb(239, 239, 234)');
			
			this.element.fadeOutX(_fadeSpeed, function() {
				if ($.browser.msie)
					$('.title, strong', _t.wrapper).css('background', 'none');
			});
			this.visible = false;
		};
		
		this.resize = function() {
			this.element.height(main.height);
			var h = Math.round(main.height * .925 - 75 * _scaleNav / 100);
			this.content.height(h);
			this.content.css('top', Math.round(main.height * 0.025) + 'px');
			if ($.browser.msie && parseInt($.browser.version) < 7)
				this.wrapper.width(this.content.width() - (this.wrapper.height() + 10 > h ? 24 : 0));
			$('.divVScrollerBar, .divVScrollerBarTrace', this.content).height(h);
		};
	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	function oAlbum(id, gallery)
	{
		var _t = this;
		
		this.id = id;
		this.type = 2;
		this.visible = false;
		this.element = $('<div id="' + id + '" class="album" style="display:none;"></div>').insertBefore(blend.element);
		this.picture = [];
		this.max = gallery[2].length;
		this.loadCounter = this.max;
		this.loading = null;
		this.showIndex = null;
		this.index = new oIndex(this);
		
		this.create = function()
		{
			for(var index = 0; index <= gallery[2].length; index++)
			{
				var filename = index ? 'pic_' + gallery[2][index - 1][0] + '.jpg' : gallery[3][0];
				
				this.picture[index] = {
					div:	d = $('<div id="' + id + '_' + index + '" style="display: none;" class="picdiv' + (!index ? ' description' : '') + '">' + '</div>')
								.appendTo(this.element),
					img:	$('<img class="picture">')
								.setSrc(root + 'files/gallery/' + filename, function() { _t.pictureLoaded(this)} )
								.click(function(){ 
									$.historyLoad(id + "/" + (_t.showIndex < _t.max ? parseInt(_t.showIndex) + 1 : _t.showIndex));
									return false;
								 })
								 .appendTo(d),
					width:	w = index ? gallery[2][index - 1][1] / gallery[2][index - 1][2] : gallery[3][1] / gallery[3][2],
					thumb:	index ? this.index.add(index, filename, w) : null,
					visible:false
				};
				
				if (!$.browser.opera)
				{
					this.picture[index].div.addClass('loading');
					this.picture[index].img.hide();
				}
			}
			
			if (!$.browser.opera)	
				this.loadNextPicture();
				
			this.description = {};
			this.description.element = $('<div id="desc_' + id + '" class="page"></div>');
			this.description.content = $('<div class="content"></div>');
			this.description.wrapper = $('<div class="contentWrapper">' +
												'<p class="title">' + gallery[0] + '</p>' +
												(gallery[1] ? gallery[1] : '') + 
												(gallery[4] ? '<div class="description2">' + gallery[4] + '</div>' : '') + 
												'</div>')
											.appendTo(this.description.content);
											
			
			this.description.content.appendTo(this.description.element);
			this.description.element.insertBefore(menu.menu);
			
			if (!$.browser.msie || parseInt($.browser.version) > 6)
				this.description.content
					.css('overflow', 'hidden')
					.customScroller({_fadeSpeed: 4, vertical:1, horizontal:0});
					
			$('<div class="play"><span>play&gt;</span></div>')
				.click(function() { control.play( _fadeSpeed + _playtime); })
				.appendTo(this.description.element);	
			
			this.description.element.hide();
		};
		
		this.loadNextPicture = function() {
			this.loading = this.max - this.loadCounter;
			this.picture[this.loading].img.loadSrc();
		};
		
		this.pictureLoaded = function() {
			
			this.picture[this.loading].div.removeClass('loading');
			this.picture[this.loading].img.fadeInX(_fadeSpeed);
			
			if (this.loading)
				this.picture[this.loading].thumb.element.attr('src', this.picture[this.loading].img.attr('src'));
			
			this.loadCounter--;
			this.loading = null;
			
			if (this.loadCounter >= 0 && this.visible)
				setTimeout(function () { _t.loadNextPicture(); }, 5);
		};
		
		this.show = function(index) {
			
			this.index.close();
			
			index = Math.min(!index ? 0 :index, this.max);
						
			if (!this.picture[index].visible)
			{
				this.resize(index);
				
				if (this.visible) 
				{
					this.picture[index].div.fadeIn(_fadeSpeed, function() {
						_t.index.setPosition(index);
					});
					
					this.picture[this.showIndex].div.fadeOut(_fadeSpeed);

					if (!this.showIndex)
						this.description.element.fadeOutX(_fadeSpeed);

					this.picture[this.showIndex].visible = false;
				}
				else
				{
					this.picture[index].div.show();
					this.index.setPosition(index);
				}
				
				if (!index)
					this.description.element.fadeInX(_fadeSpeed);
					
				this.picture[index].visible = true;
				this.showIndex = index;
			}
			
			if (!this.visible)
			{
				this.element.fadeInX(_fadeSpeed);
				this.visible = true;
			}
			
			control.set(this.id, index, this.max);
			
			if (this.loading === null && this.loadCounter >= 0)
				this.loadNextPicture();
		};
		
		this.hide = function(index) {
			
			index = Math.min(!index ? 0 :index, this.max);
			
			if (this.visible)
			{
				this.element.fadeOutX(_fadeSpeed, function() {
					_t.picture[index].div.hide();
				});

				if (!index)
					this.description.element.fadeOutX(_fadeSpeed);

				this.index.close();
				
				this.picture[index].visible = false;
				this.showIndex = null;
				this.visible = false;
			}
		};
		
		this.resize = function(index) {
			
			w = Math.round(main.height * this.picture[index].width);
			if (w > main.width)
				w = main.width;
			
			this.picture[index].img
				.height(main.height)
				.width(w);
				
			if (!index)
			{
				this.description.element.height(main.height);
				var h = Math.round(main.height * .925 - 75 * _scaleNav / 100);
				this.description.content.height(h - 20 * _scaleNav / 100);
				this.description.content.css('top', Math.round(main.height * 0.025) + 'px');
				if ($.browser.msie && parseInt($.browser.version) < 7)
					this.description.wrapper.width(this.description.content.width() - 24);
				$('.divVScrollerBar, .divVScrollerBarTrace', this.description.content).height(h);
			}
			
			this.index.resize();
		};

		this.create(id, gallery);		
	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	function oIndex(parent)
	{
		this.element = $('<div id="index_' + id + '" class="index"><div class="dropShadow"></div></div>')
			.width(parseInt(_scale * 2.75))
			.appendTo(menu.menu);
		this.scroller = $('<div class="scroller"></div>').appendTo(this.element);
		this.thumbnails = $('<div class="thumbnails"></div>').appendTo(this.scroller);
		this.thumb = [];
		this.isOpen = false;
		this.scrolling = false;
		this.indexWidth = null;
		this.indexHeight = null;
		this.scrollerWidth = null;
		this.thumbnailWidth = null;
		this.factor = 1;
		this.buttons = null;
		this.position = 0;
		this.active = null;
		
		this.add = function(index, filename, w) {

			var t = this.thumb[index] = {
				element:	$('<img id="thumb_' + id + '_' + index + '">')
							.appendTo(this.thumbnails)
							.click(function() {
								var pos = this.id.lastIndexOf('_');
								$.historyLoad(this.id.substring(6, pos) + "/" + this.id.substring(pos + 1));
								return false;
							}),
				width:		w
			};
			
			if ($.browser.opera)
				t.element.attr('src', root + 'files/gallery/' + filename);

			return t;
		};
		
		this.open = function() {
			
			if (this.isOpen)
			{
				this.close();
				return;
			}
			
			if (control.playing)
				control.stop();
			
			this.isOpen = true;
			this.resize();
			this.setPosition(content.showIndex);
			
			this.element.fadeInX(_fadeSpeed / 2);
			
		};
		
		this.close = function() {
			
			if (!this.isOpen)
				return;
	
			this.element.fadeOutX(_fadeSpeed / 2);
			this.isOpen = false;
		};
		
		this.resize = function() {

			if (!this.isOpen) return;
			this.indexHeight = this.element.height();
			this.indexWidth = this.scrollerWidth = parseInt(_scale * 2.75);

			this.thumbnailWidth = 0;
		
			for (var i = 1; i < this.thumb.length; i++)
				this.thumbnailWidth += Math.round(this.indexHeight * this.thumb[i].width) + 1;
				
			if (this.thumbnailWidth > this.indexWidth)
			{
				var nw = parseInt(20 * _scaleNav / 100);
				this.scrollerWidth -=  2 * nw;
				
				this.element.width(this.indexWidth);
				
				this.scroller
					.css('left', nw + 'px')
					.width(this.scrollerWidth);
					
				var _t = this;
					
				if (!this.buttons)
					this.buttons = {
						prev:	$('<div class="indexLeft"> </div>')
									.width(nw)
									.css('background-image', 'url(' + root + 'img/left' + _buttonsize + '.gif)')
									.appendTo(this.element)
									.mouseover(function() {
										_t.factor = 1;
										_t.scrolling = true;
										_t.scroll(1);
									})
									.mouseout(function() {
										_t.scrolling = false;
									}),
		
						next:	$('<div class="indexRight"> </div>')
									.width(nw)
									.css('background-image', 'url(' + root + 'img/play' + _buttonsize + '.gif)')
									.appendTo(this.element)
									.mouseover(function() {
										_t.factor = 1;
										_t.scrolling = true;
										_t.scroll(-1);
									})
									.mouseout(function() {
										_t.scrolling = false;
									})
					};
				else
				{
					this.buttons.prev.width(nw).css('background-image', 'url(' + root + 'img/left' + _buttonsize + '.gif)');
					this.buttons.next.width(nw).css('background-image', 'url(' + root + 'img/play' + _buttonsize + '.gif)');
				}
			}
			else
			{
				this.scroller.css('left', '0px').width(this.thumbnailWidth);
				this.position = 0;
				this.indexWidth = this.thumbnailWidth;
				this.element.width(this.thumbnailWidth);
					
				if (this.buttons)
				{
					this.buttons.prev.remove();
					this.buttons.next.remove();
					this.buttons = null;
				}
			}	
		};
		
		this.scroll = function(dir) {
			
			if (!this.scrolling)
				return;
		
			this.position += dir * 10;
			
			if (this.position > 0)
			{
				this.position = 0;
				this.scrolling = false;
			}
			
			if (this.position < this.scrollerWidth - this.thumbnailWidth)
			{
				this.position = this.scrollerWidth - this.thumbnailWidth;
				this.scrolling = false;
			}
		
			var t = this;
			this.thumbnails.animate({
				left: this.position + 'px'
			}, 25, 'linear', function() { t.scroll(dir) });
		};
		
		this.setPosition = function(index) {
			
			if (index == this.active || !index)
				return;
			
			if (this.active)
				this.thumb[this.active].element.removeClass('active');
			
			this.thumb[index].element.addClass('active');
			this.active = index;
				
			var pos = 0;
			
			if (this.thumbnailWidth > this.indexWidth)
			{
				var w = 0;
		
				for (var i = 1; i < index; i++)
					w += parseInt(this.indexHeight * this.thumb[i].width);
					
				w -= this.scrollerWidth / 2 - this.indexHeight * this.thumb[index].width / 2;
				
				if (w < 0) w = 0;
				
				if (w > this.thumbnailWidth - this.scrollerWidth)
					w = this.thumbnailWidth - this.scrollerWidth;
					
				this.position = -w;
				
				this.thumbnails.css('left', this.position + 'px');
			}
		};
	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	function resizePage()
	{
		var h = Math.round($(window).height() * 0.4);
		var w = Math.round(Math.pow((-16.2 + Math.sqrt (262.44 + ($(window).width() - 50) * 12)) / 6, 2));
		
		_scale = Math.max(Math.min(Math.min(h, w), 400), 175);
		_scaleNav = Math.min(100, parseInt(Math.pow(_scale/300, 0.5) * 100));
		_buttonsize = Math.max(8, Math.min(13, Math.round(13 * _scaleNav / 100))) - 1;
		
		main.resize();
		randomPicture.resize();
		menu.resize();
		control.resize();			
		content.resize();
	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	function href(e)
	{
		if ($(e).hasClass('disable'))
			return;
		var hash = e.href;
		if (hash.indexOf('#') == -1)
		{
			if (hash.substring(7) == 'http;//')
			{
				hash = hash.substring(7);
				hash = 'http://' + hash.replace(root, root + '#');
			}
			else
				hash = hash.replace(root, root + '#');
		}
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
		return false;
	}
	
	
	//----------------------------------------------------------------------------------------------------
	
	
	function UnCryptMailto( s )
	{
		var n = 0;
		var r = "";
		for( var i = 0; i < s.length; i++)
		{
		    n = s.charCodeAt( i );
		    if( n >= 8364 )
		    {
		        n = 128;
		    }
		    r += String.fromCharCode( n - 1 );
		}
		return r;
	}
	
	function linkTo_UnCryptMailto( s )
	{
		location.href=UnCryptMailto( s );
	}
	
	
	//----------------------------------------------------------------------------------------------------
		
	if (_redirect)
		return;
		
	$('body').disableTextSelect();
	$('noscript').remove();
	$('body script').remove();
	
	randomPicture.init();
	menu.init();
	control.init();
	
	
	resizePage();
	$(window).resize(function(){
		resizePage();
	});
	
	$.historyInit(main.show, "/");

	buttons = ['prev', 'next', 'play', 'stop', 'index', 'back', 'left'];
	for (var i = 9; i <= 12; i++)
		for(var j = 0; j < buttons.length; j++)
		{
			var img = new Image();
			img.src = root + 'img/' + buttons[j] + j + '.gif';
		}
});