$(function(){
	//$('#submit').click(function(){console.log('submit clicked');});
	$('[rel=shadowbox]').attr('rel','shadowbox;width='+$(document).width()+';height='+$(document).height());
	
	$('#portfolioContent').hide();

	// Determine what to load based on URL
	var pageLoc = String(window.location);
	pageLoc = pageLoc.split("#");
	pageLoc[1] == "contact" ? $('#contact').click()
	: pageLoc[1] == "portfolio" ? changeContent($('#portfolio'))
	: pageLoc[1] == "resume" ? $('#resume').click()
	: $('#home').click(); 

	// Featured Projects
	$.featureList(
			$("#featuredList li a"),
			$("#featuredItem li"), {
				start_item: 1,
				braket_loc3: [-3, 91, 186]
			});
	
	
	// Navigation
	var bgPos = [0,-75,-190,-285];
	$('#nav li a')
		.mouseover(function(){
			var bgTraker = $('#nav li a').index(this);

			$(this).stop().animate({backgroundPosition:"(" + bgPos[bgTraker] + "px 0px)"}, {duration:500});
		})
		.mouseout(function(){
			var bgTraker = $('#nav li a').index(this);
			$(this).not('.current').stop().animate({backgroundPosition:"(" + bgPos[bgTraker] + "px -44px)"}, {duration:200});
		})
		.click(function(){
			if (this.id != "contact") {
				//console.log('not contact');
				$('#contactContainer').animate({marginLeft: '-400px'}, 400);
				$('#wrapper').animate({width: '1054px',paddingLeft: '0px'}, 400);
			}else{
				//console.log('contact');
				$('#contactContainer').animate({marginLeft: '0px'}, 400);
				$('#wrapper').animate({width: '1454px',paddingLeft: '450px'}, 400);
				return false;
			}
			var contentContainer = $('#' + $(this).attr('id') + 'Content');
			if ( $(this).attr('id') != 'resume' && $(this).attr('id') != 'contact'){
				if(!$(contentContainer).is(':visible')){
					currentNav($(this));
					$('#bodyContainer').animate({'height': $(contentContainer).height() +'px'}, 500,function(){$(this).css('height','auto');});
					$('.currentContent').stop().removeClass('currentContent').hide();
					$(contentContainer).stop().addClass('currentContent').fadeIn('fast');
					return false;
				}
			};
		});
	
	function changeContent(whichNav){
		var contentContainer = $('#' + $(whichNav).attr('id') + 'Content');
		if ( $(whichNav).attr('id') != 'resume'){
			if(!$(contentContainer).is(':visible')){
				currentNav($(whichNav));
				//console.log('height : '+ $('#portfolioContent').height() +'px');
				$('#bodyContainer').animate({'height': $(contentContainer).height() +'px'}, 500,function(){$(this).css('height','auto');});
				$('.currentContent').stop().removeClass('currentContent').hide();
				$(contentContainer).stop().addClass('currentContent').fadeIn('fast');
				$(contentContainer).fadeIn('fast');
				return false;
			}
		};
	}
	
	function currentNav(navItem){
		var bgTraker = $('#nav li a').index(navItem);
		var z = 0;
		$('#nav li a').addClass("current").not(navItem).removeClass("current").each(function(i){
			if(z == bgTraker){z = bgTraker + 1};
			$(this).animate({backgroundPosition:"(" + bgPos[z] + "px -44px)"}, {duration:200});
			z++
		});
	}
	
	//portfolio
	$('.moreProjInfo').css({'display':'none'});
	var startProjHeight = 234;
	var newProjHeight = 255;
	$('.portfolioProject:nth-child(3n)').addClass('lastInRow');
	var projDetail = '<li class="projectDetails"></li>';
	$('.portfolioProject:nth-child(3n)').after(projDetail);
	$('.selectedProject p').height((204 + $('.selectedProject h2').height())-24);
	
	$('#portfolioContainer li').not('.selected,projectDetails').css('cursor','pointer').hover(function(){
		var $this = $(this);
		var overlayCSS = {
	        'width' : $this.children('img').width(),
	        'height' : $this.children('img').height(),
	        'opacity' : '.3'
	      }
		$('.projectOverlay').css(overlayCSS).fadeTo('normal',.3);
		$this.not('.selected').children('h2').css('background-color' , '#b06113').end().children('img').css('border-color' , '#b06113');
		$this.children('.projectChoices').fadeIn();
	},function(){
		$(this).not('.selected').children('h2').css('background-color','#3a5871').end().children('img').css('border-color' , '#3a5871');
		$(this).children('.projectChoices').fadeOut();
	}).click(function(){
		var whichProject = $(this);
		whichProject.children('.projectChoices').fadeOut();
		displayProject(whichProject);
	});
	function displayProject(whichProject){
		if (!whichProject.hasClass('selected') && !whichProject.hasClass('projectDetails')) {
			$('#portfolioContainer li').not('.projectDetails, ol li').animate({height: startProjHeight +'px', backgroundColor:'#3a5871'}, 200).end().removeClass('selected').children('h2').css('background-color', '#3a5871').end().children('img').css('border-color', '#3a5871');
			whichProject.addClass('selected').animate({height: newProjHeight + 'px', backgroundColor:'#b06113'}, 200,function(){whichProject.nextAll('.projectDetails').eq(0).slideDown();});
			$('.projectDetails').slideUp();
			$('.projectDetails').html(whichProject.find('div.moreProjInfo').html());
			whichProject.children('h2').css('background-color', '#b06113').end().children('img').css('border-color', '#b06113');
		}
	}
	$('a.moreInfo').click(function(){
		displayProject($(this).parent().parent());
		$(this).parent().fadeOut();
		return false;
	});
	
	$('#closeSlide').click(function(){
		$('#contactContainer').animate({marginLeft: '-400px'}, 400);
		$('#wrapper').animate({width: '1054px',paddingLeft: '0px'}, 400);
	});
	$('#contactContainer').hide().fadeIn().animate({height: $(document).height() + 'px'},function(){$('#closeSlide').fadeIn();});
	$('input, textarea').focus(function(){$(this).css('border' , '1px solid #b06113').animate({backgroundColor:'#f2decd'})}).blur(function(){$(this).css('border' , '1px solid #3a5871').animate({backgroundColor:'#fff'});})
	$('#submit').click(function(){/*console.log('clicked submit');*/$(this).css('border' , '1px solid #b06113').animate({backgroundColor:'#f2decd'}).css('border' , '1px solid #3a5871').animate({backgroundColor:'#fff'});})

	//Contact Form Validation
	$("#contactForm").validate({
		rules: {
			name: {required: true},
			email: {required: true, email: true},
			phone: {required: true},
			message: {required: true}
		},
		messages: {
			name: {required: "This field is required"},
			email: {required: "This field is required"},
			phone: {required: "This field is required"},
			message: {required: "This field is required"}
		},
		submitHandler: function(form) {
			var data_string = $("#contactForm").serialize();
			$.ajax({
			    type:       "POST",
			    url:        "lib/php/form_submission.php",
			    data:       data_string,
			    success:    function() {
				$('#contactForm input:not(#submit), #contactForm textarea').val('');
				// Create the thank you box
				var thankYou = $('<div/>', {
					id: 'thankYouContainer',
					css: {
						position: 'absolute',
						left: '5px',
						top: '75px',
						width: '315px',
						height: '175px',
						color: '#fff',
						fontSize: '60px',
						lineHeight: '70px',
						textAlign: 'center',
						padding: '120px 30px',
						background: '#b06113',
						border: '1px solid #fff',
						display: 'none'
					},
					text: 'Thank You!'
				});
				$('#contactForm').after(thankYou);
				$('#thankYouContainer').fadeIn().delay(1000).fadeOut();
				var t=setTimeout("$('#closeSlide').click()",1200);

			    }//end success function


			}); //end ajax call
		 }
	});
});