/** Copyright Josselin CORNOU June 2011 **/
/** I feel free to have a look to this script, if you want to use it or you need explication about this script, 
don't hesitate to ask me. jocornou [at] gmail [dot] com **/

/** jquery part **/
$(document).ready(function(){


/** Contain 
1 - Title
2 - Hash Tag

function loadVisual load or hide the visual
 **/
 
 
/** Object intemNavigation **/ 
function ItemNav(tag,label, div, endFunction)
{
	this.tag = tag;
	this.label = label;
	this.div = div;
	this.endFunction = endFunction;
}

ItemNav.prototype.init = function()
{
	for(var x in this.div)
	{
		var tmp = this.div[x];
		$(tmp.idTag).animate(tmp.cssPropertiesHide,0,null);
		$(tmp.idTag).addClass("hideBox");
	}
}
ItemNav.prototype.show = function ()
{
	var y = this.div.length-1;
	for(var x=0;x <this.div.length;x++)
	{
		
		
		var tmp = this.div[x];
		
		// for IE
		$(tmp.idTag).removeClass("hideBox");
		
		if(x!=y)
			$(tmp.idTag).animate(tmp.cssPropertiesShow, tmp.timeShow,null);
		else
			$(tmp.idTag).animate(tmp.cssPropertiesShow, tmp.timeShow,this.endFunction);
			
		
	}
	
}
ItemNav.prototype.hide = function (bool)
{
	for(var x in this.div)
	{
		var tmp = this.div[x];
		$(tmp.idTag).animate(tmp.cssPropertiesHide, tmp.timeHide,
		function(){
			// for IE :
			$(this).addClass("hideBox");
			
			}
		);
		
	}
}


ItemNav.prototype.toString = function(){
	return "Josselin CORNOU - "+this.label;
}

/** Property for the main objects**/
function PropWindow(idTag, cssPropertiesShow, timeShow, cssPropertiesHide, timeHide  ){
	this.idTag = idTag;
	this.cssPropertiesShow = cssPropertiesShow;
	this.cssPropertiesHide = cssPropertiesHide;
	this.timeShow = timeShow;
	this.timeHide = timeHide;
}
PropWindow.prototype.toString = function(){
	return this.idTag + " " + this.cssPropertiesShow;
}
/** end declaration **/
var hashObject = new ItemNav("","");

var navigation = new Array();

navigation[0] = new ItemNav("aboutme","About Me",
			[new PropWindow("#myFeed",
					{"margin-left":"50px"},600,
					{"margin-left":"-1100px"},600),
			 new PropWindow("#aboutMe",
					{"margin-left":"0px"},600,
					{"margin-left":"-1100px"},600), 
			  new PropWindow("body",
					{backgroundPosition:"10px 5px"},600,
					{backgroundPosition:"10px 5px"},0)
			], 
			null
);
navigation[1] = new ItemNav("CV","C.V.",
		[new PropWindow("#myCV",
						{"margin-top":"0","opacity":"1"},600,
						{"margin-top":"-608","opacity":"0"},600), 
			  new PropWindow("body",
					{backgroundPosition:"5px 10px"},600,
					{backgroundPosition:"5px 10px"},0)],
					null
);
navigation[2] = new ItemNav("portfolio","Portfolio",
		[new PropWindow("#myPortfolio",
				{"opacity":"1",	"margin-top":"0"},600,
				{"margin-top":"608", "opacity":"0"},500), 
			  new PropWindow("body",
					{backgroundPosition:"5px 0px"},600,
					{backgroundPosition:"5px 0px"},0)],
					function(){ 
					
						if(!bSettedIsotope)
						{
						
							$("#project-container").load("ajax/projectContainer.php",function(){
								setIsotope();
								projectContainer.isotope('reLayout');
								setProjectEventListener();
							});
						}
					}
);


navigation[3] = new ItemNav("contact","Contact Me",		
		[new PropWindow("#myContact",
				{"margin-left":"0","opacity":"1" },600,
				{"margin-left":"1300", "opacity":"0" },500), 
			  new PropWindow("body",
					{backgroundPosition:"-0px 5px"},600,
					{backgroundPosition:"-0px 5px"},0)]);


function loadSetFlickR(divFlickR, setID){
	// Load the classic theme
			Galleria.loadTheme('js/themes/classic/galleria.classic.js');
			// Initialize Galleria

			
			divFlickR.galleria({
				flickr: 'set:'+setID,
				max: 20,
				thumbSize: 'mini',
				flickrOptions: {
					sort: 'interestingness-desc'
				}
			});

}
 
function loadShareButton(){
	$('.googlePlusShareBox').html("<g:plusone size=\"tall\" href=\"http://josselin-cornou.com\"></g:plusone>");

}
 

/** function **/
function loadPage(){
		
	// Parse hash
	
	
	// Load shareButton(
	loadShareButton();	
		
	jQuery(document).bind('keydown', 'left',function (evt){document.location.hash = navigation[0].tag; return false; });
	jQuery(document).bind('keydown', 'up', function (evt){document.location.hash = navigation[1].tag; return false; });
	jQuery(document).bind('keydown', 'down', function (evt){document.location.hash = navigation[2].tag; return false; });
	jQuery(document).bind('keydown', 'right', function (evt){document.location.hash = navigation[3].tag; return false; });
	
	// Hide all element
	$("#logo").css("margin-top","-60px");
	$("#mainMenuNavigation").css("left","-1100px");
	// Hide all windows :
	for (var i=0; i < navigation.length; i++)
	{
		navigation[i].init();
	}
	// default loading
	if(document.location.hash =="")
	{
		document.location.hash ="aboutme";
	}
	else
	{
		var booleanFinded=false;
		// Load the content of the page
		for(var i =0; i<navigation.length;i++)
		{
			var tmp1 =document.location.hash;
			var args = tmp1.split('/');
			if("#"+navigation[i].tag == args[0] )
			{
				loadContent(i);
				booleanFinded=true;
				break;
			}
		}
		
		if(booleanFinded == false)
			loadContent(0);
	}


	$("#logo").animate({"margin-top":"10px"}, 500, function() {
		$("#mainMenuNavigation").animate({left:0}, 200, null);
    	$("#logo").animate({"margin-top":"0px"}, 200, null);
	});
}

$(window).hashchange(function(){
		for(var i =0; i<navigation.length;i++)
		{
			var tmp1 =document.location.hash;
			var args = tmp1.split('/');
			if("#"+navigation[i].tag == args[0] )
			{
				loadContent(i);
				booleanFinded=true;
				break;
			}
		}
});



/** **/
function loadContent(id){
	
	
	
	
	// Save the new Hash
	var newHash=document.location.hash;
	var element = navigation[id];
	var newTag='li.nav.'+element.tag;
	// Chargement menu 
	var listMenu = $("li.nav");
	

	$('li.nav.selected').removeClass("selected");

	$(newTag).addClass("selected");
	/*
	if( listMenu[id].hasClass(newHash))
	{
		listMenu[id].className += " selected";
	}
	else
	{
		for(var i=0; i< listMenu.length; i++)
		{
			if(listMenu[i].id == id)
				listMenu[id].className += " selected";
				
		}
	}
	*/
	
	if(hashObject != null)
	{
		// Hide the previous part
		hashObject.hide();
	}
	// Show the new part
	// Let's change the hash
	hashObject=element;
	document.location.hash = element.tag;
	//Load the new visual
	hashObject.show();
	/** Change the hash **/

}
	
	function changeMenuCV(element){
		var slideValue=(parseInt(element.id)*-350)+"px";
		$(".rightPartMover").animate({ "top":slideValue}, 200, function() {});
		/** replace the old navlink **/	
		if($('ul.leftMenuCV li.selected').length)
		{
			$('ul.leftMenuCV li.selected')[0].className = "";
		}
		element.className += "selected";
	}
	
	function changeMenuCVbyId(value){
		var slideValue=(value*-350)+"px";
		$(".rightPartMover").animate({ "top":slideValue}, 200, function() {});
		/** replace the old navlink **/	
		if($('ul.leftMenuCV li.selected').length)
		{
			$('ul.leftMenuCV li.selected')[0].className = "";
		}
		var lstLi = $('#myCV ul.leftMenuCV li');
		for(var id=0; id < lstLi.length; id++ )
		{
			if(lstLi[id].id == value)
			{
				lstLi[id].className += "selected";
				break;
			}
		}
		
	}
	

	$('ul.leftMenuCV li').click(function() {
		changeMenuCV(this);
	});
	

	$('#myCV .leftPart .downClass').click(function() {
		
		/** get the selectedElement **/	
		if($('ul.leftMenuCV li.selected').length)
		{
			var element = $('ul.leftMenuCV li.selected')[0];
			if(element.id <2 && element.id>=0)
			{
				changeMenuCVbyId(parseInt(element.id)+1);
			}
		}
	});
	
	$('#myCV .leftPart .upClass').click(function() {
		/** get the selectedElement **/	
		if($('ul.leftMenuCV li.selected').length)
		{
			var element = $('ul.leftMenuCV li.selected')[0];
			if(element.id <= 2 && element.id>0)
			{
				changeMenuCVbyId(parseInt(element.id)-1);
			}
		}
	});
	
	
	$("#CVXPcontainer .catDetail").hover(
		//handle in 
		function(e){
			
			$textToShow = $(this).children(".detail").html();
			if($textToShow == null)
				return;
			
			//$textToShow = $(this).position().top
			//add the text
			
			
			$("#tt").css("top",$(this).position().top+$(this).height()+180);
			$("#tt").css("left",$(this).position().left +230);
			$("#tt").html($textToShow);
			$('#tt').css("opacity",0);
			$("#tt").css("display","block");
			
			$("#tt").animate({opacity: 1},300,null);
			
			
			/*$("#tt").animate({top: $(this).position().top + $(this).height(),
							  left: $(this).position().left +$(this).width()
			
			},300,null);*/
			
			
			}, 
		
		// handle out
		function(e){
			$("#tt").css("display","none");
			}
		
		
		
		)
	
	$('#topShare').hover(function(){

		$('#topShare .container').animate({ width:"200px" }, 200, function() {});

	},
						function(){
		$('#topShare .container').animate({ width:"0px" }, 200, function() {});
	}
	
	);
	$('#downloadCV').hover(function(){
		$(this).animate({ opacity:1 }, 200, function() {});
	},function() {
		$(this).animate({  opacity:0.6 }, 400, function() {});
	});
	
	
	$('.share').hover(function(){
		$(this).animate({ opacity:1 }, 200, function() {});
	},function() {
		$(this).animate({  opacity:0.8}, 400, function() {});
	});
	
	/*
	$('.project').hover(function(){
		if(!$('#project-container').hasClass("minimized"))
			$(".boxcaption", this).animate({ top:"0px" }, 400, function() {});
	},function() {
		if(!$('#project-container').hasClass("minimized"))
		{
			var value = $(".boxcaption", this).height()-30;
			$(".boxcaption", this).animate({  top: value+"px"}, 400, function() {});
		}
	});
	*/
	
	var projectContainer = $('#project-container');
	
	var bSetProjectEventListener= false;
	function setProjectEventListener(){
		$('.project').click(function(){
			
			var tmp = this;
			if(!$('#project-container').hasClass("minimized"))
			{
				$('#projectDetails').addClass("showed");
				$('#project-container').animate({width:"21%"},400,
				function() {
					
					$('#project-container').addClass("minimized");
					
					projectContainer.isotope({layoutMode : 'straightDown'});
					$('.rightPart .menu').show(400);
					projectContainer.isotope('reLayout');
				});
			}
			
			
			
			
			
			$('#contentProjectDetail').animate({opacity:"0"},200, 
			
			function() {
					$('#contentProjectDetail').html("<img src=\"images/icons/ajax-loader-h.gif\" title=\"Loading\" /> ");
					$('#contentProjectDetail').animate({opacity:"1"},0,null); 
					
					$('#contentProjectDetail').load("ajax/projectDetail.php?id="+$(".id",tmp).html(),function(){
						$('#contentProjectDetail').animate({opacity:"0"},0,function(){
							$('#contentProjectDetail').animate({opacity:"1"},200, null);	
						});
						var divSelect = $("#setFlickR");
						
						if(divSelect != undefined){
							///Test
							var selector = $("#setFlickR").attr('data-filter');
							if(selector!= undefined)
							{
								loadSetFlickR(divSelect, selector);
							}
						}
						
						
						$('#contentProjectDetail .play').click(
							function(){
								$('.informationContentProject').scrollTo($('#projectvideo'),400);
							}
						);
						
						$('#contentProjectDetail .image').click(
							function(){
								$('.informationContentProject').scrollTo($('#setFlickR'),400);
							}
						);
						$('#contentProjectDetail .details').click(
							function(){
								$('.informationContentProject').scrollTo(0,400);
							}
						);
					
					});
			});
		});
		
		
		
	}
	
	var chute = 350;
	$('.rightPart .menu .downClass').click(function(){
		var top = $("#project-container").offset().top;
		var height = parseInt($("#project-container").css("height"));
		var heightInDiv = height;
		var newVal = top - chute;
		//console.log("top:"+top+" newVal:"+newVal+" heightInDiv:"+heightInDiv+" chutte:"+chute);
		
	
		
		if( newVal < -heightInDiv)
		{	
			$("#project-container").animate({ "top":-heightInDiv}, 400, function() {});
		}
		else {
			$("#project-container").animate({ "top":newVal}, 400, function() {});
		}
	});
	
	$('.rightPart .menu .upClass').click(function(){
		var top = $("#project-container").offset().top;
		var height = parseInt($("#project-container").css("height"));
		var heightInDiv = height;
		var newVal = top + chute;
		//console.log("top:"+top+" newVal:"+newVal+" heightInDiv:"+heightInDiv+" chutte:"+chute);
		
		if(top>0)
		{
			$("#project-container").animate({ "top":"0px"}, 400, function() {});
			return;
		}
		if(top==0)
		{
			return;
		}
		
		if( newVal < -heightInDiv)
		{	
			$("#project-container").animate({ "top":-heightInDiv}, 400, function() {});
		}
		else {
			$("#project-container").animate({ "top":newVal}, 400, function() {});
		}
	});
	
	
	
	
	
	$('#projectDetails .item-close').click(function(){
		
		
		if($('#project-container').hasClass("minimized"))
		{
			$('#projectDetails').removeClass("showed");
			projectContainer.isotope({layoutMode : 'masonry'});
			$('#project-container').animate({width:"100%"},400,
			function() {
				$("#project-container").animate({ "top":"0px"}, 400, function() {});
				$('#project-container').removeClass("minimized");
				
				$('.rightPart .menu').hide(100);
				projectContainer.isotope('reLayout')
				
			});
		}
		$('#projectDetails .title').text($(".title",this).html());		
		$('#projectDetails .date').text($(".date",this).html());	
		
	});
	
	
	
	function showDetailOneProject(project){
		$(project).animate({ width:"414px", height:"325px"}, 200, 
			function() {
				projectContainer.isotope('reLayout');
				$(project).addClass("selected")});
	}
	
	var bSettedIsotope = false;
	
	function setIsotope(){
	// Set up isotope
	
	projectContainer.isotope({
  // options
	  itemSelector : '.project',
	  layoutMode : 'masonry',
	  
	  masonry : {
          columnWidth :3
        },
	});
	bSettedIsotope = true;
	
	
	// set up the event listener for filter :
	$('#filters li').click(function(){
	  
	  var selector = $(this).attr('data-filter');
	  $("#project-container").animate({ "top":"0px"}, 0, function() {});
	  $("#filters li").removeClass("selected");
	   $(this).addClass("selected");
	  projectContainer.isotope({ filter: selector });
	  return true;
	  
	  
	});

	
	}
	
	


	
	$('#logo').click(function() {
		window.location = "http://www.josselin-cornou.com/"

	});


	



/** chargement de la page **/
/** load the default hash**/
loadPage();




});

// made by Josselin CORNOU -- joselin-cornou.com
