$(document).ready(function() {




	$('body').addClass('js-active');




	/**
	*
	*	ADD MOUSE WHEEL SUPPORT
	*
	**/
	
	/* Copyright (c) 2006 Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
	 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
	 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
	 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
	 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
	 *
	 * $LastChangedDate: 2007-12-20 09:02:08 -0600 (Thu, 20 Dec 2007) $
	 * $Rev: 4265 $
	 *
	 * Version: 3.0
	 * 
	 * Requires: $ 1.2.2+
	 */

	(function($) {

	$.event.special.mousewheel = {
		setup: function() {
			var handler = $.event.special.mousewheel.handler;

			// Fix pageX, pageY, clientX and clientY for mozilla
			if ( $.browser.mozilla )
				$(this).bind('mousemove.mousewheel', function(event) {
					$.data(this, 'mwcursorposdata', {
						pageX: event.pageX,
						pageY: event.pageY,
						clientX: event.clientX,
						clientY: event.clientY
					});
				});

			if ( this.addEventListener )
				this.addEventListener( ($.browser.mozilla ? 'DOMMouseScroll' : 'mousewheel'), handler, false);
			else
				this.onmousewheel = handler;
		},

		teardown: function() {
			var handler = $.event.special.mousewheel.handler;

			$(this).unbind('mousemove.mousewheel');

			if ( this.removeEventListener )
				this.removeEventListener( ($.browser.mozilla ? 'DOMMouseScroll' : 'mousewheel'), handler, false);
			else
				this.onmousewheel = function(){};

			$.removeData(this, 'mwcursorposdata');
		},

		handler: function(event) {
			var args = Array.prototype.slice.call( arguments, 1 );

			event = $.event.fix(event || window.event);
			// Get correct pageX, pageY, clientX and clientY for mozilla
			$.extend( event, $.data(this, 'mwcursorposdata') || {} );
			var delta = 0, returnValue = true;

			if ( event.wheelDelta ) delta = event.wheelDelta/120;
			if ( event.detail     ) delta = -event.detail/3;

			event.data  = event.data || {};
			event.type  = "mousewheel";

			// Add delta to the front of the arguments
			args.unshift(delta);
			// Add event to the front of the arguments
			args.unshift(event);

			return $.event.handle.apply(this, args);
		}
	};

	$.fn.extend({
		mousewheel: function(fn) {
			return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
		},

		unmousewheel: function(fn) {
			return this.unbind("mousewheel", fn);
		}
	});

	})(jQuery);














	/**
	*
	*	INITIALIZE SCROLLBARS
	*
	**/

	$('.scroll-area').jScrollPane();














	/**
	*
	*	SERVICE NAV SUB MENUS
	*
	**/

	$('#btn-websites, #btn-languages').each(function(a, b) {
	
		$(b)
			.mouseenter(function() {
				$(this).find('>a').addClass('hover');
				$(this).find('ul').stop(true, true).slideDown(150);
			})
			.mouseleave(function() {
				$(this).find('>a').removeClass('hover');
				$(this).find('ul').slideUp(150);
			});
	
	});











	/**
	*
	*	SEARCH + AJAX CALL FOR SEARCH
	*
	**/
	
	search_manager = {
	
		_transition_duration: 500,
		
		_is_loading: false,
		
		//
		//	init
		//
		init: function() {

			$('#btn-search .search-form:first input.query:first').attr('title',$('#btn-search .search-form:first label.query:first').text());

			$('#btn-search>a').click(function(e) {
				e.preventDefault();
				if($(this).next('.search-form').is(':visible')) {
					$(this).removeClass('hover');
					$(this).next('.search-form').stop(true, true).slideUp(150);
					$('#search-results').slideUp(150);
				} else {
					$(this).addClass('hover');
					$(this).next('.search-form').slideDown(150, function() {
						$('#FORM0_query').focus();
					});
					if($('#btn-search .search-form:first input.query:first').attr('value') != '' && $('#btn-search .search-form:first input.query:first').attr('value') != $('#btn-search .search-form:first input.query:first').attr('title')) {
						$('#search-results').slideDown(150);
						$('#search-results .scroll-area').jScrollPane();
					}
				}

			});

			$('.btn-close-search').click(function(e) {

				e.preventDefault();
				if($(this).parents('.search-form').is(':visible')) {
					$(this).parents('#btn-search').find('>a').removeClass('hover');
					$(this).parents('.search-form').stop(true, true).slideUp(150);
					$('#search-results').slideUp(150);
				}

			});

			$('#btn-search .search-form:first input.query:first').attr('value', $('#btn-search .search-form:first input.query:first').attr('title'));
			$('#btn-search .search-form:first input.query:first').bind({
				focus: function() {
					if($(this).attr('value') == $('#btn-search .search-form:first input.query:first').attr('title')) {
						$(this).attr('value', '');
					}
				},
				blur: function() {
					if($(this).attr('value') == '') {
						$(this).attr('value', $('#btn-search .search-form:first input.query:first').attr('title'));
					}
				}
			});

			$('#search-form').submit(function(e) {

				e.preventDefault();
				
				if($('#btn-search .search-form:first input.query:first').attr('value') != '' && $('#btn-search .search-form:first input.query:first').attr('value') != $('#btn-search .search-form:first input.query:first').attr('title')) {
					search_manager.load();
				}

			});
			
		},
		
		//
		//	load
		//
		load: function() {
		
			if(!search_manager._is_loading) {
			
				search_manager._is_loading = true;

				if($('#search-results').is(':visible')) {
					$('#search-results').prepend('<div id="search-ajax-loading"><img src="'+TEMPLATE_DIR+'css/imgs/loading.gif" alt="" /></div>');
				}
				
				params = new Object();
				$('#search-form input.hidden').each(function(i) {
					obj = $('#search-form input.hidden:eq('+i+')');
					params[obj.attr('name')] = obj.val();
      				});
      				
				params[$('#search-form').attr('name')+'[query]'] = $('#search-form input.query:first').attr('value');
				
        if(typeof(_gaq)=='object')
        {
          _gaq.push(function() {
				    var pageTracker = _gat._getTracker('UA-16675431-1');
				    link.href = pageTracker._getLinkerUrl($('#search-form').attr('action')+($('#search-form').attr('action')));
				  });
				}
				
				$.ajax({ 
					type: 'POST', 
					url: $('#search-form').attr('action')+($('#search-form').attr('action').indexOf('?')>-1 ? '&' : '?')+'json=1', 
					dataType: 'json',
					cache: false,
					
					data: params,

					success: function(data, status) {

						search_manager.switch_contents(data);
						search_manager._is_loading = false;

					},

					error: function() {
						$('#search-ajax-loading').remove();
						search_manager._is_loading = false;
					}

				});
			}
		
		},
		
		//
		//	switch contents
		//
		switch_contents: function(json) {
		
			$('#search-ajax-loading').remove();
			$('#search-results div.scroll-area').html(json.search_results+'<br class="clear" />');
			$('#search-results div.result-count').html(json.result_count);
			if(json.search_results == '') { $('#search-results div.results-container').hide(); } else { $('#search-results div.results-container').show(); }
			$('#search-results').slideDown(150);
			$('#search-results .scroll-area').jScrollPane();
			
			content_call.init();
		
		}
		
	};
	
	search_manager.init();














	/**
	*
	*	AJAX CALLS FOR CONTENT
	*
	**/
	
	content_call = {
	
		_transition_duration: 500,
	
		_is_loading: false,
		
		_elements_with_ajax_call: 'ul.rex-navi0 a, #search-results ul a, div#header h1 a, div#small-nav a, #body-content-left ul a, #body-content-right a, #body-content-center a[class!=Link-nach-Extern][class!=EMailLink][target!=_blank]',
	
		//
		//	init
		//
		init: function() {
		
			_ajax_elements = $(content_call._elements_with_ajax_call);
			_ajax_elements.unbind('click.content_call');
			_ajax_elements.bind('click.content_call', content_call.click);
			
			if($('#body-content-right div.scroll-area *').length == 0) { $('#body-content-right').width(0); }
			if($('#body-content-left div.scroll-area *').length == 0) { $('#body-content-left').width(0); }
		
		},
		
		//
		//	onclick handling
		//
		click: function(e) {
		
			e.preventDefault();
			
			if(!content_call._is_loading) {

				content_call._is_loading = true;

				if($(this).parents('ul.rex-navi0')) {
					content_call.set_active_navigation($(this));
				}
				content_call.load($(this));
			}
		
		},
		
		//
		//	load manager
		//
		load: function(elmt) {
			$('#body').prepend('<div id="ajax-loading"><img src="'+TEMPLATE_DIR+'css/imgs/loading.gif" alt="" /></div>');
			
      if(typeof(_gaq)=='object')
      {
  			_gaq.push(function() {
  			  var pageTracker = _gat._getTracker('UA-16675431-1');
  			  link.href = pageTracker._getLinkerUrl(elmt.attr('href')+(elmt.attr('href')));
  			});
  		}

			$.ajax({      
				url: elmt.attr('href')+(elmt.attr('href').indexOf('?')>-1 ? '&' : '?')+'json=1',
				dataType: 'json',
				cache: false,
				
				data: {
					'href': elmt.attr('href')
				},

				success: function(data, status) {

					content_call.switch_contents(data);

				},

				error: function() {
					$('#ajax-loading').remove();
					content_call._is_loading = false;
				}

			});
		},
		
		//
		//	switch contents
		//
		switch_contents: function(json) {
		
			// left and right
			content_call.switch_content_animation('left', json);
			content_call.switch_content_animation('right', json);
			
			// center	
			$('#body-content-center')
				.prepend('<div class="fading" />')
				.find('div.fading')
				.fadeIn(content_call._transition_duration, function() {

					$(this).parent().find('.scroll-area').html(json.content_center+'<br class="clear" />').parent().prev().fadeOut(content_call._transition_duration, function() {

						$(this).remove();

					});

				});
			
			// headline
			$('#header h2')
				.wrapInner('<div class="fading" />')
				.find('div.fading')
				.fadeOut(content_call._transition_duration, function() {

					$(this).html(json.headline).fadeIn(content_call._transition_duration, function() {

						$(this).children().unwrap();

					});

				});
				
			// background
			$('#body-background').before('<div id="body-background-tmp" style="background-image: '+$('#body-background').css('background-image').replace(/\"/, '')+'" />');
			$('#body-background').css('background-image', 'url('+json.background_image+')');
			$('#body-background-tmp').fadeOut((content_call._transition_duration*2), function() {
				$(this).remove();
			});
			
			
			
			// finish
			
			window.setTimeout(function() {
				$('#body .scroll-area').jScrollPane();
			}, content_call._transition_duration + 50);
			
			window.setTimeout(function() { 
				content_call._is_loading = false;
				$('#ajax-loading').remove();
				content_call.init();
				contact_form.init();
			}, (content_call._transition_duration*2));
		
		},
		
		//
		//	switch content animation for left and right content
		//
		switch_content_animation: function(position, json) {
			if(position == 'left') {
			 
				var _width = 224;
				var _json_data = json.content_left;
			} else {
				var _width = 176;
				var _json_data = json.content_right;
			}
			
			if($('#body-content-'+position).width() == 0 && _json_data != '') {
			
				$('#body-content-'+position)
					.prepend('<div class="fading" />')
					.find('.scroll-area')
					.html(_json_data+'<br class="clear" />')
					.parent().prev().show();
					
				$('#body-content-'+position)
					.animate(
						{ 'width': _width },
						{
							'duration': content_call._transition_duration,
							'complete': function() {
							
								$(this).find('div.fading').fadeOut(content_call._transition_duration, function() {
								
									$(this).remove();
								
								});
							
							}
						});
				
			}
			
			else if($('#body-content-'+position).width() > 0 && _json_data == '') {
			
				$('#body-content-'+position)
					.prepend('<div class="fading" />')
					.find('div.fading')
					.fadeIn(content_call._transition_duration);
					
				$('#body-content-'+position).animate(
					{ 'width': 0 }, 
					{ 
						'duration': content_call._transition_duration,
						'complete': function() {

							$(this).find('div.fading').remove();

						}
					});
				
			} else if($('#body-content-'+position).width() > 0 && _json_data != '') {
			
				$('#body-content-'+position)
					.prepend('<div class="fading" />')
					.find('div.fading')
					.fadeIn(content_call._transition_duration, function() {
						
						$(this).parent().find('.scroll-area').html(_json_data+'<br class="clear" />').parent().prev().fadeOut(content_call._transition_duration, function() {
						
							$(this).remove();
						
						});
				
					});
				
			}
		
		},
		
		//
		//	set active item for top-level navigation
		//
		set_active_navigation: function(elmt) {
		
			if(!elmt.parents().is('#body')) {
				$('ul.rex-navi0 a, ul.rex-navi0 li, #small-nav a, #small-nav li').removeClass('active rex-active');
			} else {
				elmt.parents('ul').find('a, li').removeClass('active rex-active');
			}
			
			// home?
			if(elmt.parent().is('h1')) {
				$('ul.rex-navi0 a:first').addClass('active');
			} else {
				elmt.addClass('active');
			}
		
		}
	
	
	
	
	};

	content_call.init();














	/**
	*
	*	CONTACT FORM
	*
	**/
	
	contact_form = {
	  _input: 'f0003',
		_input_unternehmen: 'f0003_0',
		_input_privat: 'f0003_1',
		_class_unternehmen: 'Unternehmen',
		_class_privat: 'Privatperson',
	
		//
		//	init
		//
		init: function() {
	
      	
		  $('input[name*='+contact_form._input+']').click(function(e) {
				contact_form.set_mode($(this), false);
			});
			contact_form.set_initial_mode();
		},
		
		//
		//	set mode
		//
		set_mode: function(elmt, at_once) {
			
			var _duration = (at_once) ? 0 : 300;
		
			var _mode = elmt.attr('id').indexOf(contact_form._input_unternehmen)>-1 ? contact_form._class_unternehmen : contact_form._class_privat;
			var _mode_off = (_mode == contact_form._class_unternehmen) ? contact_form._class_privat : contact_form._class_unternehmen;
			var _form = $(elmt).closest('form');
      	
			_form.find('.'+_mode).each(function(a, b) {
				if(!$(b).is('.'+_mode_off)) {
					$(b).slideDown(_duration);
				}
			});
			_form.find('.'+_mode_off).each(function(a, b) {
				if(!$(b).is('.'+_mode)) {
					$(b).slideUp(_duration);
				}
			});
		
		},
		
		//
		//	set initial mode
		//
		set_initial_mode: function() {
		
			if($('input#'+contact_form._input_privat).length > 0) {
				$('input#'+contact_form._input_privat)[0].checked = false;
				$('input#'+contact_form._input_unternehmen)[0].checked = true;
				contact_form.set_mode($('input#'+contact_form._input_unternehmen), true);
			}
		
		}
	
	
	}
	
	contact_form.init();












	/**
	*
	*	IE ENHANCEMENTS
	*
	**/
	
	if($.browser.msie) {
	
		$('ul.rex-navi0 li a').hover(function() {

			if(!$(this).is('.rex-active, .active')) {
				var _img = TEMPLATE_DIR + 'css/imgs/bg_red-arrow_animated.gif?' + Math.floor(Math.random()*10000);
				$(this).css('background-image', 'url('+_img+')');
			}

		}, function() {

			$(this).removeAttr('style');

		});
	
	}









});

