$.noConflict();

// Alles was beim Laden der Seite ausgeführt werden soll
jQuery(function($){
	
	/****************************************************
	* ALLGEMEIN											*
	****************************************************/
	/*	Häufig benutzte Objekte in Variablen speichern
	 **/
	var $slideContent = $('#slideContent'),
		$main = $('#main'),	
		$content = $('#content');
	
	/*	An den IE6 und älter eine Startseite ohne Animation ausliefern
	 * 	der Rest bekommt die animierte Seite
	 **/
	if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) {
		$('.text', $main).css({ 'margin-top': '-160px' });
	}
	else {
		/*	Wenn JavaScript aktiv ist, kann über die body-Class "js"
		 *	spezielles CSS geliefert werden
		 **/
		$('body')
		.removeClass('nojs')
		.addClass('js');
		
		/*	Die vier Hauptbereiche einklappen wenn die aktuelle Seite
		 *	die Startseite ist.
		 **/
		if ($('body').is('#page_start')) {
			$('.section', $main)
			.removeClass('quadruple')
			.addClass('single');
		}
		
		$('#page_start #main .text')
		.each(function(){
		
			//if ($(this).children('p').length > 2) {
				$(this)
					.children('p')
						.first()
							.nextAll('p')
							.css({ 'display': 'none' })
						.end()
					.end()
				.end()
				.append('<p style="text-align: right;"><a href="#" class="slideOutMore">mehr ...</a></p>');
			//}
			
		});
		
		$('.slideOutMore', $main)
		.click(function(){
			
			$(this)
			.fadeOut(250)
				.parents('.text')
					.children('p')
						.first()
							.siblings('p')
							.slideDown(250)
						.end()
					.end()
				.end()
				.parents('.section')
				.css({ 'height': 'auto' });
			
			return false;
			
		});
		
		/*	Slides Positionieren
		 *	Die Slides mit ZItaten außerhalb des Divs positionieren
		 */
		$slideContent
		.children('.section')
			.first()
				.siblings()
				.css({ 'left': '100%' });
		
	}
	
	// Zeilenumbrüche in <pre>-Tags korrigieren
	$('pre')
	.each(function(i){
		$(this).html($(this).html().replace(/<br>/g, '\n').replace(/<br \/>/g, '\n'));
	});
	
	/****************************************************
	* INTERNET EXPLORER SELECTORS / CSS3 FIXES			*
	****************************************************/
	/*	last-child
	 **/
	$('.section:last-child', $content)
	.css({ 'margin': '0' })
		.children('.title')
		.css({ 'border': 'none' });
	
	/*	first headline
	 **/
	$('.text', $main)
	.first()
	.find('h1')
		.first();
	
	$('.section .title')
	.first()
		.siblings('.title')
		.remove();
	
	/* Internet Explorer 6 only
	 **/
	if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
				
		$('.section .title', $content)
		.css({ 'border-right': 'none' });
		
		$('.section .title + .text', $content)
		.css({
			'width': '700px',
			'margin': '0 0 0 235px',
			'padding': '10px 0 10px 19px',
			'border-left': '1px dashed rgb(96, 96, 96)'
		});
		
		$('#page_start .title + .text')
		.css({ 'margin-top': '0px' })
			.children('h1')
			.css({ 'margin': '.1em 0 1em' });
	}
	
});

// Alles was nach dem Laden ausgeführt werden soll
jQuery(window).load(function(){
							 
	/****************************************************
	* ALLGEMEIN											*
	****************************************************/
	/*	Um das jQuery-übliche "$" benutzen zu können speichere
	 *	es in der Variable "$".
	 **/
	var $ = jQuery;
	
	/*	Häufig benutzte Objekte in Variablen speichern
	 **/
	var //$header = $('#header'),
		$headerNav = $('#headerNav'),
		$search = $('#search'),
		$searchField = $('#searchField'),
		//$slides = $('#slides'),
		$slideContent = $('#slideContent'),
		//$slideControls = $('#slideControls'),
		//$content = $('#content'),
		$main = $('#main'),
		$mainNav = $('#mainNav'),
		$aside = $('#aside'),
		$fixedNav = $('#fixedNav'),
		$sliderAreaBg = $('#sliderAreaBg');
	
	/*	Den ersten Buchstaben von h1 Überschriften mit einem
	 *	CSS-Stil ausstatten
	 **/
	if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 9) {
		$('h1', $('.title, #aside'))
		.each(function(index){
			
			if ($(this).has('a').length != 0) {
				var replacement = $(this).children('a').text().replace(/^(.{1})(.*)/, '<span class="hilite">$1</span>$2');
				$(this).children('a').html(replacement);
			}
			else {
				var replacement = $(this).text().replace(/^(.{1})(.*)/, '<span class="hilite">$1</span>$2');
				$(this).html(replacement);
			}
		});
	}
	
	/****************************************************
	* ANIMATIONEN & FUNKTIONEN							*
	****************************************************/
	
	/*	GET URL VARS
	 **/
	function getUrlVars()
	{
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	}
	
	/*	KOPFNAVIGATION
	 *	Animationen und Kontrollen der Kopfnavigation
	 **/
	$headerNav
		.children('li:not(#searchWrapper)')
		.hover(
			function(){
				$(this)
					.has('ul')
					.css({
						'border': '1px solid rgb(160, 160, 160)',
						'border-top': 'none',
						'border-bottom': 'none'
					})
					.end()
					.children('ul, div')
					.stop(true, true)
					.css({
						'display': 'block',
						'z-index': '0'
					});
			},
			function(){
				$(this)
					.removeAttr('style')
					.children('a:not(:only-child)')
					.css({
						'background': 'rgb(32, 32, 32)',
						'color': 'rgb(240, 240, 240)'
					})
					.siblings('ul, div')
					.stop(true, true)
					.css({
						'display': 'none',
						'z-index': '0'
					})
					.siblings('a')
					.removeAttr('style');
			}
		);
	
	
	/*	KONTAKTDATEN EINBLENDEN
	 **/
	var lastLink = $headerNav.children('li:last-child').prev().children('a').attr('href');
	$headerNav
		.children('li:last-child')
		.prev()
		.append('<div id="navContact"><h2><a href="' + lastLink + '">Kontakt</a></h2><p><strong>open</strong> knowledge <strong>GmbH</strong><br />Bismarckstrasse 13<br />26122 Oldenburg<br /><br />Fon: +49 (0)441 4082-0<br />Fax: +49 (0)441 4082-111<br /><br /><a href="mailto:kontakt@openknowledge.de">kontakt@openknowledge.de</a><br /><br /><a href="' + lastLink + '" class="more">mehr ...</a></p></div>');
	
	
	/*	SUCHFUNKTION
	 *	Suchfeld leeren und Animationen
	 **/
	var initContent = 'suchen ...';
	
	/*$search
	.css({ 'opacity': .25 })
	.hover(
		function(){ $(this).fadeTo(250, 1); },
		function(){ $(this).fadeTo(250, .25); }
	);*/
	
	$('#searchWrapper a')
	.click(function(){
		$('#search').show();
		return false;
	});
	
	$('#searchClose')
	.click(function(){
		$('#search').hide();
		return false;
	});
	
	$searchField
	.attr('value', initContent)
	.focus(function(){
		
		var $this = $(this),
			content = $this.attr('value');
		
		if (content == initContent) {
			$this.attr('value', '');
		}
		
	})
	.blur(function(){
		
		var $this = $(this),
			content = $this.attr('value');
		
		if (content == '') {
			$this.attr('value', initContent);
		}
		else {
			//$this.css({ 'opacity': '1' });
		}
		
	});
	
	$('#sendButton')
	.click(
		function(){
			
			if ($searchField.val() == '' || $searchField.val() == initContent) {
				$searchField.focus();
				return false;
			}
			
		}
	);
	
	
	/*	HAUPTAUSWAHL
	 *	Klick auf einen der Hauptpunkte (Analyse, Beratung, Konzeption, Entwicklung)
	 **/
	var aux = false;
	
	function animateItem ($this) {
		
		var $sectionTitle = $this,
			sectionTitleText = $sectionTitle.children('h1').text(),
			$sectionParent = $sectionTitle.parents('.section'),
			textHeight = $sectionTitle.siblings('.text').innerHeight();
		
		if ($sectionParent.width() != 960) {
			
			$sectionParent
			.delay(50)
			.animate(
				{ 'margin-right': '0' },
				500,
				function(){
					$(this)
					.animate(
						{ 'width': '960px' },
						500,
						function(){
							$(this)
							.animate(
								{ 'height': textHeight + 'px' },
								250,
								function(){
									
									var itemClass = $(this).attr('class'),
										citeBackgroundImagePath = 'fileadmin/template/img/citebackgrounds/' + sectionTitleText.toLowerCase() + '.jpg';
									
									// entsprechende Item-Nummer aus der CSS-Klasse extrahieren
									itemClass = itemClass.replace(/.*(item[0-9]{1}).*/, '$1');									
									
									// Kopftitel anpassen
									document.title = sectionTitleText + ' // open knowledge';
									
									// Zitathintergrund anpassen
									$sliderAreaBg
									.html('<img src="' + citeBackgroundImagePath + '" alt="" />');
									
									$('.sliderContent.area')
									.css({
										'background-image': 'url(' + citeBackgroundImagePath + ')'
									});
									
									// Die Animationen
									$(this)
									.css({ 'margin-bottom': '0' })
									.removeClass('single')
									.addClass('quadruple')
										.children('.text')
										.customFadeIn(250);	
									
									$mainNav
									.fadeIn(500)
										.children('li')
											.children('a[class!=' + itemClass + ']')
											.fadeTo(500, 0.5)
												.children('a.' + itemClass)
												.fadeIn(500);
									
									// Google Analytics Tracking
									// pageTracker._trackPageview('/homepage/' + itemName);
									
								}
							);
						}
					);
				}
			)
				.siblings()
				.animate(
					{
						'width': '0',
						'margin-right': '0'
					},
					495,
					function(){
						$(this)
						.css({
							'display': 'none',
							'width': '960px',
							'margin': '0'
						})
						.removeClass('single')
						.addClass('quadruple')
							.children('.text')
							.css({
								'display': 'block'
							});
					}
				)
				.children('.title')
				.css({
					'padding-right': '0',
					'border-right-width': '0'
				});
			
			$sectionTitle
			.css({
				'padding-right': '0',
				'border-right-width': '0'
			});
			
		}
		
		aux = true;
		
		return false;
		
	}
	
	function slideItem ($this) {
		var currName = $this.attr('class'),
			$currItem = $('.section.' + currName, $main),
			currItemTitleText = $('.section.' + currName + ' .title h1', $main).text(),
			citeBackgroundImagePath = 'fileadmin/template/img/citebackgrounds/' + currItemTitleText.toLowerCase() + '.jpg';;
		
		if ($currItem.css('display') == 'none') {
			// Icon Transparenz ändern
			$this
			.stop(true, true)
			.fadeTo(250, 1)
				.parents('li')
					.siblings()
						.children('a')
						.fadeTo(250, 0.5);
			
			// Section einblenden
			$currItem
			.stop(true, true)
			.slideDown(
				500,
				function(){
					document.title = currItemTitleText + ' | open knowledge';
					
					$sliderAreaBg
					.html('<img src="' + citeBackgroundImagePath + '" alt="" />');
					
					$('.sliderContent.area')
					.css({
						'background-image': 'url(' + citeBackgroundImagePath + ')'
					});
				}
			)
				.siblings()
				.slideUp(500);
		}
		
		return false;
	}
	
	$('#page_start #main .title')
	.click(function(){ var $this = $(this); animateItem($this); });
	
	var anchorHash = window.location.hash.substr(1);
	
	if (!aux && anchorHash != '') {
		var $item = $('#page_start #main .' + anchorHash + ' .title');
		animateItem($item);
	}
	
	/*	HAUPTAUSWAHL NAVIGATION (ICONLEISTE)
	 *	Animation nach Klick auf eines der Icons
	 **/
	if ($('body').is('#page_start')) {
		
		if (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6) {
			$('a', $mainNav)
			.click(function(){
				
				var $this = $(this),
					currName = $this.attr('class'),
					$currItem = $('.section.' + currName, $main),
					currPosition = $currItem.offset().top;
				
				$('html, body')
				.animate(
					{
						scrollTop: currPosition
					},
					500
				)
				.scrollTop(currPosition);
				
				// Icon Transparenz ändern
				$this
					.parent('li')
					.addClass('active')
						.siblings()
						.removeClass('active');
				
				return false;
				
			});
		}
		else {
			$('a', $mainNav)
			.click(function(){
				slideItem($(this));
				return false;
			});
		}
	}
	
	
	/*	FIXED NAVIGATION
	 *	bringt den Benutzer nach oben (anstatt zu scrollen)
	 **/
	
	// Position des Kategorie-Icons
	var $marker = $('.title .icon', $main);
	
	if ($marker.length > 0) {
	
		var markerPositionTop = $marker.offset().top + $marker.outerHeight(),
			markerPositionLeft = 0;
	
		// Verhalten beim Scrollen der Seite bzw. beim Ändern der Größe des Browsers
		$(window)
		.scroll(function(){
			
			if ($(this).scrollTop() > markerPositionTop && $('body').is(':not(#page_start)')) {
				
				markerPositionLeft = $marker.offset().left + $marker.outerWidth() - $fixedNav.outerWidth();
				$fixedNav.css({ 'left': markerPositionLeft + 'px' });
				
				if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) {
					$fixedNav
					.css({
						'position': 'absolute',
						'top': $(this).scrollTop() + 'px',
						'left': '150px'
					});
				}
				
				if ($fixedNav.is(':not(:visible)')) {
					$fixedNav.customFadeIn(250);
				}
				
			}
			else {
				if ($fixedNav.is(':visible')) {
					$fixedNav.customFadeOut(50);
				}
			}
			
		})
		.resize(function(){
			
			if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) {
				$fixedNav.css({ 'left': '150px' });
			}
			else {
				markerPositionLeft = $marker.offset().left + $marker.outerWidth() - $fixedNav.outerWidth();
				$fixedNav.css({ 'left': markerPositionLeft + 'px' });
			}
			
		});
		
	}
	
	// Verhalten bei Klick auf "nach Oben"
	$fixedNav
	.click(function(){
		
		$('html, body')
		.animate({ scrollTop: 0 }, 500);
		
		return false;
		
	});
	
	// NEWS / BLOG Filter
	$('#categoryFilter')
	.click(function(){
		var action = $(this).parents('form').attr('action'),
			category = $(this).siblings('select').val();
		
		if (category != 0) window.location = action + '&no_cache=1&categories=' + category;
		else window.location = action + '&no_cache=1';
		
		return false;
	})
	.parent()
	.find('option[value="' + getUrlVars()['categories'] + '"]')
	.attr('selected', 'selected');

}); // END OF BEHAVIOR


/****************************************************
* PLUGINS											*
****************************************************/

/*
	jQuery fadeIn/fadeOut IE cleartype glitch fix
	by Benjamin Michael Novakovic
	
	http://blog.bmn.name/2008/03/jquery-fadeinfadeout-ie-cleartype-glitch/
*/
(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);
