/* Author: Stefan Reichert
	15.12.2011
*/

/*
JavaScript for the demo: Recreating the Nikebetterworld.com Parallax Demo
Demo: Recreating the Nikebetterworld.com Parallax Demo
Author: Ian Lunn
Author URL: http://www.ianlunn.co.uk/
Demo URL: http://www.ianlunn.co.uk/demos/recreate-nikebetterworld-parallax/
Tutorial URL: http://www.ianlunn.co.uk/blog/code-tutorials/recreate-nikebetterworld-parallax/

License: http://creativecommons.org/licenses/by-sa/3.0/ (Attribution Share Alike).

Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
*/

$(document).ready(function() {
	
	// aktuelles Jahr für Footer
		var jetzt = new Date();
		var Jahr = jetzt.getFullYear();
		$(".year").html(Jahr);
	
	// Add Scrolling to Navigation
		$('#nav').localScroll();
		// Twitter Status auslesen
		$('#tweets').tweetable({username: 'fanfarian', time: true, limit: 5, replies: false, position: 'append'});
	
	var winW = 768;
	if (document.body && document.body.offsetWidth) {
	 winW = document.body.offsetWidth;
	}
	if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ) {
	 winW = document.documentElement.offsetWidth;
	}
	if (window.innerWidth && window.innerHeight) {
	 winW = window.innerWidth;
	}
	
	// no parallax on mobile or Tablet
	if(winW > 992)
	{
		//save selectors as variables to increase performance
		var $window = $(window);
		var $sr_nav = $('#nav');
		var $headerBG = $('#header');
		var $blogBG = $('#blog');
		var $sr_aboutBG = $('#sr_about');
		var $sr_portfolioBG = $('#sr_portfolio');
		var windowHeight = $window.height();
			
		//function that is called for every pixel the user scrolls. Determines the position of the background
		/*arguments: 
			x = horizontal position of background
			windowHeight = height of the viewport
			pos = position of the scrollbar
			adjuster = adjust the position of the background
			inertia = how fast the background moves in relation to scrolling
		*/
		function newPos(x, windowHeight, pos, adjuster, inertia){
			return x + "% " + (-((windowHeight + pos) - adjuster) * inertia)  + "px";
		}
		
		//function to be called whenever the window is scrolled or resized
		function Move(){ 
			var pos = $window.scrollTop(); //position of the scrollbar
			
			// Navigation verschieben, je nach scrolling
			if(pos<=1199) {
				$(".topnav li.NAVheader").siblings('.selected').removeClass('selected').mouseleave();
				$(".topnav li.NAVheader").addClass('selected');
				
				if (winW >= 1200)
					$(".topnav li.NAVheader").find("span").stop().animate({ marginTop: "-80" }, 250);
				else
					$(".topnav li.NAVheader").find("span").stop().animate({ marginTop: "-60" }, 250);
				
				$(".topnav li.NAVsr_about").find("span").stop().animate({ marginTop: "0" }, 0);
				$(".topnav li.NAVsr_portfolio").find("span").stop().animate({ marginTop: "0" }, 0);
				$(".topnav li.NAVfooter").find("span").stop().animate({ marginTop: "0" }, 0);
				
				if (winW >= 992)
					$('#nav').animate({top:230},{duration:500,queue:false});
			}
			else if(pos>=1200 && pos<=2499) {
				$(".topnav li.NAVsr_about").siblings('.selected').removeClass('selected').mouseleave();
				$(".topnav li.NAVsr_about").addClass('selected');
				
				if (winW >= 1200)
					$(".topnav li.NAVsr_about").find("span").stop().animate({ marginTop: "-80" }, 250);
				else
					$(".topnav li.NAVsr_about").find("span").stop().animate({ marginTop: "-60" }, 250);
				
				$(".topnav li.NAVheader").find("span").stop().animate({ marginTop: "0" }, 0);
				$(".topnav li.NAVsr_portfolio").find("span").stop().animate({ marginTop: "0" }, 0);
				$(".topnav li.NAVfooter").find("span").stop().animate({ marginTop: "0" }, 0);
				
				if (winW >= 992)
					$('#nav').animate({top:30},{duration:500,queue:false});
			}
			else if(pos>=2500 && pos<=3399) {
				$(".topnav li.NAVsr_portfolio").siblings('.selected').removeClass('selected').mouseleave();
				$(".topnav li.NAVsr_portfolio").addClass('selected');
				
				if (winW >= 1200)
					$(".topnav li.NAVsr_portfolio").find("span").stop().animate({ marginTop: "-80" }, 250);
				else
					$(".topnav li.NAVsr_portfolio").find("span").stop().animate({ marginTop: "-60" }, 250);
				
				$(".topnav li.NAVheader").find("span").stop().animate({ marginTop: "0" }, 0);
				$(".topnav li.NAVsr_about").find("span").stop().animate({ marginTop: "0" }, 0);
				$(".topnav li.NAVfooter").find("span").stop().animate({ marginTop: "0" }, 0);
				
				if (winW >= 992)
					$('#nav').animate({top:30},{duration:500,queue:false});
			}
			else if(pos>=3400) {
				$(".topnav li.NAVfooter").siblings('.selected').removeClass('selected').mouseleave();
				$(".topnav li.NAVfooter").addClass('selected');
				
				if (winW >= 1200)
					$(".topnav li.NAVfooter").find("span").stop().animate({ marginTop: "-80" }, 250);
				else
					$(".topnav li.NAVfooter").find("span").stop().animate({ marginTop: "-60" }, 250);
					
				$(".topnav li.NAVheader").find("span").stop().animate({ marginTop: "0" }, 0);
				$(".topnav li.NAVsr_about").find("span").stop().animate({ marginTop: "0" }, 0);
				$(".topnav li.NAVsr_portfolio").find("span").stop().animate({ marginTop: "0" }, 0);
			
				if (winW >= 992)
					$('#nav').animate({top:30},{duration:500,queue:false});
			}
			// Parallax für die gepunkteten Hintergründe
			$('#container').css({'backgroundPosition': newPos(0, windowHeight, pos, 900, 0.25) + ", " + newPos(0, windowHeight, pos, 500, 0.6) + ", " + newPos(0, windowHeight, pos, 1200, 0.8) + ", " + newPos(0, windowHeight, pos, 1100, 0.4)});
		}
		
		$window.resize(function(){ Move(); });		
		$window.bind('scroll', function(){ Move(); });
	}	

	//if (screen.width > 768)
	if(winW > 768)
	{	
		// Formularfelder Schriftgröße automatisch anpassen
		$('input#name').inputfit();
		$('input#email').inputfit();
			
		// Prepare Navigation
		if (screen.width >= 1200)
			av = -80;
		else
			av = -60;
			
		$(".topnav li").prepend("<span></span>");
		$(".topnav li").each(function() { var linkText = $(this).find("a").html(); $(this).find("span").show().html(linkText); }); 
		// Home -> Selected by Default
		$(".topnav li.selected").each(function() { $(this).find("span").stop().animate({ marginTop: av },0); });
		// Animated Hover and Active/Clicked Items
		$(".topnav li").hover(function() {	$(this).find("span").stop().animate({ marginTop: av }, 250);}, function() {if(!$(this).hasClass('selected')) {	$(this).find("span").stop().animate({ marginTop: "0" }, 250);}});
		$(".topnav li").click(function() {	if(!$(this).hasClass('selected')) {	$(this).siblings('.selected').removeClass('selected').mouseleave();	$(this).addClass('selected');}});
	}
		// Skillbox Animation
		$('.sr_about_skillbox').hover(function(){
			$(this).addClass( 'hover' );
				$('.sr_about_skill_title', this).animate({'margin-top':'-35px'},{queue:true,duration:400});
				$('.sr_about_skill_title ul', this).show(300);
			}, function() {
				$(this).removeClass( 'hover' );
				$('.sr_about_skill_title', this).animate({'margin-top':'0px'},{queue:true,duration:400});
				$('.sr_about_skill_title ul', this).hide(200);
		});
	
	// Run Matt Kersley's jQuery Responsive menu plugin (see plugins.js)
		if ($.fn.mobileMenu) {
			$('ol#id').mobileMenu({
				switchWidth: 768,                   // width (in px to switch at)
				topOptionText: 'Choose a page',     // first option text
				indentString: '&nbsp;&nbsp;&nbsp;'  // string for indenting nested items
			});
		}
	
	// Run Mathias Bynens jQuery placeholder plugin (see plugins.js)
		if ($.fn.placeholder){
			$('input, textarea').placeholder();		
		}
	
});

	$(window).load(function() {		
		$('#slider1').nivoSlider({effect:'fade',directionNav:false,manualAdvance:true});
		$('#slider2').nivoSlider({effect:'fade',directionNav:false,manualAdvance:true});
		$('#slider3').nivoSlider({effect:'fade',directionNav:false,manualAdvance:true});
	});
	
	
	$(function() {  
	  $(".submit_btn").click(function() {  
	  	var name  = $("input#name").val();
		var email = $("input#email").val();
		var text  = $("textarea#text").val();  
		var dataString = 'name='+ name + '&email=' + email + '&text=' + text;  
		
		$.ajax({  
		  type: "POST",  
		  url: "cfg/contactmail.php",  
		  data: dataString,  
		  success: function() {  
			$('#contact_form').html("<div id='mail_send_message'></div>");  
			
			$('#mail_send_message')
			.hide().
			html("<p>Contact form submitted!<br>")
			.fadeIn(1000, function() {  
			  $('#mail_send_message').append("I will be in touch soon.<img src='css/footer_contact_tick.png' alt='Form submitted' class='float_left mail_ok' width='50' ></p>");  
			});  
		  }  
		});  
		return false;
	  });  
	});

/*
  * Normalized hide address bar for iOS & Android
  * (c) Scott Jehl, scottjehl.com
  * MIT License

(function( win ){
	var doc = win.document;
	
	// If there's a hash, or addEventListener is undefined, stop here
	if( !location.hash && win.addEventListener ){
		
		//scroll to 1
		window.scrollTo( 0, 1 );
		var scrollTop = 1,
			getScrollTop = function(){
				return win.pageYOffset || doc.compatMode === "CSS1Compat" && doc.documentElement.scrollTop || doc.body.scrollTop || 0;
			},
		
			//reset to 0 on bodyready, if needed
			bodycheck = setInterval(function(){
				if( doc.body ){
					clearInterval( bodycheck );
					scrollTop = getScrollTop();
					win.scrollTo( 0, scrollTop === 1 ? 0 : 1 );
				}	
			}, 15 );
		
		win.addEventListener( "load", function(){
			setTimeout(function(){
				//at load, if user hasn't scrolled more than 20 or so...
				if( getScrollTop() < 20 ){
					//reset to hide addr bar at onload
					win.scrollTo( 0, scrollTop === 1 ? 0 : 1 );
				}
			}, 0);
		} );
	}
})( this );
*/

