$(function() {
		   
	var $paneTarget = $("body");
	
	var $about_status = false;
	
	$('a#close_about_txt').click(function(){
		if ($about_status) {
			$('#about_txt').slideUp("slow");
			$('a#close_about_txt').html("Do you want to know me better?");
			$paneTarget.stop().scrollTo(0, 800, {offset: -50});
			$about_status = false;
		} else {
			$paneTarget.stop().scrollTo(0, 800, {offset: -50});
			$('#about_txt').slideDown("slow");
			$('a#close_about_txt').html("Slide and close");
			$about_status = true;
		}
		return false;
	});
	
	$('.about_btn').click(function(){
		$paneTarget.stop().scrollTo(0, 800, {offset: -50});
		$('#about_txt').slideDown("slow");
			$('a#close_about_txt').html("Slide and close");
			$about_status = true;
		return false;
	});
	
	$('.homelink').click(function(){
		//$paneTarget.stop().scrollTo('#introduction', 800);
		$paneTarget.stop().scrollTo(0, 800);
		return false;
	});
	
	$('.gallery_btn').click(function(){
		yOffset = (getWindowHeight() - document.getElementById("gallery").clientHeight) / 2;
		if (yOffset < 0)
			yOffset = 0;
									 
		$paneTarget.stop().scrollTo('#gallery', 800, {offset: -yOffset});
		return false;
	});
	
	$('.services_btn').click(function(){
		yOffset = (getWindowHeight() - document.getElementById("services").clientHeight) / 2;
		if (yOffset < 0)
			yOffset = 0;
		
		$paneTarget.stop().scrollTo('#services', 800, {offset: -yOffset});
		return false;
	});
	
	$('.contact_btn').click(function(){
		yOffset = (getWindowHeight() - document.getElementById("footer").clientHeight) / 2;
		if (yOffset < 0)
			yOffset = 0;
		
		$paneTarget.stop().scrollTo('#footer', 800, {offset: -yOffset});
		return false;
	});
	
	$('#services_list li a:eq(0)').addClass('selected');
	$('#services_content').scrollTo(0, 0);
	$('#services_list li a').click(function(){
		$('#services_list li a.selected').removeClass('selected');
		$(this).addClass('selected');
		
		var $paneTarget = $('#services_content');
		var $currentIndex = $(this).parent().prevAll().length;
		var $target = $paneTarget.find('div.service_item:eq('+$currentIndex+')');
		$paneTarget.stop().scrollTo($target, 800);
		return false;
	});
	
	$('#about_list li a:eq(0)').addClass('selected');
	$('#about_content').scrollTo(0, 0);
	$('#about_list li a').click(function(){
		$('#about_list li a.selected').removeClass('selected');
		$(this).addClass('selected');
		
		var $paneTarget = $('#about_content');
		var $currentIndex = $(this).parent().prevAll().length;
		var $target = $paneTarget.find('div.about_item:eq('+$currentIndex+')');
		$paneTarget.stop().scrollTo($target, 800);
		return false;
	});
	
	/*var $port_index = 0;
	var $grid_val = 9;
	$('#p_next').click(function(){
		var $paneTarget = $('#p_grid');
		$port_index = $port_index + $grid_val;
		if ($port_index > $('div.p_thumbnail').length) {
			$port_index = 0;
		}
		var $target = $paneTarget.find('div.p_thumbnail:eq('+$port_index+')');
		$paneTarget.stop().scrollTo($target, 800);
		return false;
	});*/
	
	$("a.fancy").fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 0,
		'overlayOpacity' : 0.6,
		'padding' : 3
	});
	
	
	$(".p_thumbnail").hover(function(){
		//$(this).animate({"bottom": "0px"});
		$(this).find('.p_title_upper').stop().animate({"top": "0px"}, 500);
		$(this).find('.p_title_bottom').stop().animate({"bottom": "-100px"}, 500);		
    },
	function(){
		$(this).find('.p_title_upper').stop().animate({"top": "-100px"}, 400);
		$(this).find('.p_title_bottom').stop().animate({"bottom": "0px"}, 400);
    });
	
	
	
	/*** Form ***/
	var nameDef = $("#fromName").val();
	var messageDef = $("#message").val();
	var emailDef = $("#emailTo").val();
	//
	$('#fromName').each(function () {
		if ($(this).val() == '') {
			$(this).val(nameDef);
		}
	}).focus(function () {
		if ($(this).val() == nameDef) {
			$(this).val('');
		}
	}).blur(function () {
		if ($(this).val() == '') {
			$(this).val(nameDef);
		}
	});
	
	$('#emailTo').each(function () {
		if ($(this).val() == '') {
			$(this).val(emailDef);
		}
	}).focus(function () {
		if ($(this).val() == emailDef) {
			$(this).val('');
		}
	}).blur(function () {
		if ($(this).val() == '') {
			$(this).val(emailDef);
		}
	});
	
	$('#message').each(function () {
		if ($(this).val() == '') {
			$(this).val(messageDef);
		}
	}).focus(function () {
		if ($(this).val() == messageDef) {
			$(this).val('');
		}
	}).blur(function () {
		if ($(this).val() == '') {
			$(this).val(messageDef);
		}
	});
	//
	$("#emailmebtn").click(function(){
		$("#form_status").hide("fast");
		$(".error").removeClass("error");
		$("#form_status").html('');
		$("#javacheck").val(Math.random());
		var javacheckVal = $("#javacheck").val();
		
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var emailToVal = $("#emailTo").val();
		if (emailToVal == '') {
			$("#emailTo").addClass("error");
			hasError = true;
		} else if (!emailReg.test(emailToVal)) {	
			$("#emailTo").addClass("error");
			hasError = true;
		}
		
		var fromVal = $("#fromName").val();
		if (fromVal == '' || fromVal == nameDef ) {
			$("#fromName").addClass("error");
			hasError = true;
		}
		
		var messageVal = $("#message").val();
		if(messageVal == '' || messageVal == messageDef ) {
			$("#message").addClass("error");
			hasError = true;
		}
		
		if (hasError == false) {
			$.post(mailpath,
   				{ fromName: fromVal, emailTo: emailToVal, message: messageVal, javacheck: javacheckVal },
   					function (data){
						$("#emailme").slideUp("normal", function() {				   
							$("#form_status").html('<strong>Success!</strong> Your email has been sent.').show("slow");
							window.setTimeout(function() {
								$("#form_status").hide("slow");
								$("#emailme").slideDown("normal");
							}, 5000);
						});
   					}
				 );
		} else {
			$("#form_status").html('Please fill in all required fields').show("slow");
		}
		
		return false;
	});
});

function getWindowHeight() {
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	  }
	  return myHeight;
}
