/**
 * @author martinhochreiter, www.martinhochreiter.at
 */

//flag for page scrolling to not trigger waypoint
var pageJump = false;

$(document).ready( function() {
	
	$('div.content').css({'min-height':$(window).height()+'px'});
	
	/*
	 * waypoint
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	$('.content').waypoint(function(e, dir) {
		
		if( dir == 'up') {
			var link = $('a[rel='+ $(this).prev().attr('id') +']');
		}else{
			var link = $('a[rel='+ $(this).attr('id') +']');
		}
		
		if( !pageJump ){
			activateLink( link );
		}
		
	});
	
	var hash = window.location.hash.substring(1);
	activateLink( $('a[rel='+ hash +']') );
	
	/*
	 * navigation & scrolling
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	$('#navigation a').bind('click',function(e){
		pageJump = true;
		
		//scroll
		var scrollto = $( '#'+ $(this).attr('rel') );
		$('body').scrollTo( scrollto,750,{'onAfter':resetPageJump} );
		
		//set active
		activateLink( $(this) );
		
		return false;
	});		
	
	$('#topbar').click(function(e){
		$('body').scrollTo( 0,750 );
	});	
	
	/*
	 * send form
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	$('form').submit(function() {
		
		var form			= $(this);
		var action 			= form.attr('action');
		var serializedForm 	= form.serialize();
		var id	 			= form.attr('id');
		var button			= form.find('input.submit');
		
		if( id == 'form-5' ){
			var thankU = 'Thank you for ordering our publication(s).';
		}else{
			var thankU = 'Thank you for you registration';
		}
		
		if( validateForm( id ) ){
			
			button.attr("disabled", "disabled");
			form.find('.loading').fadeIn();
			
			$.ajax({
				type: "POST",
				url: action,
				data: serializedForm,
				success: function(msg){
				     if( msg == 'true' ){
				    	 button.removeAttr("disabled");
				    	 form.find('.loading').fadeOut();
				    	 resetForm(id);
				    	 form.find('div.errorbox').fadeOut(500, function(){
				    		 form.find('div.okbox').html(thankU).fadeIn();
				    	 });
				     }
				}
			});
		}else{
			
			form.find('div.okbox').fadeOut(500, function(){
				form.find('div.errorbox').html('Please fill out all fields correctly.').fadeIn();
			});
			
		}
		
		return false;
	});	
	
	//remove error class on focus
	$(':input').bind('click focus', function(){
		$(this).parent().removeClass('error');
	});
	
	//select course if clicking on link
	$('.course a.button').bind('click', function(){
		
		var id = $(this).attr('rel');
		$('input[value="'+ id +'"]').attr('checked', true);
		
		$('body').scrollTo( $('div.courseregform'), 500, { offset:{left: 0, top:-50 } });		
		
		return false;
	});
	
	/*
	 * ie6
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	if( $.browser.msie && $.browser.version < 7){
		var html = 'Please update your browser to view this website correctly';
		$('body').append('<div class="message">'+ html +'</div>');
	}
	 
});

function resetForm( formId ){
	$(':input','#'+formId)
		.not(':button, :submit, :reset, :hidden, :checkbox')
		.val('')
		.removeAttr('checked')
		.removeAttr('selected')
		.parent().removeClass('error');
	
	$(':checkbox').attr('checked',false);
}

function validateForm( formId ){
	
	var bool = true;
	
	$(':input','#'+formId).not(':button, :submit, :reset, :hidden').each(function(){
		
		if( $(this).parent().hasClass('required') && $(this).val() == '' ){
			$(this).parent().addClass('error');
			bool = false;
		}
		
	});
	
//		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
//		if(reg.test( $('#contact-email').val() ) == false) {
//			$('#contact-email').addClass('error');
//			bool = false;
//		}
	
	return bool;
}

function protectMail( s ){
	var n = 0;
	var r = "";
	for( var i = 0; i < s.length; i++){
		n = s.charCodeAt( i );
		if( n >= 8364 ){
			 n = 128;
		}
	r += String.fromCharCode( n - 1 );
	}
	return r;
}	

function linkTo_UnCryptMailto( s ){
	location.href=protectMail( s );
}

function resetPageJump(){
	pageJump = false;
}

function debug(v){
	if( console ){
		console.log(v);
	}
}

function activateLink(link){

	if( !link.parent().hasClass('active') ){
		
		//deactivate all
		$('#navigation li').removeClass('active');
		
		//active this
		link.parent().addClass('active');
		
		//set navi image
		$('.navi-image').fadeOut();
		if( link.next().hasClass('navi-image') ){
			link.next().fadeIn();
		}
		
	}
} 		



