/* Author:
Hanswurst von der hinteren Alpe
*/

var originalPosition = $('header').position(); 
var logoTopOrig = $('#logo').position().top;
var autoScroll = true;
var autoClick = false;
  
  
function scrollHandler(event)
{
	if($(window).scrollTop() <= $('header').height() - 200)
	{
		$('#main').css('margin-top', 10);
	/*
	  $('#main').animate({marginTop: 0}, 600, 'easeInBack', function(){
		 $("nav a").attr('class', 'inactive');
		 $("nav a:first").attr('class', 'active');

	  }); // header_height
	 */
	}

	if($(window).scrollTop() > $('header').height() - 30)
	  {
		  // make navigation fixed on top
		  $('nav').css('position', 'fixed');
		  $('nav').css('top', 0);
		  autoScroll = false;	
	  }
	  else
	  {
		  if(parseInt($('#main').css('margin-top'), 10) <= 0 
				&& !autoScroll 
				//&& !autoClick
				//&& $("nav a:first").attr('class') == 'inactive'
			)
		  {
			  //window.log($("nav li:contains(a.inactive):lt(" + $("nav a.active").parent('li').index() + ")").length);
			  if($("nav li:lt(" + $("nav a.active").parent('li').index() + ")").length > 0)
			  {
			    
				
				//$("nav li:eq(" + $("nav a.active").parent('li').index() + ")").last().find('a').click();
				$('#main').animate({marginTop: '+=400px'}, 10, 'linear', function(){
					 if(parseInt($('#main').css('margin-top'), 10) > 30)
					 {
						$('#main').css('margin-top', 10);
					 }
				});
				
				$(window).scrollTop($('header').height());
				//$("nav a.active").prevAll('a.inactive').click();
				
				//autoClick = true;
			  }
		  }
		  else
		  {
			  // scroll to top first in content area
			  // set original position of navigation
			  //$("nav a:first").click();
			  $('nav').css('position', 'relative');
			  $('nav').css('top', 0);
			  $('#logo').css('top', logoTopOrig);
			  autoScroll = false;
		  }
	  }

	$('article').each(function() {
		if(($(this).offset().top + $(this).outerHeight() - $(window).scrollTop()) > 10)
		{
			$("nav a").attr('class', 'inactive');
			$('nav a[href="#' + $(this).attr('id') + '"]').attr('class', 'active');
			return false;
		}
	});
	  
}


function logos()
{
	next = $("#container footer .slider li.active").next('li');
	if(next.length == 0) 
	{
		next = $("#container footer .slider li:first");
	}
	
	$("#container footer .slider li:not(.active)").css('display', 'none').css('opacity', 0);
	
	$("#container footer .slider li.active").animate({opacity: 0}, 200, 'linear', function() {
		$(this).attr('class', '').css('display', 'none');
		next.css('display', 'inline');
		next.animate({opacity: 100}, 300, 'linear', function() {
			$(this).attr('class', 'active');		
		});
	});
	
	
}

var start = new Date(2012, 1, 22, 19, 0, 0, 0);
		
function countdown()
{
	var now = new Date();
	var time_left = new Date(start - now);
	$('.countdown').html(time_left.getDay() + 'd ' + time_left.getHours() + 'h ' + time_left.getMinutes() + 'm ' + time_left.getSeconds() + ',' + time_left.getMilliseconds() + 's');
}
			
			
			
$(document).ready(function(){
	window.setInterval(logos, 5000);
	window.setInterval(countdown, 100);

    scrollHandler();
  
    $(window).scroll(function(event) {
	  scrollHandler(event);  
    });	
	
   $("nav a").click(function(event){
	 event.preventDefault();
	 var link = $(this);
     var header_height = $('header').height();
	 var position_top = $('#' + link.attr('href').split('#')[1]).position().top;
	 
	 $('#main').animate({marginTop: (Math.ceil(position_top)-60)*(-1)}, 600, 'easeInBack', function(){
		 autoScroll = true;
		 $("nav a").attr('class', 'inactive');
		 link.attr('class', 'active');
		 if(!autoClick)
		 {
			$(window).scrollTop(header_height);
		 }
		 autoClick = false;
	 }); // header_height
   });
   
   
	// ajax form
	
	//$("#registration").validate();
	/*
	var form = $("#registration").validate({
			submitHandler: function(form) {
				$('#submit').hide();
				$.ajax({
					type: 'POST',
					url: $("#registration").attr('action'),
					data: $("#registration").serialize(),
					success: function(data) {
						$('#message').html(data.message)
					},
					error: function() {
						$('#message').html("Fehler, bitte nochmals probieren. Alle Felder sind Pflichtfelder!");
						$('#submit').show();
					},
					dataType: 'json'
				});
				return false;
			}
		});	
	*/

	$.validator.addMethod(
		"austrianDate",
		function(value, element) {
			// put your own logic here, this is just a (crappy) example
			return value.match(/^\d\d?\.\d\d?\.\d\d\d\d$/);
		},
		"Bitte Datum im Format TT.MM.JJJJ angeben."
	);

	
	$.validator.addMethod("defaultInvalid", function(value, element) 
	{
		if (element.value == element.defaultValue)
		{
			return false;
		}
		return true;
	});
	
	
	$("#registration").validate({
		submitHandler: function() {
			$('#submit').hide();
			$.ajax({
				type: 'POST',
				url: $("#registration").attr('action'),
				data: $("#registration").serialize(),
				success: function(data) {
					$('#message').html(data.message)
				},
				error: function() {
					$('#message').html("Fehler, bitte nochmals probieren. Alle Felder sind Pflichtfelder!");
					$('#submit').show();
				},
				dataType: 'json'
			});
			return false;
		}
	});
	

	$('#rider_type').change(function() {
		if($(this).val() == 'board')
		{
			$('.stance').css('visibility', 'visible');
		}
		else
		{
			$('.stance').css('visibility', 'hidden');
		}
	});
	
	
	
});








