$(document).ready(function() {
	//Ease the scrolling
	jQuery(function( $ ){
		$.easing.elasout = function(x, t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.5;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return a*Math.pow(2,-12*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
		};
	
		$('ul.links').click(function(e){
			e.preventDefault();
			var link = e.target;
			link.blur();
			if( link.title )
				$(this).parent().find('span.message').text(link.title);
		});
		
		$('div.pane').scrollTo( 0 );
		$.scrollTo( 0 );
		
		$('a').click(function(){
			 jQuery('html,body').queue([]).stop(); 
			$.scrollTo( this.hash, 1000, { easing:'elasout' });
			$(this.hash).find('span.message').text( this.title );
			return false;
		});
	});
	
	//Fade topmenu links
	$(function() {
		$("#header_menu h2").css("opacity","0.8");
		$("#header_menu h2").hover(function () {
		$(this).stop().animate({
		opacity: 1.0
		}, "slow");
		},
		
		function () {
		$(this).stop().animate({
		opacity: 0.8
		}, "slow");
		});
	});
	//Fade ul links
	$(function() {
		$("#collaborations ul li").css("opacity","0.7");
		$("#collaborations ul li").hover(function () {
		$(this).stop().animate({
		opacity: 1.0
		}, "slow");
		},
		
		function () {
		$(this).stop().animate({
		opacity: 0.7
		}, "slow");
		});
	});
	
	//Collaborations navigation
	$("#nav1").click(function(event){
		$('#text_content').hide();
		$('#text_content').load('subpages/1.html', function() {
 			 $('#text_content').fadeIn('slow');
		});	
	});
	$("#nav2").click(function(event){
		$('#text_content').hide();
		$('#text_content').load('subpages/2.html', function() {
 			 $('#text_content').fadeIn('slow');
		});	
	});
	$("#nav3").click(function(event){
		$('#text_content').hide();
		$('#text_content').load('subpages/3.html', function() {
 			 $('#text_content').fadeIn('slow');
		});	
	});
	$("#nav4").click(function(event){
		$('#text_content').hide();
		$('#text_content').load('subpages/4.html', function() {
 			 $('#text_content').fadeIn('slow');
		});	
	});
	$("#nav5").click(function(event){
		$('#text_content').hide();
		$('#text_content').load('subpages/5.html', function() {
 			 $('#text_content').fadeIn('slow');
		});	
	});
	$("#nav6").click(function(event){
		$('#text_content').hide();
		$('#text_content').load('subpages/6.html', function() {
 			 $('#text_content').fadeIn('slow');
		});	
	});
	$("#nav7").click(function(event){
		$('#text_content').hide();
		$('#text_content').load('subpages/7.html', function() {
 			 $('#text_content').fadeIn('slow');
		});	
	});
	
	//Form validation
	$("div.success").hide();
	$("div.valid").hide();
	$("div.invalid").hide();
	var illegalName = /[\!#¤%\\*¨<>]/;
	var illegalNumber = /[^0-9\-+()\s]/;
	
	  $("#formName").bind("change", function() {
		  	if (($(this).val() == "Namn / Företag") || ($(this).val() == "") || (illegalName.test($(this).val()))){
	 		var nameIsOk = 1;
			$("#valid_name").hide()
			$("#invalid_name").fadeIn('fast')
			   } else {
			var nameIsOk = 0;
			$("#invalid_name").hide();
	   		$("#valid_name").fadeIn('fast')
			};
	 });
	 
	 $("#formPhone").bind("change", function() {
		   	if (($(this).val() == "Telefon (inkl riktnummer)") || ($(this).val() == "") || ($(this).val().length < 8) || (illegalNumber.test($(this).val()))){
	 		var phoneIsOk = 1;
			$("#valid_phone").hide()
			$("#invalid_phone").fadeIn('fast')
			   } else {
			var phoneIsOk = 0;
			$("#invalid_phone").hide()
	   		$("#valid_phone").fadeIn('fast')
			};
	 });
	 
	 $("#formEmail").bind("change", function() {
		   	if (($(this).val() == "E-post") || ($(this).val() == "") || ($(this).val().indexOf("@") == -1) || ($(this).val().indexOf(".") == -1) || (illegalName.test($(this).val()))){
	 		var emailIsOk = 1;
			$("#valid_email").hide()
			$("#invalid_email").fadeIn('fast')
			   } else {
			var emailIsOk = 0;
			$("#invalid_email").hide()
	   		$("#valid_email").fadeIn('fast')
			};
	 });
	 

	
	//Send form
	$("form#mailform").submit(function() {
		var name = $('#formName').attr('value');
		var phone = $('#formPhone').attr('value');
		var mail  = $('#formEmail').attr('value');
		var message  = $('#formMessage').attr('value');
		$.ajax({
			type: "POST",  
			url: "contact.php",  
			data: "name="+ name +"& phone="+ phone +"& mail="+ mail +"& message="+ message,  
			success: function(data){
			$('div.success').html(data);
			$('form#mailform').hide(function(){$('div.success').fadeIn();});
				 $('button#goback').click(function(event) {
		$('div.success').hide();
		$('form#mailform').show();  
	});
			}
		});  
		return false;
	});
});