if (/iPad|iPhone|iPod|Android/.test(navigator.userAgent)) {
	var is_mobile = true;
} else {
	var is_mobile = false;
}

/*
 * jQuery wrapper for iScroll
 * http://www.sanraul.com/2011/01/12/jquery-wrapper-for-iscroll/
 */
(function($){
    $.fn.iscroll = function(options){
		if(this.data('iScrollReady') == null){
			var that = this;
            var options =  $.extend({}, options);
				options.onScrollEnd = function(){
					that.triggerHandler('onScrollEnd', [this]);
				};
			arguments.callee.object  = new iScroll(this.get(0), options);
			// NOTE: for some reason in a complex page the plugin does not register
			// the size of the element. This will fix that in the meantime.
			setTimeout(function(scroller){
				scroller.refresh();
			}, 1000, arguments.callee.object);
			this.data('iScrollReady', true);
		}else{
			arguments.callee.object.refresh();
		}
		return arguments.callee.object;
	};
})(jQuery);


/*
 * jQuery afterresize event plugin
 * http://github.com/roxeteer/jquery-afterresize
 *
 * Copyright (c) 2010 Visa Kopu, visa@visakopu.net
 *
 * Licensed under the BSD license.
 * http://github.com/roxeteer/jquery-afterresize/wiki/License
 */
(function(b){var a=function(d){var c=b(this);if(c.data("resizetimer")){window.clearTimeout(c.data("resizetimer"))}c.data("resizetimer",window.setTimeout(function(){c.trigger("afterresize")},300))};b.event.special.afterresize={add:function(c){b(this).bind("resize",a);var d=c.handler;c.handler=function(e){return d.apply(this,arguments)}},remove:function(c){b(this).unbind("resize",a)}};b.fn.extend({afterresize:function(c){if(b.isFunction(c)){b(this).bind("afterresize",c)}else{b(this).trigger("afterresize")}return this}})})(jQuery);


/*jslint browser: true */ /*global jQuery: true */

/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

// TODO JsDoc

/**
 * Create a cookie with the given key and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String key The key of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given key.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String key The key of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function (key, value, options) {
	
	// key and at least value given, set cookie...
	if (arguments.length > 1 && String(value) !== "[object Object]") {
		options = jQuery.extend({}, options);

		if (value === null || value === undefined) {
			options.expires = -1;
		}

		if (typeof options.expires === 'number') {
			var days = options.expires, t = options.expires = new Date();
			t.setDate(t.getDate() + days);
		}
		
		value = String(value);
		
		return (document.cookie = [
			encodeURIComponent(key), '=',
			options.raw ? value : encodeURIComponent(value),
			options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
			options.path ? '; path=' + options.path : '',
			options.domain ? '; domain=' + options.domain : '',
			options.secure ? '; secure' : ''
		].join(''));
	}

	// key and possibly options given, get cookie...
	options = value || {};
	var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
	return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};


// $ UI Datepicker / Timepicker
$(function($) {
	$.datepicker.regional['de'] = {
		prevText:        'zurück',
		nextText:        'weiter',
		monthNames:      ['Jänner','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'],
		dayNames:        ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
		dayNamesMin:     ['So','Mo','Di','Mi','Do','Fr','Sa'],
		firstDay:        1,
		buttonText:      'Kalender',
		isRTL:           false
	};
	$.datepicker.regional['en'] = {
		buttonText:      'Calendar'
	};
	$.datepicker.regional['fr'] = {
		prevText:        'préc',
		nextText:        'suiv',
		monthNames:      ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre'], 
		dayNames:        ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'], 
		dayNamesMin:     ['Di','Lu','Ma','Me','Je','Ve','Sa'],
		firstDay:        0,
		buttonText:      'Calendrier',
		isRTL:           false
	};
	$.datepicker.regional['it'] = {
		prevText:        'prec',
		nextText:        'succ',
		monthNames:      ['Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'], 
		dayNames:        ['Domenica','Lunedì','Martedì','Mercoledì','Giovedì','Venerdì','Sabato'], 
		dayNamesMin:     ['Do','Lu','Ma','Me','Gi','Ve','Sa'], 
		firstDay:        1,
		buttonText:      'Calendario',
		isRTL:           false
	};
	$.datepicker.setDefaults($.datepicker.regional[$('html').attr('lang')]);
	$.datepicker.setDefaults({
		buttonImage:     '/fileadmin/template/img/datepicker.png',
		buttonImageOnly: true,
		dateFormat:      'yy-mm-dd', // ISO 8601
		minDate:         '0y',
		showOn:          'both',
		showOtherMonths: true,
		beforeShowDay:  function(date) {
			if (date.getDay() == 0 || date.getDay() == 6) { 
				return [false,""]; 
			} else {
				return [true, ""];
			}
		}
	});
	
	$.timepicker.regional = {
		'en': {
			currentText:   'Now',
			closeText:     'Done',
			ampm:          false,
			timeOnlyTitle: 'Choose time',
			timeText:      'Time',
			hourText:      'Hour',
			minuteText:    'Minute',
			secondText:    'Second',
			timezoneText:  'Time zone'
		},
		'de': {
			currentText:   'jetzt',
			closeText:     'Ok',
			ampm:          false,
			timeOnlyTitle: 'Zeit wählen',
			timeText:      'Zeit',
			hourText:      'Stunde',
			minuteText:    'Minute',
			secondText:    'Sekunde',
			timezoneText:  'Zeitzone'
		}
	};
	
	$.timepicker.setDefaults($.timepicker.regional[$('html').attr('lang')]);
	$.timepicker.setDefaults({
		buttonImage:     '/fileadmin/template/img/timepicker.png',
		buttonImageOnly: true,
		stepHour: 1,
		stepMinute: 15,
		stepSecond: 10,
		hourMin: 9,
		hourMax: 16,
		timeFormat:    'hh:mm tt'
	});
});


function addPickers() {
	$('input[type=date], input[type=time]').each(
		function() {
			element = $(this);
			replacement = element.clone().attr('type', 'text');
			element.replaceWith(replacement);
			switch(element.attr('type')) {
				case 'date':
					replacement.datepicker();
					break;
				case 'time':
					replacement.timepicker({timeFormat:    'hh:mm tt'});
					break;
				case 'datetime':
					replacement.datetimepicker();
					break;
			}
		}
	);
}

function home_supersize(resize) {
	if(resize) {
		// resize
		$('.home #end>div').each(function() {
			$(this).css({
				'width'  : '',
				'height' : ''
			});
		});
	} else {
		// ready
		$('body.home').append('<ul class="endcontrols"><li class="prev">‹</li><li class="next">›</li></ul>');
	}

	$('.home #end .teaser>img').each(function() {
		width = 1280;
		height = 1024;
		
		maxW   = $(window).width();
		maxH   = $(window).height();
		
		ratio = Math.max(maxW/width, maxH/height);
		
		$(this).css({
			'width':       Math.ceil(width*ratio) + 'px',
			'height':      Math.ceil(height*ratio) + 'px',
			'margin-left': '-' + (((width*ratio)-maxW)/2) + 'px',
			'margin-top':  '-' + (((height*ratio)-maxH)/2) + 'px'
		});
	});
		
	if(!resize) {
		if($('.home #end .section>.teaser').length > 1) {
			$('.home #end')
				.cycle(
					{
						fx:     'fade',
						timeout: 10000,
						speed:   (!is_mobile ? 3000 : 0),
						prev: 'body.home .endcontrols .prev',
						next: 'body.home .endcontrols .next'
					}
				);
				
			$('body.home .endcontrols>li').bind(
				'click',
				function() {
					$('.home #end').cycle('pause');
				}
			);
		}
	}
}

function init_menu2() {
	switch($('#menu2>ul>li>a').width()) {
		case 472:
			items = 2;
			break;
		case 310:
			items = 3;
			break;
		case 228:
			items = 4;
			break;
		default:
			items = 5;
			break;
	}
	
	if($('#menu2>ul>li').length > items) {
		if(!is_mobile) {
			$('#menu2')
				.append('<ul class="controls"><li class="prev"></li><li class="next"></li></ul>')
				.append('<div class="slider-parent ui-corner-all"><div class="slider" /></div>');

			$('#menu2 ul.img').clone().removeClass('img').addClass('text').appendTo('#menu2');
			$('#menu2 ul.text a').removeAttr('style').slice(0, items).addClass('cur');
			$('#menu2 ul.text>li').css('width', ((800/$('#menu2>ul.text>li').length)-4) + 'px');
			
			$('#menu2 ul.img').css('width', ($('#menu2 ul.img>li').length*($('#menu2 ul.img>li>a').width()+15))+'px').wrap('<div class="scroller" />');

			$('.slider').slider(
				{
					min: 0,
					max: ($('#menu2 ul.img>li').length-items),
					step: 0.1,
					slide: function(event, ui) {
						$('#menu2 ul.img').stop(true).animate(
							{
								left: '-' + ui.value*($('#menu2 ul.img>li>a').width()+15) + 'px'
							}, 50
						);
						$('#menu2 ul.text a').removeClass('cur').slice(Math.round(ui.value), items+Math.round(ui.value)).addClass('cur');
					},
					change: function(event, ui) { 
						$('#menu2 ul.img').stop(true).animate(
							{
								left: '-' + Math.round(ui.value)*($('#menu2 ul.img>li>a').width()+15) + 'px'
							}, 250, function() { $('.slider').slider('value', Math.round(ui.value)); }
						);
						$('#menu2 ul.text a').removeClass('cur').slice(Math.round(ui.value), items+Math.round(ui.value)).addClass('cur');
					}
				}
			);

			handle_width = (800/$('#menu2 ul.img>li').length*items);
			slider_width = 800-handle_width;

			$('#menu2 .ui-slider .ui-slider-handle').css({
				width: Math.floor(handle_width) + 'px',
				marginLeft: '-' + Math.floor(handle_width/2) + 'px'
			});
			
			$('#menu2 .slider').css({
				width: Math.floor(slider_width) + 'px',
				marginLeft: Math.floor((800-slider_width)/2) + 'px'
			});
			
			$('#menu2 .next').click(
				function() {
					$('.slider').slider('value', ($('.slider').slider('value') + 1));
				}
			);
			$('#menu2 .prev').click(
				function() {
					$('.slider').slider('value', ($('.slider').slider('value') - 1));
				}
			);

		} else {
			$('#menu2 ul.img>li:last-child').css('margin-right', '0');
			$('#menu2 ul.img').css('width', (($('#menu2 ul.img>li').length*($('#menu2 ul.img>li>a').width()+15))-15)+'px');
			$('#menu2').jScrollPane();
		}
	}
}


function toggle_background(show) {
	if (show == true) {
		$('body.home #end').cycle('resume');
		$('body.home').css('background-color', '');
		$('body.home .endcontrols').css('display', '');
		$('body.home #end').css('opacity', '');
	} else {
		$('body.home #end').cycle('pause');
		$('body.home').css('background-color', '#000000');
		$('body.home .endcontrols').css('display', 'none');
		$('body.home #end').css('opacity', '.33');
	}
}

$(window).afterresize(function() {
	home_supersize(true);
});


$(document).ready(function() {

	addPickers();
	$('body')
		.ajaxStop(
			function() {}
		)
		.ajaxStop(
			function() {
				addPickers();
			}
		);


	// teaser und content element
	$('body:not(.home) #teaser .section:has(.teaser:has(.overlay)), #end .section:has(.teaser:has(.overlay))').each(function() {
		$(this).next().appendTo($('.overlay', this));
	});
	
	home_supersize();
	// wrap span element into a.button
	$('a.button:not(:has(input, span))').wrapInner('<span />');
	
	// zoom icon für Bilder
	$('.csc-textpic-imagewrap a[href$=".jpg"], .csc-textpic-imagewrap a[href$=".png"], .csc-textpic-imagewrap a[href$=".gif"], .csc-textpic-imagewrap a[href$=".jpeg"] ').append('<span class="image_zoom" />');
		
	$('div:not(.csc-textpic-border) .csc-textpic-image dt, div:not(.csc-textpic-border) .csc-textpic-single-image, div:not(.csc-textpic-border) li.csc-textpic-image').each(function(){
		$(this)
			.addClass('border-radius')
			.append('<div class="top-left" />')
			.append('<div class="top-right" />')
			.append('<div class="bottom-left" />')
			.append('<div class="bottom-right" />');
	});
	

	// font size selector
	$('#aside .inner').append('<div id="fontsize"><ul><li class="small">A</li><li class="medium">A</li><li class="large">A</li></ul></div>');
	
	
	switch($.cookie('fontsize')) {
		case 'large':
			$('body').css('font-size', '16px');
			$('#fontsize li:eq(2)').addClass('cur');
			break;
		case 'medium':
			$('body').css('font-size', '14px');
			$('#fontsize li:eq(1)').addClass('cur');
			break;
		case 'small':
		default:
			$('body').css('font-size', '12px');
			$('#fontsize li:eq(0)').addClass('cur');
			break;
	}
	
	$('#fontsize li').click(
		function() { 
			$('#fontsize li').removeClass('cur');
			
			$.cookie('fontsize', '', { expires: -1, path: '/' });
			switch($(this).attr('class')) {
				case 'small':
					$('body').css('font-size', '12px');
					$.cookie('fontsize', 'small', { expires: 365, path: '/' });
					break;
				case 'medium':
					$('body').css('font-size', '14px');
					$.cookie('fontsize', 'medium', { expires: 365, path: '/' });
					break;
				case 'large':
					$('body').css('font-size', '16px');
					$.cookie('fontsize', 'large', { expires: 365, path: '/' });
					break;
			}
			$(this).addClass('cur');
		}
	);
	
	$('#sitemap>.section').slideUp(0);
	$('#sitemap a.button').toggle(
		function() {
			$('html').animate(
				{ scrollTop: $(document).height()},
				500
			);
			
			$('body.home #menu li').removeClass('root');
			$('body.home #teaser').empty().css('display', 'none');
			
			$('body.home .img4.on').trigger('click');
			$('#aside .inner').css('border-bottom-width', '0');
			$('#sitemap .button').addClass('button_on');
			$('#sitemap>.section').slideDown(
				500,
				function() {
					toggle_background(false);
				}
			);
		},
		function() {
			$('body.home #menu li').removeClass('root');
			$('body.home #teaser').empty().css('display', 'none');
			
			$('#sitemap>.section').slideUp(
				250,
				function() {
					toggle_background(true);
					$('#aside .inner').css('border-bottom-width', '1px');
					$('#sitemap .button').removeClass('button_on');
				}
			);
		}
	);
	
	if (!is_mobile) {
		// menu, startseite
		$('body.home #aside').append('<div id="teaser" />');
		$('body.home #menu li:not(.root)')
			.live(
				'click',
				function(e) {
					$('body.home #menu li').removeClass('root');
					$('body.home #teaser .submenu').css('display', 'none');	
					if($(this).filter('.menu44, .menu176, .menu46').length != 0) {
						e.preventDefault();
						$('body.home #col0').slideUp(
							500,
							function() {
								$('body.home .img4').removeClass('on');
							}
						);
						$('#sitemap>.section').slideUp(
							250,
							function() {
								$('#aside .inner').css('border-bottom-width', '1px');
								$('#sitemap .button').removeClass('button_on');
							}
						);
						toggle_background(false);
						$(this).addClass('root');
						$('body.home #teaser')
							.empty()
							.css('display', 'block');
						$.ajax({
							type: 'GET',
							url: $('a', this).attr('href'),
							data: 'type=10',
							dataType: 'xml',
							success: function(xml) {
								
								$('body.home #teaser').append($('menu2', xml).text());
								init_menu2();
							}
						});
					} 
				}
			);		
	}
	
	
	$('body.home #end, body.home #header, body.home #aside .inner').bind(
		'click',
		function() {
			toggle_background(true);
			$('body.home #menu li').removeClass('root');
			$('body.home #teaser').css('display', 'none');
			$('body.home #col0').slideUp(
				500,
				function() {
					$('body.home .img4').removeClass('on');
				}
			);
			$('#sitemap>.section').slideUp(
				250,
				function() {
					$('#aside .inner').css('border-bottom-width', '1px');
					$('#sitemap .button').removeClass('button_on');
				}
			);
		}
	);
	
	// menu2
	if($('#menu2').length == 1) {
		init_menu2();
	}
	
	// teaser controls
	if($('body:not(.home) #teaser .section>.teaser, body.home #end .section>.teaser').length > 1) {
		$('body:not(.home) #teaser .section:has(.teaser)')
			.wrapAll('<div class="scroller" />')
			.parent('.scroller')
			.after('<ul class="controls"><li class="prev">‹</li><li class="next">›</li></ul>')
			
		$('#teaser .scroller')
			.cycle(
				{
					fx:     'scrollHorz',
					easing: 'easeInOutQuad',
					timeout: 5500,
					speed:   1000,
					random:  0,
					pager: '#teaser .controls',
					pagerAnchorBuilder: function(index, slide) {
						return '<li><a href="#">' + (index+1) + '</a></li>';
					},
					prev: '#teaser .prev',
					next: '#teaser .next',
					activePagerClass: 'cur',
					updateActivePagerLink: function(pager, currSlide, activePagerClass) { 
						$('li', pager)
							.removeClass(activePagerClass) 
							.filter('li:eq('+(currSlide+2)+')').addClass(activePagerClass); 
					}
				}
			);
			
			$('#teaser ul.controls>li').bind(
				'click',
				function() {
					$('#teaser .scroller').cycle('pause');
				}
			);
			
			$('body').keypress(function(event) {
				switch(event.keyCode) {
					case 37:
						$('#teaser .scroller').cycle('prev');
						break;
					case 39:
						$('#teaser .scroller').cycle('next');
						break;
				}
			});
	}
	
	
	$('.gallery').each(function() {
		$(this)
			.delay(10).queue(function() {
				width = 0;
				$('.csc-textpic-image', this).each(function() {
					width = width + $(this).outerWidth(true);
				});
				$('.csc-textpic-imagecolumn', this).width(width + 'px');
				$(this).dequeue();
			})
			.delay(10).queue(function() {
				if($('.csc-textpic-imagecolumn', this).width() > 972) {
					$('.csc-textpic-imagewrap', this).jScrollPane();
				}
			});
	});
	
	$('#col3 .frame2 dl').each(function() {
			$('dd', this).css('cursor', 'pointer');
			$(this).bind(
				'click',
				function() {
					window.location.href = $('a:eq(0)', this).attr('href');
				}
			);
		}
	);
	
	$('body:not(.home) #teaser .teaser .overlay .close, body.home #end .teaser .overlay .close').bind(
		'click',
		function() {
			$('body.home #end').cycle('pause');
			$('body:not(.home) #teaser .scroller').cycle('pause');
			$('body:not(.home) #teaser .teaser .overlay:not(.overlay_nobg), body.home #end .teaser .overlay:not(.overlay_nobg)').fadeOut(
				'fast',
				function() {
					$(this).css('display', 'none');
					$('body:not(.home) #teaser .teaser .open, body.home #end .teaser .open').each(function() {
						if($(this).parent().find('.overlay:not(.overlay_nobg)').length == 1) {
							$(this).fadeIn('fast');
						}
					});
				}
			);

		}
	);
	
	$('body:not(.home) #teaser .teaser .open, body.home #end .teaser .open').bind(
		'click',
		function() {
			$('body:not(.home) #teaser .teaser .overlay, body.home #end .teaser .overlay').fadeIn('fast');
			$('body:not(.home) #teaser .teaser .open, body.home #end .teaser .open').fadeOut('fast');
		}
	);
	
	// Google Site Search fix
	$('#search form, [name=tx_indexedsearch]').bind(
		'submit', 
		function (e){
			var form   = $(this);
			var action = form.attr('action');
			form.attr(
				'action',
				action + ((action.indexOf('?') == '-1') ? '?' : '&') + 'q=' + encodeURIComponent($('[name="tx_indexedsearch[sword]"]', form).val())
			);
		}
	);
	
	// Form placeholder

	var input = $('#search input#q');
	var label = $('#search label[for=q]');
	input.bind(
		'click focus change',
		function() {
			label.css('visibility', 'hidden');
			
			$.ajax({
				type: 'GET',
				url: window.location.href,
				data: 'type=124787&no_cache=1',
				dataType: 'xml',
				success: function(xml) {
					var data = $('autocomplete>item', xml).map(function(i) {
						return {
							uid      : $(this).attr('uid'),
							value    : $('value', this).text(),
							title    : $('title', this).text(),
							subtitle : $('subtitle', this).text(),
							image    : $('image', this).text(),
							link     : $('link', this).text()
						}
					}).get();
					
					$('#q').autocomplete({
						source: data,
						position: {
							my        : 'left top',
							at        : 'left top',
							collision : 'none',
							offset    : '-5, -5'
						},
						minLength: 3,
						open: function(event, ui) {
							$('.ui-autocomplete').css('z-index', '2005');
							$('#search').css('z-index', '2010');
						},
						focus: function( event, ui ) {
							$('#q').val(ui.item.title);
							return false;
						},
						select: function(event, ui) { 
							window.location.href = ui.item.link;
							//$('#q').parent('form').trigger('submit');
						}
					}).data('autocomplete')._renderItem = function(ul, item) {
						return $('<li></li>')
							.data( "item.autocomplete", item )
							.append( '<a><img src="/' + item.image + '"/>' + item.title + '<br /><span class="subtitle">' + item.subtitle + '</span></a>' )
							.appendTo(ul);
					}
					
					$('#q').autocomplete().data('autocomplete')._renderMenu = function(ul, items) {
						var self = this;
						$.each( items, function( index, item ) {
							if (index < 5) {
								self._renderItem(ul, item);
							}
						});
						$('<li><a class="button"><span>Alle Suchergebnisse</span></a></li>').appendTo(ul).bind(
							'click',
							function() {
								$('form[name=quick_search]').trigger('submit');
							}
						);
					}
				}
			});
		}
	);
	input.bind(
		'blur',
		function() {
			if (input.val() == '') {
				label.css('visibility', '');
			}
		}
	);
	
	$('#sitelinks .menu171 a').bind(
		'click',
		function(event) {
			event.preventDefault();
			$('body.home #menu li').removeClass('root');
			$('body.home #teaser').empty().css('display', 'none');
			$('body.home #col0').slideUp(
				500,
				function() {
					$('body.home .img4').removeClass('on');
				}
			);
			$('#sitemap>.section').slideUp(
				250,
				function() {
					$('#aside .inner').css('border-bottom-width', '1px');
					$('#sitemap .button').removeClass('button_on');
				}
			);
			toggle_background(true);
			element = $(this);
			//element.parent().css('visibility', 'hidden');
			$('#sitelinks li').css('z-index', '10');
			$('#sitelinks .menu172').css('z-index', '10000').css('visibility', 'visible');
			$('body').append('<div id="callback_overlay" /><div id="callback"><span class="before" /><div class="content"></div><span class="after" />');
			$('#share_by_email_overlay, #share_by_email').remove();
			$.ajax({
				type: 'GET',
				url: element.attr('href'),
				data: 'type=10',
				dataType: 'xml',
				success: function(xml) {
					$('#callback .content').empty().append($('form', xml).text());
				}
			});
			
			$('#callback_overlay').animate(
				{opacity: 0.85},
				0,
				function() {
					$('body.home #end').cycle('pause');
					$('#callback').css('display', 'block');
				}
			).height($('html').height());
		}
	);
	
	$('#sitelinks .menu172 a').bind(
		'click',
		function(event) {
			event.preventDefault();
			$('body.home #menu li').removeClass('root');
			$('body.home #teaser').empty().css('display', 'none');
			$('body.home #col0').slideUp(
				500,
				function() {
					$('body.home .img4').removeClass('on');
				}
			);
			$('#sitemap>.section').slideUp(
				250,
				function() {
					$('#aside .inner').css('border-bottom-width', '1px');
					$('#sitemap .button').removeClass('button_on');
				}
			);

			toggle_background(true);
			element = $(this);
			//element.parent().css('visibility', 'hidden');
			$('#sitelinks li').css('z-index', '10');
			$('#sitelinks .menu171').css('z-index', '10000').css('visibility', 'visible');
			$('body').append('<div id="share_by_email_overlay" /><div id="share_by_email"><span class="before" /><div class="content"></div><span class="after" />');
			$('#callback_overlay, #callback').remove();
			$.ajax({
				type: 'GET',
				url: element.attr('href'),
				data: 'type=10',
				dataType: 'xml',
				success: function(xml) {
					$('#share_by_email .content').empty().append($('form', xml).text());
					$('#share_by_email input[name=mf_url]').val(window.location.href);
					$('#sharebyemailimg>img').clone().appendTo($('#sharebyemail_img_preview'));
					$('#sharebyemail_emailimg').attr(
						'value',
						$('#sharebyemail_img_preview>img').attr('src')
					);
				}
			});
	
			$('#share_by_email_overlay').animate(
				{opacity: 0.85},
				0,
				function() {
					$('#share_by_email').css('display', 'block');
				}
			).height($('html').height());
		}
	);
	
	$('#callback .before, #share_by_email .before, #callback_overlay, #share_by_email_overlay').live(
		'click',
		function(e) {
			e.preventDefault();
			$('#callback').remove();
			$('#share_by_email').remove();
			$('#sitelinks li').css('z-index', '10000').css('visibility', 'visible');
			//$('#sitelinks .menu171, #sitelinks .menu172').css('z-index', '10000').css('visibility', 'visible');
			$('#callback_overlay, #share_by_email_overlay').animate(
				{opacity: 0.0},
				0,
				function() {
					$(this).remove();
				}
			);
		}
	);
	
	$('body.home #col0 .section:has(.img4)').prependTo('#content');
	$('body.home #col0').slideUp(0);
	
	$('body.home .img4').bind(
		'click',
		function() {
			toggle_background(false);
			$('body.home #menu li').removeClass('root');
			$('body.home #teaser').empty().css('display', 'none');
			
			$('body.home #col0').slideDown(
				500,
				function() {
					$('body.home .img4').addClass('on');
				}
			);
		}
	);
	
	$('body.home .img4.on').live(
		'click',
		function() {
			$('body.home #menu li').removeClass('root');
			$('body.home #teaser').empty().css('display', 'none');
			
			$('body.home #col0').slideUp(
				500,
				function() {
					$('body.home .img4').removeClass('on');
					toggle_background(true);
				}
			);
		}
	);
	
	$('body.home #col0').append('<div class="scroller" /><ul class="controls"><li class="prev">‹</li><li class="next">›</li></ul>');
	$('body.home #col0 .section').appendTo('body.home #col0 .scroller');
	$('body.home #col0 .scroller .section:nth-child(3n-2)').each(
		function() {
			$(this).wrap('<div class="sectiongroup" />');
			$(this).parent().next().appendTo($(this).parent());
			$(this).parent().next().appendTo($(this).parent());
		}
	);
	$('body.home #col0 .scroller').cycle(
		{
			fx:     'scrollHorz',
			timeout: 5500,
			speed:   500,
			pager: 'body.home #col0 .controls',
			pagerAnchorBuilder: function(index, slide) {
				return '<li><a href="#">' + (index+1) + '</a></li>';
			},
			prev: 'body.home #col0 .prev',
			next: 'body.home #col0 .next',
			activePagerClass: 'cur',
			updateActivePagerLink: function(pager, currSlide, activePagerClass) { 
				$('li', pager)
					.removeClass(activePagerClass) 
					.filter('li:eq('+(currSlide+2)+')').addClass(activePagerClass); 
			}
		}
	).cycle('pause');
	
	$('body.home #col3').append('<div class="scroller" /><ul class="controls"><li class="prev">‹</li><li class="next">›</li></ul>');
	$('body.home #col3 .section').appendTo('body.home #col3 .scroller');
	$('body.home #col3 .scroller').cycle(
		{
			fx:     'scrollVert',
			timeout: 5500,
			speed:   500,
			prev: 'body.home #col3 .prev',
			next: 'body.home #col3 .next'
		}
	);
	


	$('#callback .tx-thmailformplus-pi1 form, #share_by_email .tx-thmailformplus-pi1 form').live('submit', function(e) {
			e.preventDefault();
			
			$.ajax({
				cache: false,
				type: $(this).attr('method'),
				url: $(this).attr('action'),
				data: $(this).serialize(),
				dataType: 'xml',
				success: function (xml) {					
					$('#callback .content, #share_by_email .content').empty().append($('form', xml).text());					
				}
			});
		}
	);
	
	// Language selection
	$('#lang .cur a').live(
		'click',
		function(e) {
			e.preventDefault();
			if ($('#lang').hasClass('on')) {
				$('#lang').removeClass('on');
			} else {
				$('#lang').addClass('on');
			}
		}
	);
	$('html').live(
		'click',
		function(e) {
			if ($(e.target).closest('#lang.on')[0] == null ) {
				$('#lang').removeClass('on');
			}
		}
	);

	$('#lang li:not(.cur)').live(
		'click',
		function(e) {
			$('#lang li').removeClass('cur');
			$(this).addClass('cur');
			$('#lang').removeClass('on');
		}
	);
});

