// $Id: main.js 29 2012-01-06 00:09:53Z jfabregat $


/**
 * Actions au chargement et fonctions globales
 */
var scrollTimeout = false;

// Chargement des blocs de la page
$(document).ready(function() {
	loadBlocks();
});
// Scroll pour le menu
$(window).scroll(function() {
	// Si Safari
	if (navigator.userAgent.indexOf('AppleWebKit') != -1) {
		if (scrollTimeout) clearTimeout(scrollTimeout);
		scrollTimeout = setTimeout(menuScroll, 5);
	}
	// Sinon
	else {
		menuScroll();
	}
});


function menuScroll() {
	// sélection dans le menu
	$('#menuLinks a').removeClass('selected');
	var blockHover = 0;
	$('#content div.block').each(function() {
		if ($(document).scrollTop() >= ($(this).offset().top + $(this).height())) {
			blockHover = $(this).attr('id').substr(5);
		}
	});
	//alert(blockHover);
	$($('#menuLinks a')[blockHover]).addClass('selected');
	//location.href = '#block'+(parseInt(blockHover) + 1);

	// positionnement du menu
	if ($(document).scrollTop() > $('#menu').offset().top) {
		$('#menuFixed').css('position', 'fixed');
		$('#content').css('margin-top', $('#menuFixed').height()+'px');
	}
	else if ($(document).scrollTop() != $('#menu').offset().top || $(document).scrollTop() < $('#homeImg').height()) {
		$('#menuFixed').css('position', 'relative');
		$('#content').css('margin-top', '0');
		$('#menuLinks a').removeClass('selected');
	}
}

function loadBlocks(i) {
	if (typeof i == "undefined") i = 1;
	$.get('contentBlock'+i+'.'+lang+'.php', function(html) {
		if (i < 5) {
			loadBlocks(i + 1);
			$('#loading').before(html);
		}
		else {
			$('#loading').replaceWith(html);

			// après le chargement des blocks
			if (location.href.indexOf('#block') != -1) {
				goToBlock(location.href.substr(location.href.indexOf('#block') + 6));
				//location.href = location.href.substr(0, location.href.indexOf('#block'));
			}
			autoScrollBlock1();
		}
	})
}

function goToBlock(i) {
	$.scrollTo('#block'+i, {
		offset: {left: 0, top: -($('#menu').height() + 50)},
		duration: 500,
		easing: 'easeInOutQuint'
	});
}

function goToTop() {
	$.scrollTo('#homeImg', {
		duration: 500
	});
}

function setLanguage(lang) {
	if ($('#menuLinks a.selected').length) {
		location.href = 'setLanguage.php?lang='+lang+'&block='+$('#menuLinks a.selected').attr('rel');
	}
	else {
		location.href = 'setLanguage.php?lang='+lang;
	}

}


/**
 * Block 1
 */
var collection = {
	id: 1,
	scrollOptions: {
		duration: 500
	}
}
var autoScrollTimeout = false;

function scrollToNextCollection() {
	collection.id = (collection.id == 3) ? 1 : collection.id + 1;
	$('#collections').scrollTo('#collection'+collection.id, collection.scrollOptions);
	autoScrollBlock1();
}

function scrollToPrevCollection() {
	collection.id = (collection.id > 1) ? collection.id - 1 : 3;
	$('#collections').scrollTo($('#collection'+collection.id), collection.scrollOptions);
	autoScrollBlock1();
}

function autoScrollBlock1() {
	if (autoScrollTimeout) clearTimeout(autoScrollTimeout);
	autoScrollTimeout = setTimeout(function() {
		scrollToNextCollection();
		autoScrollBlock1();
	}, 5000);
}

/**
 * Block 5
 */
function checkContactForm() {
	$.ajax({
		url: 'sendMessage.php',
		type: 'POST',
		data: $('#block5 form input, #block5 form textarea'),
		success: function(result) {
			if (result.error == "false") {
				$('#block5 form input, #block5 form textarea').each(function() {
					$(this).val('');
				});
			}
			alert(result.message);
		}
	})
}
