$(document).ready(function(){
	
	function constVars(){
		
		winTop = $(window).scrollTop();
		winLeft = $(window).scrollLeft();
		winHeight = $(window).height();
		winBtm = winTop + winHeight;
		winWidth = $(window).width();
		winRight = winLeft + winWidth;
		
		winMiddle = winLeft + (winWidth / 2);
	};
	
	var currWinScrollTop;
	
	setInterval(constVars,1);
	
	
	// TITLE CLICK
	
	$(".site-title a").bind("click", preventDefault);
	

	
	
	///// ISOTOPE, NAV
	
	$('.content-wrap').isotope({
		masonry: {
			columnWidth: 20
		}
	});
	
	
	function preventDefault(e) {
    	e.preventDefault();
	}
	
	$(".isoFilter a").bind("click", preventDefault);
	
	//Save last filter clicked
	
	var currFilter;
	
	$('.isoFilter a').click(function(){
		
		$('.'+currFilter+' a').css('text-decoration', 'none');//if underlined, remove
		
		currFilter = $(this).parent().attr("class");//set current active filter
		
		$('.'+currFilter+' a').css('text-decoration', 'underline');//underline
		
		$('.site-title a, .post-info a').css('text-decoration', 'none');//make sure site-title never gets underlined, etc.
	
	});
		
		
	//All
	
	$(".isoFilter .all a").click(function(){
		$('.content-wrap').isotope({ filter: '.title, .post' });
	});
	
	
	//Digital
	
	$(".isoFilter .digital a").click(function(){
		$('.content-wrap').isotope({ filter: '.title, .digital' });
	});	
	
	$(".isoFilter .infographics a").click(function(){
		$('.content-wrap').isotope({ filter: '.title, .infographics' });
	});	
	
	$(".isoFilter .web a").click(function(){
		$('.content-wrap').isotope({ filter: '.title, .web' });
	});	
	
	
	//Identity
	
	$(".isoFilter .identity a").click(function(){
		
		$('.content-wrap').isotope({ filter: '.title, .identity' });
	});
	
	//Identity
	
	$(".isoFilter .logo a").click(function(){
		
		$('.content-wrap').isotope({ filter: '.title, .logo' });
	});
	
	
	//Art
	
	$(".isoFilter .art a").click(function(){
		$('.content-wrap').isotope({ filter: '.title, .art' });
	});
	
	
	//Me
	
	$(".isoFilter .me a").click(function(){
		$('.content-wrap').isotope({ filter: '.title, .me' });
	});
	
	
	//Contact form
	
	$(".contact a").bind("click", preventDefault);
	
	$('#contact_form').fadeTo(0,0);
	
	$('#contact_form').height(1);
	
	$('#contact_form').hover(
		function () {
		
		}, 
  		function () {
			closeContactFormDiv();
		}
	);
	
	function openContactFormDiv(){
		
		$('#contact_form').show().animate({
  					opacity: 1,
					height: 250
					},{
					duration: 500,
					easing: 'easeInOutQuad',
					queue: false,
					complete : function (){
							
					}
			});
		
			$('.title').animate({
  					height: 460
					},{
					duration: 500,
					easing: 'easeInOutQuad',
					queue: false,
					complete : function (){
							
					}
			});
	
	}
	
	var titleOrigHeight = $('.title').height();
	
	function closeContactFormDiv(){
		
		$('#contact_form').show().animate({
  					opacity: 1,
					height: 1
					},{
					duration: 500,
					easing: 'easeInOutQuad',
					queue: false,
					complete : function (){
							
					}
			});
		
			$('.title').animate({
  					height: titleOrigHeight
					},{
					duration: 500,
					easing: 'easeInOutQuad',
					queue: false,
					complete : function (){
							
					}
			});
	
	}
	
	$(".contact a").click(function(){
		
		openContactFormDiv();
			
	});
	
	
	///// READ MORE FUNCTIONALITY
	
	$('div.expandable').expander({
    	slicePoint:       160,
    	expandPrefix:     '&hellip;',
    	expandText:       '',
    	userCollapseText: '<strong>^</strong>',
		preserveWords: false,
		expandEffect: 'slideDown',
      	expandSpeed: 0,
      	collapseEffect: 'slideUp',
      	collapseSpeed: 0
	});
	
	
	///// POST HOVER, CLICK, LOAD PROJECT
	
	$('.post').hover(
		function () {
			$(this).addClass('activePost');
		}, 
  		function () {
			$(this).removeClass('activePost');
		}
	);
	
	$(".post-img a").bind("click", preventDefault);
	
	
	
	/// CLICK
	
	function closeproject(){
	
		$('.project-view').animate({
  					opacity: 0
					},{
					duration: 500,
					easing: 'easeInOutQuad',
					queue: false,
					complete : function (){
							$(this).remove();
							$(window).scrollTop(currWinScrollTop);
					}
			});
		
			$('.title-info').fadeIn('slow');
		
		if(currFilter === undefined || currFilter == 'all') {//check saved filter
			currFilter = "post";
		}
		
			$('.title').animate({
  					width: 300,
					height: 200
					},{
					duration: 200,
					easing: 'easeInOutQuad',
					queue: false,
					complete : function (){
						$('.content-wrap').isotope({ filter: '.title, .'+currFilter });
					}
			});
			
		$('.loading-gif').remove();
		$('.activeLoadPost').find('.post-img').fadeTo(0, 1);
		$('.activeLoadPost').find('.post-content, .post-info').show();
		$('.activeLoadPost').removeClass('activeLoadPost')
		
		
	
	}
	
	function createProject() {//create the div to load into
		$('.content-wrap').after('<div class="project-view"><div class="back-btn"><a href="">< BACK &nbsp;&nbsp;[ESC]</a></div><div class="loaded-content"></div></div>');
		$('.project-view').hide();
	}
	
	function projectIntro() {//slide in the loaded project div
		currWinScrollTop = $(window).scrollTop();
		$('#contact_form').height(0);
		$('.content-wrap').isotope({ filter: '.title' });
		$('.title-info').fadeOut('fast');
		
		
		$('.title').removeClass('oneheight').animate({
  					width: winWidth-60,
					height: 80
					},{
					duration: 200,
					easing: 'easeInOutQuad',
					queue: false,
					complete : function (){
							$(window).scrollTop(0);
					}
		});
		
		
		
		$('.project-view').show();
		
		$(".back-btn a").bind("click", preventDefault);
		
		$(".back-btn a").click(function(){
			closeproject();
		});
		
		$(document).keyup(function(e) {
  			if (e.keyCode == 27) { $('.back-btn a').click(); }   // esc
		});
	}
	
	$(".post-content").click(function(){
		$(this).parent().find('.post-img').click();
	});
	
	$(".post-img").click(function(){
		
		$(this).parent().addClass('activeLoadPost');
		$('.activeLoadPost').find('.post-img').fadeTo('slow', 0.5);
		$('.activeLoadPost').find('.post-content, .post-info').hide();
		$('.activeLoadPost').find('.post-img').after('<div class="loading-gif"><img src="images/loading.gif" width="80" height="20" /></div>');
		
		createProject();
		
		projName = $(this).parent().find('.post-img img').attr("alt");
		
		//SET LOCATION
		//baseURL = "http://jacoboneal.com"
		//window.location = initLocation+'#'+projName
		
		$('.loaded-content').load('projectphp/'+projName+'.php .project-content',
			
			function() {
            	projectIntro();
			}
		);
		
	});
	
	

});//document ready end
