/*--- ajax tabs ---*/
function ajaxTabs(t_hold){
	if(!t_hold) t_hold = $('body');
	var _speed = 500; //in ms
	t_hold.find('ul.tabset').each(function(){
		if(!this._atf){
			this._atf = true;
			var _hold = $('#'+$(this).attr('title'));
			var _btn = $(this).find('a');
			var _loader = $('<div class="loader">Loading...</div>');
			_loader.css({opacity:0, display:'none'});
			_hold.append(_loader);
			
			var _a = _btn.index(_btn.filter('.active:eq(0)'));
			if(_a == -1) _a = 0;
			if(_a == 0) $('html').addClass('with-bg');
			else $('html').removeClass('with-bg');
			_btn.removeClass('active').eq(_a).addClass('active');
			$.ajax({
				url: _btn.eq(_a).attr('href'),
				cache: false,
				dataType: 'html',
				success: function(_html){
					_btn.eq(_a).data('box', $(_html));
					_btn.eq(_a).data('box').addClass('active-tab');
					_hold.append(_btn.eq(_a).data('box'));
					inAjaxFunct(_btn.eq(_a).data('box'));
				}
			});
			_btn.click(function(){
				changeTab(_btn.index(this));
				return false;
			});
			var _f = true;
			function changeTab(_ind){
						
				if(_f && _ind != _a){	
					if(_ind == 0) $('html').addClass('with-bg');
					else $('html').removeClass('with-bg');
					_f = false;
					_hold.stop().height(_hold.height());
					_btn.eq(_a).removeClass('active');
					_btn.eq(_ind).addClass('active');
					if($.browser.msie) _btn.eq(_a).data('box').removeClass('active-tab').hide();
					else _btn.eq(_a).data('box').stop().removeClass('active-tab').animate({opacity: 0}, _speed, function(){ $(this).hide();});
					if(_btn.eq(_ind).data('box')){
						if($.browser.msie) _btn.eq(_ind).data('box').show().addClass('active-tab');
						else _btn.eq(_ind).data('box').stop().show().addClass('active-tab').animate({opacity: 1}, _speed);
						_hold.animate({height: _btn.eq(_ind).data('box').outerHeight()}, _speed/2, function(){ $(this).height('auto');});
						_a = _ind;
						_f = true;
					}
					else{
						_loader.stop().show().animate({opacity: 1}, 100);
						$.ajax({
							url: _btn.eq(_ind).attr('href'),
							cache: false,
							dataType: 'html',
							success: function(_html){
								_btn.eq(_ind).data('box', $(_html));
								if($.browser.msie) _btn.eq(_ind).data('box').hide();
								else _btn.eq(_ind).data('box').css('opacity', 0);
								_hold.append(_btn.eq(_ind).data('box'));
								_loader.stop().animate({opacity: 0}, 100, function(){ $(this).hide();});
								if($.browser.msie) _btn.eq(_ind).data('box').show().addClass('active-tab');
								else _btn.eq(_ind).data('box').stop().show().addClass('active-tab').animate({opacity: 1}, _speed);
								_hold.animate({height: _btn.eq(_ind).data('box').outerHeight()}, _speed/2, function(){ $(this).height('auto');});
								inAjaxFunct(_btn.eq(_a).data('box'));
								_a = _ind;
								_f = true;
							}
						});
					}
				}
			}
		}
	});
}
function ajaxPosts(t_hold){
	var _speed = 500;
	if(!t_hold) t_hold = $('body');
	t_hold.find('div.posts-holder').each(function(){
		var box_hold = $(this).find('div.container');
		var _btn = $(this).find('ul.image-list > li:has("a.post-btn")');
		var _loader = $('<div class="loader">Loading...</div>');
		_loader.css({opacity:0, display:'none'});
		box_hold.append(_loader);
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		box_hold.hide();
		if(_a != -1){
			box_hold.show();
			_btn.removeClass('active').eq(_a).addClass('active');
			$.ajax({
				url: _btn.eq(_a).find('a.post-btn').attr('href'),
				cache: false,
				dataType: 'html',
				success: function(_html){
					_btn.eq(_a).data('box', $(_html));
					_btn.eq(_a).data('box').addClass('active-post');
					box_hold.append(_btn.eq(_a).data('box'));
					inAjaxFunct(_btn.eq(_a).data('box'));
				}
			});
		}
		_btn.click(function(){
			changeEl(_btn.index(this));
			return false;
		});
		var _f = true;
		function changeEl(_ind){
			if(_f){

				$(window).scrollTo($('div.posts-holder'), 500);
				
				_f = false;
				if(box_hold.is(':hidden')) box_hold.css({height:0, display:'block'});
				else box_hold.stop().css({height:box_hold.height()});
				if(_a == _ind){
					_btn.eq(_a).removeClass('active');
					if($.browser.msie) _btn.eq(_a).data('box').removeClass('active-post').hide();
					else _btn.eq(_a).data('box').stop().removeClass('active-post').animate({opacity: 0}, _speed, function(){ $(this).hide();});
					box_hold.animate({height: 0}, _speed/2, function(){ $(this).css({display:'none', height:'auto'});});
					_a = -1;
					_f = true;
				}
				else{
					if(_a != -1){
						_btn.eq(_a).removeClass('active');
						if($.browser.msie) _btn.eq(_a).data('box').removeClass('active-post').hide();
						else _btn.eq(_a).data('box').stop().removeClass('active-post').animate({opacity: 0}, _speed, function(){ $(this).hide();});
					}
					_btn.eq(_ind).addClass('active');
					if(_btn.eq(_ind).data('box')){
						if($.browser.msie) _btn.eq(_ind).data('box').addClass('active-post').show();
						else _btn.eq(_ind).data('box').stop().show().addClass('active-post').animate({opacity: 1}, _speed);
						box_hold.animate({height: _btn.eq(_ind).data('box').outerHeight()}, _speed/2, function(){ $(this).css({height:'auto'});});
						_a = _ind;
						_f = true;
					}
					else{
						_loader.stop().show().animate({opacity: 1}, 150);
						$.ajax({
							url: _btn.eq(_ind).find('a.post-btn').attr('href'),
							cache: false,
							dataType: 'html',
							success: function(_html){
								_btn.eq(_ind).data('box', $(_html));
								if($.browser.msie) _btn.eq(_ind).data('box').hide();
								else _btn.eq(_ind).data('box').css({opacity:0, display:'none'});
								box_hold.append(_btn.eq(_ind).data('box'));
								_loader.stop().animate({opacity: 0}, 150, function(){ $(this).hide();});
								inAjaxFunct(_btn.eq(_ind).data('box'));
								if($.browser.msie) _btn.eq(_ind).data('box').addClass('active-post').show();
								else _btn.eq(_ind).data('box').stop().show().addClass('active-post').animate({opacity: 1}, _speed);
								box_hold.animate({height: _btn.eq(_ind).data('box').outerHeight()}, _speed/2, function(){ $(this).css({height:'auto', overflow:'visible'});});
								_a = _ind;
								_f = true;
							}
						});
					}
				}
			}
		}
	});
}
/*--- main navigation ---*/
function siteNavigation(t_hold){
	if(!t_hold) t_hold = $('body');
	t_hold.find('#nav a, a.btn-goto').each(function(){
		if(!this._nf){
			this._nf = true;
			this.onclick = function(){
				if(this.hash && this.hash.length > 1 && $(this.hash).length){
					$(window).scrollTo($(this.hash), 500);
					return false;
				}
			}
		}
	});
}
/*--- tooltip ---*/
function initTooltip(t_hold){
	if(!t_hold) t_hold = $('body');
	var _tooltip = $('#info-tooltip');
	var tooltip_text = _tooltip.find('div.c');
	var _k = 0;
	t_hold.find('.with-tooltip').each(function(){
		if(!this._tf){
			this._tf = true;
			var _text = $(this).parent().siblings('div.tooltip-content:eq(0)');
// wegen der Spans jeweils 3x parent() hinzugefügt
if(_text.length == 0) _text = $(this).parent().parent().parent().parent().parent().siblings('div.tooltip-content:eq(0)');
if(_text.length == 0) _text = $(this).parent().parent().parent().parent().parent().parent().siblings('div.tooltip-content:eq(0)');
// ende 
			if(_text.length != 0){
				$(this).mouseenter(function(e){
					if(e.pageX - $('#wrapper').offset().left < $('#wrapper').outerWidth()/2){
						_tooltip.removeClass('right').addClass('left');
						_k = 25;
					}
					else{
						_tooltip.removeClass('left').addClass('right');
						_k = -315;
					}
					tooltip_text.html(_text.html());
					_tooltip.css({top: e.pageY, left: e.pageX+_k});
					if($.browser.msie) _tooltip.show();
					else _tooltip.stop().show().animate({opacity: 1}, 200);
					_tooltip.css({top: e.pageY - _tooltip.outerHeight()/2});
				}).mousemove(function(e){
					_tooltip.css({top: e.pageY - _tooltip.outerHeight()/2, left: e.pageX+_k});
				}).mouseleave(function(){
					if($.browser.msie) _tooltip.hide();
					else _tooltip.stop().animate({opacity: 0}, 200, function(){ $(this).hide();});
				});
			}
		}
	});
}
/*--- more info ---*/
function moreInfo(t_hold){
	var open_text = 'Weniger';
	var close_text = 'Weiterlesen';
	var _speed = 300;
	if(!t_hold) t_hold = $('body');
	t_hold.find('a.more-btn').each(function(){
		if(!this._if){
			this._if = true;
			var _btn = $(this);
			var _box = _btn.parent().prev('div.more-hold');
			var _h = _box.outerHeight();
			if(_btn.hasClass('opened')){
				_btn.text(open_text);
				_box.show();
			}
			else{
				_btn.text(close_text);
				_box.hide();
			}
			_btn.click(function(){
				if(_btn.hasClass('opened')){
					_btn.removeClass('opened');
					_btn.text(close_text);
					_box.stop().animate({height:0}, _speed, function(){ $(this).css({display:'none', height: 'auto'});});
				}
				else{
					_btn.addClass('opened');
					_btn.text(open_text);
					if(_box.is(':hidden')){
						_box.show();
						_h = _box.outerHeight();
						_box.height(0);
					}
					_box.stop().animate({height:_h}, _speed, function(){ $(this).height('auto');});
				}
				return false;
			});
		}
	});
}
/*--- in ajax block's ---*/
function inAjaxFunct(t_hold){
	moreInfo(t_hold);
	initTooltip(t_hold);
	siteNavigation(t_hold);
}
/*--- on document ready ---*/
$(document).ready(function(){
	siteNavigation();
	initTooltip();
	moreInfo();
	ajaxTabs();
	ajaxPosts();
	/*--- menu animation ---*/
	var anim_t;
	$(window).bind('scroll', function(){
		if(anim_t) clearTimeout(anim_t);
		anim_t = setTimeout(function(){
			$('#nav').animate({top: $(window).scrollTop()}, {queue:false, duration: 300});
		}, 2);
	});
	/*--- ---*/
	$('ul.section-nav').each(function(){
		var _hold = $(this);
		var _list = _hold.find('span');
		var hold_w = _hold.parent().width();
		var list_w = _hold.outerWidth();
		if(list_w < hold_w){
			var _k = Math.round((hold_w - list_w)/_list.length);
			if(_k*_list.length + list_w > hold_w) _k--;
			for(var i = 0; i < _list.length; i++){
				_list.eq(i).width(_list.eq(i).width() + _k);
			}
			_k = hold_w - _hold.outerWidth();
			if(_k > 0){
				for(var i = 0; i < _k; i++){
					_list.eq(i).width(_list.eq(i).width() + 1);
				}
			}
		}
	});
	
	
});