// I will foundation site scripts

$(document).ready(function() {
	$('a.panel').click(function () {
		current = $(this);
		var target =$(this).attr('href');
		$('#wrapper').scrollTo(target, 800);
		if((target=='#ambassadors')||(target=='#exemplifiers')){
			displayThumbnailsAnimation(target);	
		}
		collapseSlides();
		collapseAccordions();
	});
	$(window).resize(resizePanel);
	enableAccordion();
	enableSliderControl();
	addSpacer();
});

function resizePanel() {
	width = $(window).width();
	height = $(window).height();
	mask_width = width * $('.item').length;
	$('#debug').html(width  + ' ' + height + ' ' + mask_width);
	$('#wrapper, .item').css({width: width, height: height});
	$('#mask').css({width: mask_width, height: height});
	$('#wrapper').scrollTo(window.location.hash, 0);
}

function displayThumbnailsAnimation(pageId){
	$('.thumbnails').children().hide();					   
	var paras = $(pageId).find('.thumbnails').children(),
	i = 0;
	//if(paras.is(':hidden')){
	// If using jQuery 1.3 or lower, you need to do $(paras[i++] || []) to avoid an "undefined" error
	(function() {
		$(paras[i++]).fadeIn(300, arguments.callee);
	})(); 
	//}
}
function loadContent(pageId){
	$(pageId).find('.slides-container').load('ambassadors.html');
}

function enableAccordion(){
	//Set default open/close settings
	$('.accordion-container').hide(); //Hide/close all containers
	$('.accordion-header').click(function(){
		if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
			$('.accordion-header').removeClass('active').next().slideUp(); //Remove all "active" state and slide up the immediate next container
			$(this).toggleClass('active').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
		}
		return false; //Prevent the browser jump to the link anchor
	});
}

function enableSliderControl(){
	//This script displays and hides de ambassadors and exemplifiers
	$('.slide').hide();
	$('.thumbnails').children().click(function(){
		var slideToOpen= $(this).attr('data');
		//alert(slideToOpen);
		if($('.slides-container').find('.slide').hasClass('expanded-slide')){
			//alert('hay un slider abierto');
			$('.expanded-slide').slideUp(600,function(){
				$(this).removeClass('expanded-slide');
			})
		}
		setTimeout(function(){
			$('#'+slideToOpen).slideDown(600, function(){
				$(this).addClass('expanded-slide');											  
			});				
		}, 700);
	});	
	//fix to clear float on the slides
	$('.slide').each(function(){
		$(this).find('.right-col').after('<div class="clearfix"></div>');						  
	});

}
function collapseSlides(){
	setTimeout(function(){
		$('.expanded-slide').slideUp(function(){
					$(this).removeClass('expanded-slide');
		});
	}, 1000);
}

function collapseAccordions(){
	setTimeout(function(){
		$('.active').removeClass('active').next().slideUp();
	}, 1000);		
}

function addSpacer(){
	$('.content').before('<div class="spacer"></div>')	
}
