// JavaScript Document
function navAni(obj, pfad){
	if(!obj.length) return;
//Hintergrund für den Slider hinzufügen
		jQuery(obj).parent().parent().prepend(jQuery('<div />')
			.attr('class', 'background')
		);
	jQuery('.background').css({display:'none'});
	var topNaviFirstId = jQuery(obj).slice(0,1).attr('id');
	var topNaviSecondId = jQuery(obj).slice(1,2).attr('id');
	var aktPath = window.location.pathname;

//Hovereffekte für Top-Navi
	jQuery('#'+topNaviFirstId)
		.addClass('top1')
		.hover(function(){
			if(!(aktPath.indexOf('company')>-1)){
				jQuery(this).removeClass('top1').addClass('top1over');
			}
		}, function(){
				if(!(aktPath.indexOf('company')>-1)){
					jQuery(this).removeClass('top1over').addClass('top1');
				}
			});
	jQuery('#'+topNaviSecondId)
		.addClass('top2')
		.hover(function(){
			if(!(aktPath.indexOf('projects')>-1)){
				jQuery(this).removeClass('top2').addClass('top2over');
			}
		}, function(){
			if(!(aktPath.indexOf('projects')>-1)){
				jQuery(this).removeClass('top2over').addClass('top2');
			}
		});
//Top-Navi you-are-here-Indikator
		if(aktPath.indexOf('company')>-1){
			jQuery('#'+topNaviFirstId).addClass('top1over');
		}else if(aktPath.indexOf('projects')>-1){
			jQuery('#'+topNaviSecondId).addClass('top2over');
		}
	jQuery(obj).find('ul').css({display:'none'});

//Typo der Top-Navi ausblenden, Symbole hinzufügen
	jQuery(obj).children('a').addClass('navHead').wrapInner('<span class="leftout"></span>');
	jQuery(obj).find('ul').slice(0,1).first().prepend('<span id="mac"></span>');
	jQuery(obj).find('ul').slice(1,2).first().prepend('<span id="vw"></span>');
//Animationen in der Navi-Leiste
	jQuery(obj).hover(function(){
		jQuery(this).css({height:'160px'});
		jQuery('a.navHead').click(function(e){
			e.preventDefault();
		});
	//alert(jQuery(this).html());
		var offset = jQuery(this).position();
		jQuery('.background').css({
					backgroundColor:'#fff',
					border:'1px solid #cdd3d2',
					position:'absolute',
					left:offset.left,
					zIndex:'-5',
					width:'150px',
					height:obj.find('ul').height(),
					top:offset.top-15
			});
		if(pfad == '/'){
			jQuery('#anschrift').stop(true, true).slideUp('slow');
			jQuery('#vcard').stop(true, true).animate({height:'65px'});
		}
		jQuery(this).find('ul').first().stop(true, true).slideDown('slow');
		jQuery('.background').stop(true, true).slideDown('slow');
	}, function(){
		jQuery(this).find('ul').first().stop(true, true).slideUp('slow');
		jQuery('.background').stop(true, true).slideUp('slow');
		jQuery('#vcard').stop(true, true).animate({height:'195px'});
		jQuery('#anschrift').stop(true, true).slideDown('slow');
	});
}

function pageNavSlash(obj){

	if(!obj.length) return;
	
	jQuery(obj).each(function(i){
		var items = jQuery(this).children().children();
		var anzahl = jQuery(items).length;
		jQuery(items).each(function(i){
			if(anzahl-1 != i){
				jQuery(this).append('<span> /</span>');
				if(jQuery(this).next().text().trim() != 'Basecamp Login'){
					jQuery(this).next().prepend('&nbsp;');
				}
			}
		});
	});
}

function wrapperDiv(obj){
	if(!obj.length) return;
	var elem = jQuery(obj).find('h2, p');
	var anzahl = elem.length;
	var open = '<div>';
	var close = '</div>';
	var ausgabe = '';
	var header = '';
	var para = '';

	jQuery(elem).each(function(i){

		if(jQuery(this).is('h2')){
				header = '<h2>' + jQuery(this).html() + '</h2>';
				ausgabe += open + header;
			}else if(jQuery(this).is('p')){
				para = '<p>' + jQuery(this).html() + '</p>';
				ausgabe += para;
				if(jQuery(this).next().is('h2') || i+1 == anzahl){
					ausgabe += close;
				}
			}
	});
	ausgabe += '<br style="clear:both" />';
	jQuery(obj).html(ausgabe);
	var anzahl = jQuery(obj).children('div').length;
	if(anzahl == 3){
		jQuery(obj).children('div:first').addClass('wrap');
		jQuery(obj).children('div').slice(1).wrapAll('<div class="outerWrap" />');
	}
}

String.prototype.trim = function () {
	return this.replace(/^\s+/g, '').replace(/\s+$/g, '');
}


jQuery(document).ready(function(){
var path = document.location.pathname;
//alert(typeof(path));
	navAni(jQuery('.topMenu li').has('ul'), path);
	pageNavSlash(jQuery('#innerNavi div').children().has('ul'));
	jQuery('.menu li a:contains("Basecamp")').attr({target:"_blank"});
	var p3Container = document.getElementById('training-content-3p');
	var p4Container = document.getElementById('training-content-4p');
	if(jQuery('#trainings').has(p3Container).length){
		wrapperDiv('#training-content-3p');
	}else if(jQuery('#trainings').has(p4Container).length){
		wrapperDiv('#training-content-4p');
	}
});

