
/* ================================================================================================================== */
/* ==== 																			    Copyright Metrick System ==== */
/* ====																						  	  August 24 2011 ==== */
/* ====																							    Yohann Paris ==== */
/* ====																			     developer@metricksystem.com ==== */
/* ================================================================================================================== */

/* ============================================================================================ Mootools 1.3.2 ==== */

/* ================================================================================================= Functions ==== */
	
	// ---- Get window dimensions ----------------------------------------------------------
	// http://www.javascripter.net/faq/browserw.htm
	// ------------------------------------------------------------------------------------- 	   
	function getWindowDimensions(){
	
		window['width'] = 630; 
		window['height'] = 460;
		
		if (document.body && document.body.offsetWidth) {
			window['width']  = document.body.offsetWidth;
			window['height'] = document.body.offsetHeight;
		}
		if (document.compatMode=='CSS1Compat' &&
			document.documentElement &&
			document.documentElement.offsetWidth ) {
			window['width']  = document.documentElement.offsetWidth;
			window['height'] = document.documentElement.offsetHeight;
		}
		if (window.innerWidth && window.innerHeight) {
			window['width']  = window.innerWidth;
			window['height'] = window.innerHeight;
		}
		
		return window;
	}
	
	// ---- Top Menu position --------------------------------------------------------------
	//  Calcul the size of the windows to position the menu perfectly on the right side of the content
	// -------------------------------------------------------------------------------------
	
	function menuPosition() {
	
		var menu = $$('body header nav');
		var window = getWindowDimensions();
		
		if (window['width'] > 950) {
			position = (window['width'] - 950) / 2;
		} else {
			position = '5%';
		}		
				
		menu.setStyle('right', position);
	}
	
	// ---- Display Project ----------------------------------------------------------------
	//  Permet de changer quel projet est affiche dans un article
	// -------------------------------------------------------------------------------------
	
	function displayProject(id) {
		
		var projects = $(id).getElements('.project');				// Get the Project from the good article
		var	articleProjectsLinks = $(id).getElements('nav li'); 	// Get the specified elements of navigation
		var indexElementToDisplay;

		articleProjectsLinks.each(function(item, index){			// Find which links is 'used'
			if(item.hasClass('used')){
				indexElementToDisplay = index;						// Get the correct index of which project to display
			}
		});

		projects.removeClass('projectDisplay');						// Do not display any of the projects

		if ((indexElementToDisplay+1) > projects.length){			// In case the index of the menu is higher than the real number of project...
			projectToDisplay = projects[0];							// ...we want to display the first element
			articleProjectsLinks.removeClass('used');				// and we remove the current element used
			articleProjectsLinks[0].addClass('used');				// to put the first one as used.
		} else { 
			projectToDisplay = projects[indexElementToDisplay];		// ... else we just select the good element
		}
		
		projectToDisplay.getParent('article').tween('height', projectToDisplay.getSize().y);
		projectToDisplay.addClass('projectDisplay');				// Display the good project
	}
  
/* ================================================================================================= DOM Ready ==== */

	window.addEvent('domready', function() {
	
		/* ========================== Initialisation ==== */
		
		// --- Smooth Scroll
		var mySmoothScroll = new Fx.SmoothScroll({
		    links: '.nav',
		    wheelStops: false
		});
		
		// --- Display the menu at the good position with window resizing	
		new menuPosition();
	
		/* ================================== Events ==== */
		
		// --- Window Resized
		var timer;
		window.addEvent('resize', function(){
			$clear(timer);
			timer = (
				menuPosition()
			).delay(50);
		});
		
		// --- Change project in article
		var projectLinks = $$('#work article nav li');				// Get all the nav elements
		projectLinks.addEvent('click', function(){					// For each of the element we add an event						
			id = this.getParent('article').get('id');				// We get the id of the parent to know where to change the display
			articleProjectsLinks = $(id).getElements('nav li'); 	// Get the specified elements of navigation
			articleProjectsLinks.removeClass('used');				// Remove the current element used
			this.addClass('used');									// Know which one have the class 'used'
			displayProject(id);
			
			sublimevideo.stop();									// Stop the current playing video
		});
		
	});
	
	
	// -----------------------------------------------------------------
	// Slubime Video, video player
	// http://sublimevideo.net/								
	// -----------------------------------------------------------------
	
	sublimevideo.ready(function() {


	});
	

/* =================================================================================================== The End ==== */











