$(document).ready(function() {

// This one is important, many browsers don't reset scroll on refreshes
// Reset all scrollable panes to (0,0)
$('div.section').scrollTo( 0 );
// Reset the screen to (0,0)
$.scrollTo( 0 );
  	
function deactivateWaypoint(wp){
		//disattivo trutti i wp eccetto quello che mi serve
		//ma non funziona con scrolling verso l'alto
		var $wpOk = $('#'+wp.id+'').filter($.waypoints());
		var $toDel = $.waypoints().not($wpOk);
		$.each($toDel,function(i, w) { 
				 $(w).waypoint('remove'); 
		});
};

  
//initialising the localScroll on jmenu
$('#jmenu').localScroll({
	  hash:true,
	  onBefore:function( e, anchor, $target ){
      // The 'this' is the settings object, can be modified
       highlightNav(anchor);
       removeProjectOverlay();
      //before starting to slide it deactivates all the wp but not the clicked one
      //deactivateWaypoint(anchor);      
	  },
	  onAfter:function( anchor, settings ){
      // The 'this' contains the scrolled element (#content)
     
      //after slide is complete it restores all the waypoints and isSomethingClicked           	
     	// DOVREI DIRGLI SE NON è STATO CLICCATO ALCUN ELEMENTO FAI PARTIRE QUESTO ALTRIMENTI 
     	//$('.section').waypoint();
	  }
  });
    
//displaying only the correct submenu according to scroll
// waypoint creation 
	$('.section').waypoint(function(e, direction) {
		var $active = $(this);
		
		if (direction === "up") {
			$active = $active.prev();
		}
		if (!$active.length) $active.end();
		//al passaggio del wp attivo faccio queste righe
		 $('.section-active').removeClass('section-active');
     $active.addClass('section-active');
		
		$('#jmenu ul').fadeOut("fast", function(){$(this).css("display","none")});		
		$('#jmenu ul[title= "' + $active.attr("id") + '"]').fadeIn("fast", function(){$(this).css("display","block")});		
}, {
		continuous:false,
		offset: 26,
});  


//1st LEV MENU ANIMATION  
function removeProjectOverlay(){
    var idDel = $('.section-active').attr('id');
 	 	$('.slider-wrapper').fadeOut("fast", function(){$(this).css("display","none")});
		$('.section-active .overlay').fadeOut("fast", function(){$(this).css("display","none");$('#jmenu a').removeClass("lighted");});
		$('#'+idDel+'-content').empty();		
}
function isWiewingProjects(){
	if ($('.section-active .overlay').css('display')=="block"){
		return true;
	}else{
		return false;
	}
}	
function highlightNav(a){
	  $("#jmenu li a").removeClass('activeOrange');
	  $('#jmenu li a[href="#'+a.id+'"]:first-child').addClass("activeOrange");
};

function loadProject(content,path,file){	
	$.get("projects/"+path+"/"+file+".html", function(data) {
		 content.replaceWith(data);
		 callProjectsFunctions();
	});
}

   	
//2nd LEV SUBMENU ANIMATION 
$("#jmenu li ul li").click(function () {	
		var title = $(this).parent().parent().attr('title');
		var content = $("#" + title +"-content"); 
		var path='';
		var file = $(this).text().toLowerCase();	 
	  // check the section to set the path to the file    
		switch(title){
			case 'second': path = "graphic"; break;
			case 'third': path = "web"; break;
			case 'fourth': path = "media"; break;		
		}
		//load selected file 
		loadProject(content,path,file);

});//end 2°level menu click  


function callProjectsFunctions(){  
				
	if (!isWiewingProjects()){
	  $('.section-active .overlay').fadeToggle("fast", "linear");
	 	$('#jmenu a').not('.activeOrange').addClass("lighted");
 	}    	

  $('#close').click(function () {
  		removeProjectOverlay();
   });
     
  $("#moreInfo").click(function() {
		$(".nivo-caption").slideToggle("slow");
		if($("#moreInfo span").text()== '—'){
				$("#moreInfo span").text('+');
		}else{
			  $("#moreInfo span").text('—');
		}
	});//end moreInfo() click
	
	$('#slider').nivoSlider({
    effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
    slices: 15, // For slice animations
    boxCols: 8, // For box animations
    boxRows: 4, // For box animations
    animSpeed: 500, // Slide transition speed
    pauseTime: 3000, // How long each slide will show
    startSlide: 0, // Set starting Slide (0 index)
    directionNav: true, // Next & Prev navigation
    directionNavHide: true, // Only show on hover
    controlNav: true, // 1,2,3... navigation
    controlNavThumbs: false, // Use thumbnails for Control Nav
    controlNavThumbsFromRel: false, // Use image rel for thumbs
    controlNavThumbsSearch: '.jpg', // Replace this with...
    controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
    keyboardNav: true, // Use left & right arrows
    pauseOnHover: true, // Stop animation while hovering
    manualAdvance: true, // Force manual transitions
    captionOpacity: 0.7, // Universal caption opacity
    prevText: 'Prev', // Prev directionNav text
    nextText: 'Next', // Next directionNav text 
	});

	
}//end callProjectsFunctions()  


    
//ADDING THE SLASH TO ALL H2 QUOTES
$('.quote').prepend('<img src="images/slash.png" alt="slash" />');



/*
 * DC jQuery Spamless - jQuery Spamless
 * Copyright (c) 2011 Design Chemical
 *
 * Dual licensed under the MIT and GPL licenses:
 * 	http://www.opensource.org/licenses/mit-license.php
 * 	http://www.gnu.org/licenses/gpl.html
 *
 */

(function($){

	//define the new for the plugin ans how to call it
	$.fn.dcSpamless = function(options) {

		//set default options
		var defaults = {
			reverse: true, 	// level of obfuscation
			splitDomain: '[dot]', // characters used to replace '.'
			splitName: '[at]', // characters used to replace '@'
			mailto: true 	// adds mailto link
		};

		//call in the default otions
		var options = $.extend(defaults, options);
		
		return this.each(function(options){

			var domain = defaults.splitDomain, name = defaults.splitName;
			
			// replace characters
			var email = $(this).is('a') ? 
			$(this).attr('href').replace('mailto:', '').replace(domain, '.').replace(name, '@') :
			$(this).text().replace(domain, '.').replace(name, '@');
			
			// reverse
			email = defaults.reverse == true ? email.split('').reverse().join('') : email ;
			
			if($(this).is('a')){
				$(this).attr('href','mailto:'+email);
			} else {
				if(defaults.mailto == true){
					email = '<a href="mailto:'+email+'">'+email+'</a>';
				}
				$(this).html(email);
			}
		});
	};
})(jQuery);
	
	
	//setting the mail form
$('.email').dcSpamless({
    reverse: true,
    splitDomain: '//',
    splitName: '/',
    mailto: true
});


// Caricamento asincrono delle immagin dei progetti
$("#graphicContainer").jLoader({  
        simpleFileNames: false,
        imgDir: "images/",  
        imgContainer:"graphicContainer"  
      }, [  
       	"graphic/automa",
				"graphic/bauer_01",
				"graphic/bauer_02",
				"graphic/bauer_03",
				"graphic/bauer_04",
				"graphic/gotha_01",
				"graphic/gotha_02",
				"graphic/ioDonna_01",
				"graphic/ioDonna_02",
				"graphic/ioDonna_03",
				"graphic/ioDonna_04",
				"graphic/jobOdissey_01",
				"graphic/jobOdissey_02",
				"graphic/jobOdissey_03",
				"graphic/lattina_01",
				"graphic/lattina_02",
				"graphic/lattina_03",
				"graphic/lattina_04",
				"graphic/ligatures_01",
				"graphic/ligatures_02",
				"graphic/ligatures_03",
				"graphic/ligatures_04",
				"graphic/ligatures_05",
				"graphic/ligatures_06",
				"graphic/ligatures_07",
				"graphic/meliorteam_01",
				"graphic/meliorteam_02",
				"graphic/meliorteam_03",
				"graphic/meliorteam_04",
				"graphic/spoleto_01",
				"graphic/spoleto_02",
				"graphic/spoleto_03",
				"graphic/spoleto_04",
				"media/aroundwork_01",
				"media/aroundwork_02",
				"media/aroundwork_03",
				"media/aroundwork_04",
				"media/aroundwork_05",
				"media/ghostrider_01",
				"media/ghostrider_02",
				"media/ghostrider_03",
				"media/ghostrider_04",
				"media/ghostrider_05",
				"media/ghostrider_06",
				"web/creativescrunch_01",
				"web/creativescrunch_02",
				"web/creativescrunch_03",
				"web/creativescrunch_04",
				"web/creativescrunch_05",
				"web/cst_01",
				"web/cst_02",
				"web/dimagramente_01",
				"web/dimagramente_02",
				"web/dimagramente_03",
				"web/gotha_01",
				"web/gotha_02",
				"web/levada_01",
				"web/levada_02",
				"web/levada_03",
				"web/meliorteam_01",
				"web/meliorteam_02",
				"web/psicodinamica"
      ]);  
	
});//end of document ready

/* 

	jLoader  - a jQuery Image Loader
	
	Copyright © 2009 Dan Wellman
	
	Dual licensed under the MIT and GPL licenses:
  http://www.opensource.org/licenses/mit-license.php
  http://www.gnu.org/licenses/gpl.html
	
*/

(function($) {

	//define jLoader object with some default config settings
	$.jLoader = {
		defaults: {
			imgDir: "loaderImages/",
			imgContainer: "",
			imgTotal: 9,
			imgFormat: ".jpg",
			simpleFileNames: true
		}
	};
	
	//extend jquery with the plugin
	$.fn.extend({
		jLoader:function(config, fileNames) {
			
			//use defaults or properties supplied by user
			var config = $.extend({}, $.jLoader.defaults, config);
			
			//get the id of the selected element
			config.imgContainer = this.attr("id");
			
			//use simpleLoad to load numerically named images or complexLoad for user-specified file names
			(config.simpleFileNames == true) ? simpleLoad(config) : complexLoad(config, fileNames) ;
			
			//return the jquery object for chaining
			return this;
		}
	});
	
  //simple image load
	function simpleLoad(config) {
		
		//loop through images and load sequentially
		for (var x = 1; x < config.imgTotal + 1; x++) {
			
			//create image
			$("<img />").attr({ 
				id: "image" + x, 
				src: config.imgDir + x + config.imgFormat,
				title: "Image" + x
			}).appendTo("#" + config.imgContainer).css({ display: "none" });			
		}
	};
	
	//complex image load
	function complexLoad(config, fileNames) {
		
		//loop through the array in the second argument
		for (var x = 0; x < fileNames.length; x++) {
				
				//create image
				$("<img />").attr({
					id: fileNames[x],
					src: config.imgDir + fileNames[x] + config.imgFormat,
					title: fileNames[x] + "design project"
				}).appendTo("#" + config.imgContainer).css({ display: "none" });
		}
	};
	
})(jQuery);










