document.write('<script type="text/javascript" src="/js/jquery.livequery.js"></script>');
document.write('<script type="text/javascript" src="/js/jquery.tooltip.mod.js"></script>');
document.write('<script type="text/javascript" src="/js/jquery.history.js"></script>');
document.write('<script type="text/javascript" src="/js/jquery.lightbox-0.5.pack.js"></script>');
document.write('<script type="text/javascript" src="/js/jquery.form.js"></script>');
document.write('<script type="text/javascript" src="/js/jquery.scrollTo-min.js"></script>');
document.write('<script type="text/javascript" src="/js/jquery.localscroll-1.2.7-min.js"></script>');
document.write('<script type="text/javascript" src="/js/jquery.cookie.js"></script>');

var isIE = false;
var ajaxFormSubmitted;
var cookieName = 'showAnimations';
var cookieOptions = { path: '/', expires: 10 };
var showAnimations = true;
var visitedLinks = new Array();

$.ajaxSetup( { timeout: 2000 } ); 

function ajaxIndicator(action)
{
	if (action == 'show') {
		$('#ajax-loader').show();
		$('html').addClass('busy');
	} else {
		$('#ajax-loader').hide();
		$('html').removeClass('busy');	
	}
};

function pageLoad(target) 
{
	target = target.replace(/\.html/, '');
	bodyId = $('body').attr('id').replace(/www-schafft-biz---/, '');
	if (target == bodyId) {
		return false;
	}
	var loc = document.URL.split('/')[3];
	if (!target) {
		target = bodyId+'.html';
		$.historyLoad(target);
		return false;
	}

	ajaxIndicator('show');
	
	$.get(target+'.xml', function(xml) {
		appendDynamically();
		var myContent = $('content', xml).text().replace(/\{.*\}/ig, '');
		myContent = myContent.replace(/<!-- BEGIN ([\w_]+) -->([\s\S]*)<!-- END \1 -->/gim, '<div id="$1" class="on-demand">$2</div>');
		myContent = myContent.replace(/(<(code|textarea)\b[^>]*>)([\S\s]*?)<\/\2>/gim, function ($1, $2, $3, $4) { return $2+$4.replace(/</gim, '&lt;').replace(/(&#171;|&#187;)/gim, '&quot;')+'</'+$3+'>';});
		var myClaim = $('claim', xml).text();
		var myNotepadContent = $('notepadContent', xml).text();
		
		var myExtraContent = $('extraContent', xml).text();
		
		document.title = $('title', xml).text()+' ::: schafft.biz, Essen';
		$('body').attr('id', target);
		$('#claim-content').html(myClaim);
		$('#notepad-bg').html(myNotepadContent+'<span class="bg"> </span>');

		if (!isIE) {
			$.scrollTo(0, 400);

			oldBg = $('#claim div').css('backgroundImage');
			$('#claim div').attr('class', target);
			newBg = $('#claim div').css('backgroundImage');

			$('div#content').animate(
				{ opacity: 'hide' },
				750,
				'', function() {
					$('#illustration div').attr('class', target);
					$('#notepad-bg').attr('class', target);
					$('div#content div#inner').replaceWith('<div id="inner">'+myContent+'</div>');
					ajaxIndicator('hide');
					$('#nav li').children().each(function(i) {
						if (this.tagName == 'A') {
							if ($(this).attr('href').substr(1) == target+'.html') {
								formerHref  = $(this).attr('href');
								formerTitle = $(this).data('titleOrg');
								$(this).replaceWith('<span class="x" title="'+formerTitle+'" formerHref="'+formerHref+'"><span></span>'+$(this).text()+'</span>');
							}
						} else if (this.tagName == 'SPAN') {
							newHref  = $(this).attr('formerHref');
							newTitle = $(this).attr('title');
							if (!newHref) newHref = '/'+this.id+'.html';
							$(this).replaceWith('<a href="'+newHref+'" title="'+newTitle+'"><span></span>'+$(this).text()+'</a>');
						}
					});
				}
			);

			$('div#sidebar-content').animate(
				{ opacity: 'hide' },
				'slow',
				'', function() {
					$('div#sidebar-content div#sidebar-content-inner').replaceWith('<div id="sidebar-content-inner">'+myExtraContent+'</div>');
				}
			);

			if (oldBg != newBg) {
				$('#claim div:first').fadeTo(2500, 1, function() {
					$('#claim').attr('class', 'c').addClass('c-'+target);
					$('#claim div:first').fadeTo(0, 0);
				});
				$('#illustration div').fadeTo(2500, 1, function() {
					$('#illustration').attr('class', 'i').addClass('i-'+target);
					$('#illustration div').fadeTo(0, 0);
				});
			} else {
				$('#claim').attr('class', 'c').addClass('c-'+target);
				$('#illustration').attr('class', 'i').addClass('i-'+target);
			}
			$('#notepad span.bg').fadeTo(2500, 1, function() {
				$('#notepad-bg').attr('class', target);
			});

			$('div#content').animate({ opacity: 'show' }, 750);
			$('div#sidebar-content').animate({ opacity: 'show' }, 750);

		} else {

			$('div#content').show(function() {
				oldBg = $('#claim div').css('backgroundImage');
				$('#claim div').attr('class', target);
				$('#illustration div').attr('class', target);
				$('div#content div#inner').replaceWith('<div id="inner">'+myContent+'</div>');
				ajaxIndicator('hide');
				newBg = $('#claim div').css('backgroundImage');
				/*
				$('#nav li').children().each(function(i) {
					if (this.tagName == 'A') {						
						if ($(this).attr('href').substr(1) == target+'.html') {
							formerHref  = $(this).attr('href');
							alert(formerHref);
							formerTitle = $(this).data('titleOrg');
							$(this).replaceWith('<span class="x" title="'+formerTitle+'" formerHref="'+formerHref+'"><span></span>'+$(this).text()+'</span>');
						}
					} else if (this.tagName == 'SPAN') {
						newHref  = $(this).attr('formerHref');
						newTitle = $(this).attr('title');
						if (!newHref) newHref = '/'+this.id+'.html';
						$(this).replaceWith('<a href="'+newHref+'" title="'+newTitle+'"><span></span>'+$(this).text()+'</a>');
					}
				});
				*/
				$.scrollTo(0, 400);
			});
			
			
			$('div#sidebar-content div#sidebar-content-inner').replaceWith('<div id="sidebar-content-inner">'+myExtraContent+'</div>');
		
			if (oldBg != newBg) {
				$('#claim div').show(function() {
					$('#claim').attr('class', 'c').addClass('c-'+target);
					$('#claim div:first').hide();
				});

				$('#illustration div').show(function() {
					$('#illustration').attr('class', 'i').addClass('i-'+target);
					$('#illustration div').hide();
				});
			}
			
			$('#notepad span.bg').show(function() {
				$('#notepad-bg').attr('class', target);
			});			
			
			$('div#content').show();
		}

	});
};

function appendDynamically(appendTo, appendWhat)
{
	if ($('#appended-dynamically').length == 0) {
		$(appendTo).append('<div id="appended-dynamically">'+appendWhat+'</div>');
	} else {
		$('#appended-dynamically').remove();
	}
};

function toggleStyles()
{
	$('html head link').each(function() {
		if ($(this).data('formerStylesheet')) {
			$(this).attr('href', $(this).data('formerStylesheet'));
			$(this).data('formerStylesheet', '');
			$('a.toggle-styles').livequery(function() {
				$(this).text($(this).text().replace(/ ein/, ' aus'));
				$(this).next().css('display', 'none');
				$(this).next().addClass('on-demand');
				$('body').css('paddingTop', '0');
			});
		} else if ($(this).attr('rel') == 'stylesheet') {
			$(this).data('formerStylesheet', $(this).attr('href'));
			$(this).attr('href', '/styles/blank.css');
			$('a.toggle-styles').livequery(function() {
				if ($(this).next().hasClass('on-demand')) {
					$(this).text($(this).text().replace(/ aus/, ' ein'));
					$(this).next().css({
						'background-color' : '#F1EEC8', 
						'font-family' : 'Tahoma',
						'font-size' : '11px',
						'display' : 'block',
						'position' : 'absolute',
						'top' : '0',
						'left': '0',
						'width' : '79%',
						'padding': '5px 20% 5px 1%',
						'border-bottom' : '1px solid gray'
					});
					$(this).next().removeClass('on-demand');
					$('body').css('paddingTop', '2em');
				}
			});	
		}
	});
};

function IEredraw()
{
	if (isIE && parseInt($.browser.version.substr(0,1)) == 6) {
		$('#wrapper-footer').css('display', 'inline');
		$('#wrapper-footer').css('display', 'block');
	}
};

function bindAnimations()
{
	$.historyInit(pageLoad);

	if ($.browser.msie) {
		isIE = true;
		if (parseInt($.browser.version.substr(0,1)) < 8) {
			$('#content a, #sidebar-content a, #footer a').livequery(function() {
				if (this.href.substr(0,7) != 'http://' || (this.href.substr(0,7) == 'http://' && this.href.substr(0,22) == 'http://www.schafft.biz')) {
					this.href = this.href.replace(/.*\/(.+)\.html/, "/#$1.html"); ;
				}
			});
		}
	} else {
		isIE = false;
	}

	$('html').ajaxError(function(event, request, settings) { 
		ajaxIndicator('hide');
	}); 	
	
	$('a[rel=lightbox]').livequery(function() {
			$('a[rel=lightbox]').lightBox({
			overlayOpacity: 0.5,
			containerResizeSpeed: 350,
			imageLoading: '/images/layout/3.0/ajax-loader-img.gif',
			txtImage: 'Bild',
			txtOf: 'von'
		});
	});
	
	$('span.long-desc').livequery(function() {
		$(this).before('<a class="show-long-desc" href="#">mehr &hellip;</a>');
	});

	$('a.show-long-desc').live('click', function() {
		$(this).next().slideToggle('normal');
		$(this).parent().parent().find('.on-demand').slideToggle(function() {
			IEredraw();
		});
		$(this).text($(this).text().substr(0,4) == 'mehr' ? $(this).text().replace(/mehr/, 'wieder ausblenden') : $(this).text().replace(/wieder ausblenden/, 'mehr'));
		$(this).blur();
		return false;
	});

	$('label').livequery(function() {
		$(this).css('cursor', 'help').tooltip({
		    track: true,
		    delay: 0,
		    showURL: false,
		    opacity: 0.9,
		    fixPNG: true,
		    top: 0,
		    left: 15,
			fade: (isIE ? false : 500)
		});
	});

	$('a[href$=html], a[href$=/], a[href^=http]').livequery(function() {

		$(this).tooltip({
		    track: true,
		    delay: 0,
		    showURL: false,
		    opacity: 0.9,
		    fixPNG: true,
		    top: 0,
		    left: 15,
			fade: (isIE ? false : 500)
		});

		if (this.href.substr(0,7) != 'http://' || (this.href.substr(0,7) == 'http://' && this.href.substr(0,22) == 'http://www.schafft.biz')) {
			var ajaxUrl = $(this).attr('href').substr(1).replace(/#/, '');
			if (ajaxUrl == '') ajaxUrl = 'index.html';
			if (jQuery.inArray(ajaxUrl, visitedLinks) != -1) {
				$(this).addClass('visited');
			}
			$(this).click(function() {
				if (jQuery.inArray(ajaxUrl, visitedLinks) == -1) {
					visitedLinks.push(ajaxUrl);
				}
				$.historyLoad(ajaxUrl);
				return false;
			});
		} else {
			$(this).attr('target', '_blank');
		}
	});

	$('input.text, textarea.text').livequery(function() {
		$(this).focus(function() {
			$(this).addClass('focused');
			$(this).removeClass('error');
			$(this).prevAll('label.error').removeClass('error');
			if ($(this).val().substr(0,1) == '<') {
				$(this).data('valueOld', $(this).val());
				$(this).val('');
			}			
		});
		$(this).blur(function() {
			$(this).removeClass('focused');
			if ($(this).val() == '') {
				$(this).val($(this).data('valueOld'));
			} else {
				$(this).parent().next('.on-demand').slideUp();
			}
		});
	});

    $('form.ajax-form').livequery(function() {
		$(this).submit(function() {
			if ($('#'+$(this).attr('id').replace(/-/, '_').toUpperCase()+'_SUCCESS').length > 0) {
				myFormId = $(this).attr('id').replace(/-/, '_').toUpperCase();
				$(this).ajaxSubmit({
					beforeSubmit: function(formData, myForm, options) {
						ajaxFormSubmitted = myForm;
						$(myForm).addClass('form-submitted');
						options.url = options.url.replace(/\.html/, '.xml');						
						ajaxIndicator('show');					
					},
					success: function(responseXML) {
						ajaxIndicator('hide');
						if ($('success', responseXML).text() == 'true') {
							$('form.form-submitted').removeClass('form-submitted').slideUp('slow', function() {
								$('#content p:first').hide();
								$('#'+ajaxFormSubmitted.attr('id').replace(/-/, '_').toUpperCase()+'_SUCCESS').fadeIn('slow');
							});
						} else {
							$('error', responseXML).each(function() {
								$('label[for='+$(this).text()+']').addClass('error');
								$('#'+$(this).text()).addClass('error');
								$('#'+myFormId+'_ERROR_'+$(this).text().toUpperCase()).slideDown();
							});
							if (isIE && parseInt($.browser.version.substr(0,1)) == 6) {
								$('#wrapper-footer').css('display', 'inline');
								$('#wrapper-footer').css('display', 'block');
							}
						}
					},
					dataType: 'xml'
				});
				return false;
			}
		});
	});
	
	bindFunctions();
};

function bindFunctions()
{
	$('#content, #sidebar-content, #footer').localScroll({lazy:true});
	
	$('a.toggle-images').livequery('click', function() {
		$('html body *').each(function() {
			if (this.tagName == 'IMG') {
				imgSrc = $(this).attr('src');
				if (imgSrc == '/images/layout/3.0/transp.gif') {
					$(this).attr('src', $(this).data('formerSrc'));				
				} else {
					$(this).data('formerSrc', $(this).attr('src'));
					$(this).attr('src', '/images/layout/3.0/transp.gif');				
				}
			}  else {
				$(this).toggleClass('no-bg');	
			}
		});
			
		$(this).text().indexOf('ein') != -1 ? $(this).text($(this).text().replace(/ ein/, ' aus')) : $(this).text($(this).text().replace(/ aus/, ' ein'));
		$(this).next().slideToggle(function() {
			IEredraw();
		});
		return false;
	});
	
	$('a.toggle-styles').livequery('click', function() {
		toggleStyles();
		$.scrollTo(0, 400);	
		return false;
	});
	
	$('a#toggle-animations').livequery('click', function() {
		if ($.cookie(cookieName) != -1) {
			$(this).text($(this).text().replace(/ aus/, ' ein'));
			$.cookie(cookieName, -1, cookieOptions);
			unbindAnimations();
		} else {
			$(this).text($(this).text().replace(/ ein/, ' aus'));
			$.cookie(cookieName, 1, cookieOptions);
			bindAnimations();			
		}
		$(this).blur();
		return false;
	});	
};

function unbindAnimations()
{
	$('*').unbind();
	bindFunctions();
};

$(document).ready(function() {

	if ($.cookie(cookieName) == null || $.cookie(cookieName) == 1) {
		bindAnimations();
	} else {
		bindFunctions();
		showAnimations = false;
	}
	
	$('ul#website-functions').append('<li><a href="#" id="toggle-animations">Animationen '+(showAnimations ? 'aus' : 'ein')+'schalten</a></li>');
});

function openMailDialogue(obj) {
	var m = obj.innerHTML;
	m = m.replace(/\s+at\s+/, '@');
	document.location = 'mailto:'+m;
};