//nav init
var $nLeft1 = parseInt($('div.first span').css('left'));
var $nLeft2 = parseInt($('div.second').css('left'));
var $nCurrentActive;
var scrolling = 1;
var $nav = $('nav li');
$nav.addClass('off');

$('div.first span').css('left', $nLeft1+46);
$('div.second').css('left', 60).css('display', 'none');

$(document).ready(function() {
	$('nav li.active').removeClass('off');
	$nCurPos = $('nav li.active').attr('id');
	//alert($nCurPos);
	$('nav li.active div.first span').stop().animate({'left':'-=46'}, 200);
	$('nav li.active div.second').show().stop().animate({'left':'+=175', 'opacity':'1'}, 200);

});

//sections height setup
function setMinHeights() {
	$sMinHeight = $(window).height();
	$('section').css({'min-height':$sMinHeight});
	} setMinHeights();

$(window).resize(function() {
	setMinHeights();
});

//nav setup @ mouseevents
$nav.bind('mouseenter', function() {
	$nCurPos = $(this).attr('id').substr(4);
	nIsHovered = setTimeout("onMenu($nCurPos)", 100);
});

$nav.bind('mouseleave', function() {
	clearTimeout(nIsHovered);
	$nCurPos = $(this).attr('id').substr(4);
	offMenu($nCurPos);
});

$nav.bind('click', function(ev) {
	if ($nCurrentActive !== $(this).attr('id').substr(4)) {
		offActiveMenu($nCurrentActive);
	}
	$nav.removeClass('active on').addClass('off');
	$(this).addClass('active').removeClass('off');
	$nCurrentActive = $(this).attr('id').substr(4);
});

$('#nav-logo').bind('click', function() {
	offActiveMenu($nCurrentActive);
});

function onMenu($nCurPos) {
	if ($('#nav-'+$nCurPos).hasClass('off') && !($('#nav-'+$nCurPos).hasClass('active'))) {
		$('#nav-'+$nCurPos).removeClass('off').addClass('on');
		$('div.first span', '#nav-'+$nCurPos).stop().animate({'left':'-=46'}, 200);
		$('div.second', '#nav-'+$nCurPos).show().stop().animate({'left':'+=175', 'opacity':'1'}, 200);
	}
};

function onScrollMenu($nCurPos) {
	if ($('#nav-'+$nCurPos).hasClass('off')) {
		$('#nav-'+$nCurPos).removeClass('off').addClass('on');
		$('div.first span', '#nav-'+$nCurPos).stop().animate({'left':'-=46'}, 200, function() {$(this).css({'left':$nLeft1})});
		$('div.second', '#nav-'+$nCurPos).show().stop().animate({'left':'+=175', 'opacity':'1'}, 200, function() {$(this).css({'left':$nLeft2})});
	}
};

function offActiveMenu($nCurPos) {
	$('#nav-'+$nCurPos).removeClass('active').addClass('off')
	$('div.second', '#nav-'+$nCurPos).stop().animate({'left':'60', 'opacity':'0'}, 300, function() {$(this).hide()});
	$('div.first span', '#nav-'+$nCurPos).stop().animate({'left':$nLeft1+46}, 300);
}

function offMenu($nCurPos) {
	if (!$('#nav-'+$nCurPos).hasClass('active')) {
		$('#nav-'+$nCurPos).removeClass('on').addClass('off');
		$('div.second', '#nav-'+$nCurPos).stop().animate({'left':'60', 'opacity':'0'}, 300, function() {$(this).hide()});
		$('div.first span', '#nav-'+$nCurPos).stop().animate({'left':$nLeft1+46}, 300);
	}
}

//nav setup @ window scroll
$(window).scroll(function() {
	$inview = $('section:in-viewport header').parent().attr('id');
	if ($('a[hash=#' + $inview + ']') !== null) {
		$link = $('a[hash=#' + $inview + ']').parent().attr('id').substr(4);
	}
	
	if ($link != $nCurrentActive && scrolling == 1) {
		$nav.removeClass('active');
		offMenu($nCurrentActive);
		$nCurrentActive = $link;
		$('#nav-'+$nCurrentActive).addClass('active');
		onScrollMenu($nCurrentActive);
	}
});


//window scroll setup
$.localScroll.hash({
	target: '#content',
	queue:true,
	duration:1500
});

$('nav').localScroll({
	hash: true,
	duration: 400,
	easing: 'easeOutExpo',
	onBefore: function() {scrolling = 0; return scrolling},
	onAfter: function() {scrolling = 1; return scrolling}
});


//portfolio
var $wContainerWidth = $('div.work-entries').width();
var $wContainerHeight = $('div.work-entries').height();
var $wEntryWidth = $('article.entry').width();
var $wInfoHeight = $('div.info').height();
var $eHoriz;
var $entriesInfo = $('div.info', 'div.work-entries');

$('div.info').append('<span class="arrow"></span>');

$('article.entry').bind('mouseenter', function(){
	$wSpaceRight = $wContainerWidth - $(this).position().left - $wEntryWidth;
	$wSpaceBottom = $wContainerHeight - $(this).position().top
	$(this).css({'z-index':'60'});
	if ($wSpaceRight <= $wEntryWidth) {
		$eHoriz = 'l';
	} else {
		$eHoriz = 'r';
	};
	
	if ($wSpaceBottom < $wInfoHeight) {
		$eVert = 'b';
	} else {
		$eVert = 't';
	}
	$('div.info',this).addClass($eHoriz + '' + $eVert + ' active');
	$('span.arrow',this).addClass($eHoriz + '' + $eVert);
	
	function wShow() {
		$('div.info.active', '.work-entries').fadeIn(300);
	};
	
	$showInfo = setTimeout(wShow, 500);
	$('img.on-top',this).animate({'opacity':0}, 400);
});

$('article.entry').bind('mouseleave', function(){
	clearTimeout($showInfo);
	$entriesInfo.removeClass('active');
	$(this).css({'z-index':'40'});
	$('img.on-top',this).animate({'opacity':1}, 400);
	$(this).find('div.info').fadeOut(200);
});

$('#work-wrapper').scrollable({
	'circular': true,
	'speed': 400,
	'easing': 'easeOutExpo'
});


//form feedback setup
$('p.error').hide();
$('p#sent').hide();
$('#loader').hide();

$('#button-submit').live('click', function(event) {
	var $email = $('input#f-email').val();
	var $subject = $('input#f-subject').val();
	var $content = $('textarea#f-content').val();
	
	$('p.error').hide();
	$('p#sent').hide();
	
	function validEmail(email) {
		var pattern= new RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/);
		return pattern.test(email);
	}
	
	if (!validEmail($email)) {
		$('#error-email').show();
		event.preventDefault();
		return false;
	}
	
	if ($content == '' || $content == 'content') {
		$('#error-content').show();
		event.preventDefault();
		return false;
	}
	
	var dataString = 'email=' + $email + '&subject=' + $subject + '&content=' + $content;
	$('#loader').show();
	$.ajax({
		type: "POST",
		url: "plikprzetwarzajacyformularz.php",
		data: dataString,
		success: function() {
			$('#loader').fadeOut(100);
			$('#sent').show();
		},
		error: function(xhr, ajaxOptions, thrownError) {
			$('#loader').fadeOut(100);
			$('#sendingerror').show();
		}
	});
	return false;
});
