/*******************************************************************************
 * ajax.js v1.2 - 20090205
 ******************************************************************************/
/* SCRIPTS A EXECUTER APRES COMPLET CHARGEMENT DE LA PAGE */
$(document).ready(function() {

	/* Empty term if default value on focus */
	$('#term').focus(function(e) {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	});
	/* Empty term if default value on focus */
	$('#advTerm').focus(function(e) {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	});
	/* Empty term if default value on focus */
	$('#foreignProductTerm').focus(function(e) {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	});
	/* Empty term if default value (before going to server) */
	$('#rechercher').click(function(e) {
		var $term = $('#term');
		if ($term.val() == $term.attr('defaultValue') && $term.val() == 'Mot-cl\u00E9 ou Code') {
			$term.val('');
		}
	});

	/* Expand/Collapse for grid, flip to keep state */
	var flip = 0;
	$('a.toggle').click(function() {
	    var TITLE_DEPLIER = "Deplier";
	    var TITLE_REPLIER = "Replier";   
		$(this).parent().next().slideToggle();
		var title = $(this).children().attr("title");
		if( title == TITLE_REPLIER ) { 
		// if (flip++ % 2 == 0) {
			$(this).children().attr( {
				src : "../images/arrow_down.gif",
				title : TITLE_DEPLIER,
				alt : TITLE_DEPLIER
			});
		} else {
			$(this).children().attr( {
				src : "../images/arrow_up.gif",
				title : TITLE_REPLIER,
				alt : TITLE_REPLIER
			});
		}
		return false;
	});

	/* Initialize all AIM hidden */
	if ($('#AIMdetail') != null) {
		$('.AIMcontent').hide();
	}
	/* Show a specific AIM */
	jQuery.ordonanceShowAIM = function(id) {
		$('.AIMcontent').hide();
		$('#' + id).show();
	}

	/* Toggle the AIM (manage first and last if prev or next call) */
	jQuery.ordonanceToggleAIM = function(next) {
		var visibleElement = $('.AIMcontent:visible');
		if (next) {
			// If the next element exist, show it. If not, show the first
			if (visibleElement.next('.AIMcontent').length > 0) {
				visibleElement.next('.AIMcontent').show();
			} else {
				$('.AIMcontent:first').show();
			}
		} else {
			// If the prev element exist, show it. If not, show the last
			if (visibleElement.prev('.AIMcontent').length > 0) {
				visibleElement.prev('.AIMcontent').show();
			} else {
				$('.AIMcontent:last').show();
			}
		}
		visibleElement.hide();
	}

	/* Auto-check if vidal (and only vidal, not vidal free) */
	jQuery.manageOneMonthLater = function(id) {
		if ($('#' + id + ' option:selected').text().toLowerCase() == "vidal") {
			$('#subscriptionEnd').attr("checked", "checked");
			document.getElementById("vipzone").style.visibility = "visible";
		} else {
			$('#subscriptionEnd').attr("checked", "");
			document.getElementById("vipzone").style.visibility = "hidden";
		}
	}

	/* Fonction pour afficher/masquer avec un effet Fading */
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
		return this.animate( {
			opacity : 'toggle'
		}, speed, easing, callback);
	};
	/* Fonction pour afficher/masquer avec effets Slide et Fade cumules */
	jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
		return this.animate( {
			opacity : 'toggle',
			height : 'toggle'
		}, speed, easing, callback);
	};

	/*
	 * Affiche/masque le module Detail Interactions, fenetre modale avec le
	 * plugin blockUI
	 */
	$.blockUI.defaults.css = {};
	$.blockUI.defaults.overlayCSS.backgroundColor = '#666';
	$.blockUI.defaults.overlayCSS.opacity = '0.2';
	/* Close box */
	jQuery.toggleCloseBox = function() {
		$.unblockUI();
		return false;
	};
	/* Toggler function for AIM link */
	jQuery.toggleAIMLink = function() {
		$.blockUI( {
			message : $('#AIMdetail'),
			centerX : true,
			centerX : false
		});
		return false;
	};

	/* Toggler function for news */
	jQuery.toggleNews = function(e) {
		var p = '.' + e.parentNode.className;
		$(e).parent().toggleClass('active');
		$(e).addClass('read');
		$(e).toggleClass('active');
		$(e).parents('div:eq(0)').toggleClass('active');
		$(e).parents().next('.newsContent').slideToggle();
	};
	
	/* Toggler function for menu elements */
	jQuery.toggleInternational = function() {
		$moduleForeignProductSearch = $('#moduleForeignProductSearch');
		jQuery.hideMenuElements($moduleForeignProductSearch);
		if ($moduleForeignProductSearch.is(':hidden')) {
			$moduleForeignProductSearch.show();
		} else {
			$moduleForeignProductSearch.hide();
		}
		return false;
	};
	jQuery.toggleLppr = function() {
		$moduleSearch = $('#moduleSearch');
		jQuery.hideMenuElements($moduleSearch);
		if ($moduleSearch.is(':hidden')) {
			$moduleSearch.show();
		} else {
			$moduleSearch.hide();
		}
		return false;
	};
	jQuery.toggleHistoric = function() {
		$divHistoriqueNavigation = $('#divHistoriqueNavigation');
		jQuery.hideMenuElements($divHistoriqueNavigation);
		if ($divHistoriqueNavigation.is(':hidden')) {
			$divHistoriqueNavigation.show();
		} else {
			$divHistoriqueNavigation.hide();
		}
		return false;
	};
	jQuery.toggleAim = function() {
		$divOrdonnanceVirtuelle = $('#divOrdonnanceVirtuelle');
		jQuery.hideMenuElements($divOrdonnanceVirtuelle);
		if ($divOrdonnanceVirtuelle.is(':hidden')) {
			$divOrdonnanceVirtuelle.show();
		} else {
			$divOrdonnanceVirtuelle.hide();
		}
		return false;
	};
	jQuery.hideMenuElements = function($object) {
		if($object.selector != '#moduleSearch') $('#moduleSearch:visible').hide();
		if($object.selector != '#moduleForeignProductSearch') $('#moduleForeignProductSearch:visible').hide();
		if($object.selector != '#divHistoriqueNavigation') $('#divHistoriqueNavigation:visible').hide();
		if($object.selector != '#divOrdonnanceVirtuelle') $('#divOrdonnanceVirtuelle:visible').hide();
		if($object.selector != '#AIMdetail') $('#AIMdetail:visible').hide();
	};
	
	/* Masque les modules affiches */
	$('#moduleSearch').click(function(e) {
		e.stopPropagation();
	});
	$('#divHistoriqueNavigation').click(function(e) {
		e.stopPropagation();
	});
	$('#divOrdonnanceVirtuelle').click(function(e) {
		e.stopPropagation();
	});
	
	/* Gestion du champs input focus/blur */
	$('input').focus(function() {
		$(this).removeClass();
		$(this).addClass('focus');
	});

	$('input').blur(function() {
		$(this).removeClass();
		$(this).addClass('normal');
	});

	/* Centre un element dans la fenetre (horizontalement et verticalement) */
	$(document).ready(function() {
		jQuery.fn.centerScreen = function(loaded) {
			var obj = this;
			if (!loaded) {
				obj.css('top', $(window).height() / 2 - this.height() / 2);
				obj.css('left', $(window).width() / 2 - this.width() / 2);
				$(window).resize(function() {
					obj.centerScreen(!loaded);
				});
			} else {
				obj.stop();
				obj.animate( {
					top : $(window).height() / 2 - this.height() / 2,
					left : $(window).width() / 2 - this.width() / 2
				}, 200, 'linear');
			}
		}
	});
});

/* FIN EXECUTION ready() */
