if(location.href.split("/")[3].toUpperCase() == "EDITORAL") {
	document.write("<style>#editoral { display: none; }</style>");
}

addLoadEvent(function() {
	var nav = $("nav");
	if(nav) {
		$A(nav.getElementsByTagName("img")).each(function(img) {
			if(img.src.indexOf("slash") >= 0) return;
			if(img.alt == location.href.split("/")[3].toUpperCase()) {
				img.src = eventImagePath.over(img.src);
			} else {
				img.onmouseover = function() { this.src = eventImagePath.over(this.src); }
				img.onmouseout = function() { this.src = eventImagePath.out(this.src); }
			}
		});
	}
	
	var el = $("editoral");
	if(el) {
		updateDimsEditoral(el, false);
		el.style.display = "block";
		$A(el.getElementsByTagName("a")).each(function(a) {
			if(a.target == "detail") {
				a.onclick = function() {
					if(!window.detail || window.detail.closed) window.detail = window.open(this.href, "detail", "width=500,height=500,location=no,menubar=no,resizable=yes,scrollbars=no,status=no"); 
					else window.detail = window.open(this.href, "detail", "location=no,menubar=no,resizable=yes,scrollbars=no,status=no");
					window.detail.focus();
					return false;
				}
			}
		});
	}
	
	var imageBand = $("imageBandVisible");
	if(imageBand) {
		imageBand.movable = false;
		nextMove = false;
		
		imageBand.move = function() {
			var innerImages = $("imageBand");
			var moveByX = Math.log(Math.abs(this.x)+1)*10.0;
			if(this.x >= 0) moveByX = -1*moveByX;
			var oldX = parseFloat(innerImages.getStyle("left"));
			var newX = oldX+moveByX;
			
			var isWithinLeftBorder = (newX <= 0);
			var isWithinRightBorder = (newX+parseFloat(innerImages.getStyle("width")) >= getInnerSize()[0]);
			if((isWithinLeftBorder && isWithinRightBorder) || (isWithinLeftBorder && moveByX > 0) || (isWithinRightBorder && moveByX < 0)) innerImages.style.left = newX + "px";
			
			if(this.movable) {
				clearTimeout(this.nextMove);
				this.nextMove = setTimeout(function() { this.move(); }.bind(this), 20);
			} else {
			}
		};
		
		Event.observe(imageBand, "mouseover", function(e) {
			if(!this.movable) {
				Event.stop(e);
				this.movable = true;
				this.move();
			}
		}.bindAsEventListener(imageBand));

		Event.observe(imageBand, "mouseout", function(e) {
			if(this.movable) {
				Event.stop(e);
				this.movable = false;
			}
		}.bindAsEventListener(imageBand));
		
		Event.observe(imageBand, "mousemove", function(e) {
			Event.stop(e);
			var w = getInnerSize()[0]/2;
			this.x = (Event.pointerX(e)-w)/w;
		}.bindAsEventListener(imageBand));
	}
});

addResizeEvent(function() {
	var imageMain = $('imageMain');
	if(imageMain) {
		var el = $('editoral');
		if(el) updateDimsEditoral(el, false);
		else updateDims(imageMain, false);
	}
});





var arrows = [ '/images/prev.out.gif', '/images/next.out.gif', '/images/prev.over.gif', '/images/next.over.gif' ];
function loadGallery(id) {
	for(var i=0; i<arrows.length; i++) arrows[i] = Builder.node('img', { src: arrows[i] });
	getPageList(id, function(req) {
		if(req.status != 200) return false;

		imageBandWidth = 0;
		$A(req.responseXML.documentElement.getElementsByTagName('page')).each(function(page) {
			$A(page.getElementsByTagName('image')).each(function(i) {
				var imgDims = calcDims([i.getAttribute('width'), i.getAttribute('height')], null, 120);
				imageBandWidth += imgDims[0];
																 
				var img = Builder.node('img', {	src: genThumbUrl(i.getAttribute('url'), imgDims[0], imgDims[1]), alt: "" });
				img.xml = i;
				img.caption = page.getAttribute("caption");
				img.onmouseout = function() { if(gallery) gallery.restore(); };
				img.onmouseover = function() { if(gallery) gallery.show(this); };
				img.onclick = function() { $("imageBandVisible").movable = false; /*gallery.set(this);*/ };
				img.preload = function(canvasDims) {
					var dims = [this.xml.getAttribute("width"), this.xml.getAttribute("height")];
					
					// Calc 2 different dims
					var resizedDimsWidth = calcDims(dims, parseInt($("imageMain").getStyle("width")), null);
					var resizedDimsHeight = calcDims(dims, null, parseInt($("imageMain").getStyle("height")));
					
					// Find out, which dims to use
					if(resizedDimsWidth[0] <= canvasDims[0] && resizedDimsWidth[1] <= canvasDims[1]) resizedDims = resizedDimsWidth;
					else resizedDims = resizedDimsHeight;
					
					var url = genThumbUrl(this.xml.getAttribute('url'), resizedDims[0], resizedDims[1]);
					var detailedImage = Builder.node("img", { "src": url });
					detailedImage.osrc = this.xml.getAttribute("url");
					detailedImage.owidth = this.xml.getAttribute("width");
					detailedImage.oheight = this.xml.getAttribute("height");
					return detailedImage;
				}
				
				//var a = Builder.node("a", { href: "#", alt: "" }, [ img ]);
				//a.onclick = function() { return false; };
				$("imageBand").appendChild(img);
			}.bind(this));
		});
		
		$("imageBand").style.width = imageBandWidth + "px";
		updateDims($("imageMain"), true);
		
//		showPage(0);
	});
}

function loadGalleryEditoral(base, imglist) {
	for(var i=0; i<imglist.length; i++) {
		var thumb = Builder.node("img", { src: "/images/thumb.php?src=" + base + imglist[i] + "&y=90", alt: "" });
			thumb.onmouseout = function() { gallery.restore(); };
			thumb.onmouseover = function() { gallery.show(this); };
			thumb.osrc = base + imglist[i];
			thumb.preload = function(canvasDims) {
				var dims = [this.width, this.height];
					
				// Calc 2 different dims
				var resizedDimsWidth = calcDims(dims, parseInt($("imageMain").getStyle("width")), null);
				var resizedDimsHeight = calcDims(dims, null, parseInt($("imageMain").getStyle("height")));
					
				// Find out, which dims to use
				if(resizedDimsWidth[0] <= canvasDims[0] && resizedDimsWidth[1] <= canvasDims[1]) resizedDims = resizedDimsWidth;
				else resizedDims = resizedDimsHeight;
					
				var url = genThumbUrl(this.osrc, resizedDims[0], resizedDims[1]);
				var detailedImage = Builder.node("img", { "src": url });
				detailedImage.osrc = this.osrc;
				detailedImage.owidth = this.width;
				detailedImage.oheight = this.height;
				detailedImage.style.marginTop = Math.round((canvasDims[1]-resizedDims[1])/2) -20 + "px";
				return detailedImage;
			}
	
		$("imageRow").appendChild(Builder.node('td', [
			thumb
		]));
	}
	updateDimsEditoral($("editoral"), true);
}

var gallery = {
	permanentImage: null,
	
	set: function(img) {
		this.permanentImage = img;
		this.changeImage(img);
	},
	
	restore: function(img) {
		this.changeImage(this.permanentImage);
	},
	
	show: function(img) {
		this.changeImage(img);
	},
	
	changeImage: function(img, caption) {
		if(!img) return;
		var container = $("imageMain");
		flushElement(container);
		
		var newImg = img.preload([parseInt($("imageMain").getStyle("width")), parseInt($("imageMain").getStyle("height"))]);
		container.appendChild(newImg);
		
		var captionDiv = document.createElement("div");
		captionDiv.appendChild(document.createTextNode(img.caption));
		//container.appendChild(captionDiv);
	}
}
	

function getPageList(id, callback) {
	var ajax = new Ajax.Request('/imageinfo.php?id=' + id, { method: 'get', onComplete: callback });
}

pageNumber = 0;
function showPage(num) {
	pageNumber = num;
	
	var pages = $A($('image').getElementsByTagName('div'));
	$('galleryNav').getElementsByTagName('a')[0].style.visibility = (num < 1) ? 'hidden' : 'visible';
	$('galleryNav').getElementsByTagName('a')[1].style.visibility = (num >= (pages.length-1)) ? 'hidden' : 'visible';
	
	pages.each(function(p) {
		if(p.style.display != 'none') {
			p.style.display = 'none';
//			new Effect.FadeOut(p/*, { from: 0.50, to: 0.50 }*/);
		}
	});
	var page = $('image').getElementsByTagName('div')[num];
	if(!page) return false;
	$(page).setStyle({ display: 'block' });
	$(page).setOpacity(1.00);

	updateDims(page, true);
}

function showNextPage() { showPage(pageNumber+1); }
function showPrevPage() { showPage(pageNumber-1); }

function genThumbUrl(url, width, height) {
//	return url;
	var url = '/images/thumb.php?src=' + escape(url);//'/images/spacer.gif';
	if(width) url += '&x=' + parseInt(width);
	if(height) url += '&y=' + parseInt(height);
	return url;
}

function updateDims(container, updateImg) {
	// Calculate cancas for images
	var canvasDims = [ getInnerSize()[0]-200, getInnerSize()[1]-230 ];
	container.setStyle({
		width: canvasDims[0] + "px",
		height: canvasDims[1] + "px",
		marginLeft: -Math.round(canvasDims[0]/2) + "px",
		marginTop: -Math.round(canvasDims[1]/2)+95 + "px"
	});
	
	// Stores the sum of all pictures widths
	var imagesWidth = 0;
	var imagesHeight = 1000; // Nur fixiert, damit die Breiten korrekt skaliert werden in der Summenbildung aller Breiten

	var images = $A(container.getElementsByTagName('img'));
	for(var i=0; i<images.length; i++) {
		var image = images[i];
		imagesWidth += calcDims([image.owidth, image.oheight], false, imagesHeight)[0];
	}

	var fixedWidthDims = calcDims([imagesWidth, imagesHeight], canvasDims[0], false);
	var fixedHeightDims = calcDims([imagesWidth, imagesHeight], false, canvasDims[1]);

	var effectiveWidth = 0;
	for(var i=0; i<images.length; i++) {
		var image = images[i];
		if(canvasDims[0] >= fixedHeightDims[0]) { var dims = calcDims([image.owidth, image.oheight], false, fixedHeightDims[1]); }
		else { var dims = calcDims([image.owidth, image.oheight], false, fixedWidthDims[1]); }

		if(updateImg) image.src = genThumbUrl(image.osrc, dims[0], dims[1]);
		image.width = dims[0];
		image.height = dims[1];
		
		effectiveWidth += dims[0];
	}
/*
	if(updateImg) $A($("imageBand").getElementsByTagName("img")).each(function(img) {
		img.preload(canvasDims);
	});*/
}

function updateDimsEditoral(container) {
	// Calculate cancas for images
	var canvasDims = [ getInnerSize()[0]-200, getInnerSize()[1]-160 ];
	container.setStyle({
		top: "55px",
		left: "25px",
		width: canvasDims[0] + "px",
		height: canvasDims[1] + "px",
		margin: "0px"
	});
	
	var imageMain = $("imageMain");
	if(imageMain) imageMain.setStyle({
		left: "610px",
		top: "20px",
		width: getInnerSize()[0]-640 + "px",
		height: getInnerSize()[1]-160 + "px",
		margin: "0px"
	});
}

function calcDims(dims) {
	var r = (dims[0]/dims[1]);
//	if(!arguments[1] && arguments[2] && arguments[2] != 120) alert(dims[0] + " " + dims[1]);
	if(arguments[1] && arguments[2]) return [ arguments[1], arguments[2] ];
	else if(arguments[1]) return [ arguments[1], Math.round(arguments[1]/r) ];
	else if(arguments[2]) return [ Math.round(arguments[2]*r), arguments[2] ];
	else return dims;
}
