// JavaScript Setup

// jQuery Init
jQuery.noConflict();
jQuery(function($) { 
	
	
	
	
	// Back to top section links for non-IE browsers
	if ( ! $.browser.msie ) {
		$('.section').children('h3').append("<a href='#jump-top' class='scrollTop hidden' title='Back to Top'>Back to Top</a>");
		$(".section").hover(
			function(){
				$(this).find(".scrollTop").css('display', 'block');
			},
			function(){
				$(this).find(".scrollTop").css('display', 'none');
			}
		);
	}
	
	
	// Panel Toggle in Header
	$("#panel_toggle").click(function(){
	  $("#panel").slideToggle(300, function(){ $("#panel_toggle").toggleClass("panel_active"); });	  
	});
		
	
	
//	// *****************************	
//	// Start - jQuery Fancybox setup


//		// How tos at http://fancybox.net/
//		$('a[href$="jpg"], a[href$="jpeg"], a[href$="png"], a[href$="gif"]').fancybox();
//		
//		// Fancybox - Youtube
//		// <a class="youtube" title="The Social Network" href="http://www.youtube.com/watch?v=lB95KLmpLR4">Try now</a>
//		$(".youtube").click(function() {
//			$.fancybox({
//					'padding'		: 0,
//					'autoScale'		: false,
//					'transitionIn'	: 'none',
//					'transitionOut'	: 'none',
//					'title'			: this.title,
//					'width'			: 680,
//					'height'		: 495,
//					'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
//					'type'			: 'swf',
//					'swf'			: {
//						 'wmode'		: 'transparent',
//						'allowfullscreen'	: 'true'
//					}
//				});
//		
//			return false;
//		});
//		
//		// Fancybox - Vimeo
//		// <a class="vimeo" href="http://vimeo.com/20534171">vimeo test #2</a>
//		$(".vimeo").click(function() {
//			$.fancybox({
//				'padding'		: 0,
//				'autoScale'		: false,
//				'transitionIn'	: 'none',
//				'transitionOut'	: 'none',
//				'title'			: this.title,
//				'width'			: 400,
//				'height'		: 265,
//				'href'			: this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
//				'type'			: 'swf'
//			});
//	
//			return false;
//		});
//		
//		// iFrames
//		// <a class="youtube" title="The Social Network" href="http://www.youtube.com/watch?v=lB95KLmpLR4">Try now</a>
//		$(".iframe").fancybox({
//			'width'				: '75%',
//			'height'			: '75%',
//			'autoScale'     	: false,
//			'transitionIn'		: 'none',
//			'transitionOut'		: 'none',
//			'type'				: 'iframe'
//		});

//		
//	// End - Query Fancybox setup
//	// *****************************	
//	
	
	
	
	// Clear form field on click
	$("form.standard .textbox,form.standard .textarea").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	
	
	
	
	
	
	
	
	
	// Used to scroll to a specific position.
	// Edit the variables below to adjust speed and positioning
	var scrollDuration = 500; // 1000 = 1 second
	var scrollGap = 0; // in Pixels, the gap left above the scroll to point
	$('a[href*=#jump-]').click(function() 
	{
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) 
		{
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) 
			{
				var targetOffset = $target.offset().top - scrollGap;
				$('html,body').animate
					(
					{scrollTop: targetOffset}, 
					scrollDuration
					);
				return false;
			}
		}
	});

	
	
		
	// Softbutton = soft fade on hover, used for social media icons
	$(".softbutton").stop().fadeTo("fast", 0.4);
	$(".softbutton").hover(
		function(){
			$(this).stop().fadeTo("fast", 1);
		},
		function(){
			$(this).stop().fadeTo("fast", 0.4);
		}
	);
	
	
	// Toggle Content!
	$(".hidden").hide();
	$("a.toggle").click(function(event){
		if( $(this).text() == 'Show More' ) {
			$(this).text("Show Less");
		}else{
			$(this).text("Show More");
		}
		$(this).parents(".toggle-container").find(".hidden").slideToggle("normal");
		return false;
	});


	
	
	
	// contact form
	// Stop form from traditionally submitting if JavaScript is enabled
	
	var postUrl = $("#contact_form form").attr("action");
	$("#contact_form form").attr("action", "");

	$("#cf_submit").click(function() {
		var name = $("#cf_name").val();
		var email = $("#cf_email").val();
		var message = $("#cf_message").val();		
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		if(name == '' || name == 'Navn') {
			$("#error-name").fadeIn();
			hasError = true;
		}else{
			$("#error-name").fadeOut();
		}
		
		if(email == '' || email == 'Email') {
			$("#error-email").fadeIn();
			hasError = true;
		}else if(!emailReg.test(email)) {
			$("#error-email").fadeIn();
			hasError = true;
		}else{
			$("#error-email").fadeOut();
		}
		
		if(message == '' || message == 'Besked...') {
			$("#error-message").fadeIn();
			hasError = true;
		}else{
			$("#error-message").fadeOut();
		}
			
		if(hasError == true)
		{
			return false;
		}
				
		var formData = 'name='+ name + '&email=' + email + '&msg=' + message;
		var successPlaceholder = "<div id='message'></div>";
		var successHTML = "<h4>Beskeden er sendt!</h4><p>Jeg vil kontakt dig snart!</p>";

		$.ajax({
			type: "POST",
			url: postUrl,
			data: formData,
			success: function() {
				$('#contact_form').html(successPlaceholder);
				$('#message').html(successHTML);
			}
		}); // end ajax post
		return false;
	});
	// end contact form
	
	
});
