// ----------------------------------------------------------------------------------------------------
// cms.js - CMS JavaScript Part
// ----------------------------------------------------------------------------------------------------
var CMS = {

	// --------------------------------------------------------------------------------------------
	HELP : {
	// --------------------------------------------------------------------------------------------
		inManagement: false,
		// --------------------------------------------------
		setManagement : function() {
			CMS.HELP.inManagement = true;
		},
		// --------------------------------------------------
		isSafari: function() {
			return (navigator.userAgent.indexOf("Safari") != -1);
		},
		// --------------------------------------------------
		isMSIE: function() {
			return (navigator.userAgent.indexOf("MSIE") != -1);
		}
	},
	// --------------------------------------------------------------------------------------------
	TICKER : {
	// --------------------------------------------------------------------------------------------
		container : null,
		timer : null,
		abstand : 15,
		insideContainer : null,
		marginTop : 0,
		leseZeit : 4000,
		moveTime : 20,
		// --------------------------------------------------
		setContainer : function(reference) {
			CMS.TICKER.container = reference;
		},
		// --------------------------------------------------
		setAbstand : function(abstand) {
			CMS.TICKER.abstand = abstand;
		},
		// --------------------------------------------------
		move : function() {
			if (!CMS.TICKER.container)
				return ;

			if (CMS.TICKER.insideContainer) {
				window.setTimeout("CMS.TICKER.move()",CMS.TICKER.moveTime);
				return ;
			}
			var entries = CMS.TICKER.container.getElementsByTagName("DIV");
			if (CMS.TICKER.marginTop == -(entries[0].offsetHeight + CMS.TICKER.abstand)) {
				var first = entries[0];
				CMS.TICKER.container.replaceChild(entries[1],first);
				first.style.marginTop = 0;
				CMS.TICKER.container.appendChild(first);
				CMS.TICKER.marginTop = 0;
				CMS.TICKER.start();
			}
			else {
				CMS.TICKERF.marginTop-- ;
				entries[0].style.marginTop = CMS.TICKER.marginTop;
				window.setTimeout("CMS.TICKER.move()",CMS.TICKER.moveTime);
			}
		},
		// --------------------------------------------------
		enter : function() {
			CMS.TICKER.insideContainer = true;
		},
		// --------------------------------------------------
		exit : function() {
			CMS.TICKER.insideContainer = false;
		},
		// --------------------------------------------------
		start : function() {
			if (CMS.TICKER.container)
				CMS.TICKER.timer = window.setTimeout("CMS.TICKER.move()",CMS.TICKER.leseZeit);
		}
	},
	// --------------------------------------------------
	// --------------------------------------------------
	leavePage : function() {
		var request = null;

		if (CMS.HELP.inManagement)
			return ;

		// Microsoft Internet Explorer
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(e) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				// Mozilla/Firefox/Safari etc.
				try {
					request = new XMLHttpRequest();
				}
				catch(e) {}
			}
		}
		if (request) {
			var url = document.location.href;
			url += (url.indexOf('?') == -1 ? '?' : '&') + 'ajaxAction=leavePage';
			// request synchron (!) senden
			request.open('GET',url,false);
			request.send(null);
		}
	},
	// --------------------------------------------------
	// Request Functions
	// --------------------------------------------------
	request : {
		exchangeUrl : function(url,xParmName,xParmValue) {
			var qs = '';
			if ((qp = url.indexOf('?')) != -1) {
				var parms = url.substring(qp + 1).split("&");
				for (p = 0; p < parms.length; p++) {
					var parm = parms[p].split("=");
					if (parm[0] != xParmName)
						qs += (qs ? '&' : '') + parms[p];
				}
				url = url.substring(0,qp);
			}
			if (xParmName && xParmValue)
				qs += (qs ? '&' : '') + xParmName + "=" + xParmValue;

			url += (qs ? "?" + qs : '');

			return url;
		}
	},
	// --------------------------------------------------
	// gallery: CMS Image Gallery Functions
	// --------------------------------------------------
	gallery : {
		id : null,
		current : 0,
		images : null,
		details : null,
		width : null,
		height : null,
		// ------------------------------
		init : function(id,images,details) {
		// ------------------------------
			CMS.gallery.id = id;
			CMS.gallery.images = images;
			CMS.gallery.details = details;
		},
		// ------------------------------
		getImage : function() {
		// ------------------------------
			return document.getElementById("GalleryImage" + CMS.gallery.id);
		},
		// ------------------------------
		getText : function() {
		// ------------------------------
			return document.getElementById("GalleryText" + CMS.gallery.id);
		},
		// ------------------------------
		back : function() {
		// ------------------------------
			if (CMS.gallery.current > 0)
   				CMS.gallery.show(--CMS.gallery.current);
			return false;
		},
		// ------------------------------
		next : function() {
		// ------------------------------
			if (CMS.gallery.current < CMS.gallery.images.length - 1)
				CMS.gallery.show(++CMS.gallery.current);
			return false;
		},
		// ------------------------------
		show : function(index) {
		// ------------------------------
			if (index > -1 && index < CMS.gallery.images.length &&
							  index != CMS.gallery.current) {
				CMS.gallery.current = index;
				if (img = CMS.gallery.getImage()) {
					img.src = CMS.gallery.images[CMS.gallery.current];
					img.alt = CMS.gallery.details[CMS.gallery.current];
				}
				if (txt = CMS.gallery.getText())
					txt.innerHTML = CMS.gallery.details[CMS.gallery.current];
			}
			return false;
		},
		// ------------------------------
		thumbOver : function(imageRef,src) {
		// ------------------------------
//			dpos = imageRef.src.lastIndexOf('.');
//			imageRef.src = imageRef.src.substr(0,dpos) + ".over" + imageRef.src.substr(dpos);
			imageRef.src = src;
		},
		// ------------------------------
		thumbOut : function(imageRef,src) {
		// ------------------------------
			imageRef.src = src;
		},
		// ------------------------------
		diashow : {
			imageRef : null,
			parentRef : null,
			images : null,
			currentIndex : 0,
			opacity : 1.0,
			pause : 5000,
			steps : 0.05,
			delay : 60,
			random : false,	// not implemented yet :(
			infinite : true,
			// ------------------------------
			// ------------------------------
			next : function() {
				if (!CMS.gallery.diashow.images)
					return null;

				if (++CMS.gallery.diashow.currentIndex >= CMS.gallery.diashow.images.length) {
					if (!CMS.gallery.diashow.infinite)
						return null;
					CMS.gallery.diashow.currentIndex = 0;
				}
				return CMS.gallery.diashow.images[CMS.gallery.diashow.currentIndex];
			},
			// ------------------------------
			// ------------------------------
			setOpacity : function(value) {
				if (CMS.gallery.diashow.imageRef) {
					CMS.gallery.diashow.imageRef.style.filter = 'alpha(opacity=' + (value * 100) + ')';	// IE
					CMS.gallery.diashow.imageRef.style.opacity = value;									// Gecko
				}
			},
			// ------------------------------
			// ------------------------------
			fadeOut : function() {

				if (CMS.gallery.diashow.opacity > 0.0) {
					CMS.gallery.diashow.setOpacity(CMS.gallery.diashow.opacity -= CMS.gallery.diashow.steps);
					window.setTimeout("CMS.gallery.diashow.fadeOut()",CMS.gallery.diashow.delay);
				}
				else if ((src = CMS.gallery.diashow.next()) != null) {
					CMS.gallery.diashow.setOpacity(CMS.gallery.diashow.opacity = 0.0);
					CMS.gallery.diashow.imageRef.src = src;
					window.setTimeout("CMS.gallery.diashow.fadeIn()",CMS.gallery.diashow.pause);
				}
			},
			// ------------------------------
			// ------------------------------
			fadeIn : function() {
				if (CMS.gallery.diashow.opacity < 1.0) {
					CMS.gallery.diashow.setOpacity(CMS.gallery.diashow.opacity += CMS.gallery.diashow.steps);
					window.setTimeout("CMS.gallery.diashow.fadeIn()",CMS.gallery.diashow.delay);
				}
				else if ((src = CMS.gallery.diashow.next()) != null) {
					CMS.gallery.diashow.setOpacity(CMS.gallery.diashow.opacity = 1.0);
					CMS.gallery.diashow.parentRef.style.background = 'url(' + src + ') no-repeat top left';
					window.setTimeout("CMS.gallery.diashow.fadeOut()",CMS.gallery.diashow.pause);
				}
			},
			// ------------------------------
			// ------------------------------
			init : function(images,pause,delay,steps,offset,random,infinite) {
				CMS.gallery.diashow.images = images;
				CMS.gallery.diashow.pause = pause;
				CMS.gallery.diashow.delay = delay;
				CMS.gallery.diashow.steps = steps;
				CMS.gallery.diashow.currentIndex = offset;
				CMS.gallery.diashow.random = random;
				CMS.gallery.diashow.infinite = infinite;
			},
			// ------------------------------
			// ------------------------------
			checkReferences : function() {
				if (!CMS.gallery.diashow.imageRef || !CMS.gallery.diashow.parentRef) {
					CMS.gallery.diashow.imageRef = document.getElementById("GalleryImage" + CMS.gallery.id);
					CMS.gallery.diashow.parentRef = document.getElementById("DiashowContainer" + CMS.gallery.id);
					return false;
				}
				return true;
			},
			// ------------------------------
			// ------------------------------
			start : function() {
				// Referenzen pr?fen und ggf. 1/2 Sekunde warten...
				if (!CMS.gallery.diashow.checkReferences()) {
					window.setTimeout("CMS.gallery.diashow.start()",500);
					return ;
				}
				var src = CMS.gallery.diashow.next();
				if (src) {
					CMS.gallery.diashow.parentRef.style.background = 'url(' + src + ') no-repeat top left';
					window.setTimeout("CMS.gallery.diashow.fadeOut()",CMS.gallery.diashow.pause);
				}
			},
			// ------------------------------
			// ------------------------------
			wysiwygNPL : function(ref,id) {
				if (el = document.getElementById('random' + id))
					el.disabled = (!ref.checked);
				if (el = document.getElementById('labelRandom' + id))
					el.disabled = (!ref.checked);
				if (el = document.getElementById('infinite' + id))
					el.disabled = (!ref.checked);
				if (el = document.getElementById('labelInfinite' + id))
					el.disabled = (!ref.checked);
				if (el = document.getElementById('pause' + id))
					el.disabled = (!ref.checked);
				if (el = document.getElementById('labelPause' + id + '-1'))
					el.disabled = (!ref.checked);
				if (el = document.getElementById('step' + id))
					el.disabled = (!ref.checked);
				if (el = document.getElementById('delay' + id))
					el.disabled = (!ref.checked);
				if (el = document.getElementById('labelPause' + id + '-2'))
					el.disabled = (!ref.checked);
				if (el = document.getElementById('labelFading' + id + '-1'))
					el.disabled = (!ref.checked);
				if (el = document.getElementById('labelFading' + id + '-2'))
					el.disabled = (!ref.checked);
				if (el = document.getElementById('labelFading' + id + '-3'))
					el.disabled = (!ref.checked);
			}
		},
		EasyUpload : {
			image: null,
			imageWidth : null,
			imageHeight : null,
			currentSizeLabel : null,
			currentPositionLabel : null,
			elementTop : null,
			elementLeft : null,
			elementWidth : null,
			elementHeight : null,
			checkUpload : function(button) {
				if (!button.form.elements['uploadFile'].value) {
					alert('Bitte eine lokale Bilddatei ausw?hlen.');
					button.form.elements['uploadFile'].focus();
					return false;
				}
				return true;				
			},
			secureUpload : function(form) {
				if (form.elements['action'].value != 'ABORT!') {
					if (form.elements['action'].value == 'SAVE!') {
						form.elements['saveFile'].disabled = true;
					}
					else {
						form.elements['submitFile'].disabled = true;
						if (tm = document.getElementById("TopMessage")) {
							if (tbody = tm.firstChild) {
								if (tr = tbody.firstChild) {
									if (td = tr.firstChild) {
										td.innerHTML = 'Datei wird hochgeladen...';
										tm.style.visibility = 'visible';
									}
								}
							}
						}
					}
				}
			},
			getImage : function() {
				if (!CMS.gallery.EasyUpload.image) {
					CMS.gallery.EasyUpload.image = document.getElementById('EasyUploadImage');
				}
				return CMS.gallery.EasyUpload.image;
			},
			refreshSize : function(iW,iH,aW,aH) {
				if (CMS.gallery.EasyUpload.currentSizeLabel)
					CMS.gallery.EasyUpload.currentSizeLabel.innerHTML = aW + ' x ' + aH + ' Pixel (' +
																		  Math.round((aW * 100) / iW) + '%)';
				if (CMS.gallery.EasyUpload.elementWidth)
					CMS.gallery.EasyUpload.elementWidth.value = aW;
				if (CMS.gallery.EasyUpload.elementHeight)
					CMS.gallery.EasyUpload.elementHeight.value = aH;
			},
			refreshPosition : function(left,top) {
				if (CMS.gallery.EasyUpload.currentPositionLabel)
					CMS.gallery.EasyUpload.currentPositionLabel.innerHTML = left + ' / ' + top + ' Pixel';
				if (CMS.gallery.EasyUpload.elementTop)
					CMS.gallery.EasyUpload.elementTop.value = top;
				if (CMS.gallery.EasyUpload.elementLeft)
					CMS.gallery.EasyUpload.elementLeft.value = left;
			},
			zoomIn : function(iW,iH,gW,gH) {
				if (image = CMS.gallery.EasyUpload.getImage()) {
					var ratio = iW / iH;
					var aW = parseInt(image.style.width);
					var aH = parseInt(image.style.height);
					var nW = aW * 1.05;
					var nH = nW / ratio;
					CMS.gallery.EasyUpload.imageHeight = image.style.height = parseInt(nH);
					CMS.gallery.EasyUpload.imageWidth = image.style.width = parseInt(nW);

					var dH = parseInt((aH - nH) / 2);
					var dW = parseInt((aW - nW) / 2);
					var top = image.style.marginTop = parseInt(image.style.marginTop) + dH;
					var left = image.style.marginLeft = parseInt(image.style.marginLeft) + dW;
					
					CMS.gallery.EasyUpload.refreshSize(iW,iH,parseInt(nW),parseInt(nH));
					CMS.gallery.EasyUpload.refreshPosition(parseInt(left),parseInt(top));
				}
			},
			zoomOut : function(iW,iH,gW,gH) {
				if (image = CMS.gallery.EasyUpload.getImage()) {
					var ratio = iW / iH;
					var aW = parseInt(image.style.width);
					var aH = parseInt(image.style.height);
					if (aW - gW < aH - gH) {
						var nW = Math.max(aW / 1.05, gW);
						var nH = nW / ratio;
					}
					else {
						var nH = Math.max(aH / 1.05, gH);
						var nW = nH * ratio;
					}
					CMS.gallery.EasyUpload.imageHeight = image.style.height = parseInt(nH);
					CMS.gallery.EasyUpload.imageWidth = image.style.width = parseInt(nW);

					var dH = parseInt((aH - nH) / 2);
					var dW = parseInt((aW - nW) / 2);
					var top = image.style.marginTop = Math.max(Math.min(parseInt(image.style.marginTop) + dH,0),-(nH-gH));
					var left = image.style.marginLeft = Math.max(Math.min(parseInt(image.style.marginLeft) + dW,0),-(nW-gW));

					CMS.gallery.EasyUpload.refreshSize(iW,iH,parseInt(nW),parseInt(nH));
					CMS.gallery.EasyUpload.refreshPosition(parseInt(left),parseInt(top));
				}
			},
			moveUp : function(iW,iH,gW,gH) {
				if (image = CMS.gallery.EasyUpload.getImage()) {
					var aH = parseInt(image.style.height);
					var mtop = Math.max(parseInt(image.style.marginTop) - 10,-(aH-gH));
					image.style.marginTop = mtop;
					CMS.gallery.EasyUpload.refreshPosition(parseInt(image.style.marginLeft),mtop);
				}
			},
			moveDown : function(iW,iH,gW,gH) {
				if (image = CMS.gallery.EasyUpload.getImage()) {
					var mtop = Math.min(parseInt(image.style.marginTop) + 10,0);
					image.style.marginTop = mtop;
					CMS.gallery.EasyUpload.refreshPosition(parseInt(image.style.marginLeft),mtop);
				}
			},
			moveLeft : function(iW,iH,gW,gH) {
				if (image = CMS.gallery.EasyUpload.getImage()) {
					var aW = parseInt(image.style.width);
					var mleft = Math.max(parseInt(image.style.marginLeft) - 10,-(aW-gW));
					image.style.marginLeft = mleft;
					CMS.gallery.EasyUpload.refreshPosition(mleft,parseInt(image.style.marginTop));
				}
			},
			moveRight : function(iW,iH,gW,gH) {
				if (image = CMS.gallery.EasyUpload.getImage()) {
					var mleft = Math.min(parseInt(image.style.marginLeft) + 10,0);
					image.style.marginLeft = mleft;
					CMS.gallery.EasyUpload.refreshPosition(mleft,parseInt(image.style.marginTop));
				}
			},
			dragX : null,
			dragY : null,
			dragTop : null,
			dragLeft : null,
			dragImage : false,
			mouseDown : function(event) {
				if (!event) {
					var event = window.event;
					var element = event.srcElement;
					var x = event.x;
					var y = event.y
				}
				else {
					var element = event.target;
					var x = event.screenX;
					var y = event.screenY;
				}
				if (element.id == "EasyUploadImage") {
					CMS.gallery.EasyUpload.dragX = x;
					CMS.gallery.EasyUpload.dragY = y;
					CMS.gallery.EasyUpload.dragTop = parseInt(element.style.marginTop);
					CMS.gallery.EasyUpload.dragLeft = parseInt(element.style.marginLeft);
	   				CMS.gallery.EasyUpload.dragImage = element;
		   			CMS.gallery.EasyUpload.dragImage.style.cursor = 'move';
		   			return false;
		   		}
		   		return true;
			},
			mouseUp : function(event) {
				if (CMS.gallery.EasyUpload.dragImage) {
		   			CMS.gallery.EasyUpload.dragImage.style.cursor = 'default';
   					CMS.gallery.EasyUpload.dragImage = null;
	   				return false;
	   			}
	   			return true;
			},
			mouseMove : function(event) {
				if (CMS.gallery.EasyUpload.dragImage) {
					if (!event) {
						var event = window.event;
						var x = event.x;
						var y = event.y
					}
					else {
						var x = event.screenX;
						var y = event.screenY;
					}
					var dX = x - CMS.gallery.EasyUpload.dragX;
					var dY = y - CMS.gallery.EasyUpload.dragY;
					var mtop = CMS.gallery.EasyUpload.dragImage.style.marginTop = parseInt(Math.max(Math.min(CMS.gallery.EasyUpload.dragTop + dY,0),-(CMS.gallery.EasyUpload.imageHeight-CMS.gallery.height)));
					var mleft = CMS.gallery.EasyUpload.dragImage.style.marginLeft = parseInt(Math.max(Math.min(CMS.gallery.EasyUpload.dragLeft + dX,0),-(CMS.gallery.EasyUpload.imageWidth-CMS.gallery.width)));

					CMS.gallery.EasyUpload.refreshPosition(mleft,mtop);
	
					return false;
				}
				return true;
			}
		}
	},
	// --------------------------------------------------
	// tools: CMS use-/helpfull Functions
	// --------------------------------------------------
	tools : {
		toggleClass : function(ref,class1,class2) {
			if (ref)
				ref.className = (ref.className == class1 ? class2 : class1);
		},
		getParentTag : function(ref,tagName) {
			while (ref && ref.tagName != tagName) {
				ref = ref.parentNode;
			}
			return ref;
		},
		// openWindow: Firefox tabbing workaround
		openWindow : function(href,target) {
			var w = window;

			if (opener)
				w = opener.window;

			w.open(href,target);
		}
	}
};
// ----------------------------------------------------------------------------------------------------
// end of cms.js
// ----------------------------------------------------------------------------------------------------
