$(document).ready(function(){

	//nav click events
	$('#nav ul li a').not('#projectsDropDown ul li a').hover(function(){
		//$(this).stop().animate({borderRightWidth : '40px'}, 200)
	},function(){
		//$(this).stop().animate({borderRightWidth : '20px'}, 200)
	}).not('#aboutNav').not('#contactNav').click(function(){
		var targ = $(this).attr('href');
		$('#nav ul li a').not(this).not('#projectsDropDown ul li a').removeClass('selected');
		$(this).addClass('selected');
	});//end nav click
	
	$('#projectsNav').click(function(){$('#projectsDropDown').slideDown();});
	
	$('a[href=/#/contact]').click(function(e){ 
		e.preventDefault();
		$(this).toggleClass('selected');
		$('#aboutSection').hide();
		$('#nav ul li a').not(this).not('#projectsDropDown ul li a').removeClass('selected');
		$('#contactForm').slideToggle(200);
		return false;
	});
	$('#aboutNav').click(function(e){ 
		e.preventDefault();
		$(this).toggleClass('selected');
		$('#contactForm').hide();
		$('#nav ul li a').not(this).not('#projectsDropDown ul li a').removeClass('selected');
		$('#aboutSection').slideToggle(200);
		return false;
	});
	
	$('.closeBtn').click(function(e){
		e.preventDefault();
		$(this).parent().slideUp(200);
		if($(this).parent().attr('id') == 'aboutSection'){
			$('#aboutNav').removeClass('selected');
		}
		if($(this).parent().attr('id') == 'contactForm'){
			$('#contactNav').removeClass('selected');
		}
	});
	
	$('#projectsDropDown ul li a').click(function(e){
		$('#projectsDropDown ul li a').not(e.target).removeClass('selected');
		$(this).addClass('selected');
	});
	
	$('body').mousewheel(function(){onScroll()});
	
	
	//load up the images
	window.setTimeout(initImages, 2000);
	
	$.localScroll({duration: 1000});
});
var prevProj = '';
function onScroll(){
	var thisId = '';
	var theName = '';
	var theDiv = $('.projectDiv:in-viewport').each(function(){
		thisId = $(this).attr('id');
		theName = $('a.anchor', this).attr('name');
	});
	
	if(thisId != prevProj){
		var theAnchor = $('a[href=#'+theName+']');
		//console.log('a[href=#'+theName+']');
		if($('#projectsDropDown').css('display') == 'none'){ $('#projectsDropDown').slideDown(); }
		$('#projectsDropDown ul li a').not(theAnchor).removeClass('selected');
		theAnchor.addClass('selected');
	}
}
//image loader function
function initImages(){
	var projWins = $('.projectDiv').find('.projectWindow');
	projWins.each(function(){
		var thisWin = $(this);
		var theImgs = $('img', thisWin);
		theImgs.click(function(){
			var nextIndex = $.inArray(this, theImgs) + 1;
			if (nextIndex >= theImgs.length){ nextIndex = 0}
			$(this).fadeOut(700);
			$(theImgs[nextIndex]).fadeIn(700);
		});
		if(theImgs.length == 0 ){
			thisWin.css({backgroundImage : 'none'}); 
			thisWin.html('<p style="text-align:center; margin-top:250px;">There Are No Images For This Project</p>') 
		}else{ 
			thisWin.css({backgroundImage : 'url(images/browser_head.png)', backgroundPosition : 'top left'});
			$(theImgs[0]).fadeIn();
		}
	});
}

function sendMail(){
	$.ajax({
		url : 'functions/send_mail.php',
		type : 'POST',
		success : function(resp){
			$('#mailForm').html('<p>'+resp+'</p>');
		}
	})
}


