$(document).ready(function(){
		$('ul.work_thumbs li a').lightBox();
		$('#work_thumbs a').click(function(){
			return false;										
		});		
		$('input:not(#submit_btn), textarea').bind('focus', function() {
			oldValue = $(this).val();
			if(oldValue == 'Type Message here...' || oldValue == 'name' || oldValue == 'subject' || oldValue == 'email'){
				$(this).val('');
			}
		});
		$('input:not(#submit_btn), textarea').bind('blur', function() {
			newValue = $(this).val();
			if (newValue == '') {
				$(this).val(oldValue);
			}
		});
		
		$('#worknavi').append('<ul>');
		//Work pagination
		$('.work_thumbs').each(function(workcount) {
			$(this).attr("id", 'worksite_'+workcount);
			$('#worknavi ul').append('<li class="wnavigation" id="wnavi_'+workcount+'"></li>');
		});
		$('.work_thumbs').hide();
		$('#worksite_0').show();
		$('#wnavi_0').addClass('active');
		
		var actual_w_site = 0;
		var clicked_wnavi = "";
		$('.wnavigation').click(function() {
			clicked_wnavi = $(this).attr('id').substr(6);
			change_work_site(clicked_wnavi)
			return false;										
		});	
		
		function change_work_site(target_site){
			if(actual_w_site != target_site){
				$('#wnavi_'+actual_w_site).removeClass('active');
				$('#worksite_'+actual_w_site).hide();
				$('#wnavi_'+target_site).addClass('active');
				$('#worksite_'+target_site).show();
				actual_w_site = target_site
			}
		}
							   
		//This function closes all contents but shows the activeone					   
		function opensite(siteID){
			//The if-statement prevents for closing a section when a normal link is clicked
			if(siteID == 'about' || siteID == 'social' || siteID == 'work' || siteID == 'contact'){
				$('.content_inside').css({ "display":"none"});
				$('#'+siteID).css({ "display":"inline-block"});
				$('#navigation ul li a[href$="#'+siteID+'"]').addClass('active');
			};
		};
		
		//This checks if an anchorlink exists					   
		var siteanchor = document.location.toString();	
		if (siteanchor.match('#')) {
			//This grabs the anchorlink
			var activeanchor = siteanchor.split('#')[1];
			
			switch(activeanchor){ 
				case 'about': $('#content').animate({top: "190px"},500); opensite(activeanchor); break;
				case 'social': $('#content').animate({top: "190px"},500); opensite(activeanchor); break;
				case 'work': $('#content').animate({top: "190px"},500); opensite(activeanchor); break;
				case 'contact': $('#content').animate({top: "190px"},500); opensite(activeanchor); break;
				default : $('#logo').animate({top: "190px"},500); opensite('about'); break;
			};
		
		} else {
			//If there isn't a anchorlink show the logo and the first contenttab(about)
			$('#logo').animate({top: "190px"},500);
			opensite('about')
		};
		$('.opensite').click(function(){
			$('#navigation ul li a').removeClass('active');
			var siteurl = $(this).attr('href');
			var siteurl = siteurl.split('#')[1];
			opensite(siteurl);
		});
		$('#navigation ul li a').click(function(){
			if( $(this).is(".active") ){
				return false;
			}else{
				//This gets the clicked anchorlink and removes the #
				$('#navigation ul li a').removeClass('active');
				$(this).addClass('active');
				var clicked_anchor = $(this).attr('href').substr(1);
				opensite(clicked_anchor);
				return false;
			};
		});
		
		//On startup fade the closebutton to .3 opacity
		$('.close_btn').animate({opacity:0.3},1);
		
		//Logo animation
		$('#logo a').click(function(){
			$('#navigation ul li a').removeClass('active');
			$('#navigation ul li a:first').addClass('active');
			opensite('about')
			//$('#logo').animate({top: "526px"},500,function(){$('#content').animate({top: "190px"},500);});
			$('#logo').css({"z-index":1}).animate({top: "526px"},500);
			$('#content').css({"z-index":2}).animate({top: "190px"},500);
			return false;
		});
		
		//Close Button
		//On click hide the contentarea and show the logo
		$('.close_btn a').click(function(){
			//$('#content').animate({top: "526px"},500,function(){$('#logo').animate({top: "190px"},500);});
			$('#content').css({"z-index":1}).animate({top: "526px"},500);
			$('#logo').css({"z-index":2}).animate({top: "190px"},500);
			return false;		
		});
		
		//On hover fade the button in
		$('.close_btn').hover(function(){
			$(this).stop().animate({opacity:1},250);
		},function(){
			$(this).stop().animate({opacity:0.3},250);
		});
		
		$('ul#social_links li a, #about a').click(function(){
			var social_url = $(this).attr('href');									 
			window.location = social_url;
		});
		
		//Contactform functions
		var active = 0;
		function show_message(themessage){
			if(active == 0){
				active =1;
				$('#contact').prepend('<div id="message">'+themessage+'</div>');
				$('#contactform').fadeTo(0.5,250);
				$('#message').animate({'opacity':0},1,function(){$('#message').css({'display':'block'}).animate({'opacity':1},250).animate({'opacity':1},1500).animate({'opacity':0},250,function(){$('#message').remove(); active = 0;});});
			};
		};
		
		$('#submit_btn').click(function(){
			var c_message = $('#contact_message').val();
			var c_name = $('#contact_name').val();
			var c_subject = $('#contact_subject').val();
			var c_email = $('#contact_email').val();
			
			var error_message = '';
			var error_name = '';
			var error_subject = '';
			var error_email = '';
			
			if(c_message == "" || c_message == "Type Message here..." || c_message.length < 5){
				error_message = 'error';
				show_message('Please enter a message');
			}else{
				error_message = '';
				$('#contact_message').animate({'opacity':0.3});
			};
			
			if(c_name == "" || c_name == "name" || c_name.length < 3){
				error_name = 'error';
				show_message('Please enter your name');
			}else{
				error_name = '';
				$('#contact_name').animate({'opacity':0.3});
			};
			
			if(c_subject == "" || c_subject == "subject" || c_subject.length < 3){
				error_subject = 'error';
				show_message('Please enter a subject');
			}else{
				error_subject = '';
				$('#contact_subject').animate({'opacity':0.3});
			};
			
			if(c_email == "" || c_email == "email" || c_email.length < 3){
				error_email = 'error';
				show_message('Please enter your email');
			}else{
				var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
				if (!filter.test(c_email)) {
					error_email = 'error';
					show_message('Please enter a valid email');
				}else{
					error_email = '';
					$('#contact_email').animate({'opacity':0.3});
				}
			};
				
				if(error_message == '' && error_name == '' && error_subject == '' && error_email == ''){
					//Make a string with all informations
					var c_data = 'submit=1&name=' + c_name + '&subject=' + c_subject + '&email=' + c_email + '&message=' + c_message;
					//Send the datastring to the phpfile which sends it
					$.ajax({
					   type: "POST",
					   url: "sendmail.php",
					   data: c_data,
					   success: function(){
						   show_message('Your email was send successfully!');
						   $('#submit_btn').fadeOut('fast');
					   }
					});
				}else{
					
				};
				
			return false;
		});		
});
