/*
based on cnet-framework / clientside
Script: IframeShim.js
Iframe shim class for hiding elements below a floating DOM element.
*/

var IframeShim = new Class({
	options: {
		element: false,
		name: ''
	},
	initialize: function (options){
		this.setOptions(options);
		this.element = $(this.options.element);
		if(!this.element) return;
		else this.makeShim();
	},
	makeShim: function(){
		this.shim = new Element('iframe');
		this.id = this.element.id + "_shim";
		if(!this.element.getStyle('z-Index')) this.element.setStyle('z-Index',5);
 		var src = (window.location.protocol == 'https:') ? '//:' : 'javascript:void(0)';
		this.shim.setStyles({
			'position': 'absolute',
			'zIndex': (this.element.getStyle('z-Index')-1<1)?4:this.element.getStyle('z-Index')-1,
			'border': 'none',
			'filter': 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)'
		}).setProperties({
			'src':'https://secure.kempinski.com/booker/blank.htm', // 'javascript:void(0);',
			'frameborder':'0',
			'scrolling':'no',
			'id':this.id
		}).injectInside(document.body);
		if(this.options.display) this.show();
		else this.hide();
	},

/*
		Property: position
		This will reposition the iframe element. Call this when you move or resize
		the iframe element.
*/
	position: function(shim){
		var wasVis = this.element.getStyle('display')!='none';
		if(!wasVis) this.element.setStyle('display','block');
		var coordinates = this.element.getCoordinates();
		if(! wasVis) this.element.setStyle('display','none');
		var offset = {x: 0, y:0};
		if($type(this.options.margin)){
			coordinates.width = coordinates.width-(this.options.margin*2);
			coordinates.height = coordinates.height-(this.options.margin*2);
			offset.x = offset.x + this.options.margin;
			offset.y = offset.y + this.options.margin;
		}
		if($type(this.options.offset)){
			offset.x = this.left-this.options.offset.left;
			offset.y = this.top-this.options.offset.top;
		}
 		this.shim.setStyles({
			'width': coordinates.width + 'px',
			'height': coordinates.height + 'px',
			'left': coordinates.left + 'px',
			'top': coordinates.top + 'px'
		});/* .setPosition({
			relativeTo: this.element,
			offset: offset
		}); */
	},
/*
		Property: hide
		This will hide the IframeShim object. If you don't call this when you
		hide the element that's over the flash or select list, then that thing
		will still be hidden.
*/
	hide: function(){
		this.shim.setStyle('display','none');
	},

/*
		Property: show
		This will obscure any form elements or flash elements below the iframe
		shim element. Call this when you show your floating element.
*/
	show: function(){
		this.shim.setStyle('display','block');
		this.position();
	},
/*
		Property: remove
		This will remove the iframe from the DOM.
*/
	remove: function(){
		this.shim.remove();
	}
});
IframeShim.implement(new Options);
//legacy namespace
var iframeShim = IframeShim;


/*
Changing the Tips Class
*/
Tips.implement({
	options: {
		onShow: function(tip){
			tip.setStyle('visibility', 'visible');
		},
		onHide: function(tip){
			tip.setStyle('visibility', 'hidden');
		},
		maxTitleChars: 30,
		showDelay: 100,
		hideDelay: 100,
		width: false,
		className: 'tool',
		offsets: {'x': 16, 'y': 16},
		fixed: false
	},

	initialize: function(elements, options){
		this.setOptions(options);
		this.toolTip = new Element('div', {
			'class': this.options.className + '-tip',
			'styles': {
				'position': 'absolute',
				'top': '300',
				'left': '0',
				'visibility': 'hidden'
			}
		}).inject(document.body);
		if(this.options.width){
			this.toolTip.setStyle('width', this.options.width);
		}
		this.wrapper = new Element('div').inject(this.toolTip);
		$$(elements).each(this.build, this);
		if (this.options.initialize) this.options.initialize.call(this);
	},

	build: function(el){
		el.myTitle = el.href ? el.href.replace('http://', '') : (el.rel || false);
		if (el.title){
			var dual = el.title.split('::');
			if (dual.length > 1) {
				el.myTitle = dual[0].trim();
				el.myText = dual[1].trim();
			} else {
				el.myText = el.title;
			}
			el.removeAttribute('title');
		} else {
			el.myText = false;
		}
		if (el.myTitle && el.myTitle.length > this.options.maxTitleChars) el.myTitle = el.myTitle.substr(0, this.options.maxTitleChars - 1) + "…";
		el.addEvent('mouseover', function(event){
			this.start(el);
			this.locate(event, el);
		}.bindWithEvent(this));
		el.addEvent('focus', function(event){
			this.start(el);
			this.locate(event, el);
		}.bindWithEvent(this));
		if (!this.options.fixed) el.addEvent('mousemove', this.locate.bindWithEvent(this));
		el.addEvent('mouseout', this.end.bindWithEvent(this));
		el.addEvent('blur', this.end.bindWithEvent(this));
		// hack for IE Browsers
		if (/MSIE (5.5|6.)/.test(navigator.userAgent)) {
			this.tipsShim = new IframeShim({
				element: this.toolTip,
				name: 'iebugfix'
			});
		} // end hack
	},

	start: function(el){
		this.wrapper.setHTML('');
		if (el.myTitle){
			(new Element('h4').injectInside(this.wrapper)).setHTML(el.myTitle);
		}
		if (el.myText){
			(new Element('p').injectInside(this.wrapper)).setHTML(el.myText);
		}
		$clear(this.timer);
		this.timer = this.show.delay(this.options.showDelay, this);
	},

	show: function(){
		this.fireEvent('onShow', [this.toolTip]);
		// hack for IE Browsers
		if (/MSIE (5.5|6.)/.test(navigator.userAgent)) {
			this.tipsShim.show();
		} // end hack
	},

	hide: function(){
		this.fireEvent('onHide', [this.toolTip]);
		// hack for IE Browsers
		 if (/MSIE (5.5|6.)/.test(navigator.userAgent)) {
			this.tipsShim.hide();
		} // end hack
	},

	locate: function(event, el){
		if (event.type != 'focus') {
			var win = {'x': window.getWidth(), 'y': window.getHeight()};
			var scroll = {'x': window.getScrollLeft(), 'y': window.getScrollTop()};
			var tip = {'x': this.toolTip.offsetWidth, 'y': this.toolTip.offsetHeight};
			var prop = {'x': 'left', 'y': 'top'};
			for (var z in prop){
				if(!this.options.fixed){
					var pos = event.page[z] + this.options.offsets[z];
				}else{
					var pos = el.getPosition()[z] + this.options.offsets[z];
				}
				if ((pos + tip[z] - scroll[z]) > win[z]) pos = event.page[z] - this.options.offsets[z] - tip[z];
				this.toolTip.setStyle(prop[z], pos + 'px');
			};
			event.stop();
			// hack for IE Browsers
			if (/MSIE (5.5|6.)/.test(navigator.userAgent)) {
				this.tipsShim.position();
			} // end hack
		}
	}
});

/* PopUp Class */
var Popups = new Class({
	initialize: function(elements){
		this.elements = elements;
		$A(elements).each(function(el){
			el.onclick = function(){
				var aDim = el.rel.match(/[0-9]+/g);
				var contentsWidth = (aDim && (aDim[0] > 0)) ? aDim[0] : 400;
				var contentsHeight = (aDim && (aDim[1] > 0)) ? aDim[1] : screen.availHeight*.7;
				this.newWindow = window.open(el.href, el.text, "width="+contentsWidth+",height="+contentsHeight+",left=30,top=30,scrollbars=yes,resizable=yes");
  				this.newWindow.focus();
				return false;
			}.bind(this);
		}, this);
	}
});

/* Submitbutton change Class */
var Submitfx = new Class({
	initialize: function(elements,forms,options){
		this.elements = elements;
		this.forms = forms;
		this.options = options;
		$A(forms).each(function(fo){
			fo.onsubmit = function(){
				$A(elements).each(function(el){
				el.addClass('loading');
				el.value = this.options.loadingString;
				el.disabled = true;
				}.bind(this));
			}.bind(this);
		}, this);
	}
});

/* this is for slimbox */
/* to do: combine this with Tips class */
/* fixed pos for magnifier does not work in IE because getPosition doesn't work on (unpositioned) images */
var Magnifier = new Class({

	setOptions: function(options){
		this.options = {
			transitionStart: Fx.Transitions.sineInOut,
			transitionEnd: Fx.Transitions.sineInOut,
			maxTitleChars: 30,
			fxDuration: 150,
			maxOpacity: 1,
			timeOut: 100,
			className: 'tooltip'
		}
		Object.extend(this.options, options || {});
	},

	initialize: function(elements, options){
		this.elements = elements;
		this.setOptions(options);
		this.toolTip = new Element('img').setStyle('position', 'absolute').injectInside(document.body);
		this.toolTip.src = 'shared/images/magnifier.gif';
		this.toolTip.alt = 'Show Image';
		this.toolTipHeight = this.toolTip.getPosition().height;
		this.toolTipWidth = this.toolTip.getPosition().width;
		this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: this.options.fxDuration, wait: false}).hide();
		$A(elements).each(function(el){
			el.onmouseover = function(){
				/* for fixed
				var e = {};
				e.clientY = el.getPosition().top + el.getPosition().height / 2 - this.toolTipHeight/2;
				e.clientX = el.getPosition().left + el.getPosition().width / 2 - this.toolTipWidth/2;
				this.locate(e);
				*/
				this.show(el);
				return false;
			}.bind(this);
/* remove for fixed*/	el.onmousemove = this.locate.bindAsEventListener(this);
			el.onmouseout = function(){
				this.timer = $clear(this.timer);
				this.disappear();
			}.bind(this);
		}, this);
	},

	show: function(el){
		this.timer = $clear(this.timer);
		this.fx.options.transition = this.options.transitionStart;
		this.timer = this.appear.delay(this.options.timeOut, this);
	},

	appear: function(){
		this.fx.custom(this.fx.now, this.options.maxOpacity);
	},

	locate: function(evt){
/* remove for fixed*/	var doc = document.documentElement;
/* remove for fixed*/	this.toolTip.setStyles({'top': evt.clientY + doc.scrollTop -7 + 'px', 'left': evt.clientX + doc.scrollLeft -20 + 'px'});
		/* for fixed
		var topPos = evt.clientY;
		var leftPos = evt.clientX;
		this.toolTip.setStyles({'top': topPos + 'px', 'left': leftPos + 'px', 'z-index': 1000});
		*/
	},

	disappear: function(){
		this.fx.options.transition = this.options.transitionEnd;
		this.fx.custom(this.fx.now, 0);
	}

});


/* ----- Google Maps, mostly used on Hotel Sites ------- */
var GoogleMap = new Class({
	getOptions: function(){
		return {
			locationname: 'World',
			address: 'Projensdorfer Str. 324, 24106 Kiel',
			maptype: G_HYBRID_MAP
			// pictureURL: 'http://www.eformation.de/en/kempimap/index-Dateien/logo_allgemein.gif'
			// geocoordinates: {'lat': 37.423021, 'long': -122.083739}
		};
	},

	initialize: function(element, options){
		this.setOptions(this.getOptions(), options);
		this.element = element;

		if(this.options.address){
			this.location = this.options.address;
		}
		map = new GMap2(document.getElementById("googlemap"));
		icon = new GIcon();
		options = this.options;
		var geocoder = new GClientGeocoder();

		/* --- Basic configuration --- */
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.enableScrollWheelZoom();
		map.enableContinuousZoom();

		/* --- Create new icon --- */
		icon.image =  "http://www.eformation.de/shared/images/kmarker.png";
		icon.shadow = "http://www.eformation.de/shared/images/kmarker-shadow.png";
		icon.iconSize = new GSize(30, 46);
		icon.shadowSize = new GSize(66, 46);
		icon.iconAnchor = new GPoint(15, 45);
		icon.infoWindowAnchor = new GPoint(15, 45);

		/* --- On Click Information --- */
		onclickInfo =
				'<img src=../images//"'+this.options.pictureURL+'/" style=\"float: left; margin-right: 6px;\">'+
				'<strong>'+this.options.locationname+'</strong>'+
				'<br />'+this.options.address+'<br />';

		/* Call geocoder */
		if(!this.options.geocoordinates && this.options.address){
			geocoder.getLocations(this.options.address, this.setMap);
		}else{
			this.setMap();
		}

	},

	/* --- Is called when geocoder responds --- */
	setMap:	function (response){
			if (!response || response.Status.code != 200) {
				/* use geocoding from options */
				point = new GLatLng(this.options.geocoordinates.lat,
									this.options.geocoordinates.long);
			} else {
				/* when using google geocoder */
				place = response.Placemark[0];
				point = new GLatLng(place.Point.coordinates[1],
									place.Point.coordinates[0]);
			}
			marker = new GMarker(point, icon);
			map.setCenter(point,6,this.options.maptype);
			map.addOverlay(marker);
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(onclickInfo);
			});
		}
});
GoogleMap.implement(new Options);

function sprintf() {
	if( sprintf.arguments.length < 2 ) {
		return;
	}
	if (sprintf.arguments[0]) {
		var data = sprintf.arguments[0];
	} else {
		return;
	}
	for( var k=1; k<sprintf.arguments.length; ++k ) {
		switch(typeof(sprintf.arguments[k])) {
			case 'string':
				data = data.replace( /%s/, sprintf.arguments[k] );
				break;
			case 'number':
				data = data.replace( /%d/, sprintf.arguments[k] );
				break;
			case 'boolean':
				data = data.replace( /%b/, sprintf.arguments[k] ? 'true' : 'false' );
				break;
			default:
				/* function | object | undefined */
				break;
		}
	}
	return(data);
}

if( !String.sprintf ) {
	String.sprintf = sprintf;
}

Element.extend({
	getParentByMatch: function(tagName, className) {
		// if no tag or class is specified, simply get the parent element
		if (tagName == null && className == null) return $(this.parentNode);

		// store this element to a local var
		var el = this;

		// if a tag and class are specified
		if (tagName && className) {
			// work your way up the DOM by parent element
			while (el = $(el.parentNode)) {
				// if tag equals the elment's tag name and matches the class regex, return that element
				if (tagName.toLowerCase() == el.getTag() && el.className.test(className)) {
				  return el;
				}
			}
		} else if (tagName && className == null) {	// a tag but no class specified
			// work your way up the DOM by parent element
			while (el = $(el.parentNode)) {
				// if tag equals the elment's tag name, return that element
				if (tagName.toLowerCase() == el.getTag()) {
				  return el;
				}
			}
		}
		// you should never get to this point but this is here just in case
		return null;
	}
});