$(document).ready(function(){

	// Add smooth scrolling to each of the nav links
	scroll_options = {
		queue:true,
		duration:1500,
		axis:'y',
		offset:0,
		easing:'easeOutQuad'
	};
	$('nav ul, nav, #site-information > ul, #document > header > .content > ul').localScroll(scroll_options);
	
	$('nav a, #document > header li a').click(function(){
		$('nav li.current').removeClass('current');
		loc = $(this).attr('href');
		$(loc).find('a[href="' + loc + '"]').parent('li').addClass('current');
	});
	
	// Our People selector
	$('#staff_list').find('a').click(function(){
		
		// If we're clicking the same one
		if( $(this).parent('li').hasClass('current'))
		{
			return false;
		}
		
		// End all animations
		$('#our_people figure').stop(true,true);
		
		// Remove current highlight
		$('#staff_list .current').removeClass('current');
		
		var target = $(this).attr('href');
		var current = $('#our_people article.current');
		
		// hide the current content and send the image to the back
		$(current).find('header,section').hide();
		$(current).find('figure').css('z-index','1');
		
		// Show the new content
		$(target).find('header,section').show();
		
		// Show the new content, bring the image to the front and fade in the image
		$(target)
			.addClass('current')
			.find('figure')
			.hide()
			.css('z-index', '2')
			.fadeIn('slow', function(){ 
				$(current).removeClass('current'); 
			});
			
		// Highlight the current thumb		
		$(this).parent('li').addClass('current');
		
		return false;
	});

 	
	// Form Labels
	$('#contact form, #site-information').find('.text, .textarea').each(function(){
	
		var control = $(this);
		var label = $(this).find('label');
		
		label.click(function(){
			control.find('input,textarea').focus();
		});
		
		$(this).find('input, textarea').each(function(){
		
			if($(this).val() != "")
			{
				label.hide();
			}
			
			$(this)
				.focus(function(){
					label.fadeTo(0,0.2);
				})

				.blur(function(){
					if($(this).val() == "")
					{
						label.fadeTo(0,1);
						label.show();
					}
				})

				.keyup(function(){
					if($(this).val() != "")
					{
						label.hide();
					}
				});
		});
	
	});
	
	// Send form with AJAX after validating
	$('#contact form').validate({
	
		submitHandler: function(form) 
		{
			form.submit();
		//	$('#contact form .form_message').hide();
		//	$(form).ajaxSubmit({
		//		dataType:'json',
		//		beforeSubmit: function(){
		//		$('.loader').fadeTo(0,0).show().fadeTo(1000,1);
		//		},
		//			success: function(msg){
		//				if(msg.status == "3")
		//				{
		//					$('.loader').hide().text('참가 신청이 완료되었습니다.').addClass('success').show().fadeTo(3000,1).fadeTo(1000,0);
		//				}
		//				else
		//				{
		//					$('.loader').hide().text('오류가 있습니다.').addClass('error').show().fadeTo(3000,1).fadeOut(1000,0);
		//			}
		//	}
		//	});
		},

		errorPlacement: function(error, element) 
		{
			if(element.attr('name') == 'email' && $("#contact form .error").length == 1)
			{
				$("#contact form .form_message").text('이메일 주소를 정확히 입력해 주십시오.');
			}
			else
			{
				$("#contact form .form_message").text('항목을 모두 채워 주십시오.');
			}

			$("#contact form .form_message").show();
		}

	});
	
});

function checkEmail(email)
{	
	var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var emailVal = $("#" + email).val();
	return pattern.test(emailVal);
}

function popUp(winURL) { 
     window.open(winURL,"popup","width=330,height=540"); 
} 

window.onload = prepareLinks; 

function prepareLinks() { 
   var links = document.getElementsByTagName("a");
   for (var i=0; i<links.length; i++) {
       if (links[i].className == "popup") { 
       links[i].onclick = function() {
          popUp(this.getAttribute("href"));
          return false;
          }
       }
    }
}

