/*
 * This file contains the javacript code required to load dynamically the content of each page with
 * AJAX and animate nicely the page when this happens.
 */


/*
 * This function is called when the page is ready. It binds the <a> tags so that when they are
 * clicked on, the openInternalPage() function is called.
 */
jQuery(function($) {

		$('#logo>h1>a').click(openInternalPage);
		$('#menu>li>a').click(openInternalPage);
		$('#menu>li>ul>li>a').click(openInternalPage);
		bindLinksAndButtons();

	}
);



/*
 * This function is called whenever a link whose targer is the current page is clicked. It gets the
 * content of the page with AJAX and replaces it within the current page without the normal
 * animation.
 */
function openPageLink() {

	var pageToLoad = $(this).attr('href');
	$('#to_load').load(pageToLoad + ' #to_load>div', '', bindLinksAndButtons);

	return false;

}


/*
 * This function is called whenever a link whose target is whithin the site (but not the current
 * page) is clicked. It gets the content of the new page with AJAX and replaces it whithin the
 * current page with a nice animation. If the new content contains links, they are bound to this
 * function as well.
 */
function openInternalPage() {

	var pageToLoad = $(this).attr('href');

	$('#to_load').slideUp('fast', loadContent);
	$('#loader').fadeIn('fast');

	function loadContent() {
		$('#to_load').load(pageToLoad + ' #to_load>div', '', showContent);
	}

	function showContent() {
		$('#to_load').slideDown('fast', bindLinksAndButtons);
		$('#loader').fadeOut('fast');
		window.scroll(0, 0);
	}

	return false;

}


/*
 * This function is called whenever a link whose target is not within the site is clicked. It simply
 * opens the target of the link into a new window.
 */
function openExternalPage() {

	var pageToLoad = $(this).attr('href');
	window.open(pageToLoad);

	return false;

}


/*
 * This function is used to bind the links whithin the dynamic part of the page to the
 * openInternalPage() function, so when new ones are added, the openInternalPage() function will be
 * loaded when they are clicked on. Moreover, it binds the buttons to their function, so when they
 * are clicked, an ajax call is made with them.
 */
function bindLinksAndButtons() {

	$('a.page_link').click(openPageLink);
	$('a.internal_link').click(openInternalPage);
	$('a.external_link').click(openExternalPage);
	$('#navigation_bar>li>a').click(openInternalPage);

	if ($('#contact_form').length > 0) {
		$('#submit_button').click(contactFormButton);
	}

	if ($('#guestbook_form').length > 0) {
		$('#submit_button').click(guestbookFormButton);
	}

	if ($('#exhibits_form').length > 0) {
		$('#submit_button').hide();
		$('#year_select').change(exhibitionFormButton);
		$('#collective_select').change(exhibitionFormButton);
	}

	if ($('#ecrits_form').length > 0) {
		$('#submit_button').hide();
		$('#ecrits_select').change(textFormButton);
	}

	if ($('#oeuvres_form').length > 0) {
		$('#submit_button').hide();
		$('#type_select').change(oeuvresFormButton);
	}

}


/*
 * This function is called when the submit button on the contact page is clicked. It handles the
 * request to the server with ajax in order to avoid to reload the entire page.
 */
function contactFormButton() {

	var name = document.getElementById('name_input').value;
	var email = document.getElementById('email_input').value;
	var message = document.getElementById('message_input').value;
	var captcha = document.getElementById('captcha_input').value;

	$('#to_load').load('./contact' + ' #to_load>div', {submit: true, name: name, email: email, message: message, captcha: captcha}, bindLinksAndButtons);

	return false;

}


/*
 * This function is called when the submit button on the guestbook page is clicked. It handles the
 * request to the server with ajax in order to avoid to reload the entire page.
 */
function guestbookFormButton() {

	var name = document.getElementById('name_input').value;
	var message = document.getElementById('message_input').value;
	var captcha = document.getElementById('captcha_input').value;

	$('#to_load').load('./livredor?signer=true' + ' #to_load>div', {submit: true, name: name, message: message, captcha: captcha}, bindLinksAndButtons);

	return false;

}


/*
 * This function is called when the submit button on the expositions page is clicked or if one of
 * the dropdown menu in the same form changes. It handles the request to the server with ajax in
 * order to avoid to reload the entire page.
 */
function exhibitionFormButton() {

	var year_select = document.getElementById('year_select');
	var collective_select = document.getElementById('collective_select');

	var year = year_select.options[year_select.selectedIndex].value;
	var collective = collective_select.options[collective_select.selectedIndex].value;

	$('#to_load').load('./expositions?page=1&year=' + year + '&collective=' + collective + ' #to_load>div', bindLinksAndButtons);

	return false;

}


/*
 * This function is called when the submit button on the ecrits page is clicked or if one of the
 * dropdown menu in the same form changes. It handles the request to the server with ajax in order
 * to avoid to reload the entire page.
 */
function textFormButton() {

	var text_select = document.getElementById('ecrits_select');
	var text = text_select.options[text_select.selectedIndex].value;

	$('#to_load').load('./ecrits?text=' + text + ' #to_load>div', bindLinksAndButtons);

	return false;

}


/*
 * This function is called when the submit button on the oeuvres page is clicked or if one of the
 * dropdown menu in the same form changes. It handles the request to the server with ajax in order
 * to avoid to reload the entire page.
 */
function oeuvresFormButton() {

	var type_select = document.getElementById('type_select');
	var type = type_select.options[type_select.selectedIndex].value;

	$('#to_load').load('./oeuvres?type=' + type + ' #to_load>div', bindLinksAndButtons);

	return false;

}

