/**
 * @version 1.0
 *
 * @copyright Copyright 2009 WnG Solutions Sàrl, all rights reserved
 * @author Daniel Calderini <daniel [DOT] calderini [AT] wng [DOT] ch>
 * @package FVE
 */

// Empeche que jQuery n'entre en conflit avec d'autre librairies
jQuery.noConflict();


jQuery(document).ready(function() {
	WnG = new WnG_Website();
});

function redirect() {
	location.replace('http://fve.wng.ch');
}

/**
 * Gestion du site
 */
var WnG_Website = function() {
	/**
	 * Tableau de configuration du site
	 */
	this.config = {
		tools: {
			searchbox: {
				width: '200px',
				speed: 300
			},
			
			loginbox: {
				width: '210px',
				speed: 300,
				/*defaultTextLogin: 'Identifiant',
				defaultTextPass: 'Mot de passe'*/
				defaultTextLogin: '',
				defaultTextPass: ''
			}
		}
	};
	
	/**
	 * Initialisation du site.
	 *
	 * Cette fonction est appelée automatiquement une fois le site chargé (document.ready)
	 *
	 * @return void
	 */
	this.init = function() {
		this.init_tools();
		this.init_menuSub();
	};
	
	/**
	 * Initialisation de la barre d'outils
	 *
	 * @return void
	 */
	this.init_tools = function() {
		// Action du clique sur les labels
		jQuery('#searchBoxLink').click(this.tools_showSearchBox);
		jQuery('#loginBoxLink').click(this.tools_showLoginBox);
		
		// Action du clique sur les boutons
		jQuery('#searchBoxButton').click(this.tools_submitSearchBox);
		jQuery('#loginBoxButton').click(this.tools_submitLoginBox);
		jQuery('#logoutBoxLink, #logoutBoxButton').click(this.tools_submitLogoutBox);
		
		// Cache les box de login et de recherche
		jQuery('#searchBox div, #loginBox div').css({width: '1px'});
		
		// Focus et blur des champs du formulaire de connexion
		jQuery('#loginBoxTextLogin').focus(this.tools_loginCleanLoginField);
		jQuery('#loginBoxTextLogin').blur(this.tools_loginCleanLoginField);
		jQuery('#loginBoxTextPass').focus(this.tools_loginCleanPassField);
		jQuery('#loginBoxTextPass').blur(this.tools_loginCleanPassField);
	};
	
	/**
	 * Initialisation du menu secondaire
	 *
	 * @return void
	 */
	this.init_menuSub = function() {
		jQuery('.menuToggleOnclick').click(this.menuSub_openClose);
	};
	
	/**
	 * Affiche / cache la box de recherche
	 *
	 * @return bool
	 */
	this.tools_showSearchBox = function() {
		if (WnG.tools_isOpen('#searchBox div')) {
			WnG.tools_closeBox(
				'#searchBox div',
				WnG.config.tools.searchbox.speed,
				'swing'
			);
		} else {
			WnG.tools_openBox(
				'#searchBox div',
				WnG.config.tools.searchbox.width,
				WnG.config.tools.searchbox.speed,
				'swing',
				function() { jQuery('#searchBoxText').focus(); }
			);
		}
		
		return false;
	};
	
	/**
	 * Affiche / cache la box de connexion
	 *
	 * @return bool
	 */
	this.tools_showLoginBox = function() {
		if (WnG.tools_isOpen('#loginBox div')) {
			WnG.tools_closeBox(
				'#loginBox div',
				WnG.config.tools.loginbox.speed,
				'swing'
			);
		} else {
			WnG.tools_openBox(
				'#loginBox div',
				WnG.config.tools.loginbox.width,
				WnG.config.tools.loginbox.speed,
				'swing',
				function() { jQuery('#loginBoxTextLogin').focus(); }
			);
		}
		
		return false;
	};
	
	/**
	 * Ouvre une box selon les paramètres donnés
	 *
	 * @param string boxId Chemin de la box à ouvrir (p.ex. '#myDiv')
	 * @param string boxWidth Taille finale de la box, avec l'unité (p.ex. '100px')
	 * @param string animationSpeed Durée de l'animation (en millisecondes)
	 * @param string animationName Nom de l'animation jQuery à utiliser
	 * @param function callback Fonction à appeler une fois l'animation terminée
	 * @return void
	 */
	this.tools_openBox = function(boxId, boxWidth, animationSpeed, animationName, callback) {
		jQuery(boxId).css(
			{width: '1px'}
		).show().animate(
			{width: boxWidth}, animationSpeed, animationName, callback
		);
	};
	
	/**
	 * Ferme une box selon les paramètres donnés
	 *
	 * @param string boxId Chemin de la box à ouvrir (p.ex. '#myDiv')
	 * @param string animationSpeed Durée de l'animation (en millisecondes)
	 * @param string animationName Nom de l'animation jQuery à utiliser
	 * @return void
	 */
	this.tools_closeBox = function(boxId, animationSpeed, animationName) {
		jQuery(boxId).animate(
			{width: '1px'}, animationSpeed, animationName
		);
	};
	
	/**
	 * Vérifie si la box donnée est ouverte ou non
	 *
	 * @param string boxId Chemin de la box (p.ex. '#myDiv')
	 * @return bool
	 */
	this.tools_isOpen = function(boxId) {
		return parseInt(jQuery(boxId).css('width')) > 1;
	};
	
	/**
	 * Submit du formulaire de recherche
	 *
	 * @return bool Toujours false
	 */
	this.tools_submitSearchBox = function() {
		if (WnG.tools_isOpen('#searchBox div'))
			jQuery('#searchBox').submit();
		else
			WnG.tools_showSearchBox();
		
		return false;
	};
	
	/**
	 * Submit du formulaire de connexion
	 *
	 * @return bool Toujours false
	 */
	this.tools_submitLoginBox = function() {
		if (WnG.tools_isOpen('#loginBox div'))
			jQuery('#loginBox').submit();
		else
			WnG.tools_showLoginBox();
		
		return false;
	};
	
	/**
	 * Submit du formulaire de déconnexion
	 *
	 * @return bool Toujours false
	 */
	this.tools_submitLogoutBox = function() {
		jQuery('#logoutBox').submit();
		
		return false;
	};
	
	/**
	 *
	 */
	this.tools_loginCleanLoginField = function() {
		if (jQuery(this).attr('value') == WnG.config.tools.loginbox.defaultTextLogin)
			jQuery(this).attr('value', '');
		else if (jQuery(this).attr('value') == '')
			jQuery(this).attr('value', WnG.config.tools.loginbox.defaultTextLogin);
	};
	
	/**
	 *
	 */
	this.tools_loginCleanPassField = function() {
		if (jQuery(this).attr('value') == WnG.config.tools.loginbox.defaultTextPass)
			jQuery(this).attr('value', '');
		else if (jQuery(this).attr('value') == '')
			jQuery(this).attr('value', WnG.config.tools.loginbox.defaultTextPass);
	};
	
	/**
	 * Ouvre / ferme un sous-menu
	 *
	 * @return bool
	 */
	this.menuSub_openClose = function() {
		var currentLink = jQuery(this);
		var currentMenuSub = jQuery(this).next();
		
		// Le sous-menu n'a pas encore été chargé
		if (!currentMenuSub.css('height') || currentMenuSub.css('height') == 'undefined') {
			// Ajout de l'AJAX Loader
			WnG.ajaxLoader(currentLink.find('span'));
			
			// Génération de l'URL
			var currentUrl = WnG.menuSub_createUrl(jQuery(this).attr('href'));
			var parentLi = jQuery(this).parent();
			
			// Chargement du sous-menu
			jQuery.get(currentUrl, function(data) {
				currentLink.after(data);
				WnG.ajaxLoader(currentLink.find('span'), function() {
					//
					currentLink.next().slideDown('slow');
				});
			});
		}
		
		// Verification de l'etat actuel du menu
		if (currentMenuSub.is(':hidden'))
			currentMenuSub.slideDown('slow');
		else
			currentMenuSub.slideUp('slow');
		
		return false;
	};
	
	/**
	 * Génère l'URL à ouvrir pour obtenir le contenu du sous-menu
	 *
	 * @param string href Adresse de base
	 * @return string
	 */
	this.menuSub_createUrl = function(href) {
		if (href.indexOf('?') && href.indexOf('?') >= 0)
			href = href + '&type=201';
		else
			href = href + '?type=201';
		
		return href;
	};
	
	/**
	 * Ajoute ou supprime l'AJAX loader a un élément
	 *
	 * @param string element Chemin d'accès a l'élément
	 * @param function callback
	 * @return void
	 */
	this.ajaxLoader = function(element, callback) {
		
		if (jQuery(element).hasClass('ajaxLoader')) {
			jQuery(element).removeClass('ajaxLoader');
		} else {
			jQuery(element).addClass('ajaxLoader');
		}
		
		if (callback != null) { callback(); }
	};
	
	// Initialisation
	this.init();
};
