$(document).ready(function() {
	var slideLock = 0;
	var autoslide = 1;
	
	var offsets = {
		main: 0,
		home: 0,
		about: 800,
		contact: 1630
	};

	// resize blocks
	var windowHeight = $(window).height();
	if (windowHeight > 1080)
		windowHeight = 1080;

	$('.section.main, .section.about, .section.contact').each(function(){
		var $section = $(this);
		if ($section.attr('min') * 1 < windowHeight) {
			$section.css('height', windowHeight + 'px');
			if ($section.hasClass('about'))
				offsets.about = windowHeight + 16;
			if ($section.hasClass('contact'))
				offsets.contact = 2*windowHeight + 16;
		}
	});

	

	$('.projects .next').click(function() {
		autoslide = 0;
		slideProject();
		return false;
	});

	function slideProject() {
		if (!slideLock) {
			slideLock = 1;
			var $activeProject = $('.project.active');
			$activeProject.fadeOut(500, function() {
				$activeProject
						.addClass('hidden')
						.removeClass('active');
				var $nextProject = $activeProject.next('.project.hidden');
				if ($nextProject.length == 0)
					$nextProject = $('.projects .project:first');

				$nextProject
						.fadeIn(500)
						.addClass('active')
						.removeClass('hidden');

				slideLock = 0;
			});
		}
	}

	function autoslideProjects() {
		if (autoslide) {
			setTimeout(function(){
				if (autoslide) {
					slideProject();
					autoslideProjects();
				}
			}, 5000);
		}
	}

	autoslideProjects();

	$('.section.contact .form').submit(function(e) {
		var $this = $(this);
		var isValid = true;
		var params = {};
		$this.find('input[type="text"], textarea').each(function() {
			var $element = $(this);
			var value = $.trim($element.val());

			if (value.length == 0 || value == $element.attr('default')) {
				$element.addClass('error');
				isValid = false;
			} else {
				$element.removeClass('error');
				params[$element.attr('name')] = value;
			}
		});

		if (isValid) {
			$('.section.contact .form').hide();
			$('.section.contact .thnx').show();
			
			$.post('ajax_contact.php', params, function(response) {
				if (response.success) {
//					$('.section.contact .form').hide();
//					$('.section.contact .thnx').show();
				} else {
					//console.log('ERROR');
				}
			}, 'json');
		}

		return false;
	});

	$('.section.contact .send_more').click(function() {
		$('.section.contact .thnx').hide();
		$('.section.contact .form').show();

		$('textarea[default]').each(function() {
			var $this = $(this);
			$this.val($this.attr('default'));
		});

		return false;

	});


	$('input[default],textarea[default]')
			.focus(function() {
		var $this = $(this);
		if ($.trim($this.val()) == $this.attr('default'))
			$this.val('');
	})
			.blur(function() {
		var $this = $(this);
		if ($.trim($this.val()).length == 0)
			$this.val($this.attr('default'));
	});

	$('a[href*=#]').each(function() {
		
		if (this.hash) {
			var section = this.hash.slice(1);
//			if (section != 'home') {
//				var $targetAnchor = $('.section.' + section);
//			} else {
//				var $targetAnchor = $('body');
//			}
			

//			var $target = $targetAnchor.length ? $targetAnchor : false;
//			if ($target) {
			if (offsets[section] !== undefined) {
//				var targetOffset = $target.offset().top;
				var targetOffset = offsets[section];
//				console.log(section + ':' + targetOffset);
				//console.log(targetOffset);
				$(this).click(function() {
					$('html, body').animate({scrollTop: targetOffset}, 600);
					return false;
				});
			}
		}
	});

	var isMenuHover = 0;
	$('.menu a')
		.mouseover(function(){
			isMenuHover = 1;
			var $this = $(this);
			$this.parents('.menu').find('a').removeClass('selected');
		})
		.mouseout(function(){
			isMenuHover = 0;
			var $this = $(this);
			var $menu = $this.parents('.menu');
			var preSelect = $menu.attr('preselect');
			if (preSelect) {
				setTimeout(function(){
					if (!isMenuHover)
						$menu.find('a.' + preSelect).addClass('selected');
				}, 200);
				
			}
		});
});
