/*
 * String Trim
 */
String.prototype.leftTrim = function(){
	return (this.replace(/^\s+/, ""));
};
String.prototype.rightTrim = function(){
	return (this.replace(/\s+$/, ""));
};
//kombiniert "leftTrim" und "rightTrim";
String.prototype.basicTrim = function(){
	return (this.replace(/\s+$/, "").replace(/^\s+/, ""));
};
//dampft leerzeichen(-sequenzen) innerhalb einer zeichenkette auf ein einzelnes "space" ein;
String.prototype.superTrim = function(){
	return (this.replace(/\s+/g, " ").replace(/\s+$/, "").replace(/^\s+/, ""));
};
//zugabe: entfernt alle leerzeichen aus einer zeichenkette;
String.prototype.removeWhiteSpaces = function(){
	return (this.replace(/\s+/g, ""));
};
/*
 * hashChange-Extension
 */
jQuery.extend({
	hashChange: {
		hashChangeCB: [],
		lastHash: null,
		ival: null,
		ietimeout: false
	},
	hashUpdate: function(hash) {
		if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 7) {
			jQuery("#hashChange_iframe").attr('src', 'blank.html?' + hash.slice(1) + hash);
			window.location.hash = hash;
			jQuery.hashChange.lastHash = window.location.hash;
			jQuery.hashChange.ietimeout = true;
		}
		else {
			window.location.hash = hash;
			jQuery.hashChange.lastHash = window.location.hash;
		}
	}
});
jQuery.fn.extend({
	hashChange: function(f){
		if (typeof f == 'function') {
			if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 7 && jQuery('#hashChange_iframe').length == 0) {
				jQuery('<iframe id="hashChange_iframe" style="display: none;"></iframe>').prependTo('body');
				var iframe = jQuery("#hashChange_iframe").get(0).contentWindow.document;
				iframe.open();
				iframe.close();
				iframe.location.hash = window.location.hash;
				window.scrollTo(0, 0);
			}
			jQuery.hashChange.lastHash = window.location.hash;
			jQuery.hashChange.hashChangeCB.push(f);
			jQuery.hashChange.ival = window.setInterval(function(){
				if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 7) {
					if (jQuery.hashChange.ietimeout) {
						jQuery.hashChange.ietimeout = false;
						return;
					}
					var iframe = jQuery("#hashChange_iframe").get(0).contentWindow.document;
					var iframe_hash = iframe.location.hash;
					if (iframe_hash == '#' && window.location.hash == '') {
						iframe_hash = '';
					}
					if (iframe_hash != jQuery.hashChange.lastHash) {
						window.location.hash = iframe_hash;
					}
				}
				if (jQuery.hashChange.lastHash != window.location.hash) {
					jQuery.each(jQuery.hashChange.hashChangeCB, function(k, v){
						v.apply(window.location);
					});
					jQuery.hashChange.lastHash = window.location.hash;
				}
			}, 200);
		}
		else {
			jQuery.each(jQuery.hashChange.hashChangeCB, function(k, v){
				v.apply(window.location);
			});
		}
		return this; //maintain chainability
	}
});
/*
 * Window
 */
jQuery.window = {
	lastZIndex: 100,
	opened: 0,
	open: function(options, e){
		if (jQuery(this).data('opened') == 0) {
			ele = jQuery('<div></div>');
			elemente = ['tl', 'tm', 'tr', 'content', 'bl', 'bm', 'br'];
			if (options.showCenter) {
				var left = ((jQuery(window).width() / 2) + jQuery().scrollLeft()) - (parseInt(options.width) / 2) + (jQuery.window.opened * 10);
				var top = ((jQuery(window).height() / 2) + jQuery().scrollTop()) - (parseInt(options.height) / 2) + (jQuery.window.opened * 10);
			}
			else {
				var left = e.clientX + jQuery().scrollLeft();
				var top = e.clientY + jQuery().scrollTop();
				if (e.clientX + parseInt(options.width) > jQuery(window).width()) {
					left = (left - parseInt(options.width));
				}
				if (e.clientY + parseInt(options.height) > jQuery(window).height()) {
					top = (top - parseInt(options.height)) - 40;
				}
			}
			var rand = Math.round((Math.random() * 1000));
			ele.css({
				position: 'absolute',
				left: left,
				top: top,
				zIndex: (parseInt(jQuery.window.lastZIndex) + 1),
				opacity: '0',
				display: 'none',
				width: options.width,
				height: options.height
			}).attr('id', 'window_' + rand);
			jQuery.each(elemente, function(v, k){
				switch (k)
				{
					case 'tl':
					case 'tr':
					case 'bl':
					case 'br':
						e = jQuery('<div></div>');
						e.attr('class', 'window_' + k).appendTo(ele);
						break;
					case 'tm':
						e = jQuery('<div></div>');
						e.attr('class', 'window_' + k).css({
							width: (parseInt(options.width) - 40) + 'px'
						}).appendTo(ele).html(options.title);
						break;
					case 'bm':
						e = jQuery('<div></div>');
						e.attr('class', 'window_' + k).css({
							width: (parseInt(options.width) - 40) + 'px'
						}).appendTo(ele);
						break;
					case 'content':
						e = jQuery('<div></div>');
						e.css('clear', 'both').appendTo(ele);
						e = jQuery('<div></div>');
						e.attr('class', 'window_' + k).css({
							width: (parseInt(options.width) - 2) + 'px',
							height: (parseInt(options.height) - 43) + 'px'
						}).appendTo(ele);
						break;
				}
			});
			ele.appendTo('body');
			jQuery(this).data('opened', '1');
			jQuery.window.fading.apply(ele);
			jQuery.window.setEvents.apply(ele, [jQuery(this), options]);
			jQuery.window.opened++;
			jQuery.window.lastZIndex++;
			options.onComplete.apply(ele);
		}
	},
	close: function(link){
		jQuery(this).animate({
			opacity: 0
		}, 2000, function(){
			jQuery(this).remove();
		});
		jQuery.window.opened--;
		jQuery(link).data('opened', '0');
	},
	fading: function(){
		if (jQuery(this).css('display') == 'none') {
			jQuery(this).css('display', 'block').animate({
				opacity: 1
			}, 2000);
		}
		else {
			jQuery(this).animate({
				opacity: 0
			}, 2000, function(){
				jQuery(this).css('display', 'none');
			});
		}
	},
	setEvents: function(link, options){
		jQuery('div.window_tr', this).click(function(){
			jQuery.window.close.apply(jQuery(this).parent('div'), [link]);
		});
		jQuery(this).makeDragable({
			handler: jQuery('.window_tm', this).css('cursor', 'move'),
			opacity: 0.8,
			onDrop: function(){
				var zIndex = jQuery.window.lastZIndex + 1;
				this.css('zIndex', zIndex);
			},
			onDrag: function(){
				jQuery.window.lastZIndex++;
				this.css('zIndex', 999999);
			}
		});
		if (options.resizeable) {
			jQuery(this).resizable({
				handles: 'se',
				minWidth: options.width,
				minHeight: parseInt(options.height) + 43,
				resize: function(e, ui){
					width = jQuery(this).css('width');
					height = jQuery(this).css('height');
					jQuery('.window_content', this).css({
						width: (parseInt(width) - 2) + 'px',
						height: (parseInt(height) - 43) + 'px'
					});
					jQuery('.window_tm,.window_bm', this).css({
						width: (parseInt(width) - 40) + 'px'
					});
				}
			});
		}
	}
};
jQuery.fn.extend({
	window: function(options){
		options = jQuery.extend({
			width: 300,
			height: 150,
			resizeable: false,
			title: 'Window',
			showCenter: true,
			onComplete: function(){
			}
		}, options ||
		{});
		this.data('opened', '0');
		return this.click(function(e){
			jQuery.window.open.apply(this, [options, e]);
			return false;
		});
	}
});
/*
 * Dragable
 */
jQuery.makeDragable = {
	dragging: false
};
jQuery.fn.extend({
	makeDragable: function(options){
		options = jQuery.extend({
			opacity: 0.5,
			bgIframe: false,
			onDrag: function(){
			},
			onDrop: function(){
			}
		}, options ||
		{});
		this.each(function(k, i){
			var handler = jQuery(i);
			i = handler.css({
				position: 'absolute',
				margin: 0
			});
			if (options.bgIframe) {
				handler.bgiframe({
					opacity: true
				})
			}
			if (options.handler) {
				handler = jQuery(options.handler, handler);
			}
			handler.bind('mousedown', function(e){
				jQuery.makeDragable.dragging = true;
				options.onDrag.apply(i);
				var difX = e.clientX - parseInt(i.css('left').replace(/px/, '')) + jQuery().scrollLeft();
				var difY = e.clientY - parseInt(i.css('top').replace(/px/, '')) + jQuery().scrollTop();
				jQuery(document).bind('mousemove', function(event){
					i.css({
						left: event.clientX - difX + jQuery().scrollLeft(),
						top: event.clientY - difY + jQuery().scrollTop()
					});
					return false;
				});
				i.animate({
					opacity: options.opacity
				}, 200);
				return false;
			});
			handler.add(document).bind('mouseup', function(){
				if (jQuery.makeDragable.dragging) {
					jQuery.makeDragable.dragging = false;
					i.animate({
						opacity: 1
					}, 200);
					jQuery(document).unbind('mousemove');
					options.onDrop.apply(i);
				}
				return false;
			});
		});
	}
});
/*
 * Einheiten-umrechner
 */
jQuery.extend({
	UnitCalculator: {
		html: '<div style="position:absolute; top:0px; left:0px; display:none;z-index:99px;" id="UnitCalculator">' +
		'<div id="UnitCalculator_head">' +
		'<div id="UnitCalculator_handle">' +
		lang.calc_head +
		'</div>' +
		'<div id="UnitCalculator_close"></div>' +
		'</div>' +
		'<div id="UnitCalculator_content">' +
		'<br>' +
		'<select id="UnitCalculator_from"></select>' +
		'<input type="text" id="UnitCalculator_input"><br>' +
		'<select id="UnitCalculator_to"></select>' +
		'<input type="text" readony="readonly" id="UnitCalculator_output">' +
		'<br><br>' +
		'</div>' +
		'<div id="UnitCalculator_footer"></div>' +
		'</div>',
		units: {
			Leistung: {
				name: lang.calc_leist,
				options: {
					ps: {
						name: 'ps',
						to: {
							kw: {
								name: 'kw',
								value: 0.735499
							}
						}
					},
					kw: {
						name: 'kw',
						to: {
							ps: {
								name: 'ps',
								value: 1.35962
							}
						}
					}
				}	
			},Geschwindigkeit: {
				name: lang.calc_gesch,
				options: {
					knoten: {
						name: 'kn',
						to: {
							kmh: {
								name: 'km/h',
								value: 1.852
							},
							ms: {
								name: 'm/s',
								value: 0.5144
							},
							mph: {
								name: 'mph',
								value: 1.1507
							}
						}
					},
					kmh: {
						name: 'km/h',
						to: {
							ms: {
								name: 'm/s',
								value: 0.2778
							},
							mph: {
								name: 'mph',
								value: 0.6214
							},
							knoten: {
								name: 'kn',
								value: 0.540
							}
						}
					},
					mph: {
						name: 'mph',
						to: {
							kmh: {
								name: 'km/h',
								value: 1.609344
							},
							ms: {
								name: 'm/s',
								value: 0.44704
							},
							knoten: {
								name: 'kn',
								value: 0.8690
							}
						}
					},
					ms: {
						name: 'm/s',
						to: {
							kmh: {
								name: 'km/h',
								value: 3.6
							},
							mph: {
								name: 'mps',
								value: 2.237
							},
							knoten: {
								name: 'kn',
								value: 1.944
							}
						}
					}
				}
			},
			Laengen: {
				name: lang.calc_laengen,
				options: {
					meter: {
						name: 'm',
						to: {
							zoll: {
								name: 'in',
								value: 39.370079
							},
							fuss: {
								name: 'ft',
								value: 3.28084
							},
							km: {
								name: 'km',
								value: 0.001
							},
							mile: {
								name: 'mi',
								value: 0.000621
							},
							smile: {
								name: "sm",
								value: 0.00054
							}
						}
					},
					zoll: {
						name: 'in',
						to: {
							m: {
								name: 'm',
								value: 0.0254
							},
							fuss: {
								name: 'ft',
								value: 0.083333
							},
							km: {
								name: 'km',
								value: 0.000025
							},
							mile: {
								name: 'mi',
								value: 0.000016
							},
							smile: {
								name: "sm",
								value: 0.000014
							}
						}
					},
					fuss: {
						name: 'ft',
						to: {
							zoll: {
								name: 'in',
								value: 12
							},
							meter: {
								name: 'm',
								value: 0.3048
							},
							km: {
								name: 'km',
								value: 0.000305
							},
							mile: {
								name: 'mi',
								value: 0.000189
							},
							smile: {
								name: "sm",
								value: 0.000165
							}
						}
					},
					km: {
						name: 'km',
						to: {
							zoll: {
								name: 'in',
								value: 39370.07874
							},
							fuss: {
								name: 'ft',
								value: 3280.839895
							},
							meter: {
								name: 'm',
								value: 1000
							},
							mile: {
								name: 'mi',
								value: 0.621373
							},
							smile: {
								name: "sm",
								value: 0.539957
							}
						}
					},
					mile: {
						name: 'mi',
						to: {
							zoll: {
								name: 'in',
								value: 63359.842519
							},
							fuss: {
								name: 'ft',
								value: 5279.986877
							},
							km: {
								name: 'km',
								value: 1.60934
							},
							meter: {
								name: 'm',
								value: 1609.34
							},
							smile: {
								name: "sm",
								value: 0.868974
							}
						}
					},
					smile: {
						name: 'sm',
						to: {
							zoll: {
								name: 'in',
								value: 72913.385826
							},
							fuss: {
								name: 'ft',
								value: 6076.115486
							},
							km: {
								name: 'km',
								value: 1.852
							},
							mile: {
								name: 'mi',
								value: 1.150783
							},
							meter: {
								name: "m",
								value: 1852
							}
						}
					}
				}
			},
			Waehrung: {
				name: lang.calc_waehrung,
				options: {
					euro: {
						name: 'Euro',
						to: {
							usd: {
								name: 'US Dollar',
								value: 0.6486
							}
						}
					}
				}
			}
		},
		open: function(e, options){
			jQuery('#UnitCalculator').css({
				left: e.clientX + jQuery().scrollLeft(),
				top: e.clientY + jQuery().scrollTop(),
				display: 'block'
			});
			if (!options.salone) {
				inputUnit = this.innerHTML.replace(/[0-9]/g, "").basicTrim();
				jQuery('#UnitCalculator_from option').each(function(k, v){
					if (v.value == inputUnit) {
						jQuery(v).attr('selected', 'selected');
					}
					else {
						jQuery(v).removeAttr('selected');
					}
				});
			}
			var wert = parseFloat(this.innerHTML);
			if (isNaN(wert)) {
				wert = 1;
			}
			jQuery('#UnitCalculator_input').val(wert);
			jQuery('#UnitCalculator_from').triggerHandler('change');
			jQuery('#UnitCalculator').animate({
				opacity: 1
			}, 500);
		},
		close: function(){
			jQuery('#UnitCalculator').animate({
				opacity: 0
			}, 500, function(){
				jQuery(this).css('display', 'none');
			});
		},
		updateTo: function(){
			var select_from = jQuery('#UnitCalculator_from option:selected');
			var toAppend = '';
			jQuery.each(jQuery.UnitCalculator.units[select_from.parent().attr('value')], function(k, v){
				if (typeof v == 'object') {
					jQuery.each(v, function(k, v){
						if (v.name == select_from.val()) {
							jQuery.each(v.to, function(k, v){
								toAppend += '<option value="' + v.value + '">' + v.name + '</option>';
							});
						}
					});
				}
			});
			if (toAppend != '') {
				jQuery('#UnitCalculator_to').empty().append(jQuery(toAppend));
			}
			jQuery.UnitCalculator.calc.apply(jQuery('#UnitCalculator_input')[0]);
		},
		calc: function(){
			var input = parseFloat(this.value.replace(/,/i, '.'));
			if (!isNaN(input) && input != 0 && input != '') {
				var input_from = jQuery('#UnitCalculator_input');
				var input_to = jQuery('#UnitCalculator_output');
				var faktor = jQuery('#UnitCalculator_to option:selected').val();
				var wert = parseFloat(input_from.val().replace(/,/i, '.')) * faktor;
				input_to.val(wert.toFixed(3));
			}
		}
	}
});
jQuery.fn.extend({
	UnitCalculator: function(options){
		options = jQuery.extend({
			cursor: null,
			salone: false
		}, options ||
		{});
		if (jQuery('#UnitCalculator').length == 0) {
			jQuery(jQuery.UnitCalculator.html).css('opacity', 0).appendTo('body').makeDragable({
				handler: jQuery('#UnitCalculator_handle'),
				opacity: 0.8
			});
			try {
				jQuery('#UnitCalculator_from').change(function(){
					jQuery.UnitCalculator.updateTo.apply(this);
				});
				jQuery('#UnitCalculator_input').keyup(jQuery.UnitCalculator.calc);
				jQuery('#UnitCalculator_to').change(function(){
					jQuery.UnitCalculator.calc.apply(jQuery('#UnitCalculator_input')[0]);
				});
				var toAppendFrom = '';
				var toAppendTo = '';
				jQuery.each(jQuery.UnitCalculator.units, function(k, v){
					if (typeof v == 'object') {
						toAppendFrom += '<optgroup label="' + v.name + '" value="' + k + '">';
						jQuery.each(v.options, function(key, value){
							toAppendFrom += '<option value="' + value.name + '" >' + value.name + '</option>';
						});
						toAppendFrom += '</optgroup>';
					}
				});
				if (toAppendFrom != '') {
					jQuery(toAppendFrom).appendTo('#UnitCalculator_from');
				}
				if (toAppendTo != '') {
					jQuery(toAppendTo).appendTo('#UnitCalculator_to');
				}
				jQuery('#UnitCalculator_close').click(function(){
					jQuery.UnitCalculator.close();
				});
			} 
			catch (e) {
				alert(e + 'ins html');
			}
		}
		jQuery(this).data('UnitCalculator.options', options).click(function(e){
			jQuery.UnitCalculator.open.apply(this, [e, jQuery(this).data('UnitCalculator.options')]);
		}).css({
			cursor: options.cursor ? options.cursor : 'auto'
		});
	}
});
/*
 * TollTip-Extension
 */
jQuery.bToolTipp={focusedShown:false,locked:false,toggle:function(state){if(typeof state=='boolean'){jQuery.bToolTipp.disabled=state}else{jQuery.bToolTipp.disabled=!jQuery.bToolTipp.disabled}},calculateAltPos:function(left,top){}};jQuery.fn.extend({bToolTipp:function(){if(jQuery('#b_tooltip').length<1){jQuery('<div id="b_tooltip" style="display:none;">'+'<div id="tt_arrow"></div>'+'<h3></h3>'+'<div class="body"></div>'+'</div>').appendTo('body')}function showTip(e){if(jQuery.bToolTipp.focusedShown){return}if(e.type=='focus'){this.focused=true;jQuery.bToolTipp.focusedShown=true}var text=this.ttTitle.split(' ~ '),left_pos=e.clientX+15+jQuery().scrollLeft(),top_pos=e.clientY+15+jQuery().scrollTop(),tt_obj=jQuery('#b_tooltip');if(text.length>=2){tt_obj.children('h3').html(text[0]).end().children('div.body').html(text[1]).end()}else{tt_obj.children('h3').html('').end().children('div.body').html(this.ttTitle).end()}if(typeof IE7!='undefined'&&typeof IE7.recalc=='function'){IE7.recalc()}if(!(/input|select/i).test(this.tagName)){jQuery('#tt_arrow').css('display','none');jQuery(this).bind('mousemove',function(event,x,y){var pos_x=(event.clientX?event.clientX:x)+15+jQuery().scrollLeft(),pos_y=(event.clientY?event.clientY:y)+15+jQuery().scrollTop();if(jQuery(window).scrollLeft()+jQuery(window).width()<pos_x+tt_obj.width()){pos_x-=tt_obj.width()+35}if(jQuery(window).scrollTop()+jQuery(window).height()<pos_y+tt_obj.height()){pos_y-=tt_obj.height()+20}tt_obj.css({left:pos_x,top:pos_y})})}else{left_pos=jQuery(this).offset()['left']+jQuery(this).outerWidth()+12;top_pos=jQuery(this).offset()['top'];if(jQuery(window).scrollLeft()+jQuery(window).width()<left_pos+tt_obj.width()){jQuery('#tt_arrow').css({left:tt_obj.width()+10,top:-1,background:'url(design/img/tt_arrow_r.gif)'});left_pos=jQuery(this).offset()['left']-tt_obj.width()-24}else{jQuery('#tt_arrow').css({left:'',top:'',background:'url(design/img/tt_arrow.gif)'})}if(jQuery(window).scrollTop()+jQuery(window).height()<top_pos+tt_obj.height()){jQuery('#tt_arrow').css('display','none');top_pos-=tt_obj.height()+15}tt_obj.css({left:left_pos,top:top_pos})}tt_obj.css('display','block').bgiframe();if(typeof IE7!='undefined'&&typeof IE7.recalc=='function'){IE7.recalc()}}function hideTip(e){if(e.type=='blur'){this.focused=false;jQuery.bToolTipp.focusedShown=false}if(!this.focused&&!jQuery.bToolTipp.focusedShown){jQuery('#tt_arrow').css('display','block');jQuery('#b_tooltip').css('display','none');jQuery(this).unbind('mousemove')}}return this.each(function(){this.ttTitle=this.title;jQuery(this).removeAttr('title')}).focus(showTip).blur(hideTip).hover(showTip,hideTip)}});
/*
 * Link in popoeffnen
 */
jQuery.fn.extend({PopUpLink:function(options){options=jQuery.extend({name:'1aBoote',width:600,height:0,scrollbars:true,dependent:true,resizeable:false,toolbar:false,menubar:false,hotkeys:false,params:[]},options||{});var params=options.params;(options.name?params.push('name='+options.name):null);(options.width?params.push('width='+options.width):null);(options.dependent?params.push('dependent=yes'):null);(options.resizeable?params.push('resizable=yes'):null);(options.toolbar?params.push('toolbar=yes'):null);(options.menubar?params.push('menubar=yes'):null);(options.hotkeys?params.push('hotkeys=yes'):null);(options.scrollbars?params.push('scrollbars=yes'):null);return this.click(function(){window.open(this.href,'1aBoote',params.join(','));return false})}});
/*
 * YoutTube Video Popup
 */
jQuery.fn.extend({
	YoutTubeVidPopup: function() {
		this.click(function() {
			var id = this.rel;
			jQuery('body').append('<div id="yt_overlay" style="display:none; background-color:#000; position:fixed; height:100%; width:100%; top:0; left:0; z-index:1000; cursor:pointer;"></div>')
			.append('<div id="yt_vid" style="display:none; background-color:#fff; position:fixed; top:20px; left:50%; height:10px; width:10px; z-index:1001; padding:2px;">'
				+'<div style="display:none;">'
					+'<object width="425" height="344">'
						+'<param name="movie" value="http://www.youtube.com/v/'+ id +'&hl=en&fs=1"></param>'
						+'<param name="allowFullScreen" value="true"></param>'
						+'<embed src="http://www.youtube.com/v/'+ id +'&hl=en&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed>'
					+'</object>'
				+'</div>'
			+'</div>'
			+'<div id="yt_close" style="display:none; position:fixed; top:370px; left:50%; margin-left:3px; height:40px; width:213px; z-index:1001;">'
				+'<img src="/globaldesign/yt_popup/close.gif" alt="close" />'
			+'</div>');

			jQuery('#yt_overlay').css('opacity', 0).show()
				.add('#yt_close').click(function() {
					jQuery('#yt_vid div').hide();
					jQuery('#yt_overlay, #yt_vid, #yt_close').fadeOut(500, function() {
						jQuery(this).remove();
					});
				})
				.end()
			.animate({
				opacity: 0.5
			}, function() {
				jQuery('#yt_vid').fadeIn(200).animate({
					height: 344,
					width: 425,
					marginLeft: 213*-1
				}, function() {
					jQuery('div', this).show();
					jQuery('#yt_close').css({
						cursor: 'pointer'
					}).fadeIn(500);
				});
			});

			return false;
		});
	}
});
