// cmsbox (cola light)
// // // // // // // // // // // // // // // // // // // // // // // // // //
var Cmsbox = {
	
	// information
	version: 2.0,
	copyright: '(c) Copyright 2007 cmsbox GmbH. All Rights Reserved.',
		
	// initialization
	setup: function () { 
	},
	
	/*
	 * images
	 * cropping behavior
	 */
	crop: function (element) {
		element = $(element);
		var image = element.down("img");
		var relative = Position.cumulativeOffset(element);
		if (image) {
			element.observe('mousemove', function (event) {
				var x = (Event.pointerX(event) - relative[0]) / element.offsetWidth;
				var y = (Event.pointerY(event) - relative[1]) / element.offsetHeight;
				image.style.left = -1 * (image.offsetWidth - element.offsetWidth) * x + "px";
				image.style.top = -1 * (image.offsetHeight - element.offsetHeight) * y + "px";
			}.bindAsEventListener(this));
		}
	},
	
	/*
	 * lighbox
	 * initializazion
	 */
	lightbox: function (lightbox, overlay) {
		lightbox = $(lightbox); overlay = $(overlay);
		var page = Position.pageBounds(), window = Position.windowBounds();
		overlay.style.height = (page[1] > window[1] ? page[1] : window[1]) + 'px';
		lightbox.style.marginTop = Position.windowOffset()[1] + 'px'; // stay where you are
		$$("#desk input", "#desk select", "#desk textarea").each(function(each){ 
			if (Prototype.Browser.IE)
				each.style.visibility="hidden";
			each.disabled="true";
		});
		lightbox.style.display = overlay.style.display = 'block';
	},
	
	/*
	 * lightbox
	 * closing behavior
	 */
	lightboxCloseHandler: function (url, overlay) {
		document.onkeyup = function (event) {
			if (event.keyCode == Event.KEY_ESC) window.location=url;
		}.bindAsEventListener(this);
		$(overlay).onclick = function () {
			window.location = url;
		};
	},
	
	/* 
	 * transfer encoding
	 */
	decode: function (input) {
		var i = 0;
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		do {
			enc1 = keyStr.indexOf(input.charAt(i++));
			enc2 = keyStr.indexOf(input.charAt(i++));
			enc3 = keyStr.indexOf(input.charAt(i++));
			enc4 = keyStr.indexOf(input.charAt(i++));
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
			output += String.fromCharCode(chr1);
			if (enc3 != 64) output += String.fromCharCode(chr2);
			if (enc4 != 64) output += String.fromCharCode(chr3);
		} while (i < input.length);
		return output;
	},
	
	/*
	 *	page-navigation
	 *	hover behavior for menu items
	 */
	navigation: function(element) {
		element = $(element);
		if (element) {
			element.observe('mouseover', function (event) {
				element.childElements().select(function(each) { 
					return each.hasClassName('navi'); }).each(function(each) { 
						each.addClassName('hover');
						each.setStyle( { display:'block' });
				}); // #tagNavigation:edition:
			}.bindAsEventListener(this));
			element.observe('mouseout', function (event) {
				element.childElements().select(function(each) { 
					return each.hasClassName('navi'); }).each(function(each) {  // #tagNavigation:edition:
						marked = each.childElements().detect(function(hoverable) { // do not hide on selected editons
							return hoverable.hasClassName('mark'); 
						});
						if (!marked) {
							each.removeClassName('hover');
							each.setStyle( { display:'none' });								
						}
					}); 
			}.bindAsEventListener(this));
			return true;
		}
		return false;
	},
	
	/*
	 *	toolbar-menu
	 *	some browsers need javascript to enable hover behavior
	 */
	menu: function(element) {
		if (Prototype.Browser.IE) {
			element = $(element);
			if (element) {
				element.observe('mouseover', function (event) {
					element.childElements().select(function(each) { 
						return each.tagName == 'dd' | each.tagName == 'DD'; }).each(function(each) {
							each.addClassName('hover');
							each.setStyle( { display:'block' });
						});
				}.bindAsEventListener(this));
				element.observe('mouseout', function (event) {
					element.childElements().select(function(each) { 
						return each.tagName == 'dd' | each.tagName == 'DD'; }).each(function(each) { 
							each.removeClassName('hover');
							each.setStyle( { display:'none' });
						});
				}.bindAsEventListener(this));
				return true;
			}
			return false;
		}
		return true;
	},
	
	decode_utf8: function(utf8encoded) {
		var plain = ""; 
		var i=0; 
		var c=c1=c2=0;
		while(i<utf8encoded.length) {
			c = utf8encoded.charCodeAt(i);
			if (c<128) {
				plain += String.fromCharCode(c);
				i++;
			} else if((c>191) && (c<224)) {
				c2 = utf8encoded.charCodeAt(i+1);
				plain += String.fromCharCode(((c&31)<<6) | (c2&63));
				i+=2;
			} else {
				c2 = utf8encoded.charCodeAt(i+1); 
				c3 = utf8encoded.charCodeAt(i+2);
				plain += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
				i+=3;
			}
		}
		return plain;
	},
		
	encode_utf8: function(plain) {
		plain = plain.replace(/\r\n/g,"\n");
		var utf8encoded = "";
		for(var n=0; n<plain.length; n++) {
			var c=plain.charCodeAt(n);
			if (c<128)
				utf8encoded += String.fromCharCode(c);
			else if((c>127) && (c<2048)) {
				utf8encoded += String.fromCharCode((c>>6)|192);
				utf8encoded += String.fromCharCode((c&63)|128);
			} else {
				utf8encoded += String.fromCharCode((c>>12)|224);
				utf8encoded += String.fromCharCode(((c>>6)&63)|128);
				utf8encoded += String.fromCharCode((c&63)|128);
			}
		}
		return utf8encoded;
	}
	
	/*,
	infobox: function(infobox, element) {
		element = $(element);
		infobox = $(infobox);
		console.log(infobox);
		console.log(element);
		if (element && infobox) {
			// left decision
			var leftCenter = Position.cumulativeOffset(element)[0] + (element.getDimensions().width / 2) - (infobox.getDimensions().width / 2) - 20 ;
			var leftAlternative = Math.max(Position.pageBounds()[0] - infobox.getDimensions().width - 20, 20);
			infobox.style.left = (Position.pageBounds()[0] > leftCenter + infobox.getDimensions().width + 20 ? leftCenter : leftAlternative) + 'px';
			// top decision
			var topUnder = Position.cumulativeOffset(element)[1] + element.getDimensions().height + 20;
			if (Position.windowBounds()[1] > topUnder + infobox.getDimensions().height) {
				infobox.style.top = topUnder + 'px';
			} else {
				var topOver = Position.cumulativeOffset(element)[1] - 20;
				if (Position.windowBounds()[1] > topOver - infobox.getDimensions().height) {
					infobox.style.top = topOver + 'px';
				} else {
					infobox.style.top = (Position.windowBounds()[1] / 2) + 'px';
				}
			}
			new Effect.Pulsate(infobox, { delay: 0.2, duration: 2.0 });
			window.setTimeout('new Effect.Fade(\'' + infobox.id + '\');$(\'' + infobox.id + '\').style.display = \'none\';', 5000);
		}
	}
	*/	
	
};

/*
 *	browser
 *	get the dimension of the visible window-viewport
 */
Position.windowBounds = function () {
	var x = window.innerWidth || 
		self.innerWidth ||
		document.documentElement.clientWidth ||
		document.body.clientWidth ||
		0;
	var y = window.innerHeight ||
		self.innerHeigth ||
		document.documentElement.clientHeight ||
		document.body.clientHeight ||
		0;
	return [x, y]; 
};

/*
 *	browser
 *	get the dimension of the visible page
 */
Position.windowOffset = function () {
	var x = window.pageXOffset ||
		document.documentElement.scrollLeft ||
		document.body.scrollLeft || 
		0;
	var y = window.pageYOffset ||
		document.documentElement.scrollTop ||
		document.body.scrollTop ||
		0;
	return [x, y];
};

/*
 *	browser
 *	get the dimension of the entire page
 */
Position.pageBounds = function () { 
	var x = document.body.scrollWidth || 0;
	var y = (window.innerHeight && window.scrollMaxY ?
		window.innerHeight + window.scrollMaxY : 
		document.body.scrollHeight) || 0;
	return [x, y]; 
};

/* 
 *	floating menus
 *	get the absolute global position of an element
 */
Object.extend(Element.Methods, {
  getPosition: function(element) {
	var pos = { x:0, y:0 };
	var obj = element;
	do {
		pos.x += obj.offsetLeft;
		pos.y += obj.offsetTop;
	} while (obj = obj.offsetParent);
	// IE below 6.0 don't understand position = fixed -> see Supergirl._show()
	/*@cc_on
		@if (@_jscript_version < 5.7)
			pos = { x:0, y:0 };
		@end
	@*/
    return Object.toJSON(pos);
  }
});

/* 
 *	event handling
 *	extent event to catch right and middle click
 */
Object.extend(Event, {
	isRightClick: function(event) {
		return (((event.which) && (event.which == 3)) || ((event.button) && (event.button == 2)));
	},
	isMiddleClick: function(event) {
		return (((event.which) && (event.which == 4)) || ((event.button) && (event.button == 1)));
	}
});