jQuery(function( $ ){








//-------------------

$('#topnavi li a').click(function(e) {
	var target = $(this).attr('href');
	$('#flowtabs li a, #topnavi li a').removeClass('current');
	$(this).addClass('current');
	$('#sections').scrollTo(target, {
		duration: 800
	});
	
	e.preventDefault();
}); 

for (var i = 1; i <= $('#flowtabs li').length; i++) {
	$("#flowtabs li:nth-child(" + i + ")").attr('class', i - 1);
}



$('#flowtabs li a, #topnavi li a').click(function() {
	var factor  = parseInt($(this).parent().attr('class')),
		width   = $(this).outerWidth(),
		amountY = (factor === 0) ? '8' : '-' + (72*factor-8),
	//	amountX = (factor === 0) ? '40' : ((width / 2) - 12 - ($('.indicator').outerWidth() / 2)) * (-1);
		amountX = 1;
		
		$('#sections').attr('class', factor);
		
		$('#sections').bind('animated', function() {
			if ( factor === 1) {
				animateBubbles();
			} else {
				$('#team a').stop();
			}
			$('#sections').unbind('animated');
		});
	
	$('.indicator').animate({
		backgroundPosition: amountX + "px " + amountY + "px"
	}, 400);
	

});


$('.forwar_btn').click(function() {
	
//	$('#flowtabs li a.current').parent().next().find('a').trigger('click')
	
	var currSlide = parseInt($('#sections').attr('class'));

	$('#sections').bind('animated', function() {
		if ( currSlide === 0 ) {
			animateBubbles();
		} else {
			$('#team a').stop();
		}
		$('#sections').unbind('animated');
	});
	
/*	if (currSlide === 10 || currSlide === 11) {
		var factor  = (currSlide + 1),
			width   = $('#topnavi li.' + (currSlide + 1)).find('a').outerWidth(),
			amountY = '-' + (72*factor-8),
		//	amountX = ((width / 2) - 12 - ($('.indicator').outerWidth() / 2)) * (-1);
			amountX = 2;
			
		$('#sections').attr('class', factor);

		$('.indicator').animate({
			backgroundPosition: amountX + "px " + amountY + "px"
		}, 400);
			
	} else */ if (currSlide === $('#flowtabs li').length-1) {

		var factor  = 0,
			width   = $('#flowtabs li:first-child a').outerWidth(),
			amountY = '8',
			amountX = '2';
			
		$('#sections').attr('class', factor);	
		
		$('.indicator').animate({
			backgroundPosition: amountX + "px " + amountY + "px"
		}, 400);
	
	
	} else {
		var factor  = parseInt($('#flowtabs li a.current').parent().next().attr('class')),
			width   = $('#flowtabs li a.current').parent().next().find('a').outerWidth(),
			amountY = (factor === 0) ? '8' : '-' + (72*factor-8),
		//	amountX = (factor === 0) ? '40' : ((width / 2) - 12 - ($('.indicator').outerWidth() / 2)) * (-1);
			amountX = 1;
			
			$('#sections').attr('class', factor);
			
			
		
		$('.indicator').animate({
			backgroundPosition: amountX + "px " + amountY + "px"
		}, 400);
			
	}
	
		
	
});

//-------------------


/*******************************************************************************
*
* Navipille Animation
*
********************************************************************************/


function animate() {
/*
	var pille = $('#naviPille');
	
	pille.animate({
		scale: [0.97, 0.97],
		translateX: '15px',
		translateY: '5px'
	}, 2500, function() {
		pille.animate({
			scale: [1,1],
			translateX: '0px',
			translateY: '0px'
		}, 2500, function() {
			animate();
		});
	});
	*/
}

if ($('html').hasClass('no-cssanimations')) {
	//console.log("startanimate");
	animate();	
}

/*******************************************************************************
*
* Magazine Flips
*
********************************************************************************/
/*

$("ul.magazines>li").hover(
	function() {
		var $this = $(this);
		if ( !$this.hasClass("flipped")  ) {
		
			$this.flip({
				direction:'lr',
				content: $(this).find('.backSide'),
				color: '#7db9e8',
				onEnd: function() {
					$this.addClass("flipped");
				}
			});
		}
	}, 
	function() {
		var $this = $(this);
		if ( $this.hasClass("flipped") ) {
			$this.clearQueue('fx').revertFlip();
		}
	}
	
	);
*/	
	
	/*.click(function() {
		content = $(this).data('content');
		if (content) {
			$.modal("<div class='modalWin'>" + content + "</div>");
		}
		
	});
	*/

//$('body').mousemove(function() {
//	$("#magazines li.flipped").revertFlip();
//});


/*******************************************************************************
*
* Team Bubbles
*
********************************************************************************/



function animateBubbles() {
	var max 	   = 20,
		auslenkung = 0,
		factor	   = 2,
		top		   = '',
		left	   = '';
	
	$('#team a').each(function() {
		if ($(this).data('factor')) {
			factor = ( parseInt($(this).data('factor')) > 0 ) ? -1 : 1;
		} else {
			$(this).data('factor', factor);
		}
		
		auslenkung = (Math.floor(Math.random()*max+1) - 1) * factor;
		
		top = auslenkung/2 + 'px';
		left = auslenkung + 'px';
		
		$(this).stop().animate({
			translateX: left,
			translateY: top
		}, 1900, function() {
			animateBubbles();
		});
		
	});
	
}


/*
$('#team a').click(function(e) {
	
	content = $($(this).attr('href')).html();
	
	$.modal("<div class='modalWin'>" + content + "</div>");
	
	e.preventDefault();
	
});
*/






/*******************************************************************************
*
* Kontor 4 Zeugs (mit Anpassungen)
*
********************************************************************************/

$('#content').serialScroll({
		target:'#sections',
		items:'#wrapper>div', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		//prev:'a.prev',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
		next:'.forwar_btn',// Selector to the 'next' button (absolute too)
		axis:'x',// The default is 'y' scroll on both ways
		navigation:'#flowtabs li a',
		duration:500,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
		force: true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
		
		//queue:false,// We scroll on both axes, scroll both at the same time.
		//event:'click',// On which event to react (click is the default, you probably won't need to specify it)
		stop:true,// Each click will stop any previous animations of the target. (false by default)
		lock:false, // Ignore events if already animating (true by default)
		//start: 0, // On which element (index) to begin ( 0 is the default, redundant in this case )		
		cycle:true,// Cycle endlessly ( constant velocity, true is the default )
		//step:1, // How many items to scroll each time ( 1 is the default, no need to specify )
		//jump:false, // If true, items become clickable (or w/e 'event' is, and when activated, the pane scrolls to them)
		//lazy:false,// (default) if true, the plugin looks for the items on each event(allows AJAX or JS content, or reordering)
		//interval:1000, // It's the number of milliseconds to automatically go to the next
		constant:true, // constant speed
		
		onBefore:function( e, elem, $pane, $items, pos ){
		/*
		var button=$(e.target);
		console.log(button);
		if(button.parent().parent().attr("id")=="flowtabs"){
			$("#flowtabs li a").removeClass("current");
			button.addClass("current");
		}
		*/
			/**
			 * 'this' is the triggered element 
			 * e is the event object
			 * elem is the element we'll be scrolling to
			 * $pane is the element being scrolled
			 * $items is the items collection at this moment
			 * pos is the position of elem in the collection
			 * if it returns false, the event will be ignored
			 */
			 //those arguments with a $ are jqueryfied, elem isn't.
			e.preventDefault();
			if( this.blur )
				this.blur();
		},
		onAfter:function( elem ){
			elem=$(elem);
			$("#flowtabs li a").removeClass("current");
			$("#topnavi li a").removeClass("current");
			$('#nav_'+elem.attr("id")).addClass("current");
			
			$('#sections').trigger('animated');
		}
	});

/** broschueren MODAL **/

$('a.broschueren_modal').click(function(e){
	e.preventDefault();
//	var src="http://www.kontor4.de/kunden/mundm2011/aufbau";
	var src="http://www.mannundmaus.de";

	src+="/content/broschueren/"+$(this).attr("rel")+".php";
	$.modal('<iframe src="' + src + '" height="385" width="960" style="border:0" SCROLLING="no">',{
				overlayClose:true,
				opacity:100,
				autoPosition:false,
				width:'100%',
				onShow:function(dialog){
					posModal();
				},
				onOpen:modalOnOpen,
				onClose:function(dialog){
					dialog.data.fadeOut('fast', function () {
						dialog.container.fadeOut('fast', function () {
							dialog.overlay.fadeOut('fast', function () {
								$.modal.close(); // must call this!
							});
						});
					});
				}
			});
});
/** cd MODAL **/

$('a.cd_modal').click(function(e){
	e.preventDefault();
//	var src="http://www.kontor4.de/kunden/mundm2011/aufbau";
	var src="http://www.mannundmaus.de";

	src+="/content/corporatedesign/"+$(this).attr("rel")+".php";
	$.modal('<iframe src="' + src + '" height="385" width="960" style="border:0" SCROLLING="no">',{
				overlayClose:true,
				opacity:100,
				autoPosition:false,
				width:'100%',
				closeClass:'simplemodal-container-cd',
				onShow:function(dialog){
					posModal();
				},
				onOpen:modalOnOpen,
				onClose:function(dialog){
					dialog.data.fadeOut('fast', function () {
						dialog.container.fadeOut('fast', function () {
							dialog.overlay.fadeOut('fast', function () {
								$.modal.close(); // must call this!
							});
						});
					});
				}
			});
});

/** jahresberichte MODAL **/

$('a.jahresberichte_modal').click(function(e){
	e.preventDefault();
//	var src="http://www.kontor4.de/kunden/mundm2011/aufbau";
	var src="http://www.mannundmaus.de";

	src+="/content/jahresberichte/"+$(this).attr("rel")+".php";
	$.modal('<iframe src="' + src + '" height="385" width="960" style="border:0" SCROLLING="no">',{
				overlayClose:true,
				opacity:100,
				autoPosition:false,
				width:'100%',
				onShow:function(dialog){
					posModal();
				},
				onOpen:modalOnOpen,
				onClose:function(dialog){
					dialog.data.fadeOut('fast', function () {
						dialog.container.fadeOut('fast', function () {
							dialog.overlay.fadeOut('fast', function () {
								$.modal.close(); // must call this!
							});
						});
					});
				}
			});
});

/** NEWSLETTER MODAL **/

$('a.newsletter_modal').click(function(e){
	e.preventDefault();
//	var src="http://www.kontor4.de/kunden/mundm2011/aufbau";
	var src="http://www.mannundmaus.de";

	src+="/content/newsletter/"+$(this).attr("rel")+".php";
	$.modal('<iframe src="' + src + '" height="385" width="960" style="border:0" SCROLLING="no">',{
				overlayClose:true,
				opacity:100,
				autoPosition:false,
				width:'100%',
				onShow:function(dialog){
					posModal();
				},
				onOpen:modalOnOpen,
				onClose:function(dialog){
					dialog.data.fadeOut('fast', function () {
						dialog.container.fadeOut('fast', function () {
							dialog.overlay.fadeOut('fast', function () {
								$.modal.close(); // must call this!
							});
						});
					});
				}
			});
});

/** MAGAZINE MODAL **/

$('a.magazine_modal').click(function(e){
	e.preventDefault();
//	var src="http://www.kontor4.de/kunden/mundm2011/aufbau";
	var src="http://www.mannundmaus.de";

	src+="/content/magazine/"+$(this).attr("rel")+".php";
	$.modal('<iframe src="' + src + '" height="385" width="960" style="border:0" SCROLLING="no">',{
				overlayClose:true,
				opacity:100,
				autoPosition:false,
				width:'100%',
				onShow:function(dialog){
					posModal();
				},
				onOpen:modalOnOpen,
				onClose:function(dialog){
					dialog.data.fadeOut('fast', function () {
						dialog.container.fadeOut('fast', function () {
							dialog.overlay.fadeOut('fast', function () {
								$.modal.close(); // must call this!
							});
						});
					});
				}
			});
});

/** TRICKFILM MODAL **/

$('a.trickfilm_modal').click(function(e){
	e.preventDefault();
//	var src="http://www.kontor4.de/kunden/mundm2011/aufbau";
	var src="http://www.mannundmaus.de";

	src+="/content/trickfilm/"+$(this).attr("rel")+".php";
	$.modal('<iframe src="' + src + '" height="385" width="960" style="border:0" SCROLLING="no">',{
				overlayClose:true,
				opacity:100,
				autoPosition:false,
				width:'100%',
				onShow:function(dialog){
					posModal();
				},
				onOpen:modalOnOpen,
				onClose:function(dialog){
					dialog.data.fadeOut('fast', function () {
						dialog.container.fadeOut('fast', function () {
							dialog.overlay.fadeOut('fast', function () {
								$.modal.close(); // must call this!
							});
						});
					});
				}
			});
});

$('img.kontakt_modal').click(function(e){
	e.preventDefault();
//	var src="http://www.kontor4.de/kunden/mundm2011/aufbau";
	var src="http://www.mannundmaus.de";
	src+="/content/kontakt/"+$(this).attr("rel")+".php";
	$.modal('<iframe src="' + src + '" height="385" width="960" style="border:0" SCROLLING="no">',{
				overlayClose:true,
				opacity:100,
				autoPosition:false,
				width:'100%',
				onShow:function(dialog){
					posModal();
				},
				onOpen:modalOnOpen,
				onClose:function(dialog){
					dialog.data.fadeOut('fast', function () {
						dialog.container.fadeOut('fast', function () {
							dialog.overlay.fadeOut('fast', function () {
								$.modal.close(); // must call this!
							});
						});
					});
				}
			});
});

/** TEAM IMAGE HOVER */


	$('a.team_modal').hover(function(){
		var hoverbild="images/team/"+$(this).attr("rel")+"_name.png";
		$(this).find("img").attr("src",hoverbild);
	},function(){
		var normbild="images/team/"+$(this).attr("rel")+".png";
		$(this).find("img").attr("src",normbild);
	
	});
	$('a.team_modal_x').hover(function(){
		var hoverbild="images/team/"+$(this).attr("rel")+"_name.png";
		$(this).find("img").attr("src",hoverbild);
	},function(){
		var normbild="images/team/"+$(this).attr("rel")+".png";
		$(this).find("img").attr("src",normbild);
	
	});
	$('a.team_modal_x').click(function(){
		var normbild="images/team/"+$(this).attr("rel")+"_click.png";
		
		$(this).find("img").attr("src",normbild);
		//console.log(normbild);
	});
	
		$('a.team_modal').click(function (e) {
			$('#team a').stop();
			e.preventDefault();
			
			
//	var src="http://www.kontor4.de/kunden/mundm2011/aufbau";
	var src="http://www.mannundmaus.de";

			src+="/content/team/"+$(this).attr("rel")+".php";
			$.modal('<iframe src="' + src + '" height="385" width="960" style="border:0" SCROLLING="no">',{
				overlayClose:true,
				opacity:100,
				autoPosition:false,
				width:'100%',
				onShow:function(dialog){
					posModal();
				},
				onOpen:modalOnOpen,
				onClose:function(dialog){
					dialog.data.fadeOut('fast', function () {
						dialog.container.fadeOut('fast', function () {
							dialog.overlay.fadeOut('fast', function () {
								$.modal.close(); // must call this!
								animateBubbles();
							});
						});
					});
				}
			});
	
			return false;
		});
		
		/*
		$('a.kontakt').click(function (e) {
			
			var src="content/kontakt/"+$(this).attr("rel")+".php";
			$.modal('<iframe src="' + src + '" height="385" width="960" style="border:0" SCROLLING="no">',{
				overlayClose:true,
				autoPosition:false,
				onShow:function(dialog){
					posModal();
				},
				onOpen:modalOnOpen
			});
	
			return false;
		});		
		*/
		$('a.sliding').click(function (e) {
			var src="content/magazine/"+$(this).attr("rel")+".php";
			$.modal('<iframe src="' + src + '" height="385" width="960" style="border:0" SCROLLING="no">',{
				overlayClose:true,
				autoPosition:false,
				onShow:function(dialog){
					posModal();
				},
				onOpen:modalOnOpen
			});
	
			return false;
		});				

		
	$(window).resize(function() {
		posModal();
	});

	function modalOnOpen(dialog){
		dialog.overlay.fadeIn('slow', function () {
			dialog.container.fadeIn('slow', function () {
				dialog.data.fadeIn('slow');
			});
		});
	}
		
	function posModal(){
		var pos=$('#contentwrapper').offset();
		var windowwidth=$(window).width();
		var containerwidth=$('#simplemodal-container').outerWidth(true);
		var left=(windowwidth/2)-(containerwidth/2);
		
		$('#simplemodal-container').css({
			"top":pos.top+"px",
			"width":'100%'
//			"left":left+"px"
		});
		$('#simplemodal-container').find("iframe").attr("width","100%");
	}		
	
	
	
	
	
	
	/** PARTNER**/
	
	
	$('a.partnerknopf').mouseenter(function(){
		var divid="#par_"+$(this).attr("id"),
			pos=$(this).position();
		
		
		if(!$(divid).hasClass("over")){
			$('div.partnerkasten').removeClass("over").hide();
			$(divid).addClass("over");
			$(divid).css({
				top:pos.top+3,
				left:pos.left+3
				//width:'300px'
			}).show();
		}
		//console.log($(divid));
	});
	
	$('div.partnerkasten').mouseleave(function(){
		$('div.partnerkasten').removeClass("over").hide();
	});
	

//$('ul.magazines a').mouseenter(function(){
//	if(!$(this).hasClass("flipped")){
//		$(this).find('.frontSide').fadeOut("fast",function(){
//			$(this).parent().find('.backSide').fadeIn("fast",function(){
//				$(this).parent().addClass("flipped");
//			});
//		});
//	}
//});
//$('ul.magazines a').mouseleave(function(){
//	if($(this).hasClass("flipped")){
//		$(this).find('.backSide').fadeOut("fast",function(){
//			$(this).parent().find('.frontSide').fadeIn("fast",function(){
//				$(this).parent().removeClass("flipped");
//			});
//		});
//	}
//
//});




/*******************************************************************************
*
* Magazine Slides
*
********************************************************************************/




if ($('html').hasClass('no-csstransitions')) {
	
	$('.magazines:not(.TVs) li a').hover(function() {
		if ($(this).hasClass('internet_modal')) {
			$(this).find('.frontSide').stop().animate({
				left: '-214px'
			}, 500);
		} else {
			$(this).find('.frontSide').stop().animate({
				left: '-200px'
			}, 500);
		}
	}, function() {
		$(this).find('.frontSide').stop().animate({
			left: '0px'
		}, 500);
	});
	
	
	$('.magazines.TVs li a').hover(function() {
		$(this).find('.rauschen').stop().animate({
			opacity: '0'
		}, 500);
	}, function() {
		$(this).find('.rauschen').stop().animate({
			opacity: '1'
		}, 500);
	});

}

		
});



