$(function(){	
	initNav();
	$(window).scroll(positionHeader);
	$(window).resize(positionHeader);
	positionHeader();
	setInterval(positionHeader,1000);
	$('#lecture').click(showLecture);
	loadProjects();

	/*
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-191158-20']);
	_gaq.push(['_trackPageview']);
	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
		})();
	*/
	
	
});

function loadProjects(){
	$.getJSON('projects.json', function(data) {
		var projectsCode = '';
		for (var i=0; i<data.length; i++){
			var p = data[i];
			projectsCode += '<a href="images/projects/'+p.codename+'/1.jpg" class="gallery mainImage" rel="gal['+p.codename+']">'+
							'	<img src="images/projects/'+p.codename+'/thumb.jpg"/>'+
							'	<p>'+p.title+
							'	<span class="desc">'+p.desc+'</span></p>'+
							'</a>';
			if (p.shots>1){
				for (var s=2; s<=p.shots; s++){
					projectsCode += '<a href="images/projects/'+p.codename+'/'+s+'.jpg" class="gallery" rel="gal['+p.codename+']"></a>'
				}
			}
		}
		

		$('#projectsList').html(projectsCode);
		$('.gallery').prettyPhoto({theme:'light_rounded',slideshow:5000, autoplay_slideshow:true});
	});

}

function positionHeader(){
	if ($(window).scrollTop()>250){
		$('#headerContainerFixed').show();
	}else{
		$('#headerContainerFixed').hide();
	}
	//console.log($(window).scrollTop());
}


function initNav(){

	$('body').append('<div id="headerContainerFixed"></div>');
	$('#headerContainerFixed').html($('#headerContainer').html());
	$('.nav a').click(function(){
		var topMargin = Math.min(180,70+ Math.max(0,($(window).height()-650)/2));
		var target = $(this.hash).offset().top - topMargin;
		$('html,body').animate({scrollTop: target}, 800);
		return false;
	});
	$('.btnUp').click(function(){
		$('html,body').animate({scrollTop: 0}, 800);
		return false;
	});
}

function showLecture(){
/*
	var lectureEmbedCode = '<object width="935" height="600">'+
				'<param name="movie" value="http://www.youtube.com/p/E85C4368FDA8CC74?hl=he_IL&#038;fs=1&#038;iv_load_policy=3"></param>'+
				'<param name="allowFullScreen" value="true"></param>'+
				'<param name="allowscriptaccess" value="always"></param>'+
				'<param name="wmode" value="opaque"></param>'+
				'<embed src="http://www.youtube.com/p/E85C4368FDA8CC74?hl=he_IL&#038;fs=1&#038;iv_load_policy=3" type="application/x-shockwave-flash" '+
				'width="935" height="600" allowscriptaccess="always" allowfullscreen="true" wmode="opaque"></embed>'+
			'</object>';
*/
	var lectureEmbedCode = '<div id="lectureClose"">Close</div><iframe src="http://player.vimeo.com/video/15732794?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1" width="935" height="600" frameborder="0"></iframe>'


	$('html,body').animate({scrollTop: $('#lecture').offset().top -100}, 1500);
	$('#lecture').slideUp(300);
	$('#lectureVideo').slideDown(300);
	$('#lectureVideo').html(lectureEmbedCode);
	$('#lectureClose').click(hideLecture);
}
function hideLecture(){
		$('html,body').animate({scrollTop: $('#lecture').offset().top -200}, 1500);
		$('#lectureVideo').html('');
		$('#lecture').slideDown(300);
		$('#lectureVideo').slideUp(300);
}
