(function($){
	//Navigation
	
	var navList = function() {
		$('#nav li').hover(function(){
			$(this).addClass('over');
		},function(){
			$(this).removeClass('over');
		});
	};
	jQuery.fn.mySlide = function(fn){  
         	fn = fn || function(){};
			
			var s = (this.is(':hidden'))? 'block': 'none';
			this.css({display: s});
			if(s == 'block' && this[0]){
				this[0].scrollIntoView(true);
			}
         	return this;  
     };
	 
	function showTab() {
		function over() {
			var animCSS = ($.browser.msie)?{'backgroundColor': '#85c1b9', color: '#fff'}:{'backgroundColor': '#85c1b9', color: '#fff', 'borderColor': '#85c1b9'};
			$(this).stop(true, true).animate(animCSS, {duration: 250, deque: true}); 
			$(this).children('span').stop(true, true).animate({color: '#249080'}, {duration: 250, deque: true});	
		}
		function out() {
			var animCSS = ($.browser.msie)?{'backgroundColor': '#ffffff', color: '#666'}:{'backgroundColor': '#ffffff', color: '#666', 'borderColor': '#ccc'};
			$(this).stop(true, true).animate(animCSS, {duration: 250, deque: true}); 
			$(this).children('span').stop(true, true).animate({color: '#d6d6d6'}, {duration: 250, deque: true});	 
		}
     	$('#tabs a').tab({firstActive: true, tabListSel: '#tabs', onEvent: 'mouseenter,focus'})
			.hover( over, out);
	}
	
	
	// Bookmarks
var socialbookmark = {
	options: {
		showlimit: 0,
		initialshow: true
	},
    handler: function(lElm, options){		
		socialbookmark.options =  $.extend({}, socialbookmark.options, options);
        var link = lElm;
        var container = $(link).parent().find('#bookmarks');
		var elems = $(container);
		if($('ul li', elems).children().length > socialbookmark.options.showlimit){	
			var moreBtn = '<li class="more-social"><a href="">Weitere</a></li>';		
			$('ul > li:gt(1)', elems).hide();
			$('ul', elems)
				.append(moreBtn)
				.children('li.more-social')
				.bind('click', function(){
						$('ul', elems)
							.children('li:gt(1)')
							.animate({
								height: 'show',
								opacity: 'show'
							 })
						;
						$(this).remove();
						return false;
					})
			;
		}
        link.bind('click', function(){
            if(socialbookmark.toggleElem.is(':hidden')){				
                socialbookmark.show();
            } else {
                socialbookmark.hide();
            }
            return false;
        });
		socialbookmark.toggleElem = elems.hide().insertAfter(link);
		if(socialbookmark.options.initialshow){
			socialbookmark.show.call(link);
		}					
        return false;
    },
    hideNotinActElm: function(e){
        var jElm = $(e.target);
        if (e.target === socialbookmark.toggleElem[0] || jElm.parents('#bookmarks').size()) {
            return;
        }
        socialbookmark.hideElems();
    },
    show: function(){	
        socialbookmark.toggleElem.animate({
            height: 'show',
            opacity: 'show'
        });				
        $('body').bind('click', socialbookmark.hideNotinActElm);
        return false;
    },
    hide: function(){						
        socialbookmark.toggleElem.animate({
            height: "hide",
            opacity: "hide"
        });
        $('body').unbind('click', socialbookmark.hideNotinActElm);		
		socialbookmark.hideElems();	
    },
	hideElems: function(){		
		if($('ul', '#bookmarks').children('li.more-social').length === 0){
			if($('ul li', '#bookmarks').children().length > socialbookmark.options.showlimit){				
				var moreBtn = '<li class="more-social"><a href="">Weitere</a></li>';
				$('ul > li:gt(1)', '#bookmarks').animate({
					height: "hide",
					opacity: "hide"
				});
				$('ul', '#bookmarks')
					.append(moreBtn)
					.children('li.more-social')
					.bind('click', function(){
						$('ul', '#bookmarks')
							.children('li:gt(1)')
							.animate({
								height: 'show',
								opacity: 'show'
							 })
						;
						$(this).remove();
						return false;
					})
				;
			}
		}
		return false;			
	},
    init: function(sel, options){
        var jElm = $(sel);
        if (jElm.size()) {
            socialbookmark.handler(jElm, options);
        }
    }
};



	var teaserShowcase = function (){
		var teaser, indicator, stage, INDICATORLEFT = 935, shadow, STAGEWIDTH;
		
		//Prev-Next-Link-Funktion
		function switchLink(status, fast){
			var opac = (status === 'hide')?[0, 0.3]:[1,1];
			fast = (fast && fast == 'fast') ?
				'css' :
				'animate';
			this[fast]({opacity: opac[0]}, {duration: 400})
				.parent()[fast]({opacity: opac[1]}, {duration: 400});
		}
		
		//
		function showPicTitleIndicator(){
			var img = $(this),
				dl = $(this).parents('dl:first'),
				dlWidth = dl.outerWidth()/2,
				offsetLeft =  dl[0].offsetLeft,
				stageScroll = stage[0].scrollLeft,
				indicatorPos = offsetLeft - stageScroll + dlWidth - INDICATORLEFT;
			
			if(indicatorPos < -920){
				indicatorPos = -920;
				if(offsetLeft - stageScroll + (dlWidth * 2) < 40) {
					return;
				}
			} else if(indicatorPos > -515) {
				indicatorPos = -515;
				if((offsetLeft - stageScroll - STAGEWIDTH) * -1 < 40) {
					return;
				}
			}
			var description = '<h4>'+$('dd.title', dl[0]).text()+'</h4>',
				picHeight = img.css('height'),
				picWidth = img.css('width'),
				picMargin = parseInt(img.css('marginTop'), 10);
			
			description += '<p>'+$('dd:not(.title)', dl[0]).text()+'</p>';
			
			shadow.css({top: picMargin-2, left: offsetLeft+6})
				.find('div.pic-carousel-shadow').css({width: picWidth, height: picHeight})
				.end().css({display: 'block'});
			indicator.html(description)
				.css({'background-position': indicatorPos+'px 0px'});
			if(indicator.is('.inactive')){
				moveUp();
			}
		}
		function moveUp(){
			$('div.stage-design', stage[0]).stop(true, true).animate({'marginTop': '-13px'}, {duration: 500});
			indicator.stop(true, true).animate({'top': '-59px'}, {duration: 500})
				.removeClass('inactive');
			if($.browser.opera){
				$('#picture-description').hide();
			}
		}
		function moveDown(){
			$('div.stage-design', stage[0]).stop(true, true).animate({'marginTop': '0px'}, {duration: 500});
			indicator.stop(true, true).animate({'top': '1px'}, {duration: 500, queue: false, complete: function(){
				if($.browser.opera){
					$('#picture-description').show();
				}
			}}).addClass('inactive');
			shadow.css({display: 'none'});
		}
		function init(){
			teaser = $('#teaser-wrapper').scroller(
				{
					atoms: 'dl',
					nextLink: 'div.next a',
					prevLink: 'div.prev a',
					linkFn: switchLink,
					hidingWidth: 433,
					animateOptions: 400
				}
			);
			
			var teaserHeight = teaser.height()-30;
			
			if(teaser[0]){
				$('img', teaser[0]).each(function(){
					var jElm = $(this),
					height = jElm.height();
					var marginTop = (teaserHeight - height)/2;
					jElm.css({'marginTop': marginTop});
				}).bind('mouseenter', showPicTitleIndicator);;
				shadow = $('<div class="pic-carousel-shadow-out"><div class="pic-carousel-shadow"><div class="pic-bl"></div><div class="pic-tr"></div><div class="pic-br"></div></div></div>')
					.css({display: 'none'}).prependTo('#teaser-wrapper div.stage-design');
				stage = $('div.stage', teaser[0]);
				STAGEWIDTH = stage.width();
				var wrapper = $('div.stage-wrapper-out', teaser[0])
					.bind('mouseleave', moveDown);
				indicator = $('<div id="picture-indicator"></div>').appendTo(wrapper[0])
					.css({'backgroundPosition': '-920px 0'}).addClass('inactive');
				
				
				$('div.next a, div.prev a', teaser[0]).each(function(){
					var jElm = $(this);
					if(!jElm.is('.show')){
						switchLink.call(jElm, 'hide', 'fast');
					}
				});
			}
			
		}
		return {
			init: init
		};
	}();
	
	function initReports(){
		function over(){
			$('a.subtitle', this).stop(true).animate({
				'top': 0
			}, {duration: 300});
		}
		function out(){
			$('a.subtitle', this).stop(true).animate({
				'top': 107
			}, {duration: 300});
		}
		
		$('ul.reports li').hover(over, out);
	}
	
	function flashConsole(errorStr){
			alert('errorStr');
		}
			
	var flashControl = {
		startVid: function(){
			getFlash('flvPlayer').startNewVideo($('#flash-control a').index(this)+1);
			return false;
		},
		init: function(){
			$('#flash-control a').attr({'role': 'button'})
				.click(flashControl.startVid);
		}
	};
	
/*
 * Within the Help-Popup there is a link refering to an overview of all help topics
 * (with class "nf-all-help"), clicking this link will cause the overview to be loaded
 * in the original page (not in the popup) and will close the popup.
 */
 
	// Links
	function checkLinks() {
		var objAnchors, linkTitlePrefix, linkTitleHint, i;
		if (document.getElementsByTagName) {
			objAnchors = document.getElementsByTagName('a');
			for (i=0; i<objAnchors.length; i++) {
				if (objAnchors[i].getAttribute('href') && (objAnchors[i].getAttribute('rel'))) {
					objAnchors[i].onclick = function(evt) {
						return launchWindow(this, evt);
					};
					objAnchors[i].onkeypress = function(evt) {
						return launchWindow(this, evt);
					};
				}
			}
		}
	}
	// Windows
	function launchWindow(objAnchor, objEvent, width, height) {
		var iKeyCode;
		var relType = objAnchor.getAttribute('rel');
	
		if (objEvent && objEvent.type == 'keypress') {
			if (objEvent.keyCode) {
				iKeyCode = objEvent.keyCode;
			} else if (objEvent.which) {
				iKeyCode = objEvent.which;
			}
			if (iKeyCode != 13 && iKeyCode != 32) {
				return true;
			}
		}
		var win;
		if(relType && relType=="popup") { // open popup 475x550
			win = window.open(objAnchor,'win','width=' + (width ? width : "475") + ',height=' + (height ? height : "550") + ',scrollbars=yes,resizable=yes');
		} else {
			win = window.open(objAnchor,'win','scrollbars=yes,resizable=yes,location=yes,menubar=yes,status=yes,toolbar=yes');
		}
		win.focus();
		return false;
	}
	
	$.fn.imageShuffle = function(params){
		params = $.extend({}, $.fn.imageShuffle.defaults, params);
		var that = this,
			urls = params.imageUrls,
			imgUrl,
			index,
			oldIndex;
		return this.each(function(){
			var imgBufferElm = $(params.imgBuffer).appendTo($(this));
			imgBufferElm.css({
				width: that.outerWidth()
			});
			setInterval(function(){
				if(!params.transition){
					index = Math.floor(Math.random() * urls.length);					
					if(index == oldIndex){
						if(index == urls.length - 1){
							index = index - 1;
						} else if(index == 0){
							index = index + 1;
						} else {
							index++;
						}
					}					
					imageUrl = 'url("' + urls[index] + '")';					
					imgBufferElm.css('backgroundImage', imageUrl);		
					imgBufferElm.animate(
						{opacity: 1}, 
						500, 
						function(){							
							that.css('backgroundImage', imageUrl);
							imgBufferElm.css('opacity', 0);
						}
					);					
					oldIndex = index;
				}
				if(params.transition == 'smooth'){
				}
			}, params.delay);
		});
	};
    $.fn.imageShuffle.defaults = {		
        delay: 5000,
		transition: 'smooth',
		imgBuffer: '<div class="imageShuffleBuffer"></div>'
    };	
	
	function callOnDomReady(){	
		flashControl.init();	
		$('body').addClass('js-enabled');
		initReports();
		teaserShowcase.init();
		showTab();
		navList();
		socialbookmark.init('a.social-b', {
			showlimit: 2,
			initialshow: false
		});
		checkLinks();
		$('#siteinfo').imageShuffle({
			delay: 5000,
			transition: false,
			imageUrls: [
				"/img/jugendtestet2008/headers/01_header.jpg",
				"/img/jugendtestet2008/headers/02_header.jpg",
				"/img/jugendtestet2008/headers/03_header.jpg",
				"/img/jugendtestet2008/headers/04_header.jpg",
				"/img/jugendtestet2008/headers/05_header.jpg",
				"/img/jugendtestet2008/headers/06_header.jpg",
				"/img/jugendtestet2008/headers/07_header.jpg",
				"/img/jugendtestet2008/headers/08_header.jpg",
				"/img/jugendtestet2008/headers/09_header.jpg",
				"/img/jugendtestet2008/headers/10_header.jpg",
				"/img/jugendtestet2008/headers/11_header.jpg",
				"/img/jugendtestet2008/headers/12_header.jpg",
				"/img/jugendtestet2008/headers/13_header.jpg",
				"/img/jugendtestet2008/headers/14_header.jpg",
				"/img/jugendtestet2008/headers/15_header.jpg",
				"/img/jugendtestet2008/headers/16_header.jpg",
				"/img/jugendtestet2008/headers/17_header.jpg",
				"/img/jugendtestet2008/headers/18_header.jpg",
				"/img/jugendtestet2008/headers/19_header.jpg",
				"/img/jugendtestet2008/headers/20_header.jpg",
				"/img/jugendtestet2008/headers/21_header.jpg",
				"/img/jugendtestet2008/headers/22_header.jpg",
				"/img/jugendtestet2008/headers/23_header.jpg"	
			]
		});
	}
	
	
$(callOnDomReady);

})(jQuery);

		

